This commit is contained in:
CK Tan 2021-07-04 19:10:32 -07:00
parent 789930f7c9
commit 24e4472b44
1 changed files with 25 additions and 25 deletions

View File

@ -39,8 +39,8 @@
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];
@ -97,12 +97,12 @@ static void print_array(toml_array_t* arr);
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;
toml_array_t* arr; toml_array_t* arr;
toml_table_t* tab; toml_table_t* tab;
for (i = 0; 0 != (key = toml_key_in(curtab, i)); i++) { for (i = 0; 0 != (key = toml_key_in(curtab, i)); i++) {
if (0 != (arr = toml_array_in(curtab, key))) { if (0 != (arr = toml_array_in(curtab, key))) {
prindent(); prindent();
@ -186,15 +186,15 @@ static void print_table(toml_table_t* curtab)
fflush(stdout); fflush(stdout);
fprintf(stderr, "ERROR: unable to decode value in table\n"); fprintf(stderr, "ERROR: unable to decode value in table\n");
exit(1); exit(1);
} }
} }
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;
const int n = toml_array_nelem(curarr); const int n = toml_array_nelem(curarr);
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
@ -283,34 +283,34 @@ 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));
if (!tab) { if (!tab) {
fprintf(stderr, "ERROR: %s\n", errbuf); fprintf(stderr, "ERROR: %s\n", errbuf);
return; return;
} }
stack[stacktop].tab = tab; stack[stacktop].tab = tab;
stack[stacktop].key = ""; stack[stacktop].key = "";
stacktop++; stacktop++;
printf("{\n"); printf("{\n");
indent++; indent++;
print_table(tab); print_table(tab);
indent--; indent--;
printf("}\n"); printf("}\n");
stacktop--; stacktop--;
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);
} else { } else {
for (i = 1; i < argc; i++) { for (i = 1; i < argc; i++) {
FILE* fp = fopen(argv[i], "r"); FILE* fp = fopen(argv[i], "r");
@ -322,6 +322,6 @@ int main(int argc, const char* argv[])
cat(fp); cat(fp);
fclose(fp); fclose(fp);
} }
} }
return 0; return 0;
} }