toml: nested maps support

This commit is contained in:
MihailRis 2024-05-20 02:09:01 +03:00
parent c94d40ab02
commit 45862269e7
2 changed files with 14 additions and 7 deletions

View File

@ -137,16 +137,23 @@ void toml::parse(
}
}
std::string toml::stringify(dynamic::Map& root) {
std::string toml::stringify(dynamic::Map& root, const std::string& name) {
std::stringstream ss;
for (auto& sectionEntry : root.values) {
ss << "[" << sectionEntry.first << "]\n";
auto sectionMap = std::get_if<dynamic::Map_sptr>(&sectionEntry.second);
for (auto& entry : (*sectionMap)->values) {
if (!name.empty()) {
ss << "[" << name << "]\n";
}
for (auto& entry : root.values) {
if (!std::holds_alternative<dynamic::Map_sptr>(entry.second)) {
ss << entry.first << " = ";
ss << entry.second << "\n";
}
ss << "\n";
}
for (auto& entry : root.values) {
if (auto submap = std::get_if<dynamic::Map_sptr>(&entry.second)) {
ss << "\n" << toml::stringify(
**submap, name.empty() ? entry.first : name+"."+entry.first
);
}
}
return ss.str();
}

View File

@ -10,7 +10,7 @@ class SettingsHandler;
namespace toml {
std::string stringify(SettingsHandler& handler);
std::string stringify(dynamic::Map& root);
std::string stringify(dynamic::Map& root, const std::string& name="");
dynamic::Map_sptr parse(std::string_view file, std::string_view source);
void parse(