#include "ConfigFile.h" #include const char SEPERATOR = '/'; std::string trim(std::string const& source, char const* delims = " \t\r\n") { std::string result(source); std::string::size_type index = result.find_last_not_of(delims); if (index != std::string::npos) result.erase(++index); index = result.find_first_not_of(delims); if (index != std::string::npos) result.erase(0, index); else result.erase(); return result; } ConfigFile::ConfigFile() { } ConfigFile::ConfigFile(std::string const& configFile) { Open(configFile); } bool ConfigFile::Open(std::string const& configFile) { std::ifstream file(configFile.c_str()); std::string line; std::string name; std::string value; std::string inSection; int posEqual; if (!file) { return false; } while (std::getline(file,line)) { if (! line.length()) continue; if (line[0] == '#') continue; if (line[0] == ';') continue; if (line[0] == '[') { inSection=trim(line.substr(1,line.find(']')-1)); continue; } posEqual=line.find('='); name = trim(line.substr(0,posEqual)); value = trim(line.substr(posEqual+1)); content_[inSection+SEPERATOR+name]=Value(value); } return true; } Value const& ConfigFile::GetValue(std::string const& section, std::string const& entry) const { std::map::const_iterator ci = content_.find(section + SEPERATOR + entry); if (ci == content_.end()) throw "does not exist"; return ci->second; } Value const& ConfigFile::GetValue(std::string const& section, std::string const& entry, double value) { try { return GetValue(section, entry); } catch (const char *) { return content_.insert(std::make_pair(section+SEPERATOR+entry, Value(value))).first->second; } } Value const& ConfigFile::GetValue(std::string const& section, std::string const& entry, std::string const& value) { try { return GetValue(section, entry); } catch (const char *) { return content_.insert(std::make_pair(section+SEPERATOR+entry, Value(value))).first->second; } }