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
[*.{c,h}]
indent_style = tab
indent_size = 4
indent_style = space
indent_size = 2
[Makefile]
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
#define TOML_H
#include <stdio.h>
#include <stdint.h>
#include <stdio.h>
#ifdef __cplusplus
#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.
* Caller must toml_free(the-return-value) after use.
*/
TOML_EXTERN toml_table_t* toml_parse_file(FILE* fp,
char* errbuf,
int errbufsz);
TOML_EXTERN toml_table_t *toml_parse_file(FILE *fp, char *errbuf, int errbufsz);
/* Parse a string containing the full config.
* Return a table on success, or 0 otherwise.
* Caller must toml_free(the-return-value) after use.
*/
TOML_EXTERN toml_table_t* toml_parse(char* conf, /* NUL terminated, please. */
char* errbuf,
int errbufsz);
TOML_EXTERN toml_table_t *toml_parse(char *conf, /* NUL terminated, please. */
char *errbuf, int errbufsz);
/* Free the table returned by toml_parse() or toml_parse_file(). Once
* this function is called, any handles accessed through this tab
* 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
* 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.
*/
struct toml_timestamp_t {
struct { /* internal. do not use. */
int year, month, day;
int hour, minute, second, millisec;
char z[10];
} __buffer;
int *year, *month, *day;
int *hour, *minute, *second, *millisec;
char* z;
struct { /* internal. do not use. */
int year, month, day;
int hour, minute, second, millisec;
char z[10];
} __buffer;
int *year, *month, *day;
int *hour, *minute, *second, *millisec;
char *z;
};
/*-----------------------------------------------------------------
* Enhanced access methods
*/
struct toml_datum_t {
int ok;
union {
toml_timestamp_t* ts; /* ts must be freed after use */
char* s; /* string value. s must be freed after use */
int b; /* bool value */
int64_t i; /* int value */
double d; /* double value */
} u;
int ok;
union {
toml_timestamp_t *ts; /* ts must be freed after use */
char *s; /* string value. s must be freed after use */
int b; /* bool value */
int64_t i; /* int value */
double d; /* double value */
} u;
};
/* on arrays: */
/* ... 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. */
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_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_timestamp_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_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_timestamp_at(const toml_array_t *arr, int idx);
/* ... retrieve array or table using index. */
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_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);
/* on tables: */
/* ... 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 */
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. */
TOML_EXTERN toml_datum_t toml_string_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_int_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);
TOML_EXTERN toml_datum_t toml_string_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_int_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. */
TOML_EXTERN toml_array_t* toml_array_in(const toml_table_t* tab,
const char* key);
TOML_EXTERN toml_table_t* toml_table_in(const toml_table_t* tab,
const char* key);
TOML_EXTERN toml_array_t *toml_array_in(const toml_table_t *tab,
const char *key);
TOML_EXTERN toml_table_t *toml_table_in(const toml_table_t *tab,
const char *key);
/*-----------------------------------------------------------------
* lesser used
*/
/* 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
i:int, d:double, b:bool, s:string, t:time, D:date, T:timestamp, 'm'ixed
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 */
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 */
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 */
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 */
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*/
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
*/
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 void toml_set_memutil(void* (*xxmalloc)(size_t),
void (*xxfree)(void*));
TOML_EXTERN void toml_set_memutil(void *(*xxmalloc)(size_t),
void (*xxfree)(void *));
/*--------------------------------------------------------------
* deprecated
*/
/* A raw value, must be processed by toml_rto* before using. */
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_at(const toml_array_t* arr, int idx);
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_rtoi(toml_raw_t s, int64_t* 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_rtots(toml_raw_t s, toml_timestamp_t* ret);
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_at(const toml_array_t *arr, int idx);
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_rtoi(toml_raw_t s, int64_t *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_rtots(toml_raw_t s, toml_timestamp_t *ret);
#endif /* TOML_H */

View file

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

View file

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