2017-03-19 05:20:51 +08:00
|
|
|
|
# tomlc99
|
2019-10-10 07:40:11 +08:00
|
|
|
|
TOML in c99; v0.5.0 compliant.
|
2017-03-19 05:20:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Usage
|
|
|
|
|
|
|
|
|
|
Please see the `toml.h` file for details. What follows is a simple example that
|
|
|
|
|
parses this config file:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
[server]
|
2018-01-04 17:56:40 +08:00
|
|
|
|
host = "www.example.com"
|
2017-03-19 05:20:51 +08:00
|
|
|
|
port = 80
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
For each config param, the code first extracts a raw value and then
|
|
|
|
|
convert it to a string or integer depending on context.
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
FILE* fp;
|
|
|
|
|
toml_table_t* conf;
|
|
|
|
|
toml_table_t* server;
|
|
|
|
|
const char* raw;
|
|
|
|
|
char* host;
|
|
|
|
|
int64_t port;
|
|
|
|
|
char errbuf[200];
|
|
|
|
|
|
|
|
|
|
/* open file and parse */
|
|
|
|
|
if (0 == (fp = fopen(FNAME, "r"))) {
|
2019-10-10 07:44:07 +08:00
|
|
|
|
return handle_error();
|
2017-03-19 05:20:51 +08:00
|
|
|
|
}
|
|
|
|
|
conf = toml_parse_file(fp, errbuf, sizeof(errbuf));
|
|
|
|
|
fclose(fp);
|
|
|
|
|
if (0 == conf) {
|
2019-10-10 07:44:07 +08:00
|
|
|
|
return handle_error();
|
2017-03-19 05:20:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* locate the [server] table */
|
|
|
|
|
if (0 == (server = toml_table_in(conf, "server"))) {
|
2019-10-10 07:44:07 +08:00
|
|
|
|
return handle_error();
|
2017-03-19 05:20:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* extract host config value */
|
|
|
|
|
if (0 == (raw = toml_raw_in(server, "host"))) {
|
2019-10-10 07:44:07 +08:00
|
|
|
|
return handle_error();
|
2017-03-19 05:20:51 +08:00
|
|
|
|
}
|
|
|
|
|
if (toml_rtos(raw, &host)) {
|
2019-10-10 07:44:07 +08:00
|
|
|
|
return handle_error();
|
2017-03-19 05:20:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* extract port config value */
|
|
|
|
|
if (0 == (raw = toml_raw_in(server, "port"))) {
|
2019-10-10 07:44:07 +08:00
|
|
|
|
return handle_error();
|
2017-03-19 05:20:51 +08:00
|
|
|
|
}
|
|
|
|
|
if (toml_rtoi(raw, &port)) {
|
2019-10-10 07:44:07 +08:00
|
|
|
|
return handle_error();
|
2017-03-19 05:20:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* done with conf */
|
|
|
|
|
toml_free(conf);
|
|
|
|
|
|
|
|
|
|
/* use host and port */
|
|
|
|
|
do_work(host, port);
|
|
|
|
|
|
|
|
|
|
/* clean up */
|
|
|
|
|
free(host);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 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:
|
|
|
|
|
|
|
|
|
|
```
|
2019-10-09 07:58:18 +08:00
|
|
|
|
% make
|
|
|
|
|
% cd test1
|
|
|
|
|
% bash build.sh # do this once
|
|
|
|
|
% bash run.sh # this will run the test suite
|
|
|
|
|
```
|
|
|
|
|
|
2017-03-19 05:20:51 +08:00
|
|
|
|
|
2019-10-09 07:58:18 +08:00
|
|
|
|
To test against the standard test set provided by iarna/toml:
|
2017-03-19 05:20:51 +08:00
|
|
|
|
|
2019-10-09 07:58:18 +08:00
|
|
|
|
```
|
|
|
|
|
% make
|
|
|
|
|
% cd test2
|
|
|
|
|
% bash build.sh # do this once
|
|
|
|
|
% bash run.sh # this will run the test suite
|
|
|
|
|
```
|