# 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](https://github.com/cktan/tomlcpp).
* Compatible with [TOML v1.0.0](https://toml.io/en/v1.0.0).
* Tested with multiple test suites, including
[toml-lang/toml-test](https://github.com/toml-lang/toml-test) and
[iarna/toml-spec-tests](https://github.com/iarna/toml-spec-tests).
* Provides very simple and intuitive interface.
## Usage
Please see the `toml.h` file for details. The following is a simple example that
parses this config file:
```toml
[server]
host = "www.example.com"
port = [ 8080, 8181, 8282 ]
```
These are the usual steps for getting values from a file:
1. Parse the TOML file.
2. Traverse and locate a table in TOML.
3. Extract values from the table.
4. Free up allocated memory.
Below is an example of parsing the values from the example table.
```c
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include "toml.h"
static void error(const char* msg, const char* msg1)
{
fprintf(stderr, "ERROR: %s%s\n", msg, msg1?msg1:"");
exit(1);
}
int main()
{
FILE* fp;
char errbuf[200];
// 1. Read and parse toml file
fp = fopen("sample.toml", "r");
if (!fp) {
error("cannot open sample.toml - ", strerror(errno));
}
toml_table_t* conf = toml_parse_file(fp, errbuf, sizeof(errbuf));
fclose(fp);
if (!conf) {
error("cannot parse - ", errbuf);
}
// 2. Traverse to a table.
toml_table_t* server = toml_table_in(conf, "server");
if (!server) {
error("missing [server]", "");
}
// 3. Extract values
toml_datum_t host = toml_string_in(server, "host");
if (!host.ok) {
error("cannot read server.host", "");
}
toml_array_t* portarray = toml_array_in(server, "port");
if (!portarray) {
error("cannot read server.port", "");
}
printf("host: %s\n", host.u.s);
printf("port: ");
for (int i = 0; ; i++) {
toml_datum_t port = toml_int_at(portarray, i);
if (!port.ok) break;
printf("%d ", (int)port.u.i);
}
printf("\n");
// 4. Free memory
free(host.u.s);
toml_free(conf);
return 0;
}
```
#### Accessing Table Content
TOML tables are dictionaries where lookups are done using string keys. In
general, all access functions on tables are named `toml_*_in(...)`.
In the normal case, you know the key and its content type, and retrievals can be done
using one of these functions:
```c
toml_string_in(tab, key);
toml_bool_in(tab, key);
toml_int_in(tab, key);
toml_double_in(tab, key);
toml_timestamp_in(tab, key);
toml_table_in(tab, key);
toml_array_in(tab, key);
```
You can also interrogate the keys in a table using an integer index:
```c
toml_table_t* tab = toml_parse_file(...);
for (int i = 0; ; i++) {
const char* key = toml_key_in(tab, i);
if (!key) break;
printf("key %d: %s\n", i, key);
}
```
#### Accessing Array Content
TOML arrays can be deref-ed using integer indices. In general, all access methods on arrays are named `toml_*_at()`.
To obtain the size of an array:
```c
int size = toml_array_nelem(arr);
```
To obtain the content of an array, use a valid index and call one of these functions:
```c
toml_string_at(arr, idx);
toml_bool_at(arr, idx);
toml_int_at(arr, idx);
toml_double_at(arr, idx);
toml_timestamp_at(arr, idx);
toml_table_at(arr, idx);
toml_array_at(arr, idx);
```
#### toml_datum_t
Some `toml_*_at` and `toml_*_in` functions return a toml_datum_t
structure. The `ok` flag in the structure indicates if the function
call was successful. If so, you may proceed to read the value
corresponding to the type of the content.
For example:
```
toml_datum_t host = toml_string_in(tab, "host");
if (host.ok) {
printf("host: %s\n", host.u.s);
free(host.u.s); /* FREE applies to string and timestamp types only */
}
```
** IMPORTANT: if the accessed value is a string or a timestamp, you must call `free(datum.u.s)` or `free(datum.u.ts)` respectively after usage. **
## Building and installing
A normal *make* suffices. You can also simply include the
`toml.c` and `toml.h` files in your project.
Invoking `make install` will install the header and library files into
/usr/local/{include,lib}.
Alternatively, specify `make install prefix=/a/file/path` to install into
/a/file/path/{include,lib}.
## Testing
To test against the standard test set provided by toml-lang/toml-test:
```sh
% 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:
```sh
% make
% cd test2
% bash build.sh # do this once
% bash run.sh # this will run the test suite
```
没有合适的资源?快使用搜索试试~ 我知道了~
TOML C library.zip
共151个文件
toml:66个
res:62个
c:5个
需积分: 5 0 下载量 46 浏览量
2023-12-31
11:14:52
上传
评论
收藏 51KB ZIP 举报
温馨提示
TOML C library
资源推荐
资源详情
资源评论
收起资源包目录
TOML C library.zip (151个子文件)
toml.c 55KB
toml_cat.c 7KB
toml_json.c 5KB
t1.c 3KB
toml_sample.c 1KB
.editorconfig 190B
.gitignore 286B
.gitignore 43B
.gitignore 16B
.gitignore 7B
toml.h 7KB
LICENSE 1KB
Makefile 969B
Makefile 90B
README.md 5KB
README.md 592B
arr1.toml.res 648B
arrtab2.toml.res 386B
string4.toml.res 264B
tab03.toml.res 209B
arrtab7.toml.res 178B
arrtab1.toml.res 175B
string3.toml.res 175B
float1.toml.res 167B
string5.toml.res 165B
inlinetab1.toml.res 165B
tab11.toml.res 160B
keys11.toml.res 159B
keys10.toml.res 159B
string6.toml.res 152B
ts1.toml.res 151B
string1.toml.res 138B
string7.toml.res 134B
tab01.toml.res 129B
keys04.toml.res 125B
keys01.toml.res 125B
int0.toml.res 123B
tab08.toml.res 114B
int1.toml.res 112B
tab09.toml.res 110B
tab02.toml.res 90B
keys05.toml.res 87B
string0.toml.res 86B
keys00.toml.res 83B
float6.toml.res 83B
tab07.toml.res 82B
arr2.toml.res 81B
tab04.toml.res 80B
keys08.toml.res 79B
ts3.toml.res 68B
comment.toml.res 60B
inlinetab2.toml.res 59B
string8.toml.res 48B
ts5.toml.res 44B
float3.toml.res 41B
float2.toml.res 41B
float4.toml.res 41B
kvpair2.toml.res 39B
bool1.toml.res 37B
keys12.toml.res 35B
ts2.toml.res 35B
arrtab4.toml.res 30B
keys02.toml.res 28B
float5.toml.res 28B
kvpair1.toml.res 28B
arrtab6.toml.res 27B
inlinetab3.toml.res 26B
tab05.toml.res 26B
keys09.toml.res 26B
arrtab3.toml.res 26B
tab10.toml.res 26B
keys07.toml.res 26B
keys06.toml.res 26B
tab06.toml.res 26B
arrtab5.toml.res 26B
ts4.toml.res 24B
kvpair0.toml.res 21B
keys03.toml.res 18B
libtoml.pc.sample 248B
run.sh 821B
RUN.sh 252B
build.sh 179B
build.sh 167B
run.sh 111B
arr1.toml 438B
string4.toml 363B
float6.toml 277B
string3.toml 275B
string1.toml 273B
arrtab2.toml 271B
arrtab3.toml 256B
arrtab6.toml 242B
keys09.toml 225B
int1.toml 224B
tab09.toml 211B
arrtab5.toml 199B
tab11.toml 177B
string5.toml 174B
int0.toml 164B
string7.toml 160B
共 151 条
- 1
- 2
资源评论
暮苍梧~
- 粉丝: 41
- 资源: 258
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功