Skip to content

Commit 312d9b8

Browse files
committed
Update TOML C99 library
From https://github.com/cktan/tomlc99, merge commits after 894902820a3ea2f1ec470cd7fe338bde54045cf5 (2022-09-12) up to and including df627177cd1e80176c7a5245f26fd3b8e6187368 (2025-05-01)
1 parent c2e543a commit 312d9b8

File tree

8 files changed

+95
-73
lines changed

8 files changed

+95
-73
lines changed

third_party/tomlc99/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# tomlc99
22

3+
> **Note: there is a newer version of this library available at [tomlc17](https://github.com/cktan/tomlc17).**
4+
35
TOML in c99; v1.0 compliant.
46

57
If you are looking for a C++ library, you might try this wrapper: [https://github.com/cktan/tomlcpp](https://github.com/cktan/tomlcpp).
68

79
* Compatible with [TOML v1.0.0](https://toml.io/en/v1.0.0).
810
* Tested with multiple test suites, including
9-
[BurntSushi/toml-test](https://github.com/BurntSushi/toml-test) and
11+
[toml-lang/toml-test](https://github.com/toml-lang/toml-test) and
1012
[iarna/toml-spec-tests](https://github.com/iarna/toml-spec-tests).
1113
* Provides very simple and intuitive interface.
1214

@@ -174,7 +176,7 @@ Alternatively, specify `make install prefix=/a/file/path` to install into
174176
175177
## Testing
176178
177-
To test against the standard test set provided by BurntSushi/toml-test:
179+
To test against the standard test set provided by toml-lang/toml-test:
178180
179181
```sh
180182
% make

third_party/tomlc99/test1/build.sh

100644100755
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1+
#!/usr/bin/env bash
12

23
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
34

4-
mkdir -p $DIR/goworkspace
5-
export GOPATH=$DIR/goworkspace
6-
go get github.com/BurntSushi/toml-test@latest # install test suite
7-
go install github.com/BurntSushi/toml/cmd/toml-test-decoder@latest # e.g., install my parser
8-
cp $GOPATH/bin/* .
9-
5+
export GOBIN=$DIR
6+
go install github.com/toml-lang/toml-test/cmd/toml-test@latest # install test suite

third_party/tomlc99/test1/run.sh

100644100755
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
#!/usr/bin/env bash
2+
13
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
24

3-
rm -f tests
4-
ln -s ./goworkspace/pkg/mod/github.com/\!burnt\!sushi/toml-test@v0.1.0/tests
5-
./toml-test ../toml_json
5+
$DIR/toml-test $DIR/../toml_json

third_party/tomlc99/test2/build.sh

100644100755
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
#!/usr/bin/env bash
2+
13
set -e
24

35
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
46

57
[ -d toml-spec-tests ] || git clone https://github.com/cktan/toml-spec-tests.git
6-

third_party/tomlc99/test2/run.sh

100644100755
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
if ! (which jq >& /dev/null); then
1+
#!/usr/bin/env bash
2+
3+
if ! (which jq >& /dev/null); then
24
echo "ERROR: please install the 'jq' utility"
35
exit 1
46
fi
@@ -12,11 +14,11 @@ for i in toml-spec-tests/values/*.toml; do
1214
fname="${fname%.*}"
1315
echo -n $fname ' '
1416
res='[OK]'
15-
if (../toml_json $fname.toml >& $fname.json.out); then
17+
if (../toml_json $fname.toml >& $fname.json.out); then
1618
jq -S . $fname.json.out > t.json
1719
mv t.json $fname.json.out
1820
if [ -f $fname.json ]; then
19-
if ! (diff $fname.json $fname.json.out >& /dev/null); then
21+
if ! (diff $fname.json $fname.json.out >& /dev/null); then
2022
res='[FAILED]'
2123
else
2224
rm -f $fname.json.out
@@ -32,10 +34,10 @@ done
3234
#
3335
# NEGATIVE tests
3436
#
35-
for i in toml-spec-tests/errors/*.toml; do
37+
for i in toml-spec-tests/errors/*.toml; do
3638
echo -n $i ' '
3739
res='[OK]'
38-
if (../toml_json $i >& $i.json.out); then
40+
if (../toml_json $i >& $i.json.out); then
3941
res='[FAILED]'
4042
fi
4143
echo ... $res

third_party/tomlc99/toml.c

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -221,54 +221,54 @@ int toml_ucs_to_utf8(int64_t code, char buf[6]) {
221221
110xxxxx 10xxxxxx
222222
*/
223223
if (code <= 0x000007FF) {
224-
buf[0] = (unsigned char) (0xc0 | (code >> 6));
225-
buf[1] = (unsigned char) (0x80 | (code & 0x3f));
224+
buf[0] = (unsigned char)(0xc0 | (code >> 6));
225+
buf[1] = (unsigned char)(0x80 | (code & 0x3f));
226226
return 2;
227227
}
228228

229229
/* 0x00000800 - 0x0000FFFF:
230230
1110xxxx 10xxxxxx 10xxxxxx
231231
*/
232232
if (code <= 0x0000FFFF) {
233-
buf[0] = (unsigned char) (0xe0 | (code >> 12));
234-
buf[1] = (unsigned char) (0x80 | ((code >> 6) & 0x3f));
235-
buf[2] = (unsigned char) (0x80 | (code & 0x3f));
233+
buf[0] = (unsigned char)(0xe0 | (code >> 12));
234+
buf[1] = (unsigned char)(0x80 | ((code >> 6) & 0x3f));
235+
buf[2] = (unsigned char)(0x80 | (code & 0x3f));
236236
return 3;
237237
}
238238

239239
/* 0x00010000 - 0x001FFFFF:
240240
11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
241241
*/
242242
if (code <= 0x001FFFFF) {
243-
buf[0] = (unsigned char) (0xf0 | (code >> 18));
244-
buf[1] = (unsigned char) (0x80 | ((code >> 12) & 0x3f));
245-
buf[2] = (unsigned char) (0x80 | ((code >> 6) & 0x3f));
246-
buf[3] = (unsigned char) (0x80 | (code & 0x3f));
243+
buf[0] = (unsigned char)(0xf0 | (code >> 18));
244+
buf[1] = (unsigned char)(0x80 | ((code >> 12) & 0x3f));
245+
buf[2] = (unsigned char)(0x80 | ((code >> 6) & 0x3f));
246+
buf[3] = (unsigned char)(0x80 | (code & 0x3f));
247247
return 4;
248248
}
249249

250250
/* 0x00200000 - 0x03FFFFFF:
251251
111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
252252
*/
253253
if (code <= 0x03FFFFFF) {
254-
buf[0] = (unsigned char) (0xf8 | (code >> 24));
255-
buf[1] = (unsigned char) (0x80 | ((code >> 18) & 0x3f));
256-
buf[2] = (unsigned char) (0x80 | ((code >> 12) & 0x3f));
257-
buf[3] = (unsigned char) (0x80 | ((code >> 6) & 0x3f));
258-
buf[4] = (unsigned char) (0x80 | (code & 0x3f));
254+
buf[0] = (unsigned char)(0xf8 | (code >> 24));
255+
buf[1] = (unsigned char)(0x80 | ((code >> 18) & 0x3f));
256+
buf[2] = (unsigned char)(0x80 | ((code >> 12) & 0x3f));
257+
buf[3] = (unsigned char)(0x80 | ((code >> 6) & 0x3f));
258+
buf[4] = (unsigned char)(0x80 | (code & 0x3f));
259259
return 5;
260260
}
261261

262262
/* 0x04000000 - 0x7FFFFFFF:
263263
1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
264264
*/
265265
if (code <= 0x7FFFFFFF) {
266-
buf[0] = (unsigned char) (0xfc | (code >> 30));
267-
buf[1] = (unsigned char) (0x80 | ((code >> 24) & 0x3f));
268-
buf[2] = (unsigned char) (0x80 | ((code >> 18) & 0x3f));
269-
buf[3] = (unsigned char) (0x80 | ((code >> 12) & 0x3f));
270-
buf[4] = (unsigned char) (0x80 | ((code >> 6) & 0x3f));
271-
buf[5] = (unsigned char) (0x80 | (code & 0x3f));
266+
buf[0] = (unsigned char)(0xfc | (code >> 30));
267+
buf[1] = (unsigned char)(0x80 | ((code >> 24) & 0x3f));
268+
buf[2] = (unsigned char)(0x80 | ((code >> 18) & 0x3f));
269+
buf[3] = (unsigned char)(0x80 | ((code >> 12) & 0x3f));
270+
buf[4] = (unsigned char)(0x80 | ((code >> 6) & 0x3f));
271+
buf[5] = (unsigned char)(0x80 | (code & 0x3f));
272272
return 6;
273273
}
274274

@@ -412,8 +412,10 @@ static void *expand(void *p, int sz, int newsz) {
412412
if (!s)
413413
return 0;
414414

415-
memcpy(s, p, sz);
416-
FREE(p);
415+
if (p) {
416+
memcpy(s, p, sz);
417+
FREE(p);
418+
}
417419
return s;
418420
}
419421

@@ -423,8 +425,10 @@ static void **expand_ptrarr(void **p, int n) {
423425
return 0;
424426

425427
s[n] = 0;
426-
memcpy(s, p, n * sizeof(void *));
427-
FREE(p);
428+
if (p) {
429+
memcpy(s, p, n * sizeof(void *));
430+
FREE(p);
431+
}
428432
return s;
429433
}
430434

@@ -2212,6 +2216,7 @@ int toml_rtos(toml_raw_t src, char **ret) {
22122216
if (!src)
22132217
return -1;
22142218

2219+
// for strings, first char must be a s-quote or d-quote
22152220
int qchar = src[0];
22162221
int srclen = strlen(src);
22172222
if (!(qchar == '\'' || qchar == '"')) {
@@ -2220,12 +2225,14 @@ int toml_rtos(toml_raw_t src, char **ret) {
22202225

22212226
// triple quotes?
22222227
if (qchar == src[1] && qchar == src[2]) {
2223-
multiline = 1;
2224-
sp = src + 3;
2225-
sq = src + srclen - 3;
2226-
/* last 3 chars in src must be qchar */
2227-
if (!(sp <= sq && sq[0] == qchar && sq[1] == qchar && sq[2] == qchar))
2228+
multiline = 1; // triple-quote implies multiline
2229+
sp = src + 3; // first char after quote
2230+
sq = src + srclen - 3; // first char of ending quote
2231+
2232+
if (!(sp <= sq && sq[0] == qchar && sq[1] == qchar && sq[2] == qchar)) {
2233+
// last 3 chars in src must be qchar
22282234
return -1;
2235+
}
22292236

22302237
/* skip new line immediate after qchar */
22312238
if (sp[0] == '\n')
@@ -2234,13 +2241,18 @@ int toml_rtos(toml_raw_t src, char **ret) {
22342241
sp += 2;
22352242

22362243
} else {
2237-
sp = src + 1;
2238-
sq = src + srclen - 1;
2239-
/* last char in src must be qchar */
2240-
if (!(sp <= sq && *sq == qchar))
2244+
sp = src + 1; // first char after quote
2245+
sq = src + srclen - 1; // ending quote
2246+
if (!(sp <= sq && *sq == qchar)) {
2247+
/* last char in src must be qchar */
22412248
return -1;
2249+
}
22422250
}
22432251

2252+
// at this point:
2253+
// sp points to first valid char after quote.
2254+
// sq points to one char beyond last valid char.
2255+
// string len is (sq - sp).
22442256
if (qchar == '\'') {
22452257
*ret = norm_lit_str(sp, sq - sp, multiline, 0, 0);
22462258
} else {

third_party/tomlc99/toml.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#define TOML_H
2727

2828
#ifdef _MSC_VER
29-
#pragma warning(disable: 4996)
29+
#pragma warning(disable : 4996)
3030
#endif
3131

3232
#include <stdint.h>

third_party/tomlc99/toml_json.c

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,16 @@ static void print_raw(const char *s) {
9191
else
9292
millisec[0] = 0;
9393
if (ts.year && ts.hour) {
94-
printf("{\"type\":\"datetime\",\"value\":\"%04d-%02d-%02dT%02d:%02d:%02d%"
94+
printf("{\"type\":\"%s\",\"value\":\"%04d-%02d-%02dT%02d:%02d:%02d%"
9595
"s%s\"}",
96+
(ts.z ? "datetime" : "datetime-local"),
9697
*ts.year, *ts.month, *ts.day, *ts.hour, *ts.minute, *ts.second,
9798
millisec, (ts.z ? ts.z : ""));
9899
} else if (ts.year) {
99-
printf("{\"type\":\"date\",\"value\":\"%04d-%02d-%02d\"}", *ts.year,
100+
printf("{\"type\":\"date-local\",\"value\":\"%04d-%02d-%02d\"}", *ts.year,
100101
*ts.month, *ts.day);
101102
} else if (ts.hour) {
102-
printf("{\"type\":\"time\",\"value\":\"%02d:%02d:%02d%s\"}", *ts.hour,
103+
printf("{\"type\":\"time-local\",\"value\":\"%02d:%02d:%02d%s\"}", *ts.hour,
103104
*ts.minute, *ts.second, millisec);
104105
}
105106
} else {
@@ -149,36 +150,43 @@ static void print_table_array(toml_array_t *curarr) {
149150
}
150151

151152
static void print_array(toml_array_t *curarr) {
152-
toml_array_t *arr;
153-
const char *raw;
154-
int i;
155-
156153
if (toml_array_kind(curarr) == 't') {
157154
print_table_array(curarr);
158155
return;
159156
}
160157

161-
printf("{\"type\":\"array\",\"value\":[");
162-
switch (toml_array_kind(curarr)) {
158+
printf("[");
163159

164-
case 'v':
165-
for (i = 0; 0 != (raw = toml_raw_at(curarr, i)); i++) {
166-
printf("%s", i > 0 ? "," : "");
167-
print_raw(raw);
168-
}
169-
break;
160+
const char *raw;
161+
toml_array_t *arr;
162+
toml_table_t *tab;
170163

171-
case 'a':
172-
for (i = 0; 0 != (arr = toml_array_at(curarr, i)); i++) {
173-
printf("%s", i > 0 ? "," : "");
164+
const int n = toml_array_nelem(curarr);
165+
for (int i = 0; i < n; i++) {
166+
printf("%s", i > 0 ? "," : "");
167+
168+
if (0 != (arr = toml_array_at(curarr, i))) {
174169
print_array(arr);
170+
continue;
171+
}
172+
173+
if (0 != (tab = toml_table_at(curarr, i))) {
174+
print_table(tab);
175+
continue;
176+
}
177+
178+
raw = toml_raw_at(curarr, i);
179+
if (raw) {
180+
print_raw(raw);
181+
continue;
175182
}
176-
break;
177183

178-
default:
179-
break;
184+
fflush(stdout);
185+
fprintf(stderr, "ERROR: unable to decode value in array\n");
186+
exit(1);
180187
}
181-
printf("]}");
188+
189+
printf("]");
182190
}
183191

184192
static void cat(FILE *fp) {

0 commit comments

Comments
 (0)