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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
#include "json.h"
#include <stdarg.h>
#include <stdio.h>
static char buf[1024];
__attribute__((format(printf, 3, 4)))
static bool printi(const stream_t *stream, int depth, const char *format, ...) {
for (int i = 0; i < depth; i++)
if (stream_write(stream, "\t", 1) == false)
return false;
va_list list;
va_start(list, format);
int len;
if ((len = vsnprintf(buf, 1024, format, list)) < 0)
return false;
if (stream_write(stream, buf, len) == false)
return false;
return true;
}
static bool json_print_impl(const tag_t *tag, const stream_t *stream, int depth);
static bool json_print_byte_array(const tagdata_t *data, const stream_t *stream) {
if (printi(stream, 0, "[") == false)
return false;
for (int32_t i = 0; i < data->b_arr.size; i++) {
if (i != 0)
if (printi(stream, 0, ",") == false)
return false;
if (printi(stream, 0, "%hhd", data->b_arr.data[i]) == false)
return false;
}
if (printi(stream, 0, "]") == false)
return false;
return true;
}
static bool json_print_int_array(const tagdata_t *data, const stream_t *stream) {
if (printi(stream, 0, "[") == false)
return false;
for (int32_t i = 0; i < data->i_arr.size; i++) {
if (i != 0)
if (printi(stream, 0, ",") == false)
return false;
if (printi(stream, 0, "%d", data->i_arr.data[i]) == false)
return false;
}
if (printi(stream, 0, "]") == false)
return false;
return true;
}
static bool json_print_long_array(const tagdata_t *data, const stream_t *stream) {
if (printi(stream, 0, "[") == false)
return false;
for (int32_t i = 0; i < data->l_arr.size; i++) {
if (i != 0)
if (printi(stream, 0, ",") == false)
return false;
if (printi(stream, 0, "%ld", data->l_arr.data[i]) == false)
return false;
}
if (printi(stream, 0, "]") == false)
return false;
return true;
}
static bool json_print_string(const tagdata_t *data, const stream_t *stream) {
if (data->string.size > 0) {
if (printi(stream, 0, "\"%.*s\"", data->string.size, data->string.data) == false)
return false;
} else {
if (printi(stream, 0, "\"\"") == false)
return false;
}
return true;
}
static bool json_print_compound(const tagdata_t *data, const stream_t *stream, int depth) {
if (printi(stream, 0, "{\n") == false)
return false;
bool first = true;
for (uint32_t i = 0; i < data->compound.capacity; i++) {
if (data->compound.entries[i].name == NULL)
continue;
if (!first && printi(stream, 0, ",\n") == false)
return false;
first = false;
if (json_print_impl(&data->compound.entries[i], stream, depth + 1) == false)
return false;
}
if (printi(stream, 0, "\n") == false || printi(stream, depth, "}") == false)
return false;
return true;
}
static bool json_print_list(const tagdata_t *data, const stream_t *stream, int depth) {
if (printi(stream, 0, "[\n") == false)
return false;
for (int32_t i = 0; i < data->list.size; i++) {
if (i != 0 && printi(stream, 0, ",\n") == false)
return false;
if (json_print_impl(&data->list.tags[i], stream, depth + 1) == false)
return false;
}
if (printi(stream, 0, "\n") == false || printi(stream, depth, "]") == false)
return false;
return true;
}
static bool json_print_data(const tag_t *tag, const stream_t *stream, int depth) {
bool ok = true;
switch (tag->type) {
case TAG_BYTE:
ok = printi(stream, 0, "%hhd", tag->data.b);
break;
case TAG_SHORT:
ok = printi(stream, 0, "%hd", tag->data.s);
break;
case TAG_INT:
ok = printi(stream, 0, "%d", tag->data.i);
break;
case TAG_LONG:
ok = printi(stream, 0, "%ld", tag->data.l);
break;
case TAG_FLOAT:
ok = printi(stream, 0, "%.9g", tag->data.f);
break;
case TAG_DOUBLE:
ok = printi(stream, 0, "%.17g", tag->data.d);
break;
case TAG_BYTE_ARRAY:
ok = json_print_byte_array(&tag->data, stream);
break;
case TAG_STRING:
ok = json_print_string(&tag->data, stream);
break;
case TAG_LIST:
ok = json_print_list(&tag->data, stream, depth);
break;
case TAG_COMPOUND:
ok = json_print_compound(&tag->data, stream, depth);
break;
case TAG_INT_ARRAY:
ok = json_print_int_array(&tag->data, stream);
break;
case TAG_LONG_ARRAY:
ok = json_print_long_array(&tag->data, stream);
break;
case TAG_END:
break;
}
return ok;
}
static bool json_print_impl(const tag_t *tag, const stream_t *stream, int depth) {
if (tag->name_len > 0) {
printi(stream, depth, "\"%.*s\":\t", tag->name_len, tag->name);
} else {
for (int i = 0; i < depth; i++)
printi(stream, 0, "\t");
}
return json_print_data(tag, stream, depth);
}
bool json_print(const tag_t *tag, const stream_t *stream) {
if (json_print_impl(tag, stream, 0) == false)
return false;
if (stream_write(stream, "\n", 1) == false)
return false;
return true;
}
|