void ConfigParser::parse( const std::string& line )
{
// if line is empty or starts with the comment character return from parse
if( line.size() == 0 || line[ 0 ] == '#' )
return;
// if line does not end with a ; report error and skip line
if( line[ line.length() - 1 ] != ';' ) {
std::cerr << "Malformed setting found in file " << _filename << " on line "
<< std::to_string( _lineIndex ) << ": " << line
<< "\nReason: No ';' found" << std::endl;
return;
}
std::size_t equalSign = line.find( '=' );
// when no '=' is found report error and skip line
if( equalSign == std::string::npos ) {
std::cerr << "Malformed setting found in file " << _filename << " on line "
<< std::to_string( _lineIndex ) << ": " << line
<< "\nReason: No '=' found" << std::endl;
return;
}
// everything is ok, so parse the key-value pair and store it
std::string key{}, value{};
key = line.substr( 0, equalSign );
trim( key );
// make key always lower-case
std::transform( key.begin(), key.end(), key.begin(), ::tolower );
value = line.substr( equalSign + 1, line.length() - (equalSign + 1) - 1 );
trim( value );
// first look into data if 'key' is already present
auto itr = _data.find( key );
if( itr == _data.end() ) {
// if not add key-value pair
_data[ key ] = value;
} else {
// if yes report this error and skip the line
std::cerr << "Duplicate key " << key << " found in file " << _filename << " at line "
<< _lineIndex << ". Skipping value" << std::endl;
return;
}
}