A Chicken Scheme wrapper for the C TOML library
Go to file
CK Tan 84fd6725e0 Merge branch 'master' of github.com:cktan/tomlc99 2020-11-01 17:53:04 -08:00
test1
test2 updated Note.txt to reflect expected error conditions in test2/ 2020-07-14 16:57:35 -07:00
unittest
.editorconfig Add editorconfig file 2020-06-17 17:43:36 -05:00
.gitignore
LICENSE
Makefile Add enhanced access methods 2020-11-01 17:52:57 -08:00
README.md Merge branch 'master' of github.com:cktan/tomlc99 2020-11-01 17:53:04 -08:00
toml.c Add enhanced access methods 2020-11-01 17:52:57 -08:00
toml.h Add enhanced access methods 2020-11-01 17:52:57 -08:00
toml_cat.c
toml_json.c

README.md

tomlc99

TOML in c99; v1.0 compliant.

If you are looking for a C++ library, you might try this wrapper: https://github.com/cktan/tomlcpp.

Usage

Please see the toml.h file for details. What follows is a simple example that parses this config file:

[server]
	host = "www.example.com"
	port = 80

The steps for getting values from our file is usually :

  1. Parse the whole TOML file.
  2. Get a single table from the file.
  3. Find a value from the table.
  4. Then, free up that memory if needed.

Below is an example of parsing the values from the example table.

  1. Parse the whole TOML file.
FILE* fp;
toml_table_t* conf;
char errbuf[200];

/* Open the file. */
if (0 == (fp = fopen("path/to/file.toml", "r"))) {
	return handle_error();
}

/* Run the file through the parser. */
conf = toml_parse_file(fp, errbuf, sizeof(errbuf));
if (0 == conf) {
	return handle_error();
}

fclose(fp);

/* Alternatively, use `toml_parse` which takes a string rather than a file. */
conf = toml_parse("A null terminated string that is TOML\0", errbuf, sizeof(errbuf));
  1. Get a single table from the file.
toml_table_t* server;

/* Locate the [server] table. */
if (0 == (server = toml_table_in(conf, "server"))) {
	return handle_error();
}
  1. Find a value from the table.
/* Extract 'host' config value. */
toml_access_t host = toml_string_in(server, "host");
if (!host.ok) {
	toml_free(conf);
	return handle_error();
}

toml_access_t port = toml_int_in(server, "port");
if (!port.ok) {
	toml_free(conf);
	free(host.u.s);
	return handle_error();
}

printf("host %s\n", host.u.s);
printf("port %d\n", port.u.i);

  1. Then, free up that memory if needed.
/* Use `toml_free` on the table returned from `toml_parse[_file]`. */
toml_free(conf);

/* Free any string values returned from access functions. */
free(host.u.s);

Building

A normal make suffices. Alternately, you can also simply include the toml.c and toml.h files in your project.

Testing

To test against the standard test set provided by BurntSushi/toml-test:

% make
% cd test1
% bash build.sh   # do this once
% bash run.sh     # this will run the test suite

To test against the standard test set provided by iarna/toml:

% make
% cd test2
% bash build.sh   # do this once
% bash run.sh     # this will run the test suite