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

987
toml.c

File diff suppressed because it is too large Load diff

24
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,17 +42,14 @@ 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
@ -62,7 +57,6 @@ TOML_EXTERN toml_table_t* toml_parse(char* conf, /* NUL terminated, please. */
*/ */
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.
@ -78,7 +72,6 @@ struct toml_timestamp_t {
char *z; char *z;
}; };
/*----------------------------------------------------------------- /*-----------------------------------------------------------------
* Enhanced access methods * Enhanced access methods
*/ */
@ -112,11 +105,14 @@ 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,
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_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_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_double_in(const toml_table_t *arr,
TOML_EXTERN toml_datum_t toml_timestamp_in(const toml_table_t* arr, const char* key); 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);
@ -158,7 +154,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
*/ */
@ -173,5 +168,4 @@ 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,15 +27,15 @@
#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 {
@ -47,14 +47,12 @@ 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) {
static void print_string(const char* s)
{
int ok = 1; int ok = 1;
for (const char *p = s; *p && ok; p++) { for (const char *p = s; *p && ok; p++) {
int ch = *p; int ch = *p;
@ -77,24 +75,38 @@ static void print_string(const char* s)
} }
switch (ch) { switch (ch) {
case 0x8: printf("\\b"); continue; case 0x8:
case 0x9: printf("\\t"); continue; printf("\\b");
case 0xa: printf("\\n"); continue; continue;
case 0xc: printf("\\f"); continue; case 0x9:
case 0xd: printf("\\r"); continue; printf("\\t");
case '"': printf("\\\""); continue; continue;
case '\\': printf("\\\\"); continue; case 0xa:
default: printf("\\0x%02x", ch & 0xff); continue; 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("\""); 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) static void print_timestamp(toml_datum_t d) {
{
if (d.u.ts->year) { if (d.u.ts->year) {
printf("%04d-%02d-%02d%s", *d.u.ts->year, *d.u.ts->month, *d.u.ts->day, printf("%04d-%02d-%02d%s", *d.u.ts->year, *d.u.ts->month, *d.u.ts->day,
d.u.ts->hour ? "T" : ""); d.u.ts->hour ? "T" : "");
@ -110,10 +122,7 @@ static void print_timestamp(toml_datum_t d)
} }
} }
static void print_table(toml_table_t *curtab) {
static void print_table(toml_table_t* curtab)
{
toml_datum_t d; toml_datum_t d;
int i; int i;
const char *key; const char *key;
@ -195,9 +204,7 @@ static void print_table(toml_table_t* curtab)
} }
} }
static void print_array(toml_array_t *curarr) {
static void print_array(toml_array_t* curarr)
{
toml_datum_t d; toml_datum_t d;
toml_array_t *arr; toml_array_t *arr;
toml_table_t *tab; toml_table_t *tab;
@ -272,11 +279,7 @@ static void print_array(toml_array_t* curarr)
} }
} }
static void cat(FILE *fp) {
static void cat(FILE* fp)
{
char errbuf[200]; char errbuf[200];
toml_table_t *tab = toml_parse_file(fp, errbuf, sizeof(errbuf)); toml_table_t *tab = toml_parse_file(fp, errbuf, sizeof(errbuf));
@ -298,9 +301,7 @@ static void cat(FILE* fp)
toml_free(tab); toml_free(tab);
} }
int main(int argc, const char *argv[]) {
int main(int argc, const char* argv[])
{
int i; int i;
if (argc == 1) { if (argc == 1) {
cat(stdin); cat(stdin);
@ -309,8 +310,8 @@ int main(int argc, const char* argv[])
FILE *fp = fopen(argv[i], "r"); FILE *fp = fopen(argv[i], "r");
if (!fp) { if (!fp) {
fprintf(stderr, "ERROR: cannot open %s: %s\n", fprintf(stderr, "ERROR: cannot open %s: %s\n", argv[i],
argv[i], strerror(errno)); strerror(errno));
exit(1); exit(1);
} }
cat(fp); cat(fp);

View file

@ -26,33 +26,46 @@
#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++) { for (; *s; s++) {
int ch = *s; int ch = *s;
switch (ch) { switch (ch) {
case '\b': printf("\\b"); break; case '\b':
case '\t': printf("\\t"); break; printf("\\b");
case '\n': printf("\\n"); break; break;
case '\f': printf("\\f"); break; case '\t':
case '\r': printf("\\r"); break; printf("\\t");
case '"': printf("\\\""); break; break;
case '\\': printf("\\\\"); break; case '\n':
default: printf("%c", ch); break; 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) static void print_raw(const char *s) {
{
char *sval; char *sval;
int64_t ival; int64_t ival;
int bval; int bval;
@ -78,16 +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%s%s\"}", printf("{\"type\":\"datetime\",\"value\":\"%04d-%02d-%02dT%02d:%02d:%02d%"
"s%s\"}",
*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, millisec, (ts.z ? ts.z : ""));
(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");
@ -95,17 +108,14 @@ static void print_raw(const char* s)
} }
} }
static void print_array(toml_array_t *arr); static void print_array(toml_array_t *arr);
static void print_table(toml_table_t* curtab) static void print_table(toml_table_t *curtab) {
{
int i; int i;
const char *key; const char *key;
const char *raw; const char *raw;
toml_array_t *arr; toml_array_t *arr;
toml_table_t *tab; toml_table_t *tab;
printf("{"); printf("{");
for (i = 0; 0 != (key = toml_key_in(curtab, i)); i++) { for (i = 0; 0 != (key = toml_key_in(curtab, i)); i++) {
@ -126,8 +136,7 @@ static void print_table(toml_table_t* curtab)
printf("}"); 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;
@ -139,8 +148,7 @@ static void print_table_array(toml_array_t* curarr)
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;
@ -173,10 +181,7 @@ static void print_array(toml_array_t* curarr)
printf("]}"); printf("]}");
} }
static void cat(FILE *fp) {
static void cat(FILE* fp)
{
char errbuf[200]; char errbuf[200];
toml_table_t *tab = toml_parse_file(fp, errbuf, sizeof(errbuf)); toml_table_t *tab = toml_parse_file(fp, errbuf, sizeof(errbuf));
@ -191,9 +196,7 @@ static void cat(FILE* fp)
toml_free(tab); toml_free(tab);
} }
int main(int argc, const char *argv[]) {
int main(int argc, const char* argv[])
{
int i; int i;
if (argc == 1) { if (argc == 1) {
cat(stdin); cat(stdin);
@ -202,8 +205,8 @@ int main(int argc, const char* argv[])
FILE *fp = fopen(argv[i], "r"); FILE *fp = fopen(argv[i], "r");
if (!fp) { if (!fp) {
fprintf(stderr, "ERROR: cannot open %s: %s\n", fprintf(stderr, "ERROR: cannot open %s: %s\n", argv[i],
argv[i], strerror(errno)); strerror(errno));
exit(1); exit(1);
} }
cat(fp); cat(fp);

View file

@ -1,18 +1,15 @@
#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() {
int main()
{
FILE *fp; FILE *fp;
char errbuf[200]; char errbuf[200];
@ -50,7 +47,8 @@ int main()
printf("port: "); printf("port: ");
for (int i = 0;; i++) { for (int i = 0;; i++) {
toml_datum_t port = toml_int_at(portarray, i); toml_datum_t port = toml_int_at(portarray, i);
if (!port.ok) break; if (!port.ok)
break;
printf("%d ", (int)port.u.i); printf("%d ", (int)port.u.i);
} }
printf("\n"); printf("\n");