From 431cbdbe7d3ae8c45458dcf914717b0365ecd99a Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 8 Jan 2022 16:31:09 +0800 Subject: ... --- include/cru/common/Base.hpp | 2 +- include/cru/toml/TomlDocument.hpp | 54 +++++++++++++++++++++++++++++++++++++++ include/cru/toml/TomlParser.hpp | 29 +++++++++++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 include/cru/toml/TomlDocument.hpp create mode 100644 include/cru/toml/TomlParser.hpp (limited to 'include') 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 +#include + +namespace cru::toml { +class TomlSection { + public: + CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(TomlSection) + CRU_DEFAULT_COPY(TomlSection) + CRU_DEFAULT_MOVE(TomlSection) + + public: + std::optional 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 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 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 + +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 cache_; +}; +} // namespace cru::toml -- cgit v1.2.3