diff --git a/src/core/serializers/TomlReader.h b/src/core/serializers/TomlReader.h index ea24f5c..d8460c1 100644 --- a/src/core/serializers/TomlReader.h +++ b/src/core/serializers/TomlReader.h @@ -6,16 +6,19 @@ #include #include #include +#include +#include using Path = std::filesystem::path; -class TomlSection +class TomlTable { public: using Comment = std::pair; + using KeyValuePairs = std::unordered_map; - TomlSection(const std::string& tag) - : mTag(tag) + TomlTable(const std::string& header) + : mHeader(header) { } @@ -25,33 +28,60 @@ public: mComments.push_back(comment); } - void addSection(std::unique_ptr section) + void addTable(std::unique_ptr table) { - mSections[section->getTag()] = std::move(section); + mTables[table->getHeader()] = std::move(table); } - void addValue(const std::string& key, const std::string& value) + void addKeyValuePair(const std::string& key, const std::string& value) { - mValues[key] = value; + mMap[key] = value; } - std::string getTag() const + std::string getHeader() const { - return mTag; + return mHeader; + } + + TomlTable* getTable(const std::string& path) + { + return mTables[path].get(); + } + + KeyValuePairs getKeyValuePairs() const + { + return mMap; } private: unsigned mLineOffset{ 0 }; - std::string mTag; - std::unordered_map > mSections; - std::unordered_map mValues; + std::string mHeader; + std::unordered_map > mTables; + KeyValuePairs mMap; std::vector mComments; }; class TomlContent { +public: + TomlContent() + : mRootTable(std::make_unique("root")) + { + + } + + TomlTable* getRootTable() const + { + return mRootTable.get(); + } + + TomlTable* getTable(const std::string& path) const + { + return mRootTable->getTable(path); + } + private: - std::unique_ptr mRootSection; + std::unique_ptr mRootTable; }; class TomlReader @@ -72,35 +102,45 @@ public: { const auto lines = File(input_path).readLines(); mLastSectionOffset = 0; - mWorkingSection = std::make_unique("root"); + mWorkingTable = mContent->getRootTable(); for (const auto& line : lines) { processLine(line); mLastSectionOffset++; } + + mWorkingTable = nullptr; } void processLine(const std::string& line) { bool in_comment{ false }; - bool in_tag{ false }; + bool in_header{ false }; + bool found_key{ false }; std::string working_string; + std::string key_string; for (auto c : line) { if (c == '#' && !in_comment) { in_comment = true; } - else if (c == '[' && !in_comment && !in_tag) + else if (c == '[' && !in_comment && !in_header) { - in_tag = true; + in_header = true; } - else if (c == ']' && in_tag) + else if (c == '=' && !in_comment && !in_header) + { + found_key = true; + key_string = working_string; + working_string = ""; + } + else if (c == ']' && in_header) { break; } - else if (in_comment || in_tag) + else { working_string += c; } @@ -108,17 +148,36 @@ public: if (in_comment) { - std::cout << "Found comment at: " << mLastSectionOffset << " with value " << working_string; - mWorkingSection->addComment({ mLastSectionOffset, working_string }); + mWorkingTable->addComment({ mLastSectionOffset, working_string }); } - else if (in_tag) + else if (in_header) { - std::cout << "Found tag at: " << mLastSectionOffset << " with value " << working_string; + onHeader(working_string); } + else if (found_key) + { + std::locale locale; + key_string.erase(std::remove_if(key_string.begin(), key_string.end(), [locale](unsigned char c) {return std::isspace(c, locale); }), key_string.end()); + working_string.erase(std::remove_if(working_string.begin(), working_string.end(), [locale](unsigned char c) {return std::isspace(c, locale); }), working_string.end()); + onKeyValuePair(key_string, working_string); + } + } + + void onHeader(const std::string& header) + { + auto new_table = std::make_unique(header); + auto table_temp = new_table.get(); + mWorkingTable->addTable(std::move(new_table)); + mWorkingTable = table_temp; + } + + void onKeyValuePair(const std::string key, const std::string value) + { + mWorkingTable->addKeyValuePair(key, value); } private: unsigned mLastSectionOffset{ 0 }; std::unique_ptr mContent; - std::unique_ptr mWorkingSection; + TomlTable* mWorkingTable{nullptr}; }; \ No newline at end of file diff --git a/test/core/TestTomlReader.cpp b/test/core/TestTomlReader.cpp index 8152438..4517ea6 100644 --- a/test/core/TestTomlReader.cpp +++ b/test/core/TestTomlReader.cpp @@ -1,14 +1,21 @@ #include "TomlReader.h" #include +#include int main() { - const auto data_loc = std::filesystem::path(__FILE__) / "../data"; + const auto data_loc = std::filesystem::path(__FILE__) / "../../data"; const auto sample_toml_file = data_loc / "sample_toml.toml"; auto reader = TomlReader(); reader.read(sample_toml_file); + auto themes_table = reader.getContent()->getTable("themes"); + for (const auto& items : themes_table->getKeyValuePairs()) + { + std::cout << "Got entry with key: " << items.first << " and val " << items.second << std::endl; + } + return 0; } \ No newline at end of file