From b0d90c3fdc77d5b9373cb623f310ea7d30a86c4c Mon Sep 17 00:00:00 2001 From: Bjørn Lindeijer Date: Tue, 1 Mar 2005 18:45:41 +0000 Subject: Simplified configuration. --- src/configuration.cpp | 132 +++++++++++++++++++++----------------------------- src/configuration.h | 24 +-------- 2 files changed, 56 insertions(+), 100 deletions(-) (limited to 'src') 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 #include #include +#include +#include +#include -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::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::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::iterator iter = options.find(key); + if (iter != options.end()) { + return atof(options[key].c_str()); } return deflt; } diff --git a/src/configuration.h b/src/configuration.h index 03950230..6f1cfb11 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -76,29 +76,9 @@ class Configuration { * \param deflt Default option if not there or error. */ float getValue(std::string key, float deflt); - private: - /** - * Returns wether they given key exists. - */ - bool keyExists(std::string key); - - /** - * A simple data structure to store the value of a configuration - * option. - */ - class OptionValue { - public: - /** - * Constructor. - */ - OptionValue(); - std::string stringValue; - float numericValue; - }; - - std::map iniOptions; - std::map::iterator iter; + private: + std::map options; }; #endif -- cgit v1.2.3-60-g2f50