diff options
Diffstat (limited to 'src/server/shared/Configuration')
-rwxr-xr-x | src/server/shared/Configuration/Config.cpp | 108 | ||||
-rwxr-xr-x | src/server/shared/Configuration/Config.h | 36 |
2 files changed, 67 insertions, 77 deletions
diff --git a/src/server/shared/Configuration/Config.cpp b/src/server/shared/Configuration/Config.cpp index 9c3a906cd2b..d5a3a7e46bc 100755 --- a/src/server/shared/Configuration/Config.cpp +++ b/src/server/shared/Configuration/Config.cpp @@ -17,90 +17,98 @@ */ #include "Config.h" +#include <ace/Auto_Ptr.h> #include <ace/Configuration_Import_Export.h> +#include <ace/Thread_Mutex.h> -static bool GetValueHelper(ACE_Configuration_Heap* mConf, const char *name, ACE_TString &result) +namespace ConfigMgr { - if (!mConf) - return false; - ACE_GUARD_RETURN(ACE_Thread_Mutex, guard, (sConfig->mMtx), false); +namespace +{ + typedef ACE_Thread_Mutex LockType; + typedef ACE_Guard<LockType> GuardType; - ACE_TString section_name; - ACE_Configuration_Section_Key section_key; - ACE_Configuration_Section_Key root_key = mConf->root_section(); + std::string _filename; + ACE_Auto_Ptr<ACE_Configuration_Heap> _config; + LockType m_configLock; - int i = 0; - while (mConf->enumerate_sections(root_key, i, section_name) == 0) + // Defined here as it must not be exposed to end-users. + bool GetValueHelper(const char* name, ACE_TString &result) { - mConf->open_section(root_key, section_name.c_str(), 0, section_key); - if (mConf->get_string_value(section_key, name, result) == 0) - return true; - ++i; - } + GuardType guard(m_configLock); - return false; -} + if (_config.get() == 0) + return false; -Config::Config() : mConf(NULL) -{ -} + ACE_TString section_name; + ACE_Configuration_Section_Key section_key; + const ACE_Configuration_Section_Key &root_key = _config->root_section(); -Config::~Config() -{ - delete mConf; -} + 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; + } -bool Config::SetSource(const char *file) -{ - mFilename = file; - return Reload(); + return false; + } } -bool Config::Reload() +bool Load(const char* file) { - delete mConf; - mConf = new ACE_Configuration_Heap; - if (mConf->open() == 0) + GuardType guard(m_configLock); + + if (file) + _filename = file; + + _config.reset(new ACE_Configuration_Heap); + if (_config->open() == 0) { - ACE_Ini_ImpExp config_importer(*mConf); - if (config_importer.import_config(mFilename.c_str()) == 0) + ACE_Ini_ImpExp config_importer(*_config.get()); + if (config_importer.import_config(_filename.c_str()) == 0) return true; } - delete mConf; - mConf = NULL; + _config.reset(); return false; } -std::string Config::GetStringDefault(const char * name, std::string def) +std::string GetStringDefault(const char* name, const std::string &def) { ACE_TString val; - return GetValueHelper(mConf, name, val) ? val.c_str() : def; + return GetValueHelper(name, val) ? val.c_str() : def; }; -bool Config::GetBoolDefault(const char * name, const bool def) +bool GetBoolDefault(const char* name, bool def) { ACE_TString val; - if (!GetValueHelper(mConf, name, val)) + + if (!GetValueHelper(name, val)) return def; - const char* str = val.c_str(); - if(strcmp(str, "true") == 0 || strcmp(str, "TRUE") == 0 || - strcmp(str, "yes") == 0 || strcmp(str, "YES") == 0 || - strcmp(str, "1") == 0) - return true; - else - return false; + return (val == "true" || val == "TRUE" || val == "yes" || val == "YES" || + val == "1"); }; -int32 Config::GetIntDefault(const char * name, const int32 def) +int GetIntDefault(const char* name, int def) { ACE_TString val; - return GetValueHelper(mConf, name, val) ? atoi(val.c_str()) : def; + return GetValueHelper(name, val) ? atoi(val.c_str()) : def; }; -float Config::GetFloatDefault(const char * name, const float def) +float GetFloatDefault(const char* name, float def) { ACE_TString val; - return GetValueHelper(mConf, name, val) ? (float)atof(val.c_str()) : def; + return GetValueHelper(name, val) ? (float)atof(val.c_str()) : def; }; + +const std::string & GetFilename() +{ + GuardType guard(m_configLock); + return _filename; +} + +} // namespace diff --git a/src/server/shared/Configuration/Config.h b/src/server/shared/Configuration/Config.h index fa42bdcfc6c..46d019fd800 100755 --- a/src/server/shared/Configuration/Config.h +++ b/src/server/shared/Configuration/Config.h @@ -19,37 +19,19 @@ #ifndef CONFIG_H #define CONFIG_H -#include "Common.h" -#include <ace/Singleton.h> -#include "Define.h" +#include <string> -class ACE_Configuration_Heap; - -class Config +namespace ConfigMgr { - friend class ACE_Singleton<Config, ACE_Null_Mutex>; - Config(); - public: - ~Config(); - - bool SetSource(const char *file); - bool Reload(); - - std::string GetStringDefault(const char * name, std::string def); - bool GetBoolDefault(const char * name, const bool def); - int32 GetIntDefault(const char * name, const int32 def); - float GetFloatDefault(const char * name, const float def); - - std::string GetFilename() const { return mFilename; } - - ACE_Thread_Mutex mMtx; + bool Load(const char *file = NULL); - private: - std::string mFilename; - ACE_Configuration_Heap* mConf; -}; + std::string GetStringDefault(const char* name, const std::string& def); + bool GetBoolDefault(const char* name, bool def); + int GetIntDefault(const char* name, int def); + float GetFloatDefault(const char* name, float def); -#define sConfig ACE_Singleton<Config, ACE_Null_Mutex>::instance() + const std::string & GetFilename(); +} #endif |