format with clang-format

This commit is contained in:
CK Tan 2021-12-25 23:42:50 -08:00
parent 64e280e20b
commit 32c38751b9
6 changed files with 2401 additions and 2360 deletions

View file

@ -6,8 +6,8 @@ insert_final_newline = false
trim_trailing_whitespace = true trim_trailing_whitespace = true
[*.{c,h}] [*.{c,h}]
indent_style = tab indent_style = space
indent_size = 4 indent_size = 2
[Makefile] [Makefile]
indent_style = tab indent_style = tab

3727
toml.c

File diff suppressed because it is too large Load diff

130
toml.h
View file

@ -25,10 +25,8 @@
#ifndef TOML_H #ifndef TOML_H
#define TOML_H #define TOML_H
#include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h>
#ifdef __cplusplus #ifdef __cplusplus
#define TOML_EXTERN extern "C" #define TOML_EXTERN extern "C"
@ -44,134 +42,130 @@ typedef struct toml_datum_t toml_datum_t;
/* Parse a file. Return a table on success, or 0 otherwise. /* Parse a file. Return a table on success, or 0 otherwise.
* Caller must toml_free(the-return-value) after use. * Caller must toml_free(the-return-value) after use.
*/ */
TOML_EXTERN toml_table_t* toml_parse_file(FILE* fp, TOML_EXTERN toml_table_t *toml_parse_file(FILE *fp, char *errbuf, int errbufsz);
char* errbuf,
int errbufsz);
/* Parse a string containing the full config. /* Parse a string containing the full config.
* Return a table on success, or 0 otherwise. * Return a table on success, or 0 otherwise.
* Caller must toml_free(the-return-value) after use. * Caller must toml_free(the-return-value) after use.
*/ */
TOML_EXTERN toml_table_t* toml_parse(char* conf, /* NUL terminated, please. */ TOML_EXTERN toml_table_t *toml_parse(char *conf, /* NUL terminated, please. */
char* errbuf, char *errbuf, int errbufsz);
int errbufsz);
/* Free the table returned by toml_parse() or toml_parse_file(). Once /* Free the table returned by toml_parse() or toml_parse_file(). Once
* this function is called, any handles accessed through this tab * this function is called, any handles accessed through this tab
* directly or indirectly are no longer valid. * directly or indirectly are no longer valid.
*/ */
TOML_EXTERN void toml_free(toml_table_t* tab); TOML_EXTERN void toml_free(toml_table_t *tab);
/* Timestamp types. The year, month, day, hour, minute, second, z /* Timestamp types. The year, month, day, hour, minute, second, z
* fields may be NULL if they are not relevant. e.g. In a DATE * fields may be NULL if they are not relevant. e.g. In a DATE
* type, the hour, minute, second and z fields will be NULLs. * type, the hour, minute, second and z fields will be NULLs.
*/ */
struct toml_timestamp_t { struct toml_timestamp_t {
struct { /* internal. do not use. */ struct { /* internal. do not use. */
int year, month, day; int year, month, day;
int hour, minute, second, millisec; int hour, minute, second, millisec;
char z[10]; char z[10];
} __buffer; } __buffer;
int *year, *month, *day; int *year, *month, *day;
int *hour, *minute, *second, *millisec; int *hour, *minute, *second, *millisec;
char* z; char *z;
}; };
/*----------------------------------------------------------------- /*-----------------------------------------------------------------
* Enhanced access methods * Enhanced access methods
*/ */
struct toml_datum_t { struct toml_datum_t {
int ok; int ok;
union { union {
toml_timestamp_t* ts; /* ts must be freed after use */ toml_timestamp_t *ts; /* ts must be freed after use */
char* s; /* string value. s must be freed after use */ char *s; /* string value. s must be freed after use */
int b; /* bool value */ int b; /* bool value */
int64_t i; /* int value */ int64_t i; /* int value */
double d; /* double value */ double d; /* double value */
} u; } u;
}; };
/* on arrays: */ /* on arrays: */
/* ... retrieve size of array. */ /* ... retrieve size of array. */
TOML_EXTERN int toml_array_nelem(const toml_array_t* arr); TOML_EXTERN int toml_array_nelem(const toml_array_t *arr);
/* ... retrieve values using index. */ /* ... retrieve values using index. */
TOML_EXTERN toml_datum_t toml_string_at(const toml_array_t* arr, int idx); TOML_EXTERN toml_datum_t toml_string_at(const toml_array_t *arr, int idx);
TOML_EXTERN toml_datum_t toml_bool_at(const toml_array_t* arr, int idx); TOML_EXTERN toml_datum_t toml_bool_at(const toml_array_t *arr, int idx);
TOML_EXTERN toml_datum_t toml_int_at(const toml_array_t* arr, int idx); TOML_EXTERN toml_datum_t toml_int_at(const toml_array_t *arr, int idx);
TOML_EXTERN toml_datum_t toml_double_at(const toml_array_t* arr, int idx); TOML_EXTERN toml_datum_t toml_double_at(const toml_array_t *arr, int idx);
TOML_EXTERN toml_datum_t toml_timestamp_at(const toml_array_t* arr, int idx); TOML_EXTERN toml_datum_t toml_timestamp_at(const toml_array_t *arr, int idx);
/* ... retrieve array or table using index. */ /* ... retrieve array or table using index. */
TOML_EXTERN toml_array_t* toml_array_at(const toml_array_t* arr, int idx); TOML_EXTERN toml_array_t *toml_array_at(const toml_array_t *arr, int idx);
TOML_EXTERN toml_table_t* toml_table_at(const toml_array_t* arr, int idx); TOML_EXTERN toml_table_t *toml_table_at(const toml_array_t *arr, int idx);
/* on tables: */ /* on tables: */
/* ... retrieve the key in table at keyidx. Return 0 if out of range. */ /* ... retrieve the key in table at keyidx. Return 0 if out of range. */
TOML_EXTERN const char* toml_key_in(const toml_table_t* tab, int keyidx); TOML_EXTERN const char *toml_key_in(const toml_table_t *tab, int keyidx);
/* ... returns 1 if key exists in tab, 0 otherwise */ /* ... returns 1 if key exists in tab, 0 otherwise */
TOML_EXTERN int toml_key_exists(const toml_table_t* tab, const char* key); TOML_EXTERN int toml_key_exists(const toml_table_t *tab, const char *key);
/* ... retrieve values using key. */ /* ... retrieve values using key. */
TOML_EXTERN toml_datum_t toml_string_in(const toml_table_t* arr, const char* key); TOML_EXTERN toml_datum_t toml_string_in(const toml_table_t *arr,
TOML_EXTERN toml_datum_t toml_bool_in(const toml_table_t* arr, const char* key); const char *key);
TOML_EXTERN toml_datum_t toml_int_in(const toml_table_t* arr, const char* key); TOML_EXTERN toml_datum_t toml_bool_in(const toml_table_t *arr, const char *key);
TOML_EXTERN toml_datum_t toml_double_in(const toml_table_t* arr, const char* key); TOML_EXTERN toml_datum_t toml_int_in(const toml_table_t *arr, const char *key);
TOML_EXTERN toml_datum_t toml_timestamp_in(const toml_table_t* arr, const char* key); TOML_EXTERN toml_datum_t toml_double_in(const toml_table_t *arr,
const char *key);
TOML_EXTERN toml_datum_t toml_timestamp_in(const toml_table_t *arr,
const char *key);
/* .. retrieve array or table using key. */ /* .. retrieve array or table using key. */
TOML_EXTERN toml_array_t* toml_array_in(const toml_table_t* tab, TOML_EXTERN toml_array_t *toml_array_in(const toml_table_t *tab,
const char* key); const char *key);
TOML_EXTERN toml_table_t* toml_table_in(const toml_table_t* tab, TOML_EXTERN toml_table_t *toml_table_in(const toml_table_t *tab,
const char* key); const char *key);
/*----------------------------------------------------------------- /*-----------------------------------------------------------------
* lesser used * lesser used
*/ */
/* Return the array kind: 't'able, 'a'rray, 'v'alue, 'm'ixed */ /* Return the array kind: 't'able, 'a'rray, 'v'alue, 'm'ixed */
TOML_EXTERN char toml_array_kind(const toml_array_t* arr); TOML_EXTERN char toml_array_kind(const toml_array_t *arr);
/* For array kind 'v'alue, return the type of values /* For array kind 'v'alue, return the type of values
i:int, d:double, b:bool, s:string, t:time, D:date, T:timestamp, 'm'ixed i:int, d:double, b:bool, s:string, t:time, D:date, T:timestamp, 'm'ixed
0 if unknown 0 if unknown
*/ */
TOML_EXTERN char toml_array_type(const toml_array_t* arr); TOML_EXTERN char toml_array_type(const toml_array_t *arr);
/* Return the key of an array */ /* Return the key of an array */
TOML_EXTERN const char* toml_array_key(const toml_array_t* arr); TOML_EXTERN const char *toml_array_key(const toml_array_t *arr);
/* Return the number of key-values in a table */ /* Return the number of key-values in a table */
TOML_EXTERN int toml_table_nkval(const toml_table_t* tab); TOML_EXTERN int toml_table_nkval(const toml_table_t *tab);
/* Return the number of arrays in a table */ /* Return the number of arrays in a table */
TOML_EXTERN int toml_table_narr(const toml_table_t* tab); TOML_EXTERN int toml_table_narr(const toml_table_t *tab);
/* Return the number of sub-tables in a table */ /* Return the number of sub-tables in a table */
TOML_EXTERN int toml_table_ntab(const toml_table_t* tab); TOML_EXTERN int toml_table_ntab(const toml_table_t *tab);
/* Return the key of a table*/ /* Return the key of a table*/
TOML_EXTERN const char* toml_table_key(const toml_table_t* tab); TOML_EXTERN const char *toml_table_key(const toml_table_t *tab);
/*-------------------------------------------------------------- /*--------------------------------------------------------------
* misc * misc
*/ */
TOML_EXTERN int toml_utf8_to_ucs(const char* orig, int len, int64_t* ret); TOML_EXTERN int toml_utf8_to_ucs(const char *orig, int len, int64_t *ret);
TOML_EXTERN int toml_ucs_to_utf8(int64_t code, char buf[6]); TOML_EXTERN int toml_ucs_to_utf8(int64_t code, char buf[6]);
TOML_EXTERN void toml_set_memutil(void* (*xxmalloc)(size_t), TOML_EXTERN void toml_set_memutil(void *(*xxmalloc)(size_t),
void (*xxfree)(void*)); void (*xxfree)(void *));
/*-------------------------------------------------------------- /*--------------------------------------------------------------
* deprecated * deprecated
*/ */
/* A raw value, must be processed by toml_rto* before using. */ /* A raw value, must be processed by toml_rto* before using. */
typedef const char* toml_raw_t; typedef const char *toml_raw_t;
TOML_EXTERN toml_raw_t toml_raw_in(const toml_table_t* tab, const char* key); TOML_EXTERN toml_raw_t toml_raw_in(const toml_table_t *tab, const char *key);
TOML_EXTERN toml_raw_t toml_raw_at(const toml_array_t* arr, int idx); TOML_EXTERN toml_raw_t toml_raw_at(const toml_array_t *arr, int idx);
TOML_EXTERN int toml_rtos(toml_raw_t s, char** ret); TOML_EXTERN int toml_rtos(toml_raw_t s, char **ret);
TOML_EXTERN int toml_rtob(toml_raw_t s, int* ret); TOML_EXTERN int toml_rtob(toml_raw_t s, int *ret);
TOML_EXTERN int toml_rtoi(toml_raw_t s, int64_t* ret); TOML_EXTERN int toml_rtoi(toml_raw_t s, int64_t *ret);
TOML_EXTERN int toml_rtod(toml_raw_t s, double* ret); TOML_EXTERN int toml_rtod(toml_raw_t s, double *ret);
TOML_EXTERN int toml_rtod_ex(toml_raw_t s, double* ret, char* buf, int buflen); TOML_EXTERN int toml_rtod_ex(toml_raw_t s, double *ret, char *buf, int buflen);
TOML_EXTERN int toml_rtots(toml_raw_t s, toml_timestamp_t* ret); TOML_EXTERN int toml_rtots(toml_raw_t s, toml_timestamp_t *ret);
#endif /* TOML_H */ #endif /* TOML_H */

