TOML v1.0 in c17.
- Compatible with C99.
- Compatible with C++.
- Implements C++20 Accessors.
- Implements TOML v1.0.0.
- Passes the standard test suites.
See tomlc17.h for details.
Parsing a toml document creates a tree data structure in memory that reflects the document. Information can be extracted by navigating this data structure.
Note: you can simply include tomlc17.h
and tomlc17.c
in your
project without running make
and building the library.
The following is a simple example:
/*
* Parse the config file simple.toml:
*
* [server]
* host = "www.example.com"
* port = [8080, 8181, 8282]
*
*/
#include "../src/tomlc17.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
static void error(const char *msg, const char *msg1) {
fprintf(stderr, "ERROR: %s%s\n", msg, msg1 ? msg1 : "");
exit(1);
}
int main() {
// Parse the toml file
toml_result_t result = toml_parse_file_ex("simple.toml");
// Check for parse error
if (!result.ok) {
error(result.errmsg, 0);
}
// Extract values
toml_datum_t server = toml_get(result.toptab, "server");
toml_datum_t host = toml_get(server, "host");
toml_datum_t port = toml_get(server, "port");
// Print server.host
if (host.type != TOML_STRING) {
error("missing or invalid 'server.host' property in config", 0);
}
printf("server.host = %s\n", host.u.s);
// Print server.port
if (port.type != TOML_ARRAY) {
error("missing or invalid 'server.port' property in config", 0);
}
printf("server.port = [");
for (int i = 0; i < port.u.arr.size; i++) {
toml_datum_t elem = port.u.arr.elem[i];
if (elem.type != TOML_INT64) {
error("server.port element not an integer", 0);
}
printf("%s%d", i ? ", " : "", (int)elem.u.int64);
}
printf("]\n");
// Done!
toml_free(result);
return 0;
}
For debug build:
export DEBUG=1
make
For release build:
unset DEBUG
make
We run the official toml-test
as described
here. Refer to
this
section
for prerequisites to run the tests.
The following command invokes the tests:
make test
As of today (05/07/2025), all tests passed:
toml-test v0001-01-01 [/home/cktan/p/tomlc17/test/stdtest/driver]: using embedded tests
valid tests: 185 passed, 0 failed
invalid tests: 371 passed, 0 failed
The install command will copy tomlc17.h
, tomlcpp.hpp
and libtomlc17.a
to the $prefix/include
and $prefix/lib
directories.
unset DEBUG
make clean install prefix=/usr/local