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

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
#define TOML_H
#include <stdio.h>
#include <stdint.h>
#include <stdio.h>
#ifdef __cplusplus
#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.
* 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);
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
@ -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);
/* 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.
@ -78,7 +72,6 @@ struct toml_timestamp_t {
char *z;
};
/*-----------------------------------------------------------------
* 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 */
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_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_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);
@ -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),
void (*xxfree)(void *));
/*--------------------------------------------------------------
* 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_rtots(toml_raw_t s, toml_timestamp_t *ret);
#endif /* TOML_H */

View file

@ -27,15 +27,15 @@
#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 {
@ -47,14 +47,12 @@ 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)
{
static void print_string(const char *s) {
int ok = 1;
for (const char *p = s; *p && ok; p++) {
int ch = *p;
@ -77,24 +75,38 @@ static void print_string(const char* s)
}
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;
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_timestamp(toml_datum_t d)
{
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" : "");
@ -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;
int i;
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_array_t *arr;
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];
toml_table_t *tab = toml_parse_file(fp, errbuf, sizeof(errbuf));
@ -298,9 +301,7 @@ static void cat(FILE* fp)
toml_free(tab);
}
int main(int argc, const char* argv[])
{
int main(int argc, const char *argv[]) {
int i;
if (argc == 1) {
cat(stdin);
@ -309,8 +310,8 @@ int main(int argc, const char* argv[])
FILE *fp = fopen(argv[i], "r");
if (!fp) {
fprintf(stderr, "ERROR: cannot open %s: %s\n",
argv[i], strerror(errno));
fprintf(stderr, "ERROR: cannot open %s: %s\n", argv[i],
strerror(errno));
exit(1);
}
cat(fp);

View file

@ -26,33 +26,46 @@
#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)
{
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;
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)
{
static void print_raw(const char *s) {
char *sval;
int64_t ival;
int bval;
@ -78,16 +91,16 @@ static void print_raw(const char* s)
else
millisec[0] = 0;
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,
millisec,
(ts.z ? ts.z : ""));
millisec, (ts.z ? ts.z : ""));
} else if (ts.year) {
printf("{\"type\":\"date\",\"value\":\"%04d-%02d-%02d\"}",
*ts.year, *ts.month, *ts.day);
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);
printf("{\"type\":\"time\",\"value\":\"%02d:%02d:%02d%s\"}", *ts.hour,
*ts.minute, *ts.second, millisec);
}
} else {
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_table(toml_table_t* curtab)
{
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++) {
@ -126,8 +136,7 @@ static void print_table(toml_table_t* curtab)
printf("}");
}
static void print_table_array(toml_array_t* curarr)
{
static void print_table_array(toml_array_t *curarr) {
int i;
toml_table_t *tab;
@ -139,8 +148,7 @@ static void print_table_array(toml_array_t* curarr)
printf("]");
}
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;
@ -173,10 +181,7 @@ static void print_array(toml_array_t* curarr)
printf("]}");
}
static void cat(FILE* fp)
{
static void cat(FILE *fp) {
char errbuf[200];
toml_table_t *tab = toml_parse_file(fp, errbuf, sizeof(errbuf));
@ -191,9 +196,7 @@ static void cat(FILE* fp)
toml_free(tab);
}
int main(int argc, const char* argv[])
{
int main(int argc, const char *argv[]) {
int i;
if (argc == 1) {
cat(stdin);
@ -202,8 +205,8 @@ int main(int argc, const char* argv[])
FILE *fp = fopen(argv[i], "r");
if (!fp) {
fprintf(stderr, "ERROR: cannot open %s: %s\n",
argv[i], strerror(errno));
fprintf(stderr, "ERROR: cannot open %s: %s\n", argv[i],
strerror(errno));
exit(1);
}
cat(fp);

View file

@ -1,18 +1,15 @@
#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)
{
static void fatal(const char *msg, const char *msg1) {
fprintf(stderr, "ERROR: %s%s\n", msg, msg1 ? msg1 : "");
exit(1);
}
int main()
{
int main() {
FILE *fp;
char errbuf[200];
@ -50,7 +47,8 @@ int main()
printf("port: ");
for (int i = 0;; 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("\n");