Remove whitespaces from system config lines

to fix errors with files that have different line endings than the
system. Output a line number when something is wrong. This should be
done wherever reading from text files probably.
This commit is contained in:
Bim Overbohm 2013-05-22 19:13:55 +02:00
parent edc26aa4e1
commit c989aae1c3

View file

@ -181,28 +181,31 @@ void SystemData::loadConfig()
std::ifstream file(path.c_str()); std::ifstream file(path.c_str());
if(file.is_open()) if(file.is_open())
{ {
size_t lineNr = 0;
std::string line; std::string line;
std::string sysName, sysDescName, sysPath, sysExtension, sysCommand; std::string sysName, sysDescName, sysPath, sysExtension, sysCommand;
while(file.good()) while(file.good())
{ {
lineNr++;
std::getline(file, line); std::getline(file, line);
//remove whitespace from line through STL and lambda magic
line.erase(std::remove_if(line.begin(), line.end(), [&](char c){ return std::string("\t\r\n\v\f").find(c) != std::string::npos; }), line.end());
//skip blank lines and comments //skip blank lines and comments
if(line.empty() || line[0] == *"#") if(line.empty() || line.at(0) == '#')
continue; continue;
//find the name (left of the equals sign) and the value (right of the equals sign) //find the name (left of the equals sign) and the value (right of the equals sign)
bool lineValid = false; bool lineValid = false;
std::string varName, varValue; std::string varName;
for(unsigned int i = 0; i < line.length(); i++) std::string varValue;
{ const std::string::size_type equalsPos = line.find('=', 1);
if(line[i] == *"=") if(equalsPos != std::string::npos)
{ {
lineValid = true; lineValid = true;
varName = line.substr(0, i); varName = line.substr(0, equalsPos);
varValue = line.substr(i + 1, line.length() - 1); varValue = line.substr(equalsPos + 1, line.length() - 1);
break;
}
} }
if(lineValid) if(lineValid)
@ -242,7 +245,7 @@ void SystemData::loadConfig()
sysName = ""; sysDescName = ""; sysPath = ""; sysExtension = ""; sysCommand = "" ; sysName = ""; sysDescName = ""; sysPath = ""; sysExtension = ""; sysCommand = "" ;
} }
}else{ }else{
LOG(LogError) << "Error reading config file \"" << path << "\" - no equals sign found on line \"" << line << "\"!"; LOG(LogError) << "Error reading config file \"" << path << "\" - no equals sign found on line " << lineNr << ": \"" << line << "\"!";
return; return;
} }
} }