aboutsummaryrefslogtreecommitdiff
path: root/include/cru/base
diff options
context:
space:
mode:
Diffstat (limited to 'include/cru/base')
-rw-r--r--include/cru/base/toml/TomlDocument.h45
-rw-r--r--include/cru/base/toml/TomlParser.h32
2 files changed, 77 insertions, 0 deletions
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 <optional>
+#include <unordered_map>
+#include <string>
+
+namespace cru::toml {
+class CRU_BASE_API TomlSection {
+ public:
+ std::optional<std::string> 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<std::string, std::string> 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<std::string, TomlSection> 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 <optional>
+
+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<TomlDocument> cache_;
+};
+} // namespace cru::toml