diff options
author | Yohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer> | 2010-07-28 19:57:31 +0200 |
---|---|---|
committer | Yohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer> | 2010-07-28 19:57:31 +0200 |
commit | 2b1c0dcf269d617de1f6c203df547166661f089e (patch) | |
tree | 5d247d694f28ddbeb6e2da26b9db02eeab5c7480 /src/configuration.cpp | |
parent | 44d8856c777790449df46e3b5348a6a7433a75d8 (diff) | |
download | mana-2b1c0dcf269d617de1f6c203df547166661f089e.tar.gz mana-2b1c0dcf269d617de1f6c203df547166661f089e.tar.bz2 mana-2b1c0dcf269d617de1f6c203df547166661f089e.tar.xz mana-2b1c0dcf269d617de1f6c203df547166661f089e.zip |
Centralized configuration default values using the VariableData system.
Please note that I didn't turned all the getValue() call into new ones,
simply because I have to have config object initiated which is not
forcefully the case the branding file.
Resolves: Manasource Mantis #170.
Diffstat (limited to 'src/configuration.cpp')
-rw-r--r-- | src/configuration.cpp | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/src/configuration.cpp b/src/configuration.cpp index d8b11034..a6f691d8 100644 --- a/src/configuration.cpp +++ b/src/configuration.cpp @@ -98,6 +98,126 @@ ConfigurationObject::~ConfigurationObject() clear(); } +void Configuration::cleanDefaults() +{ + if (mDefaultsData) + { + for (DefaultsData::const_iterator iter = mDefaultsData->begin(); + iter != mDefaultsData->end(); iter++) + { + if (iter->second) + delete(iter->second); + } + mDefaultsData->clear(); + delete mDefaultsData; + mDefaultsData = 0; + } +} + +Configuration::~Configuration() +{ + cleanDefaults(); +} + +void Configuration::setDefaultValues(DefaultsData *defaultsData) +{ + cleanDefaults(); + mDefaultsData = defaultsData; +} + +int Configuration::getIntValue(const std::string &key) const +{ + int defaultValue = 0; + if (mDefaultsData) + { + DefaultsData::const_iterator itdef = mDefaultsData->find(key); + + if (itdef != mDefaultsData->end() && itdef->second + && itdef->second->getType() == Mana::VariableData::DATA_INT) + { + defaultValue = ((Mana::IntData*)itdef->second)->getData(); + } + else + { + logger->log("%s: No integer value in registry for key %s", + mConfigPath.c_str(), key.c_str()); + } + } + Options::const_iterator iter = mOptions.find(key); + return (iter != mOptions.end()) ? atoi(iter->second.c_str()) : defaultValue; +} + +std::string Configuration::getStringValue(const std::string &key) const +{ + std::string defaultValue = ""; + if (mDefaultsData) + { + DefaultsData::const_iterator itdef = mDefaultsData->find(key); + + if (itdef != mDefaultsData->end() && itdef->second + && itdef->second->getType() == Mana::VariableData::DATA_STRING) + { + defaultValue = ((Mana::StringData*)itdef->second)->getData(); + } + else + { + logger->log("%s: No string value in registry for key %s", + mConfigPath.c_str(), key.c_str()); + } + } + Options::const_iterator iter = mOptions.find(key); + return (iter != mOptions.end()) ? iter->second : defaultValue; +} + + +float Configuration::getFloatValue(const std::string &key) const +{ + float defaultValue = 0.0f; + if (mDefaultsData) + { + DefaultsData::const_iterator itdef = mDefaultsData->find(key); + + if (itdef != mDefaultsData->end() && itdef->second + && itdef->second->getType() == Mana::VariableData::DATA_FLOAT) + { + defaultValue = ((Mana::FloatData*)itdef->second)->getData(); + } + else + { + logger->log("%s: No float value in registry for key %s", + mConfigPath.c_str(), key.c_str()); + } + } + Options::const_iterator iter = mOptions.find(key); + return (iter != mOptions.end()) ? atof(iter->second.c_str()) : defaultValue; +} + +bool Configuration::getBoolValue(const std::string &key) const +{ + bool defaultValue = false; + if (mDefaultsData) + { + DefaultsData::const_iterator itdef = mDefaultsData->find(key); + + if (itdef != mDefaultsData->end() && itdef->second + && itdef->second->getType() == Mana::VariableData::DATA_BOOL) + { + defaultValue = ((Mana::BoolData*)itdef->second)->getData(); + } + else + { + logger->log("%s: No boolean value in registry for key %s", + mConfigPath.c_str(), key.c_str()); + } + } + + Options::const_iterator iter = mOptions.find(key); + if (iter != mOptions.end()) + return getBoolFromString(iter->second); + else + return defaultValue; +} + void ConfigurationObject::initFromXML(xmlNodePtr parent_node) { clear(); |