Make a start at toml reader.

This commit is contained in:
jmsgrogan 2022-10-10 08:57:32 +01:00
parent cbc9ba77d2
commit 70991e59af
4 changed files with 115 additions and 3 deletions

View file

@ -1,16 +1,124 @@
#pragma once #pragma once
#include <filesystem>; #include "File.h"
#include <filesystem>
#include <memory>
#include <unordered_map> #include <unordered_map>
#include <iostream>
using Path = std::filesystem::path; using Path = std::filesystem::path;
class TomlSection
{
public:
using Comment = std::pair<unsigned, std::string>;
TomlSection(const std::string& tag)
: mTag(tag)
{
}
void addComment(const Comment& comment)
{
mComments.push_back(comment);
}
void addSection(std::unique_ptr<TomlSection> 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<std::string, std::unique_ptr<TomlSection> > mSections;
std::unordered_map<std::string, std::string> mValues;
std::vector<Comment> mComments;
};
class TomlContent class TomlContent
{ {
//using SectionContent private:
std::unique_ptr<TomlSection> mRootSection;
}; };
class TomlReader class TomlReader
{ {
public:
TomlReader()
: mContent(std::make_unique<TomlContent>())
{
}
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<TomlSection>("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<TomlContent> mContent;
std::unique_ptr<TomlSection> mWorkingSection;
}; };

View file

@ -44,7 +44,7 @@ target_include_directories(graphics PUBLIC
"${PROJECT_SOURCE_DIR}/src/visual_elements/" "${PROJECT_SOURCE_DIR}/src/visual_elements/"
) )
target_link_libraries(graphics PUBLIC visual_elements image GL ${platform_LIBS}) target_link_libraries(graphics PUBLIC visual_elements image ${platform_LIBS})
set_property(TARGET graphics PROPERTY FOLDER src) set_property(TARGET graphics PROPERTY FOLDER src)
set_target_properties( graphics PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON ) set_target_properties( graphics PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON )

View file

@ -7,6 +7,8 @@ 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"; const auto sample_toml_file = data_loc / "sample_toml.toml";
auto reader = TomlReader();
reader.read(sample_toml_file);
return 0; return 0;
} }

View file

@ -1,4 +1,6 @@
# Site Generator Config
[themes] [themes]
location = "../personal-site-themes" location = "../personal-site-themes"
active_theme = "basic" active_theme = "basic"