View file

@ -27,295 +27,296 @@
#undef NDEBUG #undef NDEBUG
#endif #endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <stdint.h>
#include <assert.h>
#include <inttypes.h>
#include <ctype.h>
#include "toml.h" #include "toml.h"
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node_t node_t; typedef struct node_t node_t;
struct node_t { struct node_t {
const char* key; const char *key;
toml_table_t* tab; toml_table_t *tab;
}; };
node_t stack[20]; node_t stack[20];
int stacktop = 0; int stacktop = 0;
int indent = 0; int indent = 0;
static void prindent() static void prindent() {
{ for (int i = 0; i < indent; i++)
for (int i = 0; i < indent; i++) printf(" "); printf(" ");
} }
static void print_string(const char *s) {
int ok = 1;
for (const char *p = s; *p && ok; p++) {
int ch = *p;
ok = isprint(ch) && ch != '"' && ch != '\\';
}
static void print_string(const char* s) if (ok) {
{ printf("\"%s\"", s);
int ok = 1; return;
for (const char* p = s; *p && ok; p++) { }
int ch = *p;
ok = isprint(ch) && ch != '"' && ch != '\\';
}
if (ok) { int len = strlen(s);
printf("\"%s\"", s);
return;
}
int len = strlen(s); printf("\"");
for (; len; len--, s++) {
int ch = *s;
if (isprint(ch) && ch != '"' && ch != '\\') {
putchar(ch);
continue;
}
printf("\""); switch (ch) {
for ( ; len; len--, s++) { case 0x8:
int ch = *s; printf("\\b");
if (isprint(ch) && ch != '"' && ch != '\\') { continue;
putchar(ch); case 0x9:
continue; printf("\\t");
} continue;
case 0xa:
switch (ch) { printf("\\n");
case 0x8: printf("\\b"); continue; continue;
case 0x9: printf("\\t"); continue; case 0xc:
case 0xa: printf("\\n"); continue; printf("\\f");
case 0xc: printf("\\f"); continue; continue;
case 0xd: printf("\\r"); continue; case 0xd:
case '"': printf("\\\""); continue; printf("\\r");
case '\\': printf("\\\\"); continue; continue;
default: printf("\\0x%02x", ch & 0xff); continue; case '"':
} printf("\\\"");
} continue;
printf("\""); case '\\':
printf("\\\\");
continue;
default:
printf("\\0x%02x", ch & 0xff);
continue;
}
}
printf("\"");
} }
static void print_array(toml_array_t *arr);
static void print_array(toml_array_t* arr); static void print_timestamp(toml_datum_t d) {
if (d.u.ts->year) {
static void print_timestamp(toml_datum_t d) printf("%04d-%02d-%02d%s", *d.u.ts->year, *d.u.ts->month, *d.u.ts->day,
{ d.u.ts->hour ? "T" : "");
if (d.u.ts->year) { }
printf("%04d-%02d-%02d%s", *d.u.ts->year, *d.u.ts->month, *d.u.ts->day, if (d.u.ts->hour) {
d.u.ts->hour ? "T" : ""); printf("%02d:%02d:%02d", *d.u.ts->hour, *d.u.ts->minute, *d.u.ts->second);
} if (d.u.ts->millisec) {
if (d.u.ts->hour) { printf(".%03d", *d.u.ts->millisec);
printf("%02d:%02d:%02d", *d.u.ts->hour, *d.u.ts->minute, *d.u.ts->second); }
if (d.u.ts->millisec) { if (d.u.ts->z) {
printf(".%03d", *d.u.ts->millisec); printf("%s", d.u.ts->z);
} }
if (d.u.ts->z) { }
printf("%s", d.u.ts->z);
}
}
} }
static void print_table(toml_table_t *curtab) {
toml_datum_t d;
int i;
const char *key;
toml_array_t *arr;
toml_table_t *tab;
for (i = 0; 0 != (key = toml_key_in(curtab, i)); i++) {
static void print_table(toml_table_t* curtab) if (0 != (arr = toml_array_in(curtab, key))) {
{ prindent();
toml_datum_t d; printf("%s = [\n", key);
int i; indent++;
const char* key; print_array(arr);
toml_array_t* arr; indent--;
toml_table_t* tab; prindent();
printf("],\n");
continue;
}
for (i = 0; 0 != (key = toml_key_in(curtab, i)); i++) { if (0 != (tab = toml_table_in(curtab, key))) {
stack[stacktop].key = key;
stack[stacktop].tab = tab;
stacktop++;
prindent();
printf("%s = {\n", key);
indent++;
print_table(tab);
indent--;
prindent();
printf("},\n");
stacktop--;
continue;
}
if (0 != (arr = toml_array_in(curtab, key))) { d = toml_string_in(curtab, key);
prindent(); if (d.ok) {
printf("%s = [\n", key); prindent();
indent++; printf("%s = ", key);
print_array(arr); print_string(d.u.s);
indent--; printf(",\n");
prindent(); free(d.u.s);
printf("],\n"); continue;
continue; }
}
if (0 != (tab = toml_table_in(curtab, key))) { d = toml_bool_in(curtab, key);
stack[stacktop].key = key; if (d.ok) {
stack[stacktop].tab = tab; prindent();
stacktop++; printf("%s = %s,\n", key, d.u.b ? "true" : "false");
prindent(); continue;
printf("%s = {\n", key); }
indent++;
print_table(tab);
indent--;
prindent();
printf("},\n");
stacktop--;
continue;
}
d = toml_string_in(curtab, key); d = toml_int_in(curtab, key);
if (d.ok) { if (d.ok) {
prindent(); prindent();
printf("%s = ", key); printf("%s = %" PRId64 ",\n", key, d.u.i);
print_string(d.u.s); continue;
printf(",\n"); }
free(d.u.s);
continue;
}
d = toml_bool_in(curtab, key); d = toml_double_in(curtab, key);
if (d.ok) { if (d.ok) {
prindent(); prindent();
printf("%s = %s,\n", key, d.u.b ? "true" : "false"); printf("%s = %f,\n", key, d.u.d);
continue; continue;
} }
d = toml_int_in(curtab, key); d = toml_timestamp_in(curtab, key);
if (d.ok) { if (d.ok) {
prindent(); prindent();
printf("%s = %" PRId64 ",\n", key, d.u.i); printf("%s = ", key);
continue; print_timestamp(d);
} printf(",\n");
free(d.u.ts);
continue;
}
d = toml_double_in(curtab, key); fflush(stdout);
if (d.ok) { fprintf(stderr, "ERROR: unable to decode value in table\n");
prindent(); exit(1);
printf("%s = %f,\n", key, d.u.d); }
continue;
}
d = toml_timestamp_in(curtab, key);
if (d.ok) {
prindent();
printf("%s = ", key);
print_timestamp(d);
printf(",\n");
free(d.u.ts);
continue;
}
fflush(stdout);
fprintf(stderr, "ERROR: unable to decode value in table\n");
exit(1);
}
} }
static void print_array(toml_array_t *curarr) {
toml_datum_t d;
toml_array_t *arr;
toml_table_t *tab;
const int n = toml_array_nelem(curarr);
static void print_array(toml_array_t* curarr) for (int i = 0; i < n; i++) {
{
toml_datum_t d;
toml_array_t* arr;
toml_table_t* tab;
const int n = toml_array_nelem(curarr);
for (int i = 0; i < n; i++) { if (0 != (arr = toml_array_at(curarr, i))) {
prindent();
printf("[\n");
indent++;
print_array(arr);
indent--;
prindent();
printf("],\n");
continue;
}
if (0 != (arr = toml_array_at(curarr, i))) { if (0 != (tab = toml_table_at(curarr, i))) {
prindent(); prindent();
printf("[\n"); printf("{\n");
indent++; indent++;
print_array(arr); print_table(tab);
indent--; indent--;
prindent(); prindent();
printf("],\n"); printf("},\n");
continue; continue;
} }
if (0 != (tab = toml_table_at(curarr, i))) { d = toml_string_at(curarr, i);
prindent(); if (d.ok) {
printf("{\n"); prindent();
indent++; print_string(d.u.s);
print_table(tab); printf(",\n");
indent--; free(d.u.s);
prindent(); continue;
printf("},\n"); }
continue;
}
d = toml_string_at(curarr, i); d = toml_bool_at(curarr, i);
if (d.ok) { if (d.ok) {
prindent(); prindent();
print_string(d.u.s); printf("%s,\n", d.u.b ? "true" : "false");
printf(",\n"); continue;
free(d.u.s); }
continue;
}
d = toml_bool_at(curarr, i); d = toml_int_at(curarr, i);
if (d.ok) { if (d.ok) {
prindent(); prindent();
printf("%s,\n", d.u.b ? "true" : "false"); printf("%" PRId64 ",\n", d.u.i);
continue; continue;
} }
d = toml_int_at(curarr, i); d = toml_double_at(curarr, i);
if (d.ok) { if (d.ok) {
prindent(); prindent();
printf("%" PRId64 ",\n", d.u.i); printf("%f,\n", d.u.d);
continue; continue;
} }
d = toml_double_at(curarr, i); d = toml_timestamp_at(curarr, i);
if (d.ok) { if (d.ok) {
prindent(); prindent();
printf("%f,\n", d.u.d); print_timestamp(d);
continue; printf(",\n");
} free(d.u.ts);
continue;
}
d = toml_timestamp_at(curarr, i); fflush(stdout);
if (d.ok) { fprintf(stderr, "ERROR: unable to decode value in array\n");
prindent(); exit(1);
print_timestamp(d); }
printf(",\n");
free(d.u.ts);
continue;
}
fflush(stdout);
fprintf(stderr, "ERROR: unable to decode value in array\n");
exit(1);
}
} }
static void cat(FILE *fp) {
char errbuf[200];
toml_table_t *tab = toml_parse_file(fp, errbuf, sizeof(errbuf));
if (!tab) {
fprintf(stderr, "ERROR: %s\n", errbuf);
return;
}
stack[stacktop].tab = tab;
stack[stacktop].key = "";
stacktop++;
printf("{\n");
indent++;
print_table(tab);
indent--;
printf("}\n");
stacktop--;
static void cat(FILE* fp) toml_free(tab);
{
char errbuf[200];
toml_table_t* tab = toml_parse_file(fp, errbuf, sizeof(errbuf));
if (!tab) {
fprintf(stderr, "ERROR: %s\n", errbuf);
return;
}
stack[stacktop].tab = tab;
stack[stacktop].key = "";
stacktop++;
printf("{\n");
indent++;
print_table(tab);
indent--;
printf("}\n");
stacktop--;
toml_free(tab);
} }
int main(int argc, const char *argv[]) {
int i;
if (argc == 1) {
cat(stdin);
} else {
for (i = 1; i < argc; i++) {
int main(int argc, const char* argv[]) FILE *fp = fopen(argv[i], "r");
{ if (!fp) {
int i; fprintf(stderr, "ERROR: cannot open %s: %s\n", argv[i],
if (argc == 1) { strerror(errno));
cat(stdin); exit(1);
} else { }
for (i = 1; i < argc; i++) { cat(fp);
fclose(fp);
FILE* fp = fopen(argv[i], "r"); }
if (!fp) { }
fprintf(stderr, "ERROR: cannot open %s: %s\n", return 0;
argv[i], strerror(errno));
exit(1);
}
cat(fp);
fclose(fp);
}
}
return 0;
} }

