-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
executable file
·147 lines (126 loc) · 3.42 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* main.c for using JSONPARSER*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <locale.h>
#include "JSON_parser.h"
static int print(void* ctx, int type, const JSON_value* value);
int main(int argc, char* argv[]) {
int count = 0, result = 0;
FILE* input;
JSON_config config;
struct JSON_parser_struct* jc = NULL;
init_JSON_config(&config);
config.depth = 19;
config.callback = &print;
config.allow_comments = 1;
config.handle_floats_manually = 0;
/* Setting locale before parser is created.*/
if (argc >= 2) {
if (!setlocale(LC_ALL, argv[1])) {
fprintf(stderr, "Failed to set locale to '%s'\n", argv[1]);
}
} else {
fprintf(stderr, "No locale provided, the default (C) locale is being used\n");
}
jc = new_JSON_parser(&config);
input = stdin;
for (; input ; ++count) {
int next_char = fgetc(input);
if (next_char <= 0) {
break;
}
if (!JSON_parser_char(jc, next_char)) {
fprintf(stderr, "JSON_parser_char: syntax error, byte %d\n", count);
result = 1;
goto done;
}
}
if (!JSON_parser_done(jc)) {
fprintf(stderr, "JSON_parser_end: syntax error\n");
result = 1;
goto done;
}
done:
delete_JSON_parser(jc);
return result;
}
static size_t s_Level = 0;
static const char* s_pIndention = " ";
static int s_IsKey = 0;
static void print_indention()
{
size_t i;
for (i = 0; i < s_Level; ++i) {
printf("%s", s_pIndention);
}
}
static int print(void* ctx, int type, const JSON_value* value)
{
switch(type) {
case JSON_T_ARRAY_BEGIN:
if (!s_IsKey) print_indention();
s_IsKey = 0;
printf("[\n");
++s_Level;
break;
case JSON_T_ARRAY_END:
assert(!s_IsKey);
if (s_Level > 0) --s_Level;
print_indention();
printf("]\n");
break;
case JSON_T_OBJECT_BEGIN:
if (!s_IsKey) print_indention();
s_IsKey = 0;
printf("{\n");
++s_Level;
break;
case JSON_T_OBJECT_END:
assert(!s_IsKey);
if (s_Level > 0) --s_Level;
print_indention();
printf("}\n");
break;
case JSON_T_INTEGER:
if (!s_IsKey) print_indention();
s_IsKey = 0;
printf("integer: "JSON_PARSER_INTEGER_SPRINTF_TOKEN"\n", value->vu.integer_value);
break;
case JSON_T_FLOAT:
if (!s_IsKey) print_indention();
s_IsKey = 0;
printf("float: %f\n", value->vu.float_value);
break;
case JSON_T_NULL:
if (!s_IsKey) print_indention();
s_IsKey = 0;
printf("null\n");
break;
case JSON_T_TRUE:
if (!s_IsKey) print_indention();
s_IsKey = 0;
printf("true\n");
break;
case JSON_T_FALSE:
if (!s_IsKey) print_indention();
s_IsKey = 0;
printf("false\n");
break;
case JSON_T_KEY:
s_IsKey = 1;
print_indention();
printf("key = '%s', value = ", value->vu.str.value);
break;
case JSON_T_STRING:
if (!s_IsKey) print_indention();
s_IsKey = 0;
printf("string: '%s'\n", value->vu.str.value);
break;
default:
assert(0);
break;
}
return 1;
}