-
Notifications
You must be signed in to change notification settings - Fork 2
/
jemi.c
398 lines (345 loc) · 10.5 KB
/
jemi.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/**
* @file jemi.c
*
* MIT License
*
* Copyright (c) 2022 R. Dunbar Poor
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
// *****************************************************************************
// Includes
#include "jemi.h"
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
// *****************************************************************************
// Private types and definitions
// *****************************************************************************
// Private (static) storage
jemi_node_t *s_jemi_pool; // user supplied block of nodes
size_t s_jemi_pool_size; // number of user-supplied nodes
jemi_node_t *s_jemi_freelist; // next available node (or null if empty)
// *****************************************************************************
// Private (static, forward) declarations
/**
* @brief Pop one element from the freelist. If available, set the type and
* return it, else return NULL.
*/
static jemi_node_t *jemi_alloc(jemi_type_t type);
/**
* @brief Print a node or a list of nodes.
*/
static void emit_aux(jemi_node_t *root, jemi_writer_t writer_fn, void *arg,
bool is_obj);
/**
* @brief Make a copy of a node and its contents, including children nodes,
* but not siblings.
*/
static jemi_node_t *copy_node(jemi_node_t *node);
/**
* @brief Write a string to the writer_fn, a byte at a time.
*/
static void emit_string(jemi_writer_t writer_fn, void *arg, const char *buf);
// *****************************************************************************
// Public code
void jemi_init(jemi_node_t *pool, size_t pool_size) {
s_jemi_pool = pool;
s_jemi_pool_size = pool_size;
jemi_reset();
}
void jemi_reset(void) {
memset(s_jemi_pool, 0, s_jemi_pool_size * sizeof(jemi_node_t));
// rebuild the freelist, using node->sibling as the link field
jemi_node_t *next = NULL; // end of the linked list
for (int i = 0; i < s_jemi_pool_size; i++) {
jemi_node_t *node = &s_jemi_pool[i];
node->sibling = next;
next = node;
}
s_jemi_freelist = next; // reset head of the freelist
}
jemi_node_t *jemi_array(jemi_node_t *element, ...) {
va_list ap;
jemi_node_t *root = jemi_alloc(JEMI_ARRAY);
va_start(ap, element);
root->children = element;
while (element != NULL) {
element->sibling = va_arg(ap, jemi_node_t *);
element = element->sibling;
}
return root;
}
jemi_node_t *jemi_object(jemi_node_t *element, ...) {
va_list ap;
jemi_node_t *root = jemi_alloc(JEMI_OBJECT);
va_start(ap, element);
root->children = element;
while (element != NULL) {
element->sibling = va_arg(ap, jemi_node_t *);
element = element->sibling;
}
return root;
}
jemi_node_t *jemi_list(jemi_node_t *element, ...) {
va_list ap;
jemi_node_t *first = element;
va_start(ap, element);
while (element != NULL) {
element->sibling = va_arg(ap, jemi_node_t *);
element = element->sibling;
}
return first;
}
jemi_node_t *jemi_float(double value) {
jemi_node_t *node = jemi_alloc(JEMI_FLOAT);
if (node) {
node->number = value;
}
return node;
}
jemi_node_t *jemi_integer(int64_t value) {
jemi_node_t *node = jemi_alloc(JEMI_INTEGER);
if (node) {
node->integer = value;
}
return node;
}
jemi_node_t *jemi_string(const char *string) {
jemi_node_t *node = jemi_alloc(JEMI_STRING);
if (node) {
node->string = string;
}
return node;
}
jemi_node_t *jemi_bool(bool boolean) {
if (boolean) {
return jemi_true();
} else {
return jemi_false();
}
}
jemi_node_t *jemi_true(void) { return jemi_alloc(JEMI_TRUE); }
jemi_node_t *jemi_false(void) { return jemi_alloc(JEMI_FALSE); }
jemi_node_t *jemi_null(void) { return jemi_alloc(JEMI_NULL); }
jemi_node_t *jemi_copy(jemi_node_t *root) {
jemi_node_t *r2 = NULL;
jemi_node_t *prev = NULL;
jemi_node_t *node;
while ((node = copy_node(root)) != NULL) {
if (r2 == NULL) {
// first time through the loop: save pointer to first element
r2 = node;
}
if (prev != NULL) {
prev->sibling = node;
}
prev = node;
root = root->sibling;
}
return r2;
}
jemi_node_t *jemi_array_append(jemi_node_t *array, jemi_node_t *items) {
if (array) {
array->children = jemi_list_append(array->children, items);
}
return array;
}
jemi_node_t *jemi_object_append(jemi_node_t *object, jemi_node_t *items) {
if (object) {
object->children = jemi_list_append(object->children, items);
}
return object;
}
jemi_node_t *jemi_object_add_keyval(jemi_node_t *object, const char *key,
jemi_node_t *value) {
if (object) {
object->children = jemi_list_append(
object->children, jemi_list(jemi_string(key), value, NULL));
}
return object;
}
jemi_node_t *jemi_list_append(jemi_node_t *list, jemi_node_t *items) {
if (list == NULL) {
return items;
} else {
jemi_node_t *prev = NULL;
jemi_node_t *node = list;
// walk list to find last element
while (node) {
prev = node;
node = node->sibling;
}
// prev is now null or points to last sibling in list.
if (prev) {
prev->sibling = items;
}
return list;
}
}
jemi_node_t *jemi_float_set(jemi_node_t *node, double number) {
if (node) {
node->number = number;
}
return node;
}
jemi_node_t *jemi_integer_set(jemi_node_t *node, int64_t integer) {
if (node) {
node->integer = integer;
}
return node;
}
/**
* @brief Update contents of a JEMI_STRING node
*
* NOTE: string must be null-terminated.
*/
jemi_node_t *jemi_string_set(jemi_node_t *node, const char *string) {
if (node) {
node->string = string;
}
return node;
}
/**
* @brief Update contents of a JEMI_BOOL node
*/
jemi_node_t *jemi_bool_set(jemi_node_t *node, bool boolean) {
if (node) {
node->type = boolean ? JEMI_TRUE : JEMI_FALSE;
}
return node;
}
void jemi_emit(jemi_node_t *root, jemi_writer_t writer_fn, void *arg) {
emit_aux(root, writer_fn, arg, false);
writer_fn('\0', arg);
}
size_t jemi_available(void) {
size_t count = 0;
jemi_node_t *node = s_jemi_freelist;
while (node) {
count += 1;
node = node->sibling;
}
return count;
}
// *****************************************************************************
// Private (static) code
static jemi_node_t *jemi_alloc(jemi_type_t type) {
// pop one node from the freelist
jemi_node_t *node = s_jemi_freelist;
if (node) {
s_jemi_freelist = node->sibling;
node->sibling = NULL;
node->type = type;
}
return node;
}
static void emit_aux(jemi_node_t *root, jemi_writer_t writer_fn, void *arg,
bool is_obj) {
int count = 0;
jemi_node_t *node = root;
while (node) {
if (is_obj && (count & 1)) {
writer_fn(':', arg);
} else if (count > 0) {
writer_fn(',', arg);
}
switch (node->type) {
case JEMI_OBJECT: {
writer_fn('{', arg);
emit_aux(node->children, writer_fn, arg, true);
writer_fn('}', arg);
} break;
case JEMI_ARRAY: {
writer_fn('[', arg);
emit_aux(node->children, writer_fn, arg, false);
writer_fn(']', arg);
} break;
case JEMI_FLOAT: {
char buf[22];
int64_t i = node->number;
if ((double)i == node->number) {
// number can be represented as an int: suppress trailing zeros
snprintf(buf, sizeof(buf), "%lld", i);
} else {
snprintf(buf, sizeof(buf), "%lf", node->number);
}
emit_string(writer_fn, arg, buf);
} break;
case JEMI_INTEGER: {
char buf[22]; // 20 digits, 1 sign, 1 null
snprintf(buf, sizeof(buf), "%lld", node->integer);
emit_string(writer_fn, arg, buf);
} break;
case JEMI_STRING: {
writer_fn('"', arg);
emit_string(writer_fn, arg, node->string);
writer_fn('"', arg);
} break;
case JEMI_TRUE: {
emit_string(writer_fn, arg, "true");
} break;
case JEMI_FALSE: {
emit_string(writer_fn, arg, "false");
} break;
case JEMI_NULL: {
emit_string(writer_fn, arg, "null");
} break;
}
count += 1;
node = node->sibling;
}
}
static jemi_node_t *copy_node(jemi_node_t *node) {
jemi_node_t *copy;
if (node == NULL) {
copy = NULL;
} else {
copy = jemi_alloc(node->type);
switch (node->type) {
case JEMI_ARRAY:
case JEMI_OBJECT: {
copy->children = jemi_copy(node->children);
} break;
case JEMI_STRING: {
copy->string = node->string;
} break;
case JEMI_FLOAT: {
copy->number = node->number;
}
case JEMI_INTEGER: {
copy->integer = node->integer;
}
default: {
// no action needed
}
} // switch()
}
return copy;
}
static void emit_string(jemi_writer_t writer_fn, void *arg, const char *buf) {
while (*buf) {
writer_fn(*buf++, arg);
}
}
// *****************************************************************************
// End of file