This repository has been archived on 2026-07-30. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
K1NG-Driver/includes/tomlc17-R260618/simple/simplecpp.cpp
T
2026-07-30 13:18:41 +02:00

59 lines
1.3 KiB
C++

/*
* Parse the config file simple.toml:
*
* [server]
* host = "www.example.com"
* port = [8080, 8181, 8282]
*
*/
#include "../src/tomlcpp.hpp"
#include <iostream>
#include <vector>
using std::cout;
static void error(const char *msg) {
fprintf(stderr, "ERROR: %s\n", msg);
exit(1);
}
int main() {
// Parse the toml file
toml::Result result = toml::parse_file_ex("simple.toml");
// Check for parse error
if (!result.ok()) {
error(result.errmsg());
}
// Extract values
std::string host;
std::vector<int64_t> port;
try {
// use the Datum::get() method
host = result.get({"server", "host"})->as_str().value();
} catch (const std::bad_optional_access &ex) {
error("missing or invalid 'server.host' property in config");
}
try {
// use the Datum::seek() method
port = result.seek("server.port")->as_intvec().value();
} catch (const std::bad_optional_access &ex) {
error("missing or invalid 'server.port' property in config");
}
// Print values
cout << "server.host = " << host << "\n";
cout << "server.port = [";
for (size_t i = 0; i < port.size(); i++) {
cout << (i ? ", " : "") << port[i];
}
cout << "]\n";
// Done! No need to call toml_free(). Result::~Result() will handle
// destruction.
return 0;
}