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" #include "Settings.h"
static std::string trim(const std::string s) 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); m_file.seekg(0);
for (std::getline(m_file,line); m_file.good(); std::getline(m_file,line)) { for (std::getline(m_file,line); m_file.good(); std::getline(m_file,line)) {
linenr++; linenr++;
std::istringstream iline; size_t keylen = line.find_first_of('=');
iline.str(line); if (keylen == std::string::npos) {
iline >> std::skipws; std::cerr << "Error parsing config line at " << m_filename << ":" << linenr << ": expected: <name> = <value> ('=' not found)";
std::string variable; continue;
std::string eq; }
iline >> variable; if (trim(line.substr(0, keylen)) != key)
if (variable != key)
continue; continue;
found = true; found = true;
iline >> eq; if (pvalue)
if (m_file.fail() || eq != "=") { *pvalue = trim(line.substr(keylen + 1));
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);
}
} }
return found; return found;
} }

View File

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