From a5ec5ff39e2ff3caeac3ca82510182beabd45cda Mon Sep 17 00:00:00 2001 From: Carlos Daniel Ruvalcaba Valenzuela Date: Fri, 14 Sep 2007 18:05:04 -0700 Subject: [PATCH] Updated configuration class, added default values and setters --- include/libfmail/configuration.h | 13 ++++++---- src/configuration.cpp | 53 +++++++++++++++++++++++++++++++++++----- 2 files changed, 55 insertions(+), 11 deletions(-) diff --git a/include/libfmail/configuration.h b/include/libfmail/configuration.h index a3e6321..395be02 100644 --- a/include/libfmail/configuration.h +++ b/include/libfmail/configuration.h @@ -22,13 +22,16 @@ class Configuration{ private: std::map conf_map; public: - Configuration(char *filename); + Configuration(char *filename = NULL); ~Configuration(); - std::string getString(char *key); - const char *getCString(char *key); - int getInt(char *key); - float getFloat(char *key); + int Load(char *filename); + int Save(char *filename); + + std::string getString(char *key, char *default_value); + const char *getCString(char *key, char *default_value); + int getInt(char *key, int default_value); + float getFloat(char *key, float default_value); void setString(char *key, char *value); void setInt(char *key, int value); diff --git a/src/configuration.cpp b/src/configuration.cpp index a0e2509..e412e77 100644 --- a/src/configuration.cpp +++ b/src/configuration.cpp @@ -25,6 +25,14 @@ with this program; if not, write to the Free Software Foundation, Inc., /* TODO: Do checking of file inputs */ Configuration::Configuration(char *filename){ + if (filename) + this->Load(filename); +} + +Configuration::~Configuration(){ +} + +int Configuration::Load(char *filename){ std::fstream conf; std::string buffer, vname, eqop, vvalue; @@ -38,38 +46,71 @@ Configuration::Configuration(char *filename){ if (eqop == "=") conf_map[vname] = vvalue; } + + return 0; } -Configuration::~Configuration(){ +/* TODO: Implement saving */ +int Configuration::Save(char *filename){ + if (filename) + return 0; + return 1; } -std::string Configuration::getString(char *key){ +std::string Configuration::getString(char *key, char *default_value){ + std::map::iterator it; + std::string ret; + + if (conf_map.find(key) == conf_map.end()){ + return std::string(default_value); + } + return conf_map[key]; } -const char* Configuration::getCString(char *key){ +const char* Configuration::getCString(char *key, char *default_value){ + if (conf_map.find(key) == conf_map.end()){ + return default_value; + } + return conf_map[key].c_str(); } -int Configuration::getInt(char *key){ +int Configuration::getInt(char *key, int default_value){ + if (conf_map.find(key) == conf_map.end()){ + return default_value; + } + return atoi(conf_map[key].c_str()); } -float Configuration::getFloat(char *key){ +float Configuration::getFloat(char *key, float default_value){ + if (conf_map.find(key) == conf_map.end()){ + return default_value; + } + return atof(conf_map[key].c_str()); } /* TODO: Fill setString */ void Configuration::setString(char *key, char *value){ - + conf_map[key] = std::string(value); } /* TODO: Fill setInt */ void Configuration::setInt(char *key, int value){ + char buffer[32]; + + sprintf(buffer, "%i", value); + conf_map[key] = std::string(buffer); } /* TODO: Fill setFloat */ void Configuration::setFloat(char *key, float value){ + char buffer[32]; + + sprintf(buffer, "%f", value); + conf_map[key] = std::string(buffer); } -- 2.11.4.GIT