#pragma once #include "File.h" #include #include #include #include using Path = std::filesystem::path; class TomlSection { public: using Comment = std::pair; TomlSection(const std::string& tag) : mTag(tag) { } void addComment(const Comment& comment) { mComments.push_back(comment); } void addSection(std::unique_ptr section) { mSections[section->getTag()] = std::move(section); } void addValue(const std::string& key, const std::string& value) { mValues[key] = value; } std::string getTag() const { return mTag; } private: unsigned mLineOffset{ 0 }; std::string mTag; std::unordered_map > mSections; std::unordered_map mValues; std::vector mComments; }; class TomlContent { private: std::unique_ptr mRootSection; }; class TomlReader { public: TomlReader() : mContent(std::make_unique()) { } TomlContent* getContent() const { return mContent.get(); } void read(const Path& input_path) { const auto lines = File(input_path).readLines(); mLastSectionOffset = 0; mWorkingSection = std::make_unique("root"); for (const auto& line : lines) { processLine(line); mLastSectionOffset++; } } void processLine(const std::string& line) { bool in_comment{ false }; bool in_tag{ false }; std::string working_string; for (auto c : line) { if (c == '#' && !in_comment) { in_comment = true; } else if (c == '[' && !in_comment && !in_tag) { in_tag = true; } else if (c == ']' && in_tag) { break; } else if (in_comment || in_tag) { working_string += c; } } if (in_comment) { std::cout << "Found comment at: " << mLastSectionOffset << " with value " << working_string; mWorkingSection->addComment({ mLastSectionOffset, working_string }); } else if (in_tag) { std::cout << "Found tag at: " << mLastSectionOffset << " with value " << working_string; } } private: unsigned mLastSectionOffset{ 0 }; std::unique_ptr mContent; std::unique_ptr mWorkingSection; };