36 configuration::~configuration(){}
38 int configuration::getintvalue(
const std::string& key )
41 std::cout<<
"Cannot find the key in the config file, Key: "<<key<<endl;
44 std::istringstream ss( this->data.operator [] ( key ) );
49 std::cout<<
"Problematic key: "<<key<<endl;
50 std::cout<<
"Error reading configuration file"<<endl;
57 double configuration::getDoubleValue(
const std::string& key )
59 if (!iskey( key ))
throw 0;
60 std::istringstream ss( this->data.operator [] ( key ) );
63 if (!ss.eof())
throw 1;
67 std::string configuration::getStringValue(
const std::string& key )
69 if (!iskey( key ))
throw 0;
70 std::istringstream ss( this->data.operator [] ( key ) );
73 if (!ss.eof())
throw 1;
77 void configuration::readFile(
const std::string filename)
79 std::string s, key, value;
80 std::ifstream confFile(&filename[0]);
81 if(!confFile.is_open())
83 std::cout<<
"Warning! Config.ini file not found!"<<std::endl;
88 while (std::getline( confFile, s ))
90 std::string::size_type begin = s.find_first_not_of(
" \f\t\v" );
93 if (begin == std::string::npos)
continue;
96 if (std::string(
"#;" ).find( s[ begin ] ) != std::string::npos)
continue;
99 std::string::size_type end = s.find(
'=', begin );
100 key = s.substr( begin, end - begin );
103 key.erase( key.find_last_not_of(
" \f\t\v" ) + 1 );
106 if (key.empty())
continue;
109 begin = s.find_first_not_of(
" \f\n\r\t\v", end + 1 );
110 end = s.find_last_not_of(
" \f\n\r\t\v" ) + 1;
112 value = s.substr( begin, end - begin );
115 this->data[ key ] = value;
122 void configuration::writeToFile(
const std::string filename)
124 ofstream confFile(&filename[0]);
125 for (std::map <std::string, std::string>::const_iterator iter = this->data.begin(); iter != this->data.end(); iter++)
126 confFile << iter->first <<
" = " << iter->second << endl;
A class to read a learning configuration from a .ini file.