Fix parsing of config file where '=' is not preceded by space

This commit is contained in:
Rogier 2015-12-16 19:16:22 +01:00
parent 919595c614
commit 7c43e42ea9
2 changed files with 11 additions and 23 deletions

View File

@ -1,4 +1,5 @@
#include <iostream>
#include "Settings.h"
static std::string trim(const std::string s)
@ -51,31 +52,16 @@ bool Settings::getGeneric(std::string key, std::string *pvalue)
m_file.seekg(0);
for (std::getline(m_file,line); m_file.good(); std::getline(m_file,line)) {
linenr++;
std::istringstream iline;
iline.str(line);
iline >> std::skipws;
std::string variable;
std::string eq;
iline >> variable;
if (variable != key)
size_t keylen = line.find_first_of('=');
if (keylen == std::string::npos) {
std::cerr << "Error parsing config line at " << m_filename << ":" << linenr << ": expected: <name> = <value> ('=' not found)";
continue;
}
if (trim(line.substr(0, keylen)) != key)
continue;
found = true;
iline >> eq;
if (m_file.fail() || eq != "=") {
std::ostringstream oss;
oss << "Error parsing '" << key << "' in file " << m_messageName << " at line " << linenr << " (missing '=')";
throw std::runtime_error(oss.str());
}
if (pvalue) {
std::string value;
iline >> value;
if (m_file.fail()) {
std::ostringstream oss;
oss << "Error parsing value for '" << key << "' in file " << m_messageName << " at line " << linenr;
throw std::runtime_error(oss.str());
}
*pvalue = trim(value);
}
if (pvalue)
*pvalue = trim(line.substr(keylen + 1));
}
return found;
}

View File

@ -30,6 +30,8 @@ DBPostgreSQL::DBPostgreSQL(const std::string &mapdir) :
if (!info_found)
throw std::runtime_error("Set postgresql_connection_info or pg_connection_info in world.mt to use the postgresql backend");
connection_info += "fallback_application_name=minetestmapper " + connection_info;
m_connection = PQconnectdb(connection_info.c_str());
if (PQstatus(m_connection) != CONNECTION_OK) {
throw std::runtime_error(std::string("Failed to connect to postgresql database: ")