--- smsdaemon/ConfigFile.cpp 2008/12/07 20:06:12 146 +++ smsdaemon/ConfigFile.cpp 2008/12/21 18:41:08 208 @@ -1,6 +1,11 @@ #include "ConfigFile.h" +#include "Util.h" #include +#include +#include + +#include const char SEPERATOR = '/'; @@ -24,13 +29,28 @@ } ConfigFile::ConfigFile(std::string const& configFile) + : filename_(configFile) +{ + Open(); +} + + +bool ConfigFile::Reload() { - Open(configFile); + content_.clear(); + return Open(); } bool ConfigFile::Open(std::string const& configFile) { - std::ifstream file(configFile.c_str()); + content_.clear(); + filename_ = configFile; + return Open(); +} + +bool ConfigFile::Open() +{ + std::ifstream file( filename_.c_str() ); std::string line; std::string name; @@ -61,8 +81,7 @@ name = trim(line.substr(0,posEqual)); value = trim(line.substr(posEqual+1)); - - content_[inSection+SEPERATOR+name]=Value(value); + content_.insert(std::make_pair(inSection+SEPERATOR+name,Value(value))); } return true; } @@ -70,13 +89,26 @@ Value const& ConfigFile::GetValue(std::string const& section, std::string const& entry) const { - std::map::const_iterator ci = content_.find(section + SEPERATOR + entry); + content_iterator ci = content_.find(section + SEPERATOR + entry); if (ci == content_.end()) throw "does not exist"; return ci->second; } +std::vector ConfigFile::GetValues(std::string const& section, std::string const& entry) const +{ + std::vector values; + std::string search = section + SEPERATOR + entry; + std::pair range = content_.equal_range(search); + + for (content_iterator it=range.first; it != range.second; ++it) + { + values.push_back( it->second ); + } + return values; +} + Value const& ConfigFile::GetValue(std::string const& section, std::string const& entry, double value) { try @@ -85,7 +117,7 @@ } catch (const char *) { - return content_.insert(std::make_pair(section+SEPERATOR+entry, Value(value))).first->second; + return content_.insert(std::make_pair(section+SEPERATOR+entry, Value(value)))->second; } } @@ -97,7 +129,52 @@ } catch (const char *) { - return content_.insert(std::make_pair(section+SEPERATOR+entry, Value(value))).first->second; + return content_.insert(std::make_pair(section+SEPERATOR+entry, Value(value)))->second; + } +} + +std::string ConfigFile::DumpConfig() const +{ + std::ostringstream ss; + for (content_iterator it=content_.begin(); it!=content_.end(); ++it) + { + ss << it->first << "=" << it->second.StringValue() << std::endl; } + return ss.str(); } + +namespace ConfigHelper +{ + std::map ParseArguments(const std::string& args) + { + std::map config; + + std::vector sections = Util::str_split(args, ";"); + + for (unsigned i=0; i params = Util::str_split( current, "="); + if (params.size() != 2) + throw std::runtime_error(std::string("syntax error in config line: ") + args); + + std::string key = Util::str_tolower(Util::str_trim(params[0])); + std::string val = Util::str_trim(params[1]); + config[key] = val; + } + + return config; + } + + int StringToInt(const std::string& input) + { + char* endptr = (char*) input.c_str(); + int retval = strtol(input.c_str(), &endptr, 10); + + if ( *endptr != 0) + throw std::runtime_error( std::string("Error: can not convert this to a number: ") + input); + + return retval; + } +}