|
@ -1,42 +1,20 @@ |
|
|
#include <toml++/toml.hpp>
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
#include <iostream>
|
|
|
#include <iostream>
|
|
|
|
|
|
#include <string>
|
|
|
#include <string_view>
|
|
|
#include <string_view>
|
|
|
|
|
|
#include <toml++/toml.hpp>
|
|
|
|
|
|
|
|
|
// g++ tomldemo.cpp -I ../libs/tomlplusplus -std=c++17
|
|
|
// g++ tomldemo.cpp -I ../libs/tomlplusplus -std=c++17
|
|
|
|
|
|
|
|
|
using namespace std::literals; |
|
|
using namespace std::literals; |
|
|
|
|
|
|
|
|
int main(int argc, char const* argv[]) { |
|
|
|
|
|
auto config = toml::parse_file("configuration.toml"); |
|
|
|
|
|
|
|
|
|
|
|
// get key-value pairs
|
|
|
|
|
|
std::string_view library_name = config["library"]["name"].value_or(""sv); |
|
|
|
|
|
std::string_view library_author = config["library"]["authors"][0].value_or(""sv); |
|
|
|
|
|
int64_t depends_on_cpp_version = config["dependencies"]["cpp"].value_or(0); |
|
|
|
|
|
|
|
|
|
|
|
// modify the data
|
|
|
|
|
|
config.insert_or_assign("alternatives", toml::array{"cpptoml", "toml11", "Boost.TOML"}); |
|
|
|
|
|
|
|
|
|
|
|
// use a visitor to iterate over heterogenous data
|
|
|
|
|
|
config.for_each([](auto& key, auto& value) { |
|
|
|
|
|
std::cout << value << "\n"; |
|
|
|
|
|
// if constexpr (toml::is_string<decltype(value)>) do_something_with_string_values(value);
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// you can also iterate more 'traditionally' using a ranged-for
|
|
|
|
|
|
for (auto&& [k, v] : config) { |
|
|
|
|
|
// ...
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv) { |
|
|
|
|
|
toml::table tbl; |
|
|
|
|
|
try { |
|
|
|
|
|
tbl = toml::parse_file("config.ini"); |
|
|
|
|
|
std::cout << tbl << "\n"; |
|
|
|
|
|
} catch (const toml::parse_error& err) { |
|
|
|
|
|
std::cerr << "Parsing failed:\n" << err << "\n"; |
|
|
|
|
|
return 1; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// re-serialize as TOML
|
|
|
|
|
|
std::cout << config << "\n"; |
|
|
|
|
|
|
|
|
|
|
|
// re-serialize as JSON
|
|
|
|
|
|
std::cout << toml::json_formatter{config} << "\n"; |
|
|
|
|
|
|
|
|
|
|
|
// re-serialize as YAML
|
|
|
|
|
|
std::cout << toml::yaml_formatter{config} << "\n"; |
|
|
|
|
|
return 0; |
|
|
return 0; |
|
|
} |
|
|
} |
|
|
|
|
|
|