diff options
| author | Yuqian Yang <crupest@crupest.life> | 2025-11-04 22:06:39 +0800 |
|---|---|---|
| committer | Yuqian Yang <crupest@crupest.life> | 2025-11-04 22:06:39 +0800 |
| commit | 8bea03e0811588e741050b598b8123865b333999 (patch) | |
| tree | dab70865e53fca11960cb899454d64db9e8ed98d /src/base | |
| parent | 1a6111e3f02b0a9cff0f81fb524b4dfb7d69854b (diff) | |
| download | cru-8bea03e0811588e741050b598b8123865b333999.tar.gz cru-8bea03e0811588e741050b598b8123865b333999.tar.bz2 cru-8bea03e0811588e741050b598b8123865b333999.zip | |
Move toml to base.
Diffstat (limited to 'src/base')
| -rw-r--r-- | src/base/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/base/toml/TomlDocument.cpp | 51 | ||||
| -rw-r--r-- | src/base/toml/TomlParser.cpp | 46 |
3 files changed, 99 insertions, 0 deletions
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<std::string> 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<std::string> 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 |
