-
Notifications
You must be signed in to change notification settings - Fork 11
/
toml_sample.cpp
49 lines (40 loc) · 934 Bytes
/
toml_sample.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "tomlcpp.hpp"
#include <iostream>
using std::cerr;
using std::cout;
void fatal(std::string msg) {
cerr << "FATAL: " << msg << "\n";
exit(1);
}
int main() {
// 1. parse file
auto res = toml::parseFile("sample.toml");
if (!res.table) {
fatal("cannot parse file: " + res.errmsg);
}
// 2. get top level table
auto server = res.table->getTable("server");
if (!server) {
fatal("missing [server]");
}
// 3. extract values from the top level table
auto [ok, host] = server->getString("host");
if (!ok) {
fatal("missing or bad host entry");
}
auto portArray = server->getArray("port");
if (!portArray) {
fatal("missing 'port' array");
}
// 4. examine the values
cout << "host: " << host << "\n";
cout << "port: ";
for (int i = 0;; i++) {
auto p = portArray->getInt(i);
if (!p.first)
break;
cout << p.second << " ";
}
cout << "\n";
return 0;
}