View file

@ -26,189 +26,192 @@
#undef NDEBUG #undef NDEBUG
#endif #endif
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <assert.h>
#include <inttypes.h>
#include "toml.h" #include "toml.h"
#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
static void print_escape_string(const char *s) {
static void print_escape_string(const char* s) for (; *s; s++) {
{ int ch = *s;
for ( ; *s; s++) { switch (ch) {
int ch = *s; case '\b':
switch (ch) { printf("\\b");
case '\b': printf("\\b"); break; break;
case '\t': printf("\\t"); break; case '\t':
case '\n': printf("\\n"); break; printf("\\t");
case '\f': printf("\\f"); break; break;
case '\r': printf("\\r"); break; case '\n':
case '"': printf("\\\""); break; printf("\\n");
case '\\': printf("\\\\"); break; break;
default: printf("%c", ch); break; case '\f':
} printf("\\f");
} break;
case '\r':
printf("\\r");
break;
case '"':
printf("\\\"");
break;
case '\\':
printf("\\\\");
break;
default:
printf("%c", ch);
break;
}
}
} }
static void print_raw(const char* s) static void print_raw(const char *s) {
{ char *sval;
char* sval; int64_t ival;
int64_t ival; int bval;
int bval; double dval;
double dval; toml_timestamp_t ts;
toml_timestamp_t ts; char dbuf[100];
char dbuf[100];
if (0 == toml_rtos(s, &sval)) { if (0 == toml_rtos(s, &sval)) {
printf("{\"type\":\"string\",\"value\":\""); printf("{\"type\":\"string\",\"value\":\"");
print_escape_string(sval); print_escape_string(sval);
printf("\"}"); printf("\"}");
free(sval); free(sval);
} else if (0 == toml_rtoi(s, &ival)) { } else if (0 == toml_rtoi(s, &ival)) {
printf("{\"type\":\"integer\",\"value\":\"%" PRId64 "\"}", ival); printf("{\"type\":\"integer\",\"value\":\"%" PRId64 "\"}", ival);
} else if (0 == toml_rtob(s, &bval)) { } else if (0 == toml_rtob(s, &bval)) {
printf("{\"type\":\"bool\",\"value\":\"%s\"}", bval ? "true" : "false"); printf("{\"type\":\"bool\",\"value\":\"%s\"}", bval ? "true" : "false");
} else if (0 == toml_rtod_ex(s, &dval, dbuf, sizeof(dbuf))) { } else if (0 == toml_rtod_ex(s, &dval, dbuf, sizeof(dbuf))) {
printf("{\"type\":\"float\",\"value\":\"%s\"}", dbuf); printf("{\"type\":\"float\",\"value\":\"%s\"}", dbuf);
} else if (0 == toml_rtots(s, &ts)) { } else if (0 == toml_rtots(s, &ts)) {
char millisec[10]; char millisec[10];
if (ts.millisec) if (ts.millisec)
sprintf(millisec, ".%03d", *ts.millisec); sprintf(millisec, ".%03d", *ts.millisec);
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%s%s\"}", printf("{\"type\":\"datetime\",\"value\":\"%04d-%02d-%02dT%02d:%02d:%02d%"
*ts.year, *ts.month, *ts.day, *ts.hour, *ts.minute, *ts.second, "s%s\"}",
millisec, *ts.year, *ts.month, *ts.day, *ts.hour, *ts.minute, *ts.second,
(ts.z ? ts.z : "")); millisec, (ts.z ? ts.z : ""));
} else if (ts.year) { } else if (ts.year) {
printf("{\"type\":\"date\",\"value\":\"%04d-%02d-%02d\"}", printf("{\"type\":\"date\",\"value\":\"%04d-%02d-%02d\"}", *ts.year,
*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\"}", printf("{\"type\":\"time\",\"value\":\"%02d:%02d:%02d%s\"}", *ts.hour,
*ts.hour, *ts.minute, *ts.second, millisec); *ts.minute, *ts.second, millisec);
} }
} else { } else {
fprintf(stderr, "unknown type\n"); fprintf(stderr, "unknown type\n");
exit(1); exit(1);
} }
} }
static void print_array(toml_array_t *arr);
static void print_table(toml_table_t *curtab) {
int i;
const char *key;
const char *raw;
toml_array_t *arr;
toml_table_t *tab;
static void print_array(toml_array_t* arr); printf("{");
static void print_table(toml_table_t* curtab) for (i = 0; 0 != (key = toml_key_in(curtab, i)); i++) {
{
int i;
const char* key;
const char* raw;
toml_array_t* arr;
toml_table_t* tab;
printf("%s\"", i > 0 ? "," : "");
print_escape_string(key);
printf("\":");
printf("{"); if (0 != (raw = toml_raw_in(curtab, key))) {
for (i = 0; 0 != (key = toml_key_in(curtab, i)); i++) { print_raw(raw);
} else if (0 != (arr = toml_array_in(curtab, key))) {
printf("%s\"", i > 0 ? "," : ""); print_array(arr);
print_escape_string(key); } else if (0 != (tab = toml_table_in(curtab, key))) {
printf("\":"); print_table(tab);
} else {
if (0 != (raw = toml_raw_in(curtab, key))) { abort();
print_raw(raw); }
} else if (0 != (arr = toml_array_in(curtab, key))) { }
print_array(arr); printf("}");
} else if (0 != (tab = toml_table_in(curtab, key))) {
print_table(tab);
} else {
abort();
}
}
printf("}");
} }
static void print_table_array(toml_array_t* curarr) static void print_table_array(toml_array_t *curarr) {
{ int i;
int i; toml_table_t *tab;
toml_table_t* tab;
printf("["); printf("[");
for (i = 0; 0 != (tab = toml_table_at(curarr, i)); i++) { for (i = 0; 0 != (tab = toml_table_at(curarr, i)); i++) {
printf("%s", i > 0 ? "," : ""); printf("%s", i > 0 ? "," : "");
print_table(tab); print_table(tab);
} }
printf("]"); printf("]");
} }
static void print_array(toml_array_t* curarr) static void print_array(toml_array_t *curarr) {
{ toml_array_t *arr;
toml_array_t* arr; const char *raw;
const char* raw; int i;
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("{\"type\":\"array\",\"value\":[");
switch (toml_array_kind(curarr)) { switch (toml_array_kind(curarr)) {
case 'v': case 'v':
for (i = 0; 0 != (raw = toml_raw_at(curarr, i)); i++) { for (i = 0; 0 != (raw = toml_raw_at(curarr, i)); i++) {
printf("%s", i > 0 ? "," : ""); printf("%s", i > 0 ? "," : "");
print_raw(raw); print_raw(raw);
} }
break; break;
case 'a': case 'a':
for (i = 0; 0 != (arr = toml_array_at(curarr, i)); i++) { for (i = 0; 0 != (arr = toml_array_at(curarr, i)); i++) {
printf("%s", i > 0 ? "," : ""); printf("%s", i > 0 ? "," : "");
print_array(arr); print_array(arr);
} }
break; break;
default: default:
break; break;
} }
printf("]}"); printf("]}");
} }
static void cat(FILE *fp) {
char errbuf[200];
toml_table_t *tab = toml_parse_file(fp, errbuf, sizeof(errbuf));
if (!tab) {
fprintf(stderr, "ERROR: %s\n", errbuf);
exit(1);
}
static void cat(FILE* fp) print_table(tab);
{ printf("\n");
char errbuf[200];
toml_table_t* tab = toml_parse_file(fp, errbuf, sizeof(errbuf)); toml_free(tab);
if (!tab) {
fprintf(stderr, "ERROR: %s\n", errbuf);
exit(1);
}
print_table(tab);
printf("\n");
toml_free(tab);
} }
int main(int argc, const char *argv[]) {
int i;
if (argc == 1) {
cat(stdin);
} else {
for (i = 1; i < argc; i++) {
int main(int argc, const char* argv[]) FILE *fp = fopen(argv[i], "r");
{ if (!fp) {
int i; fprintf(stderr, "ERROR: cannot open %s: %s\n", argv[i],
if (argc == 1) { strerror(errno));
cat(stdin); exit(1);
} else { }
for (i = 1; i < argc; i++) { cat(fp);
fclose(fp);
FILE* fp = fopen(argv[i], "r"); }
if (!fp) { }
fprintf(stderr, "ERROR: cannot open %s: %s\n", return 0;
argv[i], strerror(errno));
exit(1);
}
cat(fp);
fclose(fp);
}
}
return 0;
} }

