2017-03-18 21:20:51 +00:00
|
|
|
# tomlc99
|
|
|
|
|
2020-07-04 23:35:46 +00:00
|
|
|
TOML in c99; v0.5.0 compliant.
|
2017-03-18 21:20:51 +00:00
|
|
|
|
2020-07-04 23:35:46 +00:00
|
|
|
## Usage
|
2017-03-18 21:20:51 +00:00
|
|
|
|
|
|
|
Please see the `toml.h` file for details. What follows is a simple example that
|
|
|
|
parses this config file:
|
|
|
|
|
2020-07-04 23:35:46 +00:00
|
|
|
```toml
|
2017-03-18 21:20:51 +00:00
|
|
|
[server]
|
2020-07-04 23:35:46 +00:00
|
|
|
host = "www.example.com"
|
|
|
|
port = 80
|
2017-03-18 21:20:51 +00:00
|
|
|
```
|
|
|
|
|
2020-07-04 23:35:46 +00:00
|
|
|
The steps for getting values from our file is usually :
|
2017-03-18 21:20:51 +00:00
|
|
|
|
2020-07-04 23:35:46 +00:00
|
|
|
1. Parse the whole TOML file.
|
|
|
|
2. Get a single table from the file.
|
|
|
|
3. Find a value from the table.
|
|
|
|
4. Convert that value to the appropriate type (I.E. string, int).
|
|
|
|
5. 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.
|
2017-03-18 21:20:51 +00:00
|
|
|
|
2020-07-04 23:35:46 +00:00
|
|
|
```c
|
|
|
|
FILE* fp;
|
|
|
|
toml_table_t* conf;
|
|
|
|
char errbuf[200];
|
2017-03-18 21:20:51 +00:00
|
|
|
|
2020-07-04 23:35:46 +00:00
|
|
|
/* Open the file. */
|
|
|
|
if (0 == (fp = fopen("path/to/file.toml", "r"))) {
|
2019-10-09 23:44:07 +00:00
|
|
|
return handle_error();
|
2020-07-04 23:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Run the file through the parser. */
|
|
|
|
conf = toml_parse_file(fp, errbuf, sizeof(errbuf));
|
|
|
|
if (0 == conf) {
|
2019-10-09 23:44:07 +00:00
|
|
|
return handle_error();
|
2020-07-04 23:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
```
|
|
|
|
|
|
|
|
2. Get a single table from the file.
|
2017-03-18 21:20:51 +00:00
|
|
|
|
2020-07-04 23:35:46 +00:00
|
|
|
```c
|
|
|
|
toml_table_t* server;
|
|
|
|
|
|
|
|
/* Locate the [server] table. */
|
|
|
|
if (0 == (server = toml_table_in(conf, "server"))) {
|
2019-10-09 23:44:07 +00:00
|
|
|
return handle_error();
|
2020-07-04 23:35:46 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
3. Find a value from the table.
|
|
|
|
4. Convert that value to the appropriate type (I.E. string, int).
|
2017-03-18 21:20:51 +00:00
|
|
|
|
2020-07-04 23:35:46 +00:00
|
|
|
```c
|
|
|
|
const char* raw;
|
|
|
|
char* host;
|
|
|
|
int64_t port;
|
|
|
|
|
|
|
|
/* Extract 'host' config value. */
|
|
|
|
if (0 == (raw = toml_raw_in(server, "host"))) {
|
2019-10-09 23:44:07 +00:00
|
|
|
return handle_error();
|
2020-07-04 23:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert the raw value into a string. */
|
|
|
|
if (toml_rtos(raw, &host)) {
|
2019-10-09 23:44:07 +00:00
|
|
|
return handle_error();
|
2020-07-04 23:35:46 +00:00
|
|
|
}
|
2017-03-18 21:20:51 +00:00
|
|
|
|
2020-07-04 23:35:46 +00:00
|
|
|
/* Extract 'port' config value. */
|
|
|
|
if (0 == (raw = toml_raw_in(server, "port"))) {
|
2019-10-09 23:44:07 +00:00
|
|
|
return handle_error();
|
2020-07-04 23:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert the raw value into an int. */
|
|
|
|
if (toml_rtoi(raw, &port)) {
|
2019-10-09 23:44:07 +00:00
|
|
|
return handle_error();
|
2020-07-04 23:35:46 +00:00
|
|
|
}
|
|
|
|
```
|
2017-03-18 21:20:51 +00:00
|
|
|
|
2020-07-04 23:35:46 +00:00
|
|
|
5. Then, free up that memory if needed.
|
2017-03-18 21:20:51 +00:00
|
|
|
|
2020-07-04 23:35:46 +00:00
|
|
|
```c
|
|
|
|
/* Use `toml_free` on the table returned from `toml_parse[_file]`. */
|
|
|
|
toml_free(conf);
|
2017-03-18 21:20:51 +00:00
|
|
|
|
2020-07-04 23:35:46 +00:00
|
|
|
/* Free any values returned from `toml_rto*`. */
|
|
|
|
free(host);
|
|
|
|
free(port);
|
2017-03-18 21:20:51 +00:00
|
|
|
```
|
|
|
|
|
2020-07-04 23:35:46 +00:00
|
|
|
## Building
|
2017-03-18 21:20:51 +00:00
|
|
|
|
|
|
|
A normal *make* suffices. Alternately, you can also simply include the
|
|
|
|
`toml.c` and `toml.h` files in your project.
|
|
|
|
|
2020-07-04 23:35:46 +00:00
|
|
|
## Testing
|
2017-03-18 21:20:51 +00:00
|
|
|
|
|
|
|
To test against the standard test set provided by BurntSushi/toml-test:
|
|
|
|
|
2020-07-04 23:35:46 +00:00
|
|
|
```sh
|
|
|
|
% make
|
|
|
|
% cd test1
|
|
|
|
% bash build.sh # do this once
|
|
|
|
% bash run.sh # this will run the test suite
|
2019-10-08 23:58:18 +00:00
|
|
|
```
|
|
|
|
|
2017-03-18 21:20:51 +00:00
|
|
|
|
2019-10-08 23:58:18 +00:00
|
|
|
To test against the standard test set provided by iarna/toml:
|
2017-03-18 21:20:51 +00:00
|
|
|
|
2020-07-04 23:35:46 +00:00
|
|
|
```sh
|
|
|
|
% make
|
|
|
|
% cd test2
|
|
|
|
% bash build.sh # do this once
|
|
|
|
% bash run.sh # this will run the test suite
|
2019-10-08 23:58:18 +00:00
|
|
|
```
|