1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-09-15 06:52:34 +02:00

config: do not use dynamic memory + bail out early on error

This commit is contained in:
Anonymous Maarten 2017-02-17 01:16:26 +01:00 committed by Daniel Evans
parent 05db65dbbc
commit 1dc06d9a35

View File

@ -109,37 +109,32 @@ bool GameConfig::parseConfig(
GameConfig::ParseType srcType, const std::string &source,
ParseType destType, std::string &destination)
{
bool success = true;
pt::ptree srcTree;
if ((srcType == ParseType::STRING) || (srcType == ParseType::FILE)) {
std::istream *istream = nullptr;
switch (srcType) {
case ParseType::STRING:
istream = new std::istringstream(source);
break;
case ParseType::FILE:
istream = new std::ifstream(source);
break;
default:
//Cannot reach here
success = false;
break;
}
if (istream != nullptr) {
try {
pt::read_ini(*istream, srcTree);
} catch (pt::ini_parser_error &e) {
success = false;
RW_MESSAGE(e.what());
try {
switch (srcType) {
case ParseType::STRING:
pt::read_ini(source, srcTree);
break;
case ParseType::FILE:
{
std::ifstream ifs(source);
pt::read_ini(ifs, srcTree);
break;
}
default:
//Cannot reach here
return false;
}
} else {
success = false;
RW_MESSAGE("Unable to create stream from source.");
} catch (pt::ini_parser_error &e) {
RW_MESSAGE(e.what());
return false;
}
delete istream;
}
bool success = true;
auto read_config = [&](const std::string &key, auto &target,
const auto &defaultValue, auto &translator,
bool optional=true) {
@ -196,6 +191,9 @@ bool GameConfig::parseConfig(
read_config("input.invert_y", this->m_inputInvertY, false, boolt);
if (!success)
return success;
if ((destType == ParseType::STRING) || (destType == ParseType::FILE)) {
std::ostringstream ostream;
try {