View file

@ -1,62 +1,60 @@
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include "toml.h" #include "toml.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void fatal(const char* msg, const char* msg1) static void fatal(const char *msg, const char *msg1) {
{ fprintf(stderr, "ERROR: %s%s\n", msg, msg1 ? msg1 : "");
fprintf(stderr, "ERROR: %s%s\n", msg, msg1?msg1:""); exit(1);
exit(1);
} }
int main() {
FILE *fp;
char errbuf[200];
int main() // 1. Read and parse toml file
{ fp = fopen("sample.toml", "r");
FILE* fp; if (!fp) {
char errbuf[200]; fatal("cannot open sample.toml - ", strerror(errno));
}
// 1. Read and parse toml file toml_table_t *conf = toml_parse_file(fp, errbuf, sizeof(errbuf));
fp = fopen("sample.toml", "r"); fclose(fp);
if (!fp) {
fatal("cannot open sample.toml - ", strerror(errno));
}
toml_table_t* conf = toml_parse_file(fp, errbuf, sizeof(errbuf)); if (!conf) {
fclose(fp); fatal("cannot parse - ", errbuf);
}
if (!conf) { // 2. Traverse to a table.
fatal("cannot parse - ", errbuf); toml_table_t *server = toml_table_in(conf, "server");
} if (!server) {
fatal("missing [server]", "");
}
// 2. Traverse to a table. // 3. Extract values
toml_table_t* server = toml_table_in(conf, "server"); toml_datum_t host = toml_string_in(server, "host");
if (!server) { if (!host.ok) {
fatal("missing [server]", ""); fatal("cannot read server.host", "");
} }
// 3. Extract values toml_array_t *portarray = toml_array_in(server, "port");
toml_datum_t host = toml_string_in(server, "host"); if (!portarray) {
if (!host.ok) { fatal("cannot read server.port", "");
fatal("cannot read server.host", ""); }
}
toml_array_t* portarray = toml_array_in(server, "port"); printf("host: %s\n", host.u.s);
if (!portarray) { printf("port: ");
fatal("cannot read server.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");
printf("host: %s\n", host.u.s); // 4. Free memory
printf("port: "); free(host.u.s);
for (int i = 0; ; i++) { toml_free(conf);
toml_datum_t port = toml_int_at(portarray, i); return 0;
if (!port.ok) break;
printf("%d ", (int)port.u.i);
}
printf("\n");
// 4. Free memory
free(host.u.s);
toml_free(conf);
return 0;
} }