From 8bea03e0811588e741050b598b8123865b333999 Mon Sep 17 00:00:00 2001 From: Yuqian Yang Date: Tue, 4 Nov 2025 22:06:39 +0800 Subject: Move toml to base. --- include/cru/base/toml/TomlDocument.h | 45 ++++++++++++++++++++++++++++ include/cru/base/toml/TomlParser.h | 32 ++++++++++++++++++++ include/cru/toml/Base.h | 11 ------- include/cru/toml/TomlDocument.h | 57 ------------------------------------ include/cru/toml/TomlParser.h | 36 ----------------------- src/CMakeLists.txt | 2 -- src/base/CMakeLists.txt | 2 ++ src/base/toml/TomlDocument.cpp | 51 ++++++++++++++++++++++++++++++++ src/base/toml/TomlParser.cpp | 46 +++++++++++++++++++++++++++++ src/toml/CMakeLists.txt | 6 ---- src/toml/TomlDocument.cpp | 49 ------------------------------- src/toml/TomlParser.cpp | 45 ---------------------------- test/CMakeLists.txt | 1 - test/base/CMakeLists.txt | 1 + test/base/toml/ParserTest.cpp | 31 ++++++++++++++++++++ test/toml/CMakeLists.txt | 6 ---- test/toml/ParserTest.cpp | 31 -------------------- 17 files changed, 208 insertions(+), 244 deletions(-) create mode 100644 include/cru/base/toml/TomlDocument.h create mode 100644 include/cru/base/toml/TomlParser.h delete mode 100644 include/cru/toml/Base.h delete mode 100644 include/cru/toml/TomlDocument.h delete mode 100644 include/cru/toml/TomlParser.h create mode 100644 src/base/toml/TomlDocument.cpp create mode 100644 src/base/toml/TomlParser.cpp delete mode 100644 src/toml/CMakeLists.txt delete mode 100644 src/toml/TomlDocument.cpp delete mode 100644 src/toml/TomlParser.cpp create mode 100644 test/base/toml/ParserTest.cpp delete mode 100644 test/toml/CMakeLists.txt delete mode 100644 test/toml/ParserTest.cpp diff --git a/include/cru/base/toml/TomlDocument.h b/include/cru/base/toml/TomlDocument.h new file mode 100644 index 00000000..c0dc68af --- /dev/null +++ b/include/cru/base/toml/TomlDocument.h @@ -0,0 +1,45 @@ +#pragma once + +#include "../Base.h" + +#include +#include +#include + +namespace cru::toml { +class CRU_BASE_API TomlSection { + public: + std::optional GetValue(const std::string& key) const; + void SetValue(const std::string& key, std::string value); + void DeleteValue(const std::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 CRU_BASE_API TomlDocument { + public: + TomlSection* GetSection(const std::string& name); + TomlSection* GetSectionOrCreate(const std::string& name); + const TomlSection* GetSection(const std::string& name) const; + void SetSection(const std::string& name, TomlSection section); + void DeleteSection(const std::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/base/toml/TomlParser.h b/include/cru/base/toml/TomlParser.h new file mode 100644 index 00000000..ac7d0217 --- /dev/null +++ b/include/cru/base/toml/TomlParser.h @@ -0,0 +1,32 @@ +#pragma once + +#include "../Exception.h" +#include "TomlDocument.h" + +#include + +namespace cru::toml { +// A very simple and tolerant TOML parser. +class CRU_BASE_API TomlParsingException : public Exception { + public: + using Exception::Exception; +}; + +class CRU_BASE_API TomlParser : public Object { + public: + explicit TomlParser(std::string input); + ~TomlParser(); + + public: + TomlDocument Parse(); + + private: + // The document is empty to begin with. + void DoParse(TomlDocument& document); + + private: + std::string input_; + + std::optional cache_; +}; +} // namespace cru::toml diff --git a/include/cru/toml/Base.h b/include/cru/toml/Base.h deleted file mode 100644 index 76c7ee71..00000000 --- a/include/cru/toml/Base.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -#ifdef CRU_IS_DLL -#ifdef CRU_TOML_EXPORT_API -#define CRU_TOML_API __declspec(dllexport) -#else -#define CRU_TOML_API __declspec(dllimport) -#endif -#else -#define CRU_TOML_API -#endif diff --git a/include/cru/toml/TomlDocument.h b/include/cru/toml/TomlDocument.h deleted file mode 100644 index 22442096..00000000 --- a/include/cru/toml/TomlDocument.h +++ /dev/null @@ -1,57 +0,0 @@ -#pragma once - -#include "Base.h" - -#include "cru/base/Base.h" - -#include -#include -#include - -namespace cru::toml { -class CRU_TOML_API TomlSection { - public: - CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(TomlSection) - CRU_DEFAULT_COPY(TomlSection) - CRU_DEFAULT_MOVE(TomlSection) - - public: - std::optional GetValue(const std::string& key) const; - void SetValue(const std::string& key, std::string value); - void DeleteValue(const std::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 CRU_TOML_API TomlDocument { - public: - CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(TomlDocument) - CRU_DEFAULT_COPY(TomlDocument) - CRU_DEFAULT_MOVE(TomlDocument) - - public: - TomlSection* GetSection(const std::string& name); - TomlSection* GetSectionOrCreate(const std::string& name); - const TomlSection* GetSection(const std::string& name) const; - void SetSection(const std::string& name, TomlSection section); - void DeleteSection(const std::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.h b/include/cru/toml/TomlParser.h deleted file mode 100644 index b6532931..00000000 --- a/include/cru/toml/TomlParser.h +++ /dev/null @@ -1,36 +0,0 @@ -#pragma once - -#include "cru/base/Exception.h" -#include "cru/toml/TomlDocument.h" - -#include - -namespace cru::toml { -// A very simple and tolerant TOML parser. -class CRU_TOML_API TomlParsingException : public Exception { - public: - using Exception::Exception; -}; - -class CRU_TOML_API TomlParser { - public: - explicit TomlParser(std::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: - std::string input_; - - std::optional cache_; -}; -} // namespace cru::toml diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9f9c205f..d4df1752 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,6 +2,4 @@ add_subdirectory(base) add_subdirectory(platform) add_subdirectory(ui) add_subdirectory(parse) -add_subdirectory(toml) - add_subdirectory(ThemeBuilder) diff --git a/src/base/CMakeLists.txt b/src/base/CMakeLists.txt index ef5afe3c..3cffb125 100644 --- a/src/base/CMakeLists.txt +++ b/src/base/CMakeLists.txt @@ -14,6 +14,8 @@ add_library(CruBase io/MemoryStream.cpp log/Logger.cpp log/StdioLogTarget.cpp + toml/TomlDocument.cpp + toml/TomlParser.cpp xml/XmlNode.cpp xml/XmlParser.cpp ) diff --git a/src/base/toml/TomlDocument.cpp b/src/base/toml/TomlDocument.cpp new file mode 100644 index 00000000..fa155c80 --- /dev/null +++ b/src/base/toml/TomlDocument.cpp @@ -0,0 +1,51 @@ +#include "cru/base/toml/TomlDocument.h" + +namespace cru::toml { +std::optional TomlSection::GetValue(const std::string& key) const { + auto it = values_.find(key); + if (it == values_.end()) { + return std::nullopt; + } + return it->second; +} + +void TomlSection::SetValue(const std::string& key, std::string value) { + values_[key] = std::move(value); +} + +void TomlSection::DeleteValue(const std::string& key) { values_.erase(key); } + +TomlSection* TomlDocument::GetSection(const std::string& name) { + auto it = sections_.find(name); + if (it == sections_.end()) { + return nullptr; + } + return &it->second; +} + +const TomlSection* TomlDocument::GetSection(const std::string& name) const { + auto it = sections_.find(name); + if (it == sections_.end()) { + return nullptr; + } + return &it->second; +} + +TomlSection* TomlDocument::GetSectionOrCreate(const std::string& name) { + auto it = sections_.find(name); + if (it == sections_.end()) { + sections_[name] = TomlSection(); + return §ions_[name]; + } + return &it->second; +} + +void TomlDocument::SetSection(const std::string& name, TomlSection section) { + sections_[name] = std::move(section); +} + +void TomlDocument::DeleteSection(const std::string& name) { + sections_.erase(name); +} + +} // namespace cru::toml diff --git a/src/base/toml/TomlParser.cpp b/src/base/toml/TomlParser.cpp new file mode 100644 index 00000000..a45af712 --- /dev/null +++ b/src/base/toml/TomlParser.cpp @@ -0,0 +1,46 @@ +#include "cru/base/toml/TomlParser.h" +#include "cru/base/StringUtil.h" +#include "cru/base/toml/TomlDocument.h" + +namespace cru::toml { +TomlParser::TomlParser(std::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) { + std::vector lines = + cru::string::Split(input_, "\n", cru::string::SplitOptions::RemoveSpace); + + std::string current_section_name; + + for (auto& line : lines) { + line = cru::string::Trim(line); + if (line.starts_with("[") && line.ends_with("]")) { + current_section_name = line.substr(1, line.size() - 2); + } else if (line.starts_with("#")) { + // Ignore comments. + } else { + auto equal_index = line.find('='); + + if (equal_index == std::string::npos) { + throw TomlParsingException("Invalid TOML line: " + line); + } + + auto key = cru::string::Trim(line.substr(0, equal_index)); + auto value = cru::string::Trim(line.substr(equal_index + 1)); + + document.GetSectionOrCreate(current_section_name)->SetValue(key, value); + } + } +} +} // namespace cru::toml diff --git a/src/toml/CMakeLists.txt b/src/toml/CMakeLists.txt deleted file mode 100644 index 33b31b8f..00000000 --- a/src/toml/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -add_library(CruToml - TomlDocument.cpp - TomlParser.cpp -) -target_compile_definitions(CruToml PRIVATE CRU_TOML_EXPORT_API) -target_link_libraries(CruToml PUBLIC CruBase) diff --git a/src/toml/TomlDocument.cpp b/src/toml/TomlDocument.cpp deleted file mode 100644 index af9e2ac8..00000000 --- a/src/toml/TomlDocument.cpp +++ /dev/null @@ -1,49 +0,0 @@ -#include "cru/toml/TomlDocument.h" - -namespace cru::toml { -std::optional TomlSection::GetValue(const std::string& key) const { - auto it = values_.find(key); - if (it == values_.end()) { - return std::nullopt; - } - return it->second; -} - -void TomlSection::SetValue(const std::string& key, std::string value) { - values_[key] = std::move(value); -} - -void TomlSection::DeleteValue(const std::string& key) { values_.erase(key); } - -TomlSection* TomlDocument::GetSection(const std::string& name) { - auto it = sections_.find(name); - if (it == sections_.end()) { - return nullptr; - } - return &it->second; -} - -const TomlSection* TomlDocument::GetSection(const std::string& name) const { - auto it = sections_.find(name); - if (it == sections_.end()) { - return nullptr; - } - return &it->second; -} - -TomlSection* TomlDocument::GetSectionOrCreate(const std::string& name) { - auto it = sections_.find(name); - if (it == sections_.end()) { - sections_[name] = TomlSection(); - return §ions_[name]; - } - return &it->second; -} - -void TomlDocument::SetSection(const std::string& name, TomlSection section) { - sections_[name] = std::move(section); -} - -void TomlDocument::DeleteSection(const std::string& name) { sections_.erase(name); } - -} // namespace cru::toml diff --git a/src/toml/TomlParser.cpp b/src/toml/TomlParser.cpp deleted file mode 100644 index 1aea5b73..00000000 --- a/src/toml/TomlParser.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include "cru/toml/TomlParser.h" -#include "cru/base/StringUtil.h" -#include "cru/toml/TomlDocument.h" - -namespace cru::toml { -TomlParser::TomlParser(std::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) { - std::vector lines = cru::string::Split(input_, "\n", cru::string::SplitOptions::RemoveSpace); - - std::string current_section_name; - - for (auto& line : lines) { - line = cru::string::Trim(line); - if (line.starts_with("[") && line.ends_with("]")) { - current_section_name = line.substr(1, line.size() - 2); - } else if (line.starts_with("#")) { - // Ignore comments. - } else { - auto equal_index = line.find('='); - - if (equal_index == std::string::npos) { - throw TomlParsingException("Invalid TOML line: " + line); - } - - auto key = cru::string::Trim(line.substr(0, equal_index)); - auto value = cru::string::Trim(line.substr(equal_index + 1)); - - document.GetSectionOrCreate(current_section_name)->SetValue(key, value); - } - } -} -} // namespace cru::toml diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 22f66e40..d9b5bc3a 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -17,5 +17,4 @@ endfunction() add_subdirectory(base) add_subdirectory(platform) -add_subdirectory(toml) add_subdirectory(ui) diff --git a/test/base/CMakeLists.txt b/test/base/CMakeLists.txt index b36bbd23..38cb9d57 100644 --- a/test/base/CMakeLists.txt +++ b/test/base/CMakeLists.txt @@ -6,6 +6,7 @@ add_executable(CruBaseTest StringUtilTest.cpp SubProcessTest.cpp TimerTest.cpp + toml/ParserTest.cpp xml/ParserTest.cpp ) target_link_libraries(CruBaseTest PRIVATE CruBase CruTestBase) diff --git a/test/base/toml/ParserTest.cpp b/test/base/toml/ParserTest.cpp new file mode 100644 index 00000000..2582e3c3 --- /dev/null +++ b/test/base/toml/ParserTest.cpp @@ -0,0 +1,31 @@ +#include "cru/base/toml/TomlDocument.h" +#include "cru/base/toml/TomlParser.h" + +#include + +using namespace cru::toml; + +TEST_CASE("CruTomlParserTest Simple", "[toml]") { + TomlParser parser( + R"( +a1 = v1 +a2 = v2 +# comment + +[s1] +# comment +a3 = v3 +a4 = v4 + +[s2] +a5 = v5 +a6 = v6 + )"); + auto document = parser.Parse(); + REQUIRE(document.GetSection("")->GetValue("a1") == "v1"); + REQUIRE(document.GetSection("")->GetValue("a2") == "v2"); + REQUIRE(document.GetSection("s1")->GetValue("a3") == "v3"); + REQUIRE(document.GetSection("s1")->GetValue("a4") == "v4"); + REQUIRE(document.GetSection("s2")->GetValue("a5") == "v5"); + REQUIRE(document.GetSection("s2")->GetValue("a6") == "v6"); +} diff --git a/test/toml/CMakeLists.txt b/test/toml/CMakeLists.txt deleted file mode 100644 index a0909349..00000000 --- a/test/toml/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -add_executable(CruTomlTest - ParserTest.cpp -) -target_link_libraries(CruTomlTest PRIVATE CruToml CruTestBase) - -cru_catch_discover_tests(CruTomlTest) diff --git a/test/toml/ParserTest.cpp b/test/toml/ParserTest.cpp deleted file mode 100644 index 4a4ba212..00000000 --- a/test/toml/ParserTest.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include "cru/toml/TomlDocument.h" -#include "cru/toml/TomlParser.h" - -#include - -using namespace cru::toml; - -TEST_CASE("CruTomlParserTest Simple", "[toml]") { - TomlParser parser( - R"( -a1 = v1 -a2 = v2 -# comment - -[s1] -# comment -a3 = v3 -a4 = v4 - -[s2] -a5 = v5 -a6 = v6 - )"); - auto document = parser.Parse(); - REQUIRE(document.GetSection("")->GetValue("a1") == "v1"); - REQUIRE(document.GetSection("")->GetValue("a2") == "v2"); - REQUIRE(document.GetSection("s1")->GetValue("a3") == "v3"); - REQUIRE(document.GetSection("s1")->GetValue("a4") == "v4"); - REQUIRE(document.GetSection("s2")->GetValue("a5") == "v5"); - REQUIRE(document.GetSection("s2")->GetValue("a6") == "v6"); -} -- cgit v1.2.3