| 1 | /**************************************************************************
|
|---|
| 2 | * Copyright (C) 2004-2007 by Michael Medin <michael@medin.name> *
|
|---|
| 3 | * *
|
|---|
| 4 | * This code is part of NSClient++ - http://trac.nakednuns.org/nscp *
|
|---|
| 5 | * *
|
|---|
| 6 | * This program is free software; you can redistribute it and/or modify *
|
|---|
| 7 | * it under the terms of the GNU General Public License as published by *
|
|---|
| 8 | * the Free Software Foundation; either version 2 of the License, or *
|
|---|
| 9 | * (at your option) any later version. *
|
|---|
| 10 | * *
|
|---|
| 11 | * This program is distributed in the hope that it will be useful, *
|
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|---|
| 14 | * GNU General Public License for more details. *
|
|---|
| 15 | * *
|
|---|
| 16 | * You should have received a copy of the GNU General Public License *
|
|---|
| 17 | * along with this program; if not, write to the *
|
|---|
| 18 | * Free Software Foundation, Inc., *
|
|---|
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|---|
| 20 | ***************************************************************************/
|
|---|
| 21 | #pragma once
|
|---|
| 22 |
|
|---|
| 23 | #include <types.hpp>
|
|---|
| 24 | #include <Singleton.h>
|
|---|
| 25 | #include <string>
|
|---|
| 26 | #include <map>
|
|---|
| 27 | #include <set>
|
|---|
| 28 | #include <boost/thread/thread.hpp>
|
|---|
| 29 | #include <boost/thread/locks.hpp>
|
|---|
| 30 | #include <boost/filesystem/path.hpp>
|
|---|
| 31 | #include <boost/regex.hpp>
|
|---|
| 32 | #include <strEx.h>
|
|---|
| 33 | #include <settings/settings_core.hpp>
|
|---|
| 34 | #include <net/net.hpp>
|
|---|
| 35 |
|
|---|
| 36 | #define MUTEX_GUARD() \
|
|---|
| 37 | boost::unique_lock<boost::timed_mutex> mutex(mutex_, boost::get_system_time() + boost::posix_time::seconds(5)); \
|
|---|
| 38 | if (!mutex.owns_lock()) \
|
|---|
| 39 | throw settings_exception(_T("Failed to get mutex, cant get settings instance"));
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 | namespace settings {
|
|---|
| 43 | class SettingsInterfaceImpl : public settings_interface {
|
|---|
| 44 | protected:
|
|---|
| 45 | settings_core *core_;
|
|---|
| 46 | typedef std::list<instance_raw_ptr> parent_list_type;
|
|---|
| 47 | parent_list_type children_;
|
|---|
| 48 | boost::timed_mutex mutex_;
|
|---|
| 49 | public:
|
|---|
| 50 | struct conainer {
|
|---|
| 51 | settings_core::key_type type;
|
|---|
| 52 | int int_val;
|
|---|
| 53 | std::wstring string_val;
|
|---|
| 54 | conainer(int value) : type(settings_core::key_integer), int_val(value) {}
|
|---|
| 55 | conainer(bool value) : type(settings_core::key_bool), int_val(value?1:0) {}
|
|---|
| 56 | conainer(std::wstring value) : type(settings_core::key_string), string_val(value) {}
|
|---|
| 57 | conainer() : type(settings_core::key_string) {}
|
|---|
| 58 |
|
|---|
| 59 | std::wstring get_string() const {
|
|---|
| 60 | if (type==settings_core::key_string)
|
|---|
| 61 | return string_val;
|
|---|
| 62 | if (type==settings_core::key_integer)
|
|---|
| 63 | return strEx::itos(int_val);
|
|---|
| 64 | if (type==settings_core::key_bool)
|
|---|
| 65 | return int_val==1?_T("true"):_T("false");
|
|---|
| 66 | return _T("UNKNOWN TYPE");
|
|---|
| 67 | }
|
|---|
| 68 | int get_int() const {
|
|---|
| 69 | if (type==settings_core::key_string)
|
|---|
| 70 | return strEx::stoi(string_val);
|
|---|
| 71 | if (type==settings_core::key_integer)
|
|---|
| 72 | return int_val;
|
|---|
| 73 | if (type==settings_core::key_bool)
|
|---|
| 74 | return int_val==1?1:0;
|
|---|
| 75 | return -1;
|
|---|
| 76 | }
|
|---|
| 77 | bool get_bool() const {
|
|---|
| 78 | if (type==settings_core::key_string)
|
|---|
| 79 | return string_to_bool(string_val);
|
|---|
| 80 | if (type==settings_core::key_integer)
|
|---|
| 81 | return int_val==1?true:false;
|
|---|
| 82 | if (type==settings_core::key_bool)
|
|---|
| 83 | return int_val==1?true:false;
|
|---|
| 84 | return false;
|
|---|
| 85 | }
|
|---|
| 86 | };
|
|---|
| 87 | typedef settings_core::key_path_type cache_key_type;
|
|---|
| 88 | typedef std::map<cache_key_type,conainer> cache_type;
|
|---|
| 89 | typedef std::set<std::wstring> path_cache_type;
|
|---|
| 90 | typedef std::map<std::wstring,std::set<std::wstring> > key_cache_type;
|
|---|
| 91 | cache_type settings_cache_;
|
|---|
| 92 | path_cache_type path_cache_;
|
|---|
| 93 | key_cache_type key_cache_;
|
|---|
| 94 | std::wstring context_;
|
|---|
| 95 | net::wurl url_;
|
|---|
| 96 |
|
|---|
| 97 | //SettingsInterfaceImpl() : core_(NULL) {}
|
|---|
| 98 | SettingsInterfaceImpl(settings_core *core, std::wstring context) : core_(core), context_(context), url_(net::parse(context_)) {}
|
|---|
| 99 |
|
|---|
| 100 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 101 | /// Empty all cached settings values and force a reload.
|
|---|
| 102 | /// Notice this does not save so anhy "active" values will be flushed and new ones read from file.
|
|---|
| 103 | ///
|
|---|
| 104 | /// @author mickem
|
|---|
| 105 | void clear_cache() {
|
|---|
| 106 | MUTEX_GUARD();
|
|---|
| 107 | settings_cache_.clear();
|
|---|
| 108 | path_cache_.clear();
|
|---|
| 109 | key_cache_.clear();
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 113 | /// Set the core module to use
|
|---|
| 114 | ///
|
|---|
| 115 | /// @param core The core to use
|
|---|
| 116 | ///
|
|---|
| 117 | /// @author mickem
|
|---|
| 118 | virtual void set_core(settings_core *core) {
|
|---|
| 119 | MUTEX_GUARD();
|
|---|
| 120 | core_ = core;
|
|---|
| 121 | }
|
|---|
| 122 | settings_core* get_core() const {
|
|---|
| 123 | if (core_ == NULL)
|
|---|
| 124 | throw settings_exception(_T("FATAL ERROR: Settings subsystem not initialized"));
|
|---|
| 125 | return core_;
|
|---|
| 126 | }
|
|---|
| 127 | logger_interface* get_logger() const {
|
|---|
| 128 | if (core_ == NULL)
|
|---|
| 129 | throw settings_exception(_T("FATAL ERROR: Settings subsystem not initialized"));
|
|---|
| 130 | return core_->get_logger();
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | void add_child(std::wstring context) {
|
|---|
| 134 | MUTEX_GUARD();
|
|---|
| 135 | children_.push_back(get_core()->create_instance(context));
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 139 | /// Get a string value if it does not exist exception will be thrown
|
|---|
| 140 | ///
|
|---|
| 141 | /// @param path the path to look up
|
|---|
| 142 | /// @param key the key to lookup
|
|---|
| 143 | /// @return the string value
|
|---|
| 144 | ///
|
|---|
| 145 | /// @author mickem
|
|---|
| 146 | virtual std::wstring get_string(std::wstring path, std::wstring key) {
|
|---|
| 147 | MUTEX_GUARD();
|
|---|
| 148 | settings_core::key_path_type lookup(path,key);
|
|---|
| 149 | cache_type::const_iterator cit = settings_cache_.find(lookup);
|
|---|
| 150 | if (cit == settings_cache_.end()) {
|
|---|
| 151 | std::wstring val;
|
|---|
| 152 | try {
|
|---|
| 153 | val = get_real_string(lookup);
|
|---|
| 154 | } catch (KeyNotFoundException e) {
|
|---|
| 155 | val = get_string_from_child_unsafe(lookup);
|
|---|
| 156 | }
|
|---|
| 157 | settings_cache_[lookup] = val;
|
|---|
| 158 | return val;
|
|---|
| 159 | }
|
|---|
| 160 | return (*cit).second.get_string();
|
|---|
| 161 | }
|
|---|
| 162 | std::wstring get_string_from_child_unsafe(settings_core::key_path_type key) {
|
|---|
| 163 | for (parent_list_type::iterator it = children_.begin(); it != children_.end(); ++it) {
|
|---|
| 164 | try {
|
|---|
| 165 | return (*it)->get_string(key.first, key.second);
|
|---|
| 166 | } catch (KeyNotFoundException e) {
|
|---|
| 167 | continue;
|
|---|
| 168 | }
|
|---|
| 169 | }
|
|---|
| 170 | throw KeyNotFoundException(key);
|
|---|
| 171 | }
|
|---|
| 172 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 173 | /// Get a string value if it does not exist the default value will be returned
|
|---|
| 174 | ///
|
|---|
| 175 | /// @param path the path to look up
|
|---|
| 176 | /// @param key the key to lookup
|
|---|
| 177 | /// @param def the default value to use when no value is found
|
|---|
| 178 | /// @return the string value
|
|---|
| 179 | ///
|
|---|
| 180 | /// @author mickem
|
|---|
| 181 | virtual std::wstring get_string(std::wstring path, std::wstring key, std::wstring def) {
|
|---|
| 182 | try {
|
|---|
| 183 | return get_string(path, key);
|
|---|
| 184 | } catch (KeyNotFoundException e) {
|
|---|
| 185 | MUTEX_GUARD();
|
|---|
| 186 | settings_cache_[cache_key_type(path,key)] = def;
|
|---|
| 187 | return def;
|
|---|
| 188 | }
|
|---|
| 189 | }
|
|---|
| 190 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 191 | /// Set or update a string value
|
|---|
| 192 | ///
|
|---|
| 193 | /// @param path the path to look up
|
|---|
| 194 | /// @param key the key to lookup
|
|---|
| 195 | /// @param value the value to set
|
|---|
| 196 | ///
|
|---|
| 197 | /// @author mickem
|
|---|
| 198 | virtual void set_string(std::wstring path, std::wstring key, std::wstring value) {
|
|---|
| 199 | {
|
|---|
| 200 | MUTEX_GUARD();
|
|---|
| 201 | settings_cache_[cache_key_type(path,key)] = value;
|
|---|
| 202 | }
|
|---|
| 203 | add_key(path, key);
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | virtual void add_path(std::wstring path) {
|
|---|
| 207 | MUTEX_GUARD();
|
|---|
| 208 | path_cache_.insert(path);
|
|---|
| 209 | }
|
|---|
| 210 | virtual void add_key(std::wstring path, std::wstring key) {
|
|---|
| 211 | MUTEX_GUARD();
|
|---|
| 212 | key_cache_type::iterator it = key_cache_.find(path);
|
|---|
| 213 | if (it == key_cache_.end()) {
|
|---|
| 214 | std::set<std::wstring> s;
|
|---|
| 215 | s.insert(key);
|
|---|
| 216 | key_cache_[path] = s;
|
|---|
| 217 | } else {
|
|---|
| 218 | if ((*it).second.find(key) == (*it).second.end()) {
|
|---|
| 219 | (*it).second.insert(key);
|
|---|
| 220 | }
|
|---|
| 221 | }
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 225 | /// Get an integer value if it does not exist exception will be thrown
|
|---|
| 226 | ///
|
|---|
| 227 | /// @param path the path to look up
|
|---|
| 228 | /// @param key the key to lookup
|
|---|
| 229 | /// @return the string value
|
|---|
| 230 | ///
|
|---|
| 231 | /// @author mickem
|
|---|
| 232 | virtual int get_int(std::wstring path, std::wstring key) {
|
|---|
| 233 | MUTEX_GUARD();
|
|---|
| 234 | settings_core::key_path_type lookup(path,key);
|
|---|
| 235 | cache_type::const_iterator cit = settings_cache_.find(lookup);
|
|---|
| 236 | if (cit == settings_cache_.end()) {
|
|---|
| 237 | int val;
|
|---|
| 238 | try {
|
|---|
| 239 | val = get_real_int(lookup);
|
|---|
| 240 | } catch (KeyNotFoundException e) {
|
|---|
| 241 | val = get_int_from_child_unsafe(path, key);
|
|---|
| 242 | }
|
|---|
| 243 | settings_cache_[lookup] = val;
|
|---|
| 244 | return val;
|
|---|
| 245 | }
|
|---|
| 246 | return (*cit).second.get_int();
|
|---|
| 247 | }
|
|---|
| 248 | int get_int_from_child_unsafe(std::wstring path, std::wstring key) {
|
|---|
| 249 | for (parent_list_type::iterator it = children_.begin(); it != children_.end(); ++it) {
|
|---|
| 250 | try {
|
|---|
| 251 | return (*it)->get_int(path, key);
|
|---|
| 252 | } catch (KeyNotFoundException e) {
|
|---|
| 253 | continue;
|
|---|
| 254 | }
|
|---|
| 255 | }
|
|---|
| 256 | throw KeyNotFoundException(path, key);
|
|---|
| 257 | }
|
|---|
| 258 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 259 | /// Get an integer value if it does not exist the default value will be returned
|
|---|
| 260 | ///
|
|---|
| 261 | /// @param path the path to look up
|
|---|
| 262 | /// @param key the key to lookup
|
|---|
| 263 | /// @param def the default value to use when no value is found
|
|---|
| 264 | /// @return the string value
|
|---|
| 265 | ///
|
|---|
| 266 | /// @author mickem
|
|---|
| 267 | virtual int get_int(std::wstring path, std::wstring key, int def) {
|
|---|
| 268 | try {
|
|---|
| 269 | return get_int(path, key);
|
|---|
| 270 | } catch (KeyNotFoundException e) {
|
|---|
| 271 | MUTEX_GUARD();
|
|---|
| 272 | settings_cache_[cache_key_type(path,key)] = def;
|
|---|
| 273 | return def;
|
|---|
| 274 | }
|
|---|
| 275 | }
|
|---|
| 276 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 277 | /// Set or update an integer value
|
|---|
| 278 | ///
|
|---|
| 279 | /// @param path the path to look up
|
|---|
| 280 | /// @param key the key to lookup
|
|---|
| 281 | /// @param value the value to set
|
|---|
| 282 | ///
|
|---|
| 283 | /// @author mickem
|
|---|
| 284 | virtual void set_int(std::wstring path, std::wstring key, int value) {
|
|---|
| 285 | {
|
|---|
| 286 | MUTEX_GUARD();
|
|---|
| 287 | settings_cache_[cache_key_type(path,key)] = value;
|
|---|
| 288 | }
|
|---|
| 289 | add_key(path, key);
|
|---|
| 290 | }
|
|---|
| 291 |
|
|---|
| 292 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 293 | /// Get the type of a key (String, int, bool)
|
|---|
| 294 | ///
|
|---|
| 295 | /// @param path the path to get type for
|
|---|
| 296 | /// @param key the key to get the type for
|
|---|
| 297 | /// @return the type of the key
|
|---|
| 298 | ///
|
|---|
| 299 | /// @author mickem
|
|---|
| 300 | virtual settings_core::key_type get_key_type(std::wstring path, std::wstring key) {
|
|---|
| 301 | MUTEX_GUARD();
|
|---|
| 302 | cache_type::iterator it = settings_cache_.find(cache_key_type(path, key));
|
|---|
| 303 | if (it == settings_cache_.end())
|
|---|
| 304 | return settings_core::key_string;
|
|---|
| 305 | return it->second.type;
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 309 | /// Get a boolean value if it does not exist exception will be thrown
|
|---|
| 310 | ///
|
|---|
| 311 | /// @param path the path to look up
|
|---|
| 312 | /// @param key the key to lookup
|
|---|
| 313 | /// @return the string value
|
|---|
| 314 | ///
|
|---|
| 315 | /// @author mickem
|
|---|
| 316 | virtual bool get_bool(std::wstring path, std::wstring key) {
|
|---|
| 317 | MUTEX_GUARD();
|
|---|
| 318 | settings_core::key_path_type lookup(path,key);
|
|---|
| 319 | cache_type::const_iterator cit = settings_cache_.find(lookup);
|
|---|
| 320 | if (cit == settings_cache_.end()) {
|
|---|
| 321 | bool val;
|
|---|
| 322 | try {
|
|---|
| 323 | val = get_real_bool(lookup);
|
|---|
| 324 | } catch (KeyNotFoundException e) {
|
|---|
| 325 | val = get_bool_from_child_unsafe(path, key);
|
|---|
| 326 | }
|
|---|
| 327 | settings_cache_[lookup] = val;
|
|---|
| 328 | return val;
|
|---|
| 329 | }
|
|---|
| 330 | return (*cit).second.get_bool();
|
|---|
| 331 | }
|
|---|
| 332 | bool get_bool_from_child_unsafe(std::wstring path, std::wstring key) {
|
|---|
| 333 | for (parent_list_type::iterator it = children_.begin(); it != children_.end(); ++it) {
|
|---|
| 334 | try {
|
|---|
| 335 | return (*it)->get_bool(path, key);
|
|---|
| 336 | } catch (KeyNotFoundException e) {
|
|---|
| 337 | continue;
|
|---|
| 338 | }
|
|---|
| 339 | }
|
|---|
| 340 | throw KeyNotFoundException(path, key);
|
|---|
| 341 | }
|
|---|
| 342 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 343 | /// Get a boolean value if it does not exist the default value will be returned
|
|---|
| 344 | ///
|
|---|
| 345 | /// @param path the path to look up
|
|---|
| 346 | /// @param key the key to lookup
|
|---|
| 347 | /// @param def the default value to use when no value is found
|
|---|
| 348 | /// @return the string value
|
|---|
| 349 | ///
|
|---|
| 350 | /// @author mickem
|
|---|
| 351 | virtual bool get_bool(std::wstring path, std::wstring key, bool def) {
|
|---|
| 352 | try {
|
|---|
| 353 | return get_bool(path, key);
|
|---|
| 354 | } catch (KeyNotFoundException e) {
|
|---|
| 355 | MUTEX_GUARD();
|
|---|
| 356 | settings_cache_[cache_key_type(path,key)] = def;
|
|---|
| 357 | return def;
|
|---|
| 358 | }
|
|---|
| 359 | }
|
|---|
| 360 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 361 | /// Set or update a boolean value
|
|---|
| 362 | ///
|
|---|
| 363 | /// @param path the path to look up
|
|---|
| 364 | /// @param key the key to lookup
|
|---|
| 365 | /// @param value the value to set
|
|---|
| 366 | ///
|
|---|
| 367 | /// @author mickem
|
|---|
| 368 | virtual void set_bool(std::wstring path, std::wstring key, bool value) {
|
|---|
| 369 | {
|
|---|
| 370 | MUTEX_GUARD();
|
|---|
| 371 | settings_cache_[cache_key_type(path,key)] = value;
|
|---|
| 372 | }
|
|---|
| 373 | add_key(path, key);
|
|---|
| 374 | }
|
|---|
| 375 |
|
|---|
| 376 |
|
|---|
| 377 | // Meta Functions
|
|---|
| 378 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 379 | /// Get all (sub) sections (given a path).
|
|---|
| 380 | /// If the path is empty all root sections will be returned
|
|---|
| 381 | ///
|
|---|
| 382 | /// @param path The path to get sections from (if empty root sections will be returned)
|
|---|
| 383 | /// @return a list of sections
|
|---|
| 384 | ///
|
|---|
| 385 | /// @author mickem
|
|---|
| 386 | virtual string_list get_sections(std::wstring path) {
|
|---|
| 387 | MUTEX_GUARD();
|
|---|
| 388 | get_core()->get_logger()->debug(__FILE__, __LINE__, std::wstring(_T("Get sections for: ")) + path);
|
|---|
| 389 | string_list ret;
|
|---|
| 390 | get_cached_sections_unsafe(path, ret);
|
|---|
| 391 | get_real_sections(path, ret);
|
|---|
| 392 | get_section_from_child_unsafe(path, ret);
|
|---|
| 393 | ret.sort();
|
|---|
| 394 | ret.unique();
|
|---|
| 395 | return ret;
|
|---|
| 396 | }
|
|---|
| 397 | void get_cached_sections_unsafe(std::wstring path, string_list &list) {
|
|---|
| 398 | if (path.empty()) {
|
|---|
| 399 | BOOST_FOREACH(std::wstring s, path_cache_) {
|
|---|
| 400 | if (s.length() > 1) {
|
|---|
| 401 | std::wstring::size_type pos = s.find(L'/', 1);
|
|---|
| 402 | if (pos != std::wstring::npos)
|
|---|
| 403 | list.push_back(s.substr(0,pos));
|
|---|
| 404 | else
|
|---|
| 405 | list.push_back(s);
|
|---|
| 406 | }
|
|---|
| 407 | }
|
|---|
| 408 | } else {
|
|---|
| 409 | std::wstring::size_type path_len = path.length();
|
|---|
| 410 | BOOST_FOREACH(std::wstring s, path_cache_) {
|
|---|
| 411 | std::wstring::size_type len = s.length();
|
|---|
| 412 | if (s.length() > (path_len+1) && s.substr(0,path_len) == path) {
|
|---|
| 413 | std::wstring::size_type pos = s.find(L'/', path_len+1);
|
|---|
| 414 | if (pos != std::wstring::npos)
|
|---|
| 415 | list.push_back(s.substr(path_len+1,pos));
|
|---|
| 416 | else
|
|---|
| 417 | list.push_back(s.substr(path_len+1));
|
|---|
| 418 | }
|
|---|
| 419 | }
|
|---|
| 420 | }
|
|---|
| 421 | }
|
|---|
| 422 | void get_section_from_child_unsafe(std::wstring path, string_list &list) {
|
|---|
| 423 | for (parent_list_type::iterator it = children_.begin(); it != children_.end(); ++it) {
|
|---|
| 424 | string_list itm = (*it)->get_sections(path);
|
|---|
| 425 | list.insert(list.end(), itm.begin(), itm.end());
|
|---|
| 426 | }
|
|---|
| 427 | }
|
|---|
| 428 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 429 | /// Get all keys for a path.
|
|---|
| 430 | ///
|
|---|
| 431 | /// @param path The path to get keys under
|
|---|
| 432 | /// @return a list of keys
|
|---|
| 433 | ///
|
|---|
| 434 | /// @author mickem
|
|---|
| 435 | virtual string_list get_keys(std::wstring path) {
|
|---|
| 436 | MUTEX_GUARD();
|
|---|
| 437 | string_list ret;
|
|---|
| 438 | get_cached_keys_unsafe(path, ret);
|
|---|
| 439 | get_real_keys(path, ret);
|
|---|
| 440 | get_keys_from_child_unsafe(path, ret);
|
|---|
| 441 | ret.sort();
|
|---|
| 442 | ret.unique();
|
|---|
| 443 | return ret;
|
|---|
| 444 | }
|
|---|
| 445 | void get_cached_keys_unsafe(std::wstring path, string_list &list) {
|
|---|
| 446 | key_cache_type::iterator it = key_cache_.find(path);
|
|---|
| 447 | if (it != key_cache_.end()) {
|
|---|
| 448 | BOOST_FOREACH(std::wstring s, (*it).second) {
|
|---|
| 449 | list.push_back(s);
|
|---|
| 450 | }
|
|---|
| 451 | }
|
|---|
| 452 | }
|
|---|
| 453 | void get_keys_from_child_unsafe(std::wstring path, string_list &list) {
|
|---|
| 454 | for (parent_list_type::iterator it = children_.begin(); it != children_.end(); ++it) {
|
|---|
| 455 | string_list itm = (*it)->get_keys(path);
|
|---|
| 456 | list.insert(list.end(), itm.begin(), itm.end());
|
|---|
| 457 | }
|
|---|
| 458 | }
|
|---|
| 459 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 460 | /// Does the section exists?
|
|---|
| 461 | ///
|
|---|
| 462 | /// @param path The path of the section
|
|---|
| 463 | /// @return true/false
|
|---|
| 464 | ///
|
|---|
| 465 | /// @author mickem
|
|---|
| 466 | virtual bool has_section(std::wstring path) {
|
|---|
| 467 | throw settings_exception(_T("TODO: FIX ME: has_section"));
|
|---|
| 468 | }
|
|---|
| 469 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 470 | /// Does the key exists?
|
|---|
| 471 | ///
|
|---|
| 472 | /// @param path The path of the section
|
|---|
| 473 | /// @param key The key to check
|
|---|
| 474 | /// @return true/false
|
|---|
| 475 | ///
|
|---|
| 476 | /// @author mickem
|
|---|
| 477 | virtual bool has_key(std::wstring path, std::wstring key) {
|
|---|
| 478 | MUTEX_GUARD();
|
|---|
| 479 | settings_core::key_path_type lookup(path,key);
|
|---|
| 480 | cache_type::const_iterator cit = settings_cache_.find(lookup);
|
|---|
| 481 | if (cit != settings_cache_.end())
|
|---|
| 482 | return true;
|
|---|
| 483 | return has_real_key(lookup);
|
|---|
| 484 | }
|
|---|
| 485 |
|
|---|
| 486 | // Misc Functions
|
|---|
| 487 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 488 | /// Get a context.
|
|---|
| 489 | /// The context is an identifier for the settings store for INI/XML it is the filename.
|
|---|
| 490 | ///
|
|---|
| 491 | /// @return the context
|
|---|
| 492 | ///
|
|---|
| 493 | /// @author mickem
|
|---|
| 494 | virtual std::wstring get_context() {
|
|---|
| 495 | MUTEX_GUARD();
|
|---|
| 496 | return context_;
|
|---|
| 497 | }
|
|---|
| 498 | virtual std::wstring get_file_from_context() {
|
|---|
| 499 | return core_->find_file(url_.host + url_.path, _T(""));
|
|---|
| 500 | }
|
|---|
| 501 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 502 | /// Set the context.
|
|---|
| 503 | /// The context is an identifier for the settings store for INI/XML it is the filename.
|
|---|
| 504 | ///
|
|---|
| 505 | /// @param context the new context
|
|---|
| 506 | ///
|
|---|
| 507 | /// @author mickem
|
|---|
| 508 | virtual void set_context(std::wstring context) {
|
|---|
| 509 | MUTEX_GUARD();
|
|---|
| 510 | context_ = context;
|
|---|
| 511 | }
|
|---|
| 512 |
|
|---|
| 513 | // Save/Load Functions
|
|---|
| 514 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 515 | /// Reload the settings store
|
|---|
| 516 | ///
|
|---|
| 517 | /// @author mickem
|
|---|
| 518 | virtual void reload() {
|
|---|
| 519 | throw settings_exception(_T("TODO: FIX ME: reload"));
|
|---|
| 520 | }
|
|---|
| 521 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 522 | /// Copy the settings store to another settings store
|
|---|
| 523 | ///
|
|---|
| 524 | /// @param other the settings store to save to
|
|---|
| 525 | ///
|
|---|
| 526 | /// @author mickem
|
|---|
| 527 | virtual void save_to(std::wstring other) {
|
|---|
| 528 | instance_ptr i = get_core()->create_instance(other);
|
|---|
| 529 | save_to(i);
|
|---|
| 530 | }
|
|---|
| 531 | virtual void save_to(instance_ptr other) {
|
|---|
| 532 | if (!other)
|
|---|
| 533 | throw settings_exception(_T("Cant migrate to NULL instance!"));
|
|---|
| 534 | other->clear_cache();
|
|---|
| 535 | st_copy_section(_T(""), other);
|
|---|
| 536 | other->save();
|
|---|
| 537 | }
|
|---|
| 538 | void st_copy_section(std::wstring path, instance_ptr other) {
|
|---|
| 539 | if (!other)
|
|---|
| 540 | throw settings_exception(_T("Failed to create new instance!"));
|
|---|
| 541 | string_list list = get_sections(path);
|
|---|
| 542 | std::wstring subpath = path;
|
|---|
| 543 | // TODO: check trailing / instead!
|
|---|
| 544 | if (!subpath.empty())
|
|---|
| 545 | subpath += _T("/");
|
|---|
| 546 | for (string_list::const_iterator cit = list.begin();cit != list.end(); ++cit) {
|
|---|
| 547 | st_copy_section(subpath + *cit, other);
|
|---|
| 548 | }
|
|---|
| 549 | list = get_keys(path);
|
|---|
| 550 | for (string_list::const_iterator cit = list.begin();cit != list.end(); ++cit) {
|
|---|
| 551 | settings_core::key_path_type key(path, *cit);
|
|---|
| 552 | settings_core::key_type type = get_key_type(key.first, key.second);
|
|---|
| 553 | if (type ==settings_core::key_string) {
|
|---|
| 554 | try {
|
|---|
| 555 | other->set_string(key.first, key.second, get_string(key.first, key.second));
|
|---|
| 556 | } catch (KeyNotFoundException e) {
|
|---|
| 557 | other->set_string(key.first, key.second, _T(""));
|
|---|
| 558 | }
|
|---|
| 559 | } else if (type ==settings_core::key_integer)
|
|---|
| 560 | other->set_int(key.first, key.second, get_int(key.first, key.second));
|
|---|
| 561 | else if (type ==settings_core::key_bool)
|
|---|
| 562 | other->set_bool(key.first, key.second, get_bool(key.first, key.second));
|
|---|
| 563 | else
|
|---|
| 564 | throw settings_exception(_T("Invalid type for key: ") + key.first + _T(".") + key.second);
|
|---|
| 565 | }
|
|---|
| 566 | }
|
|---|
| 567 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 568 | /// Save the settings store
|
|---|
| 569 | ///
|
|---|
| 570 | /// @author mickem
|
|---|
| 571 | virtual void save() {
|
|---|
| 572 | MUTEX_GUARD();
|
|---|
| 573 | BOOST_FOREACH(std::wstring path, path_cache_) {
|
|---|
| 574 | set_real_path(path);
|
|---|
| 575 | }
|
|---|
| 576 | std::set<std::wstring> sections;
|
|---|
| 577 | for (cache_type::const_iterator cit = settings_cache_.begin(); cit != settings_cache_.end(); ++cit) {
|
|---|
| 578 | set_real_value((*cit).first, (*cit).second);
|
|---|
| 579 | sections.insert((*cit).first.first);
|
|---|
| 580 | }
|
|---|
| 581 | BOOST_FOREACH(std::wstring str, get_core()->get_reg_sections()) {
|
|---|
| 582 | set_real_path(str);
|
|---|
| 583 | }
|
|---|
| 584 | }
|
|---|
| 585 | /////////////////////////////////////////////////////////////////////////
|
|---|
| 586 | /// Load from another settings store
|
|---|
| 587 | ///
|
|---|
| 588 | /// @param other the other settings store to load from
|
|---|
| 589 | ///
|
|---|
| 590 | /// @author mickem
|
|---|
| 591 | virtual void load_from(instance_ptr other) {
|
|---|
| 592 | throw settings_exception(_T("TODO: FIX ME: load_from"));
|
|---|
| 593 | }
|
|---|
| 594 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 595 | /// Load from another context.
|
|---|
| 596 | /// The context is an identifier for the settings store for INI/XML it is the filename.
|
|---|
| 597 | ///
|
|---|
| 598 | /// @param context the context to load from
|
|---|
| 599 | ///
|
|---|
| 600 | /// @author mickem
|
|---|
| 601 | virtual void load_from(std::wstring context) {
|
|---|
| 602 | throw settings_exception(_T("TODO: FIX ME: load_from"));
|
|---|
| 603 | }
|
|---|
| 604 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 605 | /// Load settings from the context.
|
|---|
| 606 | ///
|
|---|
| 607 | /// @author mickem
|
|---|
| 608 | virtual void load() {
|
|---|
| 609 | throw settings_exception(_T("TODO: FIX ME: load"));
|
|---|
| 610 | }
|
|---|
| 611 |
|
|---|
| 612 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 613 | /// VIRTUAL FUNCTIONS ////
|
|---|
| 614 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 615 |
|
|---|
| 616 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 617 | /// Get all (sub) sections (given a path).
|
|---|
| 618 | /// If the path is empty all root sections will be returned
|
|---|
| 619 | ///
|
|---|
| 620 | /// @param path The path to get sections from (if empty root sections will be returned)
|
|---|
| 621 | /// @param list The list to append nodes to
|
|---|
| 622 | /// @return a list of sections
|
|---|
| 623 | ///
|
|---|
| 624 | /// @author mickem
|
|---|
| 625 | virtual void get_real_sections(std::wstring path, string_list &list) = 0;
|
|---|
| 626 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 627 | /// Get all keys given a path/section.
|
|---|
| 628 | /// If the path is empty all root sections will be returned
|
|---|
| 629 | ///
|
|---|
| 630 | /// @param path The path to get sections from (if empty root sections will be returned)
|
|---|
| 631 | /// @param list The list to append nodes to
|
|---|
| 632 | /// @return a list of sections
|
|---|
| 633 | ///
|
|---|
| 634 | /// @author mickem
|
|---|
| 635 | virtual void get_real_keys(std::wstring path, string_list &list) = 0;
|
|---|
| 636 |
|
|---|
| 637 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 638 | /// Get a string value if it does not exist exception will be thrown
|
|---|
| 639 | ///
|
|---|
| 640 | /// @param key the key to lookup
|
|---|
| 641 | /// @return the string value
|
|---|
| 642 | ///
|
|---|
| 643 | /// @author mickem
|
|---|
| 644 | virtual std::wstring get_real_string(settings_core::key_path_type key) = 0;
|
|---|
| 645 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 646 | /// Get an integer value if it does not exist exception will be thrown
|
|---|
| 647 | ///
|
|---|
| 648 | /// @param key the key to lookup
|
|---|
| 649 | /// @return the int value
|
|---|
| 650 | ///
|
|---|
| 651 | /// @author mickem
|
|---|
| 652 | virtual int get_real_int(settings_core::key_path_type key) = 0;
|
|---|
| 653 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 654 | /// Get a boolean value if it does not exist exception will be thrown
|
|---|
| 655 | ///
|
|---|
| 656 | /// @param key the key to lookup
|
|---|
| 657 | /// @return the boolean value
|
|---|
| 658 | ///
|
|---|
| 659 | /// @author mickem
|
|---|
| 660 | virtual bool get_real_bool(settings_core::key_path_type key) = 0;
|
|---|
| 661 |
|
|---|
| 662 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 663 | /// Write a value to the resulting context.
|
|---|
| 664 | ///
|
|---|
| 665 | /// @param key The key to write to
|
|---|
| 666 | /// @param value The value to write
|
|---|
| 667 | ///
|
|---|
| 668 | /// @author mickem
|
|---|
| 669 | virtual void set_real_value(settings_core::key_path_type key, conainer value) = 0;
|
|---|
| 670 |
|
|---|
| 671 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 672 | /// Write a value to the resulting context.
|
|---|
| 673 | ///
|
|---|
| 674 | /// @param key The key to write to
|
|---|
| 675 | /// @param value The value to write
|
|---|
| 676 | ///
|
|---|
| 677 | /// @author mickem
|
|---|
| 678 | virtual void set_real_path(std::wstring path) = 0;
|
|---|
| 679 |
|
|---|
| 680 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 681 | /// Check if a key exists
|
|---|
| 682 | ///
|
|---|
| 683 | /// @param key the key to lookup
|
|---|
| 684 | /// @return true/false if the key exists.
|
|---|
| 685 | ///
|
|---|
| 686 | /// @author mickem
|
|---|
| 687 | virtual bool has_real_key(settings_core::key_path_type key) = 0;
|
|---|
| 688 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 689 | /// Get the type this settings store represent.
|
|---|
| 690 | ///
|
|---|
| 691 | /// @return the type of settings store
|
|---|
| 692 | ///
|
|---|
| 693 | /// @author mickem
|
|---|
| 694 | // virtual settings_core::settings_type get_type() = 0;
|
|---|
| 695 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 696 | /// Is this the active settings store
|
|---|
| 697 | ///
|
|---|
| 698 | /// @return
|
|---|
| 699 | ///
|
|---|
| 700 | /// @author mickem
|
|---|
| 701 | // virtual bool is_active() = 0;
|
|---|
| 702 |
|
|---|
| 703 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 704 | /// Create a new settings interface of "this kind"
|
|---|
| 705 | ///
|
|---|
| 706 | /// @param context the context to use
|
|---|
| 707 | /// @return the newly created settings interface
|
|---|
| 708 | ///
|
|---|
| 709 | /// @author mickem
|
|---|
| 710 | virtual SettingsInterfaceImpl* create_new_context(std::wstring context) = 0;
|
|---|
| 711 |
|
|---|
| 712 |
|
|---|
| 713 | virtual std::wstring to_string() {
|
|---|
| 714 | std::wstring ret = get_info();
|
|---|
| 715 | if (!children_.empty()) {
|
|---|
| 716 | ret += _T("parents = [");
|
|---|
| 717 | BOOST_FOREACH(parent_list_type::value_type i, children_) {
|
|---|
| 718 | ret += i->to_string();
|
|---|
| 719 | }
|
|---|
| 720 | ret += _T("]");
|
|---|
| 721 | }
|
|---|
| 722 | return ret + _T("}");
|
|---|
| 723 | }
|
|---|
| 724 | };
|
|---|
| 725 | }
|
|---|