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)
This commit is contained in:
Vendored
+4
-2
@@ -1,12 +1,14 @@
|
|||||||
# tomlc99
|
# tomlc99
|
||||||
|
|
||||||
|
> **Note: there is a newer version of this library available at [tomlc17](https://github.com/cktan/tomlc17).**
|
||||||
|
|
||||||
TOML in c99; v1.0 compliant.
|
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).
|
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).
|
* Compatible with [TOML v1.0.0](https://toml.io/en/v1.0.0).
|
||||||
* Tested with multiple test suites, including
|
* Tested with multiple test suites, including
|
||||||
[BurntSushi/toml-test](https://github.com/BurntSushi/toml-test) and
|
[toml-lang/toml-test](https://github.com/toml-lang/toml-test) and
|
||||||
[iarna/toml-spec-tests](https://github.com/iarna/toml-spec-tests).
|
[iarna/toml-spec-tests](https://github.com/iarna/toml-spec-tests).
|
||||||
* Provides very simple and intuitive interface.
|
* Provides very simple and intuitive interface.
|
||||||
|
|
||||||
@@ -174,7 +176,7 @@ Alternatively, specify `make install prefix=/a/file/path` to install into
|
|||||||
|
|
||||||
## Testing
|
## Testing
|
||||||
|
|
||||||
To test against the standard test set provided by BurntSushi/toml-test:
|
To test against the standard test set provided by toml-lang/toml-test:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
% make
|
% make
|
||||||
|
|||||||
+3
-6
@@ -1,9 +1,6 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||||
|
|
||||||
mkdir -p $DIR/goworkspace
|
export GOBIN=$DIR
|
||||||
export GOPATH=$DIR/goworkspace
|
go install github.com/toml-lang/toml-test/cmd/toml-test@latest # install test suite
|
||||||
go get github.com/BurntSushi/toml-test@latest # install test suite
|
|
||||||
go install github.com/BurntSushi/toml/cmd/toml-test-decoder@latest # e.g., install my parser
|
|
||||||
cp $GOPATH/bin/* .
|
|
||||||
|
|
||||||
+3
-3
@@ -1,5 +1,5 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||||
|
|
||||||
rm -f tests
|
$DIR/toml-test $DIR/../toml_json
|
||||||
ln -s ./goworkspace/pkg/mod/github.com/\!burnt\!sushi/toml-test@v0.1.0/tests
|
|
||||||
./toml-test ../toml_json
|
|
||||||
|
|||||||
+2
-1
@@ -1,6 +1,7 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||||
|
|
||||||
[ -d toml-spec-tests ] || git clone https://github.com/cktan/toml-spec-tests.git
|
[ -d toml-spec-tests ] || git clone https://github.com/cktan/toml-spec-tests.git
|
||||||
|
|
||||||
|
|||||||
+2
@@ -1,3 +1,5 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
if ! (which jq >& /dev/null); then
|
if ! (which jq >& /dev/null); then
|
||||||
echo "ERROR: please install the 'jq' utility"
|
echo "ERROR: please install the 'jq' utility"
|
||||||
exit 1
|
exit 1
|
||||||
|
|||||||
Vendored
+45
-33
@@ -221,8 +221,8 @@ int toml_ucs_to_utf8(int64_t code, char buf[6]) {
|
|||||||
110xxxxx 10xxxxxx
|
110xxxxx 10xxxxxx
|
||||||
*/
|
*/
|
||||||
if (code <= 0x000007FF) {
|
if (code <= 0x000007FF) {
|
||||||
buf[0] = (unsigned char) (0xc0 | (code >> 6));
|
buf[0] = (unsigned char)(0xc0 | (code >> 6));
|
||||||
buf[1] = (unsigned char) (0x80 | (code & 0x3f));
|
buf[1] = (unsigned char)(0x80 | (code & 0x3f));
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -230,9 +230,9 @@ int toml_ucs_to_utf8(int64_t code, char buf[6]) {
|
|||||||
1110xxxx 10xxxxxx 10xxxxxx
|
1110xxxx 10xxxxxx 10xxxxxx
|
||||||
*/
|
*/
|
||||||
if (code <= 0x0000FFFF) {
|
if (code <= 0x0000FFFF) {
|
||||||
buf[0] = (unsigned char) (0xe0 | (code >> 12));
|
buf[0] = (unsigned char)(0xe0 | (code >> 12));
|
||||||
buf[1] = (unsigned char) (0x80 | ((code >> 6) & 0x3f));
|
buf[1] = (unsigned char)(0x80 | ((code >> 6) & 0x3f));
|
||||||
buf[2] = (unsigned char) (0x80 | (code & 0x3f));
|
buf[2] = (unsigned char)(0x80 | (code & 0x3f));
|
||||||
return 3;
|
return 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -240,10 +240,10 @@ int toml_ucs_to_utf8(int64_t code, char buf[6]) {
|
|||||||
11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
|
11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
|
||||||
*/
|
*/
|
||||||
if (code <= 0x001FFFFF) {
|
if (code <= 0x001FFFFF) {
|
||||||
buf[0] = (unsigned char) (0xf0 | (code >> 18));
|
buf[0] = (unsigned char)(0xf0 | (code >> 18));
|
||||||
buf[1] = (unsigned char) (0x80 | ((code >> 12) & 0x3f));
|
buf[1] = (unsigned char)(0x80 | ((code >> 12) & 0x3f));
|
||||||
buf[2] = (unsigned char) (0x80 | ((code >> 6) & 0x3f));
|
buf[2] = (unsigned char)(0x80 | ((code >> 6) & 0x3f));
|
||||||
buf[3] = (unsigned char) (0x80 | (code & 0x3f));
|
buf[3] = (unsigned char)(0x80 | (code & 0x3f));
|
||||||
return 4;
|
return 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -251,11 +251,11 @@ int toml_ucs_to_utf8(int64_t code, char buf[6]) {
|
|||||||
111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
|
111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
|
||||||
*/
|
*/
|
||||||
if (code <= 0x03FFFFFF) {
|
if (code <= 0x03FFFFFF) {
|
||||||
buf[0] = (unsigned char) (0xf8 | (code >> 24));
|
buf[0] = (unsigned char)(0xf8 | (code >> 24));
|
||||||
buf[1] = (unsigned char) (0x80 | ((code >> 18) & 0x3f));
|
buf[1] = (unsigned char)(0x80 | ((code >> 18) & 0x3f));
|
||||||
buf[2] = (unsigned char) (0x80 | ((code >> 12) & 0x3f));
|
buf[2] = (unsigned char)(0x80 | ((code >> 12) & 0x3f));
|
||||||
buf[3] = (unsigned char) (0x80 | ((code >> 6) & 0x3f));
|
buf[3] = (unsigned char)(0x80 | ((code >> 6) & 0x3f));
|
||||||
buf[4] = (unsigned char) (0x80 | (code & 0x3f));
|
buf[4] = (unsigned char)(0x80 | (code & 0x3f));
|
||||||
return 5;
|
return 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -263,12 +263,12 @@ int toml_ucs_to_utf8(int64_t code, char buf[6]) {
|
|||||||
1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
|
1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
|
||||||
*/
|
*/
|
||||||
if (code <= 0x7FFFFFFF) {
|
if (code <= 0x7FFFFFFF) {
|
||||||
buf[0] = (unsigned char) (0xfc | (code >> 30));
|
buf[0] = (unsigned char)(0xfc | (code >> 30));
|
||||||
buf[1] = (unsigned char) (0x80 | ((code >> 24) & 0x3f));
|
buf[1] = (unsigned char)(0x80 | ((code >> 24) & 0x3f));
|
||||||
buf[2] = (unsigned char) (0x80 | ((code >> 18) & 0x3f));
|
buf[2] = (unsigned char)(0x80 | ((code >> 18) & 0x3f));
|
||||||
buf[3] = (unsigned char) (0x80 | ((code >> 12) & 0x3f));
|
buf[3] = (unsigned char)(0x80 | ((code >> 12) & 0x3f));
|
||||||
buf[4] = (unsigned char) (0x80 | ((code >> 6) & 0x3f));
|
buf[4] = (unsigned char)(0x80 | ((code >> 6) & 0x3f));
|
||||||
buf[5] = (unsigned char) (0x80 | (code & 0x3f));
|
buf[5] = (unsigned char)(0x80 | (code & 0x3f));
|
||||||
return 6;
|
return 6;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -412,8 +412,10 @@ static void *expand(void *p, int sz, int newsz) {
|
|||||||
if (!s)
|
if (!s)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
memcpy(s, p, sz);
|
if (p) {
|
||||||
FREE(p);
|
memcpy(s, p, sz);
|
||||||
|
FREE(p);
|
||||||
|
}
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -423,8 +425,10 @@ static void **expand_ptrarr(void **p, int n) {
|
|||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
s[n] = 0;
|
s[n] = 0;
|
||||||
memcpy(s, p, n * sizeof(void *));
|
if (p) {
|
||||||
FREE(p);
|
memcpy(s, p, n * sizeof(void *));
|
||||||
|
FREE(p);
|
||||||
|
}
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2212,6 +2216,7 @@ int toml_rtos(toml_raw_t src, char **ret) {
|
|||||||
if (!src)
|
if (!src)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
|
// for strings, first char must be a s-quote or d-quote
|
||||||
int qchar = src[0];
|
int qchar = src[0];
|
||||||
int srclen = strlen(src);
|
int srclen = strlen(src);
|
||||||
if (!(qchar == '\'' || qchar == '"')) {
|
if (!(qchar == '\'' || qchar == '"')) {
|
||||||
@@ -2220,12 +2225,14 @@ int toml_rtos(toml_raw_t src, char **ret) {
|
|||||||
|
|
||||||
// triple quotes?
|
// triple quotes?
|
||||||
if (qchar == src[1] && qchar == src[2]) {
|
if (qchar == src[1] && qchar == src[2]) {
|
||||||
multiline = 1;
|
multiline = 1; // triple-quote implies multiline
|
||||||
sp = src + 3;
|
sp = src + 3; // first char after quote
|
||||||
sq = src + srclen - 3;
|
sq = src + srclen - 3; // first char of ending quote
|
||||||
/* last 3 chars in src must be qchar */
|
|
||||||
if (!(sp <= sq && sq[0] == qchar && sq[1] == qchar && sq[2] == qchar))
|
if (!(sp <= sq && sq[0] == qchar && sq[1] == qchar && sq[2] == qchar)) {
|
||||||
|
// last 3 chars in src must be qchar
|
||||||
return -1;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
/* skip new line immediate after qchar */
|
/* skip new line immediate after qchar */
|
||||||
if (sp[0] == '\n')
|
if (sp[0] == '\n')
|
||||||
@@ -2234,13 +2241,18 @@ int toml_rtos(toml_raw_t src, char **ret) {
|
|||||||
sp += 2;
|
sp += 2;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
sp = src + 1;
|
sp = src + 1; // first char after quote
|
||||||
sq = src + srclen - 1;
|
sq = src + srclen - 1; // ending quote
|
||||||
/* last char in src must be qchar */
|
if (!(sp <= sq && *sq == qchar)) {
|
||||||
if (!(sp <= sq && *sq == qchar))
|
/* last char in src must be qchar */
|
||||||
return -1;
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// at this point:
|
||||||
|
// sp points to first valid char after quote.
|
||||||
|
// sq points to one char beyond last valid char.
|
||||||
|
// string len is (sq - sp).
|
||||||
if (qchar == '\'') {
|
if (qchar == '\'') {
|
||||||
*ret = norm_lit_str(sp, sq - sp, multiline, 0, 0);
|
*ret = norm_lit_str(sp, sq - sp, multiline, 0, 0);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Vendored
+1
-1
@@ -26,7 +26,7 @@
|
|||||||
#define TOML_H
|
#define TOML_H
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
#pragma warning(disable: 4996)
|
#pragma warning(disable : 4996)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|||||||
Vendored
+30
-22
@@ -91,15 +91,16 @@ static void print_raw(const char *s) {
|
|||||||
else
|
else
|
||||||
millisec[0] = 0;
|
millisec[0] = 0;
|
||||||
if (ts.year && ts.hour) {
|
if (ts.year && ts.hour) {
|
||||||
printf("{\"type\":\"datetime\",\"value\":\"%04d-%02d-%02dT%02d:%02d:%02d%"
|
printf("{\"type\":\"%s\",\"value\":\"%04d-%02d-%02dT%02d:%02d:%02d%"
|
||||||
"s%s\"}",
|
"s%s\"}",
|
||||||
|
(ts.z ? "datetime" : "datetime-local"),
|
||||||
*ts.year, *ts.month, *ts.day, *ts.hour, *ts.minute, *ts.second,
|
*ts.year, *ts.month, *ts.day, *ts.hour, *ts.minute, *ts.second,
|
||||||
millisec, (ts.z ? ts.z : ""));
|
millisec, (ts.z ? ts.z : ""));
|
||||||
} else if (ts.year) {
|
} else if (ts.year) {
|
||||||
printf("{\"type\":\"date\",\"value\":\"%04d-%02d-%02d\"}", *ts.year,
|
printf("{\"type\":\"date-local\",\"value\":\"%04d-%02d-%02d\"}", *ts.year,
|
||||||
*ts.month, *ts.day);
|
*ts.month, *ts.day);
|
||||||
} else if (ts.hour) {
|
} else if (ts.hour) {
|
||||||
printf("{\"type\":\"time\",\"value\":\"%02d:%02d:%02d%s\"}", *ts.hour,
|
printf("{\"type\":\"time-local\",\"value\":\"%02d:%02d:%02d%s\"}", *ts.hour,
|
||||||
*ts.minute, *ts.second, millisec);
|
*ts.minute, *ts.second, millisec);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -149,36 +150,43 @@ static void print_table_array(toml_array_t *curarr) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void print_array(toml_array_t *curarr) {
|
static void print_array(toml_array_t *curarr) {
|
||||||
toml_array_t *arr;
|
|
||||||
const char *raw;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
if (toml_array_kind(curarr) == 't') {
|
if (toml_array_kind(curarr) == 't') {
|
||||||
print_table_array(curarr);
|
print_table_array(curarr);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("{\"type\":\"array\",\"value\":[");
|
printf("[");
|
||||||
switch (toml_array_kind(curarr)) {
|
|
||||||
|
|
||||||
case 'v':
|
const char *raw;
|
||||||
for (i = 0; 0 != (raw = toml_raw_at(curarr, i)); i++) {
|
toml_array_t *arr;
|
||||||
printf("%s", i > 0 ? "," : "");
|
toml_table_t *tab;
|
||||||
print_raw(raw);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'a':
|
const int n = toml_array_nelem(curarr);
|
||||||
for (i = 0; 0 != (arr = toml_array_at(curarr, i)); i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
printf("%s", i > 0 ? "," : "");
|
printf("%s", i > 0 ? "," : "");
|
||||||
|
|
||||||
|
if (0 != (arr = toml_array_at(curarr, i))) {
|
||||||
print_array(arr);
|
print_array(arr);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
if (0 != (tab = toml_table_at(curarr, i))) {
|
||||||
break;
|
print_table(tab);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
raw = toml_raw_at(curarr, i);
|
||||||
|
if (raw) {
|
||||||
|
print_raw(raw);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
fflush(stdout);
|
||||||
|
fprintf(stderr, "ERROR: unable to decode value in array\n");
|
||||||
|
exit(1);
|
||||||
}
|
}
|
||||||
printf("]}");
|
|
||||||
|
printf("]");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void cat(FILE *fp) {
|
static void cat(FILE *fp) {
|
||||||
|
|||||||
Reference in New Issue
Block a user