-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonwrite.c
332 lines (291 loc) · 7.24 KB
/
jsonwrite.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#define _GNU_SOURCE
#include <assert.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "jsonwrite.h"
/* having character helped debugging */
#define JWR_ARR 'A'
#define JWR_OBJ 'O'
#define JWR_KEY 'K'
/* this flag turns the characters to lowercase */
#define JWR_NXT 0x20U
#define JWR_STK_MAX 32
/* PRETTY overrides COMPACT */
#define JWR_PRETTY (1 << 0)
#define JWR_COMPACT (1 << 1)
struct jwr {
char *buf;
size_t size;
size_t pos;
int depth;
unsigned flags;
char *tos;
char stk[JWR_STK_MAX];
};
#ifdef DEBUG
void jwr_dump_r(struct jwr *jwr)
{
fprintf(stderr, "-- DUMP--\n%s\n", jwr->buf);
fputc('[', stderr);
for (char *s = jwr->stk; s <= jwr->tos; s++) fputc(*s, stderr);
fputc(']', stderr);
fputc('\n', stderr);
}
#endif /* DEBUG */
static inline bool flag_is_set(struct jwr *jwr, unsigned flag)
{
return (jwr->flags & flag) != 0;
}
static void jwr_raw(struct jwr *jwr, const char *raw, size_t len)
{
assert(jwr->pos + len < jwr->size);
(void) memcpy(&jwr->buf[jwr->pos], raw, len);
jwr->pos += len;
}
static void jwr_char(struct jwr *jwr, char c)
{
assert(jwr->pos + 1 < jwr->size);
jwr->buf[jwr->pos++] = c;
}
static void jwr_indent(struct jwr *jwr)
{
jwr_char(jwr, '\n');
for (int i = 0; i < jwr->depth * 2; i++)
{
jwr_char(jwr, ' ');
}
}
static void jwr_push(struct jwr *jwr, char type)
{
assert(jwr->tos < (jwr->stk + JWR_STK_MAX));
*(++jwr->tos) = type;
}
static bool jwr_tos_is(struct jwr *jwr, char type)
{
return (*jwr->tos & ~JWR_NXT) == type;
}
/**
* If the tos is a key, pop it off.
* This function is called after every item, and ensures that
* a key has only one value.
*/
static void jwr_popkey(struct jwr *jwr)
{
if (jwr_tos_is(jwr, JWR_KEY))
{
assert ((*jwr->tos & JWR_NXT) == JWR_NXT);
*(jwr->tos--) = '\0';
}
}
/**
* If this is a follow-up item in a list, write a comma as sepataror.
* This is done via a NXT flag in the tos item, and if it is set
* after the first item, it's presence will cause a comma to be written.
* Multiple items on top-level are not allowed.
*/
static void jwr_sep(struct jwr *jwr)
{
if (jwr->tos > jwr->stk)
{
if ((*jwr->tos & JWR_NXT) == JWR_NXT)
{
jwr_char(jwr, ',');
if (flag_is_set (jwr, JWR_PRETTY))
{
jwr_indent(jwr);
}
else if (! flag_is_set(jwr, JWR_COMPACT))
{
jwr_char(jwr, ' ');
}
}
}
else
{
/* we are top level, one leaf was already written */
assert((*jwr->tos & JWR_NXT) != JWR_NXT);
}
/* set flag for the next time */
*jwr->tos |= JWR_NXT;
}
/**
* Write a quoted string
*/
static void jwr_qstr(struct jwr *jwr, const char *s)
{
jwr_char(jwr, '"');
jwr_raw(jwr, s, strnlen(s, jwr->size));
jwr_char(jwr, '"');
}
static void jwr_open_r(struct jwr *jwr, char type)
{
char c = (type == JWR_ARR) ? '[' :
(type == JWR_OBJ) ? '{' :
-1 ;
assert(c != -1);
jwr_sep(jwr);
jwr_push(jwr, type);
jwr_char(jwr, c);
jwr->depth++;
if (flag_is_set (jwr, JWR_PRETTY))
{
jwr_indent(jwr);
}
}
/*****************************************************************************
* Public functions
*****************************************************************************/
/**
* Initialize json writing: assign a buffer
*
* \param buf buffer which to write to
* \param size size of the buffer
* \param flags JWR_PRETTY, JWR_COMPACT
*/
void jwr_init_r(struct jwr *jwr, char *buf, size_t size, unsigned flags)
{
jwr->buf = buf;
jwr->size = size;
jwr->pos = 0;
jwr->depth = 0;
jwr->flags = flags;
jwr->tos = jwr->stk;
(void) memset(jwr->stk, 0, sizeof jwr->stk);
}
/**
* Finish the json writing to the buffer.
*
* \return the size of the written json string
*/
size_t jwr_finish_r(struct jwr *jwr)
{
assert(jwr->tos == jwr->stk && (*jwr->tos & ~JWR_NXT) == '\0');
assert(jwr->depth == 0);
jwr->buf[jwr->pos] = '\0';
return jwr->pos;
}
/**
* Write a string
*/
void jwr_str_r(struct jwr *jwr, const char *s)
{
assert(!jwr_tos_is(jwr, JWR_OBJ));
jwr_sep(jwr);
jwr_qstr(jwr, s);
jwr_popkey(jwr);
}
/**
* Write an integer value
*/
void jwr_int_r (struct jwr *jwr, int64_t val)
{
int cnt;
assert (!jwr_tos_is (jwr, JWR_OBJ));
jwr_sep (jwr);
/* writes at most (size - pos) bytes including '\0' terminator */
cnt = snprintf (&jwr->buf[jwr->pos], jwr->size - jwr->pos, "%"PRIi64, val);
/* return value is the number of bytes written (or would have been written)
* excluding the '\0' terminator. Hence, a value equal or greater than the
* limit indicates that it was truncated (did not fit)
*/
assert (cnt > 0 && cnt < (int)(jwr->size - jwr->pos));
jwr->pos += (size_t) cnt;
jwr_popkey (jwr);
}
/**
* Write the literal "null"
*/
void jwr_null_r(struct jwr *jwr)
{
assert(!jwr_tos_is(jwr, JWR_OBJ));
jwr_sep(jwr);
jwr_raw(jwr, "null", 4);
jwr_popkey(jwr);
}
/**
* Write a boolean literal
*/
void jwr_bool_r(struct jwr *jwr, bool val)
{
assert(!jwr_tos_is(jwr, JWR_OBJ));
jwr_sep(jwr);
if (val) {
jwr_raw(jwr, "true", 4);
} else {
jwr_raw(jwr, "false", 5);
}
jwr_popkey(jwr);
}
/**
* Start an array
*/
void jwr_arr_r(struct jwr *jwr)
{
jwr_open_r(jwr, JWR_ARR);
}
/**
* Start a json object
*/
void jwr_obj_r(struct jwr *jwr)
{
jwr_open_r(jwr, JWR_OBJ);
}
/**
* Write a key, only string allowed.
* Keys must have a json object as parent.
*/
void jwr_key_r(struct jwr *jwr, const char *key)
{
jwr_sep(jwr);
assert(jwr_tos_is(jwr, JWR_OBJ));
jwr_push(jwr, JWR_KEY);
jwr_qstr(jwr, key);
jwr_char(jwr, ':');
if (! flag_is_set(jwr, JWR_COMPACT))
{
jwr_char(jwr, ' ');
}
}
/**
* Close the array/object
*/
void jwr_close_r(struct jwr *jwr)
{
char type = *jwr->tos & ~JWR_NXT;
char c = (type == JWR_ARR) ? ']' :
(type == JWR_OBJ) ? '}' :
-1 ;
assert(c != -1);
*(jwr->tos--) = '\0';
jwr->depth--;
if (jwr->flags & JWR_PRETTY)
{
jwr_indent(jwr);
}
jwr_char(jwr, c);
jwr_popkey(jwr);
}
/*****************************************************************************
* Non-reentrant short forms
*****************************************************************************/
/* global struct - for non-reentrant, more compact use */
static struct jwr g_jwr;
void jwr_init(char *buf, size_t size, unsigned flags) {
jwr_init_r(&g_jwr, buf, size, flags);
}
size_t jwr_finish(void) { return jwr_finish_r(&g_jwr); }
void jwr_str(const char *s) { jwr_str_r(&g_jwr, s); }
void jwr_int(int64_t val) { jwr_int_r(&g_jwr, val); }
void jwr_null(void) { jwr_null_r(&g_jwr); }
void jwr_bool(bool val) { jwr_bool_r(&g_jwr, val); }
void jwr_arr(void) { jwr_arr_r(&g_jwr); }
void jwr_obj(void) { jwr_obj_r(&g_jwr); }
void jwr_key(const char *key) { jwr_key_r(&g_jwr, key); }
void jwr_close(void) { jwr_close_r(&g_jwr); }
#ifdef DEBUG
void jwr_dump(void) { jwr_dump_r(&g_jwr); }
#endif /* DEBUG */