Skip to content

Commit

Permalink
[config] rewrite parsing + support quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
DadSchoorse committed Jun 6, 2020
1 parent 5bcf16e commit 368ab20
Showing 1 changed file with 37 additions and 20 deletions.
57 changes: 37 additions & 20 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,47 @@ namespace vkBasalt

void Config::readConfigLine(std::string line)
{
// TODO this needs to die in flames
if (line.find("#") != std::string::npos)
{
line = line.erase(line.find("#"), std::string::npos);
}
size_t space = line.find(" ");
while (space != std::string::npos)
{
line = line.erase(space, 1);
space = line.find(" ");
}
space = line.find("\t");
while (space != std::string::npos)
std::string key;
std::string value;

bool inQuotes = false;
bool foundEquals = false;

auto appendChar = [&key, &value, &foundEquals](const char& newChar) {
if (foundEquals)
value += newChar;
else
key += newChar;
};

for (const char& nextChar : line)
{
line = line.erase(space, 1);
space = line.find("\t");
if (inQuotes)
{
if (nextChar == '"')
inQuotes = false;
else
appendChar(nextChar);
continue;
}
switch (nextChar)
{
case '#': goto BREAK;
case '"': inQuotes = true; break;
case '\t':
case ' ': break;
case '=': foundEquals = true; break;
default: appendChar(nextChar); break;
}
}
size_t equal = line.find("=");
if (equal == std::string::npos)

BREAK:

if (!key.empty() && !value.empty())
{
return;
Logger::info(key + " = " + value);
options[key] = value;
}
Logger::info(line.substr(0, equal) + " = " + line.substr(equal + 1));
options[line.substr(0, equal)] = line.substr(equal + 1);
}

void Config::parseOption(const std::string& option, int32_t& result)
Expand Down

0 comments on commit 368ab20

Please sign in to comment.