This commit is contained in:
CK Tan 2020-12-22 23:09:36 -08:00
parent 63feb40e3b
commit 94ded5f2c1

View file

@ -38,7 +38,7 @@ Below is an example of parsing the values from the example table.
#include <stdlib.h>
#include "toml.h"
static void fatal(const char* msg, const char* msg1)
static void error(const char* msg, const char* msg1)
{
fprintf(stderr, "ERROR: %s%s\n", msg, msg1?msg1:"");
exit(1);
@ -53,31 +53,31 @@ int main()
// 1. Read and parse toml file
fp = fopen("sample.toml", "r");
if (!fp) {
fatal("cannot open sample.toml - ", strerror(errno));
error("cannot open sample.toml - ", strerror(errno));
}
toml_table_t* conf = toml_parse_file(fp, errbuf, sizeof(errbuf));
fclose(fp);
if (!conf) {
fatal("cannot parse - ", errbuf);
error("cannot parse - ", errbuf);
}
// 2. Traverse to a table.
toml_table_t* server = toml_table_in(conf, "server");
if (!server) {
fatal("missing [server]", "");
error("missing [server]", "");
}
// 3. Extract values
toml_datum_t host = toml_string_in(server, "host");
if (!host.ok) {
fatal("cannot read server.host", "");
error("cannot read server.host", "");
}
toml_array_t* portarray = toml_array_in(server, "port");
if (!portarray) {
fatal("cannot read server.port", "");
error("cannot read server.port", "");
}
printf("host: %s\n", host.u.s);
@ -94,7 +94,6 @@ int main()
toml_free(conf);
return 0;
}
```
#### Accessing Table Content