Start supporting dict and json types.

This commit is contained in:
jmsgrogan 2024-01-03 09:13:23 +00:00
parent 94fcc42aed
commit 5183aa821a
32 changed files with 288 additions and 247 deletions

View file

@ -0,0 +1,150 @@
#include "TomlReader.h"
#include "Char.h"
TomlTable::TomlTable(const String& header)
: mHeader(header)
{
}
void TomlTable::addComment(const Comment& comment)
{
mComments.push_back(comment);
}
void TomlTable::addTable(Ptr<TomlTable> table)
{
mTables.insert(table->getHeader(), std::move(table));
}
void TomlTable::addKeyValuePair(const String& key, const String& value)
{
mMap.insert(key, value);
}
String TomlTable::getHeader() const
{
return mHeader;
}
TomlTable* TomlTable::getTable(const String& path) const
{
return (*mTables.find(path)).value().get();
}
const TomlTable::KeyValuePairs& TomlTable::getKeyValuePairs() const
{
return mMap;
}
TomlContent::TomlContent()
: mRootTable(Ptr<TomlTable>::create("root"))
{
}
TomlTable* TomlContent::getRootTable() const
{
return mRootTable.get();
}
TomlTable* TomlContent::getTable(const String& path) const
{
return mRootTable->getTable(path);
}
TomlReader::TomlReader()
: mContent(Ptr<TomlContent>::create())
{
}
TomlContent* TomlReader::getContent() const
{
return mContent.get();
}
void TomlReader::read(const FileSystemPath& input_path)
{
Vector<String> lines;
File(input_path).readLines(lines);
mLastSectionOffset = 0;
mWorkingTable = mContent->getRootTable();
for (const auto& line : lines)
{
processLine(line);
mLastSectionOffset++;
}
mWorkingTable = nullptr;
}
void TomlReader::processLine(const String& line)
{
bool in_comment{ false };
bool in_header{ false };
bool found_key{ false };
String working_string;
String key_string;
for (auto c : line)
{
if (c == '#' && !in_comment)
{
in_comment = true;
}
else if (c == '[' && !in_comment && !in_header)
{
in_header = true;
}
else if (c == '=' && !in_comment && !in_header)
{
found_key = true;
key_string = working_string;
working_string = "";
}
else if (c == ']' && in_header)
{
break;
}
else
{
working_string += c;
}
}
if (in_comment)
{
mWorkingTable->addComment({ mLastSectionOffset, working_string });
}
else if (in_header)
{
onHeader(working_string);
}
else if (found_key)
{
key_string.eraseIf([](char c) {return Char::is_space(c); });
working_string.eraseIf([](char c) {return Char::is_space(c); });
if (working_string.size()>2 && working_string[0] == '"' && working_string[working_string.size() - 1] == '"')
{
working_string.slice(1, working_string.size() - 2, working_string);
}
onKeyValuePair(key_string, working_string);
}
}
void TomlReader::onHeader(const String& header)
{
auto new_table = Ptr<TomlTable>::create(header);
auto table_temp = new_table.get();
mWorkingTable->addTable(std::move(new_table));
mWorkingTable = table_temp;
}
void TomlReader::onKeyValuePair(const String key, const String value)
{
mWorkingTable->addKeyValuePair(key, value);
}