From 779a59e7e218e340ac5cefd3297ea970d804239d Mon Sep 17 00:00:00 2001 From: Shauren Date: Mon, 15 Jul 2013 17:31:44 +0200 Subject: Core/Config: Refactored ConfigMgr * Loading initial configuration files is now separate from loading any additional custom configs --- src/server/shared/Configuration/Config.cpp | 103 +++++++++++++++-------------- 1 file changed, 54 insertions(+), 49 deletions(-) (limited to 'src/server/shared/Configuration/Config.cpp') diff --git a/src/server/shared/Configuration/Config.cpp b/src/server/shared/Configuration/Config.cpp index 5d5cc02616b..47d59a57100 100644 --- a/src/server/shared/Configuration/Config.cpp +++ b/src/server/shared/Configuration/Config.cpp @@ -17,72 +17,79 @@ */ #include "Config.h" -#include -#include -#include +#include "Errors.h" -namespace ConfigMgr +// Defined here as it must not be exposed to end-users. +bool ConfigMgr::GetValueHelper(const char* name, ACE_TString &result) { + GuardType guard(_configLock); -namespace -{ - typedef ACE_Thread_Mutex LockType; - typedef ACE_Guard GuardType; + if (_config.get() == 0) + return false; - std::string _filename; - ACE_Auto_Ptr _config; - LockType m_configLock; + ACE_TString section_name; + ACE_Configuration_Section_Key section_key; + const ACE_Configuration_Section_Key &root_key = _config->root_section(); - // Defined here as it must not be exposed to end-users. - bool GetValueHelper(const char* name, ACE_TString &result) + int i = 0; + while (_config->enumerate_sections(root_key, i, section_name) == 0) { - GuardType guard(m_configLock); - - if (_config.get() == 0) - return false; - - ACE_TString section_name; - ACE_Configuration_Section_Key section_key; - const ACE_Configuration_Section_Key &root_key = _config->root_section(); - - int i = 0; - while (_config->enumerate_sections(root_key, i, section_name) == 0) - { - _config->open_section(root_key, section_name.c_str(), 0, section_key); - if (_config->get_string_value(section_key, name, result) == 0) - return true; - ++i; - } - - return false; + _config->open_section(root_key, section_name.c_str(), 0, section_key); + if (_config->get_string_value(section_key, name, result) == 0) + return true; + ++i; } + + return false; } -bool Load(const char* file) +bool ConfigMgr::LoadInitial(char const* file) { - GuardType guard(m_configLock); + ASSERT(file); - if (file) - _filename = file; + GuardType guard(_configLock); - _config.reset(new ACE_Configuration_Heap); + _filename = file; + _config.reset(new ACE_Configuration_Heap()); if (_config->open() == 0) - { - ACE_Ini_ImpExp config_importer(*_config.get()); - if (config_importer.import_config(_filename.c_str()) == 0) + if (LoadData(_filename.c_str())) return true; - } + _config.reset(); return false; } -std::string GetStringDefault(const char* name, const std::string &def) +bool ConfigMgr::LoadMore(char const* file) +{ + ASSERT(file); + ASSERT(_config); + + GuardType guard(_configLock); + + return LoadData(file); +} + +bool ConfigMgr::Reload() +{ + return LoadInitial(_filename.c_str()); +} + +bool ConfigMgr::LoadData(char const* file) +{ + ACE_Ini_ImpExp config_importer(*_config.get()); + if (config_importer.import_config(file) == 0) + return true; + + return false; +} + +std::string ConfigMgr::GetStringDefault(const char* name, const std::string &def) { ACE_TString val; return GetValueHelper(name, val) ? val.c_str() : def; } -bool GetBoolDefault(const char* name, bool def) +bool ConfigMgr::GetBoolDefault(const char* name, bool def) { ACE_TString val; @@ -93,22 +100,20 @@ bool GetBoolDefault(const char* name, bool def) val == "1"); } -int GetIntDefault(const char* name, int def) +int ConfigMgr::GetIntDefault(const char* name, int def) { ACE_TString val; return GetValueHelper(name, val) ? atoi(val.c_str()) : def; } -float GetFloatDefault(const char* name, float def) +float ConfigMgr::GetFloatDefault(const char* name, float def) { ACE_TString val; return GetValueHelper(name, val) ? (float)atof(val.c_str()) : def; } -const std::string & GetFilename() +std::string const& ConfigMgr::GetFilename() { - GuardType guard(m_configLock); + GuardType guard(_configLock); return _filename; } - -} // namespace -- cgit v1.2.3