diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2005-03-01 18:45:41 +0000 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2005-03-01 18:45:41 +0000 |
commit | b0d90c3fdc77d5b9373cb623f310ea7d30a86c4c (patch) | |
tree | fe1f75d5934e6648c5b99413236338cfa344ee2d /src/configuration.cpp | |
parent | 806438ec060fcf1a6f1cca3c81b8211979c601ed (diff) | |
download | mana-b0d90c3fdc77d5b9373cb623f310ea7d30a86c4c.tar.gz mana-b0d90c3fdc77d5b9373cb623f310ea7d30a86c4c.tar.bz2 mana-b0d90c3fdc77d5b9373cb623f310ea7d30a86c4c.tar.xz mana-b0d90c3fdc77d5b9373cb623f310ea7d30a86c4c.zip |
Simplified configuration.
Diffstat (limited to 'src/configuration.cpp')
-rw-r--r-- | src/configuration.cpp | 132 |
1 files changed, 54 insertions, 78 deletions
diff --git a/src/configuration.cpp b/src/configuration.cpp index 074de7c6..15c0827c 100644 --- a/src/configuration.cpp +++ b/src/configuration.cpp @@ -23,34 +23,30 @@ #include "configuration.h" +#include "log.h" + #include <math.h> #include <iostream> #include <fstream> +#include <sstream> +#include <libxml/parser.h> +#include <libxml/tree.h> -Configuration::OptionValue::OptionValue(): - numericValue(0.0f) +void Configuration::init(std::string filename) { -} - -void Configuration::init(std::string filename) { - /* NOTE: - * first a line is checked wether it is a comment or not by - * looking for INI_COMMENTER. after this another check is - * done for INI_DELIMITER if the previous check failed. - * if this line is a valid option all spaces in it get - * stripped away (including the value) and it is added to - * the list iniOptions. - */ + //xmlDocPtr doc = xmlReadFile(filename.c_str(), NULL, 0); std::ifstream inFile(filename.c_str(), std::ifstream::in); std::string inBuffer; unsigned int position; - iniOptions.clear(); + options.clear(); - while (inFile.good()) { + while (inFile.good()) + { std::getline(inFile, inBuffer, '\n'); - if (inBuffer.substr(0, 1) != INI_COMMENTER) { + if (inBuffer.substr(0, 1) != INI_COMMENTER) + { // Replace spaces with void while (inBuffer.find(" ", 0) != std::string::npos) { inBuffer.replace(inBuffer.find(" ", 0), 1, ""); @@ -61,58 +57,36 @@ void Configuration::init(std::string filename) { if (position != std::string::npos) { std::string key = inBuffer.substr(0, position); - OptionValue optionTmp; if (inBuffer.length() > position + 1) { - optionTmp.stringValue = inBuffer.substr( - position + 1, inBuffer.length()); - optionTmp.numericValue = atof( - optionTmp.stringValue.c_str()); + options[key] = + inBuffer.substr(position + 1, inBuffer.length()); } - iniOptions[key] = optionTmp; - - #ifdef __DEBUG - std::cout << "Configuration::init(" << key << ", \"" << - optionTmp.stringValue << "\" / " << - optionTmp.numericValue << ")\n"; - #endif + log("Configuration::init(%s, \"%s\")", + key.c_str(), options[key].c_str()); } } } + inFile.close(); } -bool Configuration::write(std::string filename) { +bool Configuration::write(std::string filename) +{ + std::map<std::string, std::string>::iterator iter; std::ofstream out(filename.c_str(), std::ofstream::out | std::ofstream::trunc); - char tmp[20]; - OptionValue optionTmp; - for (iter = iniOptions.begin(); iter != iniOptions.end(); iter++) { + for (iter = options.begin(); iter != options.end(); iter++) + { + log("Configuration::write(%s, \"%s\")", + iter->first.c_str(), iter->second.c_str()); + out.write(iter->first.c_str(), iter->first.length()); out.write("=", 1); - - double numericValue = iter->second.numericValue; - - if (numericValue == 0) { - out.write(iter->second.stringValue.c_str(), - iter->second.stringValue.length()); - } - else if (numericValue == floor(numericValue)) { - out << (int)numericValue; - } - else { - sprintf(tmp, "%f", numericValue); - out.write(tmp, strlen(tmp)); - strcpy(tmp, ""); - } - #ifdef __DEBUG - std::cout << "Configuration::write(" << iter->first.c_str() << - ", \"" << iter->second.stringValue << "\" / " << - numericValue << ")\n"; - #endif + out.write(iter->second.c_str(), iter->second.length()); out.write("\n", 1); } @@ -120,40 +94,42 @@ bool Configuration::write(std::string filename) { return true; } -void Configuration::setValue(std::string key, std::string value) { - OptionValue optionTmp; - #ifdef __DEBUG - std::cout << "Configuration::setValue(" << key << ", " << value << - ")\n"; - #endif - optionTmp.stringValue = value; - optionTmp.numericValue = 0; - iniOptions[key] = optionTmp; +void Configuration::setValue(std::string key, std::string value) +{ +#ifdef __DEBUG + std::cout << "Configuration::setValue(" << key << ", " << value << ")\n"; +#endif + options[key] = value; } -void Configuration::setValue(std::string key, float value) { - OptionValue optionTmp; - #ifdef __DEBUG - std::cout << "Configuration::setValue(" << key << ", " << value << - ")\n"; - #endif - optionTmp.stringValue = ""; - optionTmp.numericValue = value; - iniOptions[key] = optionTmp; +void Configuration::setValue(std::string key, float value) +{ +#ifdef __DEBUG + std::cout << "Configuration::setValue(" << key << ", " << value << ")\n"; +#endif + std::stringstream ss; + if (value == floor(value)) { + ss << (int)value; + } else { + ss << value; + } + options[key] = ss.str(); } -std::string Configuration::getValue(std::string key, std::string deflt) { - iter = iniOptions.find(key); - if (iter != iniOptions.end()) { - return iniOptions[key].stringValue; +std::string Configuration::getValue(std::string key, std::string deflt) +{ + std::map<std::string, std::string>::iterator iter = options.find(key); + if (iter != options.end()) { + return options[key]; } return deflt; } -float Configuration::getValue(std::string key, float deflt) { - iter = iniOptions.find(key); - if (iter != iniOptions.end()) { - return iniOptions[key].numericValue; +float Configuration::getValue(std::string key, float deflt) +{ + std::map<std::string, std::string>::iterator iter = options.find(key); + if (iter != options.end()) { + return atof(options[key].c_str()); } return deflt; } |