--- smsdaemon/ConfigFile.cpp 2008/12/18 23:19:08 201 +++ 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(configFile); + Open(); +} + + +bool ConfigFile::Reload() +{ + 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; @@ -113,3 +133,48 @@ } } +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; + } +}