diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2009-04-13 21:33:06 +0200 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2009-04-13 21:35:19 +0200 |
commit | 28bc8c0edb549cdbbe0832c4202630a039f738b1 (patch) | |
tree | 9989f31a5121f82af5aa264ebfe7096c5ea5030f /src/configuration.cpp | |
parent | 627e1bc7eee5b821d29b2161dec4991cae7bf9c0 (diff) | |
download | mana-28bc8c0edb549cdbbe0832c4202630a039f738b1.tar.gz mana-28bc8c0edb549cdbbe0832c4202630a039f738b1.tar.bz2 mana-28bc8c0edb549cdbbe0832c4202630a039f738b1.tar.xz mana-28bc8c0edb549cdbbe0832c4202630a039f738b1.zip |
Some cleanup of the Configuration interface
Mainly avoid all the convertions from integer to float and then to
string and also back from string to float and then to integer.
Diffstat (limited to 'src/configuration.cpp')
-rw-r--r-- | src/configuration.cpp | 40 |
1 files changed, 23 insertions, 17 deletions
diff --git a/src/configuration.cpp b/src/configuration.cpp index 3c3ae1d5..0f73e2fb 100644 --- a/src/configuration.cpp +++ b/src/configuration.cpp @@ -26,22 +26,13 @@ #include "utils/stringutils.h" #include "utils/xml.h" -void ConfigurationObject::setValue(const std::string &key, std::string value) +void ConfigurationObject::setValue(const std::string &key, + const std::string &value) { mOptions[key] = value; } -void ConfigurationObject::setValue(const std::string &key, float value) -{ - setValue(key, toString((value == (int)value) ? (int)value : value)); -} - -void Configuration::setValue(const std::string &key, float value) -{ - setValue(key, toString((value == (int)value) ? (int)value : value)); -} - -void Configuration::setValue(const std::string &key, std::string value) +void Configuration::setValue(const std::string &key, const std::string &value) { ConfigurationObject::setValue(key, value); @@ -57,15 +48,29 @@ void Configuration::setValue(const std::string &key, std::string value) } std::string ConfigurationObject::getValue(const std::string &key, - std::string deflt) + const std::string &deflt) const { - OptionIterator iter = mOptions.find(key); + Options::const_iterator iter = mOptions.find(key); return ((iter != mOptions.end()) ? iter->second : deflt); } -float ConfigurationObject::getValue(const std::string &key, float deflt) +int ConfigurationObject::getValue(const std::string &key, int deflt) const +{ + Options::const_iterator iter = mOptions.find(key); + return (iter != mOptions.end()) ? atoi(iter->second.c_str()) : deflt; +} + +unsigned ConfigurationObject::getValue(const std::string &key, + unsigned deflt) const +{ + Options::const_iterator iter = mOptions.find(key); + return (iter != mOptions.end()) ? atol(iter->second.c_str()) : deflt; +} + +double ConfigurationObject::getValue(const std::string &key, + double deflt) const { - OptionIterator iter = mOptions.find(key); + Options::const_iterator iter = mOptions.find(key); return (iter != mOptions.end()) ? atof(iter->second.c_str()) : deflt; } @@ -158,7 +163,8 @@ void Configuration::init(const std::string &filename) void ConfigurationObject::writeToXML(xmlTextWriterPtr writer) { - for (OptionIterator i = mOptions.begin(); i != mOptions.end(); i++) + for (Options::const_iterator i = mOptions.begin(), i_end = mOptions.end(); + i != i_end; ++i) { xmlTextWriterStartElement(writer, BAD_CAST "option"); xmlTextWriterWriteAttribute(writer, |