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