This commit is contained in:
2025-02-24 12:19:18 +03:00
parent 4fcb5af94b
commit 5e3cc5b31a
2 changed files with 160 additions and 42 deletions

View File

@@ -1,4 +1,5 @@
#include <iostream>
#include <strstream>
#include "../comm_server_configfile.h"
@@ -11,29 +12,50 @@ int main(int argc, char* argv[])
return 1;
}
auto print_cfg = [](auto const& cfg) {
for (auto& [key, v] : cfg.config()) {
std::cout << key << " = ";
if (auto v_str = std::get_if<1>(&v)) {
std::cout << *v_str;
} else if (auto v_vec = std::get_if<2>(&v)) {
for (auto& el : *v_vec) {
std::cout << "<" << el << "> ";
}
}
std::cout << "\n";
}
};
mcc::MccConfigfile cfg;
std::string str = R"--(key_no_val
# comment example
scalar_key = scalar-value
vec_key = 1,2,3 , 345, 4576, 79
)--";
std::istrstream ist(str.c_str());
cfg.load(ist);
std::cout << "From input stream: \n";
print_cfg(cfg);
cfg["vec_key"] = std::vector<std::string>{"10", "20", "30"};
cfg["new_key"] = "new-key-value";
cfg.save(std::string_view{argv[1]});
auto ec = cfg.load(std::string_view{argv[1]});
if (ec) {
std::cout << "ERR: " << ec.message() << "\n";
return 1;
}
for (auto& [key, v] : cfg.config()) {
std::cout << "<" << key << "> = ";
if (v.index()) {
if (v.index() > 1) {
for (auto& el : std::get<2>(v)) {
std::cout << "<" << el << "> ";
}
std::cout << "\n";
} else {
std::cout << "<" << std::get<1>(v) << ">\n";
}
} else {
std::cout << "<no value>\n";
}
}
std::cout << "\n\nFrom file (edited):\n";
print_cfg(cfg);
return 0;
}