diff options
-rw-r--r-- | include/cru/common/Base.hpp | 2 | ||||
-rw-r--r-- | include/cru/toml/TomlDocument.hpp | 54 | ||||
-rw-r--r-- | include/cru/toml/TomlParser.hpp | 29 | ||||
-rw-r--r-- | src/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/toml/CMakeLists.txt | 5 | ||||
-rw-r--r-- | src/toml/TomlDocument.cpp | 40 | ||||
-rw-r--r-- | src/toml/TomlParser.cpp | 22 |
7 files changed, 152 insertions, 1 deletions
diff --git a/include/cru/common/Base.hpp b/include/cru/common/Base.hpp index 174ac037..ef5727c5 100644 --- a/include/cru/common/Base.hpp +++ b/include/cru/common/Base.hpp @@ -40,7 +40,7 @@ #define CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(classname) \ classname() = default; \ - ~classname() override = default; + ~classname() = default; #define CRU_DEFINE_COMPARE_OPERATORS(classname) \ inline bool operator==(const classname& left, const classname& right) { \ diff --git a/include/cru/toml/TomlDocument.hpp b/include/cru/toml/TomlDocument.hpp new file mode 100644 index 00000000..9b549daa --- /dev/null +++ b/include/cru/toml/TomlDocument.hpp @@ -0,0 +1,54 @@ +#pragma once + +#include "cru/common/Base.hpp" +#include "cru/common/String.hpp" + +#include <optional> +#include <unordered_map> + +namespace cru::toml { +class TomlSection { + public: + CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(TomlSection) + CRU_DEFAULT_COPY(TomlSection) + CRU_DEFAULT_MOVE(TomlSection) + + public: + std::optional<String> GetValue(const String& key) const; + void SetValue(const String& key, String value); + void DeleteValue(const String& key); + + auto begin() { return values_.begin(); } + auto end() { return values_.end(); } + auto begin() const { return values_.begin(); } + auto end() const { return values_.end(); } + auto cbegin() const { return values_.cbegin(); } + auto cend() const { return values_.cend(); } + + private: + std::unordered_map<String, String> values_; +}; + +class TomlDocument { + public: + CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(TomlDocument) + CRU_DEFAULT_COPY(TomlDocument) + CRU_DEFAULT_MOVE(TomlDocument) + + public: + TomlSection* GetSection(const String& name); + const TomlSection* GetSection(const String& name) const; + void SetSection(const String& name, TomlSection section); + void DeleteSection(const String& name); + + auto begin() { return sections_.begin(); } + auto end() { return sections_.end(); } + auto begin() const { return sections_.begin(); } + auto end() const { return sections_.end(); } + auto cbegin() const { return sections_.cbegin(); } + auto cend() const { return sections_.cend(); } + + private: + std::unordered_map<String, TomlSection> sections_; +}; +} // namespace cru::toml diff --git a/include/cru/toml/TomlParser.hpp b/include/cru/toml/TomlParser.hpp new file mode 100644 index 00000000..b5bcaa0b --- /dev/null +++ b/include/cru/toml/TomlParser.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include "cru/toml/TomlDocument.hpp" + +#include <optional> + +namespace cru::toml { +class TomlParser { + public: + explicit TomlParser(String input); + + CRU_DELETE_COPY(TomlParser) + CRU_DELETE_MOVE(TomlParser) + + ~TomlParser(); + + public: + TomlDocument Parse(); + + private: + // The document is empty to begin with. + void DoParse(TomlDocument& document); + + private: + String input_; + + std::optional<TomlDocument> cache_; +}; +} // namespace cru::toml diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d16c075e..be8262fa 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -10,4 +10,5 @@ endif() add_subdirectory(ui) add_subdirectory(parse) +add_subdirectory(toml) add_subdirectory(xml) diff --git a/src/toml/CMakeLists.txt b/src/toml/CMakeLists.txt new file mode 100644 index 00000000..0285b454 --- /dev/null +++ b/src/toml/CMakeLists.txt @@ -0,0 +1,5 @@ +add_library(cru_toml SHARED + TomlDocument.cpp + TomlParser.cpp +) +target_link_libraries(cru_toml PUBLIC cru_base) diff --git a/src/toml/TomlDocument.cpp b/src/toml/TomlDocument.cpp new file mode 100644 index 00000000..a785b4e4 --- /dev/null +++ b/src/toml/TomlDocument.cpp @@ -0,0 +1,40 @@ +#include "cru/toml/TomlDocument.hpp" + +namespace cru::toml { +std::optional<String> TomlSection::GetValue(const String& key) const { + auto it = values_.find(key); + if (it == values_.end()) { + return std::nullopt; + } + return it->second; +} + +void TomlSection::SetValue(const String& key, String value) { + values_[key] = std::move(value); +} + +void TomlSection::DeleteValue(const String& key) { values_.erase(key); } + +TomlSection* TomlDocument::GetSection(const String& name) { + auto it = sections_.find(name); + if (it == sections_.end()) { + return nullptr; + } + return &it->second; +} + +const TomlSection* TomlDocument::GetSection(const String& name) const { + auto it = sections_.find(name); + if (it == sections_.end()) { + return nullptr; + } + return &it->second; +} + +void TomlDocument::SetSection(const String& name, TomlSection section) { + sections_[name] = std::move(section); +} + +void TomlDocument::DeleteSection(const String& name) { sections_.erase(name); } + +} // namespace cru::toml diff --git a/src/toml/TomlParser.cpp b/src/toml/TomlParser.cpp new file mode 100644 index 00000000..d54624f6 --- /dev/null +++ b/src/toml/TomlParser.cpp @@ -0,0 +1,22 @@ +#include "cru/toml/TomlParser.hpp" +#include "cru/toml/TomlDocument.hpp" + +namespace cru::toml { +TomlParser::TomlParser(String input) : input_(std::move(input)) {} + +TomlParser::~TomlParser() = default; + +TomlDocument TomlParser::Parse() { + if (cache_) { + return *cache_; + } + + cache_ = TomlDocument(); + DoParse(*cache_); + return *cache_; +} + +void TomlParser::DoParse(TomlDocument& document) { + // TODO: Implement this. +} +} // namespace cru::toml |