-
Notifications
You must be signed in to change notification settings - Fork 30
/
jWrite.c
executable file
·561 lines (501 loc) · 14.3 KB
/
jWrite.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//
// jWrite.c version 1v2
//
// A *really* simple JSON writer in C
//
// see: jWrite.h for info
//
// TonyWilk, Mar 2015
//
#define _CRT_SECURE_NO_WARNINGS // stop complaining about deprecated functions
#include <stddef.h>
#include <stdio.h>
#include <string.h> // memset()
#include "jWrite.h"
//#include <stdint.h> // definintion of uint32_t, int32_t
typedef unsigned int uint32_t;
typedef int int32_t;
// the jWrite functions take the above jWriteControl structure pointer
// to maintain state while writing a JSON string.
//
// You can opt to use a single global instance of a jWriteControl structure
// which simplifies the function parameters or to supply your own structure
//
#ifdef JW_GLOBAL_CONTROL_STRUCT
struct jWriteControl g_jWriteControl; // global control struct
#define JWC_DECL // function parameter decl is empty
#define JWC_DECL0
#define JWC(x) g_jWriteControl.x // functions access global
#define JWC_PARAM // pointer to struct is empty
#define JWC_PARAM0
#else
#define JWC_DECL struct jWriteControl *jwc, // function parameter is ptr to control struct
#define JWC_DECL0 struct jWriteControl *jwc // function parameter, no params
#define JWC(x) jwc->x // functions use pointer
#define JWC_PARAM jwc, // pointer to stuct
#define JWC_PARAM0 jwc // pointer to stuct, no params
#endif
//------------------------------------------
// Internal functions
//
void jwPutch( JWC_DECL char c );
void jwPutstr( JWC_DECL char *str );
void jwPutraw( JWC_DECL char *str );
void modp_itoa10(int32_t value, char* str);
void modp_dtoa2(double value, char* str, int prec);
void jwPretty( JWC_DECL0 );
enum jwNodeType jwPop( JWC_DECL0 );
void jwPush( JWC_DECL enum jwNodeType nodeType );
//------------------------------------------
// jwOpen
// - open writing of JSON starting with rootType = JW_OBJECT or JW_ARRAY
// - initialise with user string buffer of length buflen
// - isPretty=JW_PRETTY adds \n and spaces to prettify output (else JW_COMPACT)
//
void jwOpen( JWC_DECL char *buffer, unsigned int buflen,
enum jwNodeType rootType, int isPretty )
{
memset( buffer, 0, buflen ); // zap the whole destination buffer
JWC(buffer)= buffer;
JWC(buflen)= buflen;
JWC(bufp)= buffer;
JWC(nodeStack)[0].nodeType= rootType;
JWC(nodeStack)[0].elementNo= 0;
JWC(stackpos)=0;
JWC(error)= JWRITE_OK;
JWC(callNo)= 1;
JWC(isPretty)= isPretty;
jwPutch( JWC_PARAM (rootType==JW_OBJECT) ? '{' : '[' );
}
//------------------------------------------
// jwClose
// - closes the root JSON object started by jwOpen()
// - returns error code
//
int jwClose( JWC_DECL0 )
{
if( JWC(error) == JWRITE_OK )
{
if( JWC(stackpos) == 0 )
{
enum jwNodeType node= JWC(nodeStack)[0].nodeType;
if( JWC(isPretty) )
jwPutch( JWC_PARAM '\n' );
jwPutch( JWC_PARAM (node == JW_OBJECT) ? '}' : ']');
}else{
JWC(error)= JWRITE_NEST_ERROR; // nesting error, not all objects closed when jwClose() called
}
}
return JWC(error);
}
//------------------------------------------
// End the current array/object
//
int jwEnd( JWC_DECL0 )
{
if( JWC(error) == JWRITE_OK )
{
enum jwNodeType node;
int lastElemNo= JWC(nodeStack)[JWC(stackpos)].elementNo;
node= jwPop( JWC_PARAM0 );
if( lastElemNo > 0 )
jwPretty( JWC_PARAM0 );
jwPutch( JWC_PARAM (node == JW_OBJECT) ? '}' : ']');
}
return JWC(error);
}
//------------------------------------------
// jwErrorPos
// - Returns position of error: the nth call to a jWrite function
//
int jwErrorPos( JWC_DECL0 )
{
return JWC(callNo);
}
//------------------------------------------
// Object insert functions
//
int _jwObj( JWC_DECL char *key );
// put raw string to object (i.e. contents of rawtext without quotes)
//
void jwObj_raw( JWC_DECL char *key, char *rawtext )
{
if(_jwObj( JWC_PARAM key ) == JWRITE_OK)
jwPutraw( JWC_PARAM rawtext);
}
// put "quoted" string to object
//
void jwObj_string( JWC_DECL char *key, char *value )
{
if(_jwObj( JWC_PARAM key ) == JWRITE_OK)
jwPutstr( JWC_PARAM value );
}
void jwObj_int( JWC_DECL char *key, int value )
{
modp_itoa10( value, JWC(tmpbuf) );
jwObj_raw( JWC_PARAM key, JWC(tmpbuf) );
}
void jwObj_double( JWC_DECL char *key, double value )
{
modp_dtoa2( value, JWC(tmpbuf), 6 );
jwObj_raw( JWC_PARAM key, JWC(tmpbuf) );
}
void jwObj_bool( JWC_DECL char *key, int oneOrZero )
{
jwObj_raw( JWC_PARAM key, (oneOrZero) ? "true" : "false" );
}
void jwObj_null( JWC_DECL char *key )
{
jwObj_raw( JWC_PARAM key, "null" );
}
// put Object in Object
//
void jwObj_object( JWC_DECL char *key )
{
if(_jwObj( JWC_PARAM key ) == JWRITE_OK)
{
jwPutch( JWC_PARAM '{' );
jwPush( JWC_PARAM JW_OBJECT );
}
}
// put Array in Object
//
void jwObj_array( JWC_DECL char *key )
{
if(_jwObj( JWC_PARAM key ) == JWRITE_OK)
{
jwPutch( JWC_PARAM '[' );
jwPush( JWC_PARAM JW_ARRAY );
}
}
//------------------------------------------
// Array insert functions
//
int _jwArr( JWC_DECL0 );
// put raw string to array (i.e. contents of rawtext without quotes)
//
void jwArr_raw( JWC_DECL char *rawtext )
{
if(_jwArr( JWC_PARAM0 ) == JWRITE_OK)
jwPutraw( JWC_PARAM rawtext);
}
// put "quoted" string to array
//
void jwArr_string( JWC_DECL char *value )
{
if(_jwArr( JWC_PARAM0 ) == JWRITE_OK)
jwPutstr( JWC_PARAM value );
}
void jwArr_int( JWC_DECL int value )
{
modp_itoa10( value, JWC(tmpbuf) );
jwArr_raw( JWC_PARAM JWC(tmpbuf) );
}
void jwArr_double( JWC_DECL double value )
{
modp_dtoa2( value, JWC(tmpbuf), 6 );
jwArr_raw( JWC_PARAM JWC(tmpbuf) );
}
void jwArr_bool( JWC_DECL int oneOrZero )
{
jwArr_raw( JWC_PARAM (oneOrZero) ? "true" : "false" );
}
void jwArr_null( JWC_DECL0 )
{
jwArr_raw( JWC_PARAM "null" );
}
void jwArr_object( JWC_DECL0 )
{
if(_jwArr( JWC_PARAM0 ) == JWRITE_OK)
{
jwPutch( JWC_PARAM '{' );
jwPush( JWC_PARAM JW_OBJECT );
}
}
void jwArr_array( JWC_DECL0 )
{
if(_jwArr( JWC_PARAM0 ) == JWRITE_OK)
{
jwPutch( JWC_PARAM '[' );
jwPush( JWC_PARAM JW_ARRAY );
}
}
//------------------------------------------
// jwErrorToString
// - returns string describing error code
//
char *jwErrorToString( int err )
{
switch( err )
{
case JWRITE_OK: return "OK";
case JWRITE_BUF_FULL: return "output buffer full";
case JWRITE_NOT_ARRAY: return "tried to write Array value into Object";
case JWRITE_NOT_OBJECT: return "tried to write Object key/value into Array";
case JWRITE_STACK_FULL: return "array/object nesting > JWRITE_STACK_DEPTH";
case JWRITE_STACK_EMPTY:return "stack underflow error (too many 'end's)";
case JWRITE_NEST_ERROR: return "nesting error, not all objects closed when jwClose() called";
}
return "Unknown error";
}
//============================================================================
// Internal functions
//
void jwPretty( JWC_DECL0 )
{
int i;
if( JWC(isPretty) )
{
jwPutch( JWC_PARAM '\n' );
for( i=0; i<JWC(stackpos)+1; i++ )
jwPutraw( JWC_PARAM " " );
}
}
// Push / Pop node stack
//
void jwPush( JWC_DECL enum jwNodeType nodeType )
{
if( (JWC(stackpos)+1) >= JWRITE_STACK_DEPTH )
JWC(error)= JWRITE_STACK_FULL; // array/object nesting > JWRITE_STACK_DEPTH
else
{
JWC(nodeStack[++JWC(stackpos)]).nodeType= nodeType;
JWC(nodeStack[JWC(stackpos)]).elementNo= 0;
}
}
enum jwNodeType jwPop( JWC_DECL0 )
{
enum jwNodeType retval= JWC(nodeStack[JWC(stackpos)]).nodeType;
if( JWC(stackpos) == 0 )
JWC(error)= JWRITE_STACK_EMPTY; // stack underflow error (too many 'end's)
else
JWC(stackpos)--;
return retval;
}
void jwPutch( JWC_DECL char c )
{
if( (unsigned int)(JWC(bufp) - JWC(buffer)) >= JWC(buflen) )
{
JWC(error)= JWRITE_BUF_FULL;
}else{
*JWC(bufp)++ = c;
}
}
// put string enclosed in quotes
//
void jwPutstr( JWC_DECL char *str )
{
jwPutch( JWC_PARAM '\"' );
while( *str != '\0' )
jwPutch( JWC_PARAM *str++ );
jwPutch( JWC_PARAM '\"' );
}
// put raw string
//
void jwPutraw( JWC_DECL char *str )
{
while( *str != '\0' )
jwPutch( JWC_PARAM *str++ );
}
// *common Object function*
// - checks error
// - checks current node is OBJECT
// - adds comma if reqd
// - adds "key" :
//
int _jwObj( JWC_DECL char *key )
{
if(JWC(error) == JWRITE_OK)
{
JWC(callNo)++;
if( JWC(nodeStack)[JWC(stackpos)].nodeType != JW_OBJECT )
JWC(error)= JWRITE_NOT_OBJECT; // tried to write Object key/value into Array
else if( JWC(nodeStack)[JWC(stackpos)].elementNo++ > 0 )
jwPutch( JWC_PARAM ',' );
jwPretty( JWC_PARAM0 );
jwPutstr( JWC_PARAM key );
jwPutch( JWC_PARAM ':' );
if( JWC(isPretty) )
jwPutch( JWC_PARAM ' ' );
}
return JWC(error);
}
// *common Array function*
// - checks error
// - checks current node is ARRAY
// - adds comma if reqd
//
int _jwArr( JWC_DECL0 )
{
if(JWC(error) == JWRITE_OK)
{
JWC(callNo)++;
if( JWC(nodeStack)[JWC(stackpos)].nodeType != JW_ARRAY )
JWC(error)= JWRITE_NOT_ARRAY; // tried to write array value into Object
else if( JWC(nodeStack)[JWC(stackpos)].elementNo++ > 0 )
jwPutch( JWC_PARAM ',' );
jwPretty( JWC_PARAM0 );
}
return JWC(error);
}
//=================================================================
//
// modp value-to-string functions
// - modified for C89
//
// We use these functions as they are a lot faster than sprintf()
//
// Origin of these routines:
/*
* <pre>
* Copyright © 2007, Nick Galbreath -- nickg [at] modp [dot] com
* All rights reserved.
* http://code.google.com/p/stringencoders/
* Released under the bsd license.
* </pre>
*/
static void strreverse(char* begin, char* end)
{
char aux;
while (end > begin)
aux = *end, *end-- = *begin, *begin++ = aux;
}
/** \brief convert an signed integer to char buffer
*
* \param[in] value
* \param[out] buf the output buffer. Should be 16 chars or more.
*/
void modp_itoa10(int32_t value, char* str)
{
char* wstr=str;
// Take care of sign
unsigned int uvalue = (value < 0) ? -value : value;
// Conversion. Number is reversed.
do *wstr++ = (char)(48 + (uvalue % 10)); while(uvalue /= 10);
if (value < 0) *wstr++ = '-';
*wstr='\0';
// Reverse string
strreverse(str,wstr-1);
}
/**
* Powers of 10
* 10^0 to 10^9
*/
static const double pow10[] = {1, 10, 100, 1000, 10000, 100000, 1000000,
10000000, 100000000, 1000000000};
/** \brief convert a floating point number to char buffer with a
* variable-precision format, and no trailing zeros
*
* This is similar to "%.[0-9]f" in the printf style, except it will
* NOT include trailing zeros after the decimal point. This type
* of format oddly does not exists with printf.
*
* If the input value is greater than 1<<31, then the output format
* will be switched exponential format.
*
* \param[in] value
* \param[out] buf The allocated output buffer. Should be 32 chars or more.
* \param[in] precision Number of digits to the right of the decimal point.
* Can only be 0-9.
*/
void modp_dtoa2(double value, char* str, int prec)
{
/* if input is larger than thres_max, revert to exponential */
const double thres_max = (double)(0x7FFFFFFF);
int count;
double diff = 0.0;
char* wstr = str;
int neg= 0;
int whole;
double tmp;
uint32_t frac;
/* Hacky test for NaN
* under -fast-math this won't work, but then you also won't
* have correct nan values anyways. The alternative is
* to link with libmath (bad) or hack IEEE double bits (bad)
*/
if (! (value == value)) {
str[0] = 'n'; str[1] = 'a'; str[2] = 'n'; str[3] = '\0';
return;
}
if (prec < 0) {
prec = 0;
} else if (prec > 9) {
/* precision of >= 10 can lead to overflow errors */
prec = 9;
}
/* we'll work in positive values and deal with the
negative sign issue later */
if (value < 0) {
neg = 1;
value = -value;
}
whole = (int) value;
tmp = (value - whole) * pow10[prec];
frac = (uint32_t)(tmp);
diff = tmp - frac;
if (diff > 0.5) {
++frac;
/* handle rollover, e.g. case 0.99 with prec 1 is 1.0 */
if (frac >= pow10[prec]) {
frac = 0;
++whole;
}
} else if (diff == 0.5 && ((frac == 0) || (frac & 1))) {
/* if halfway, round up if odd, OR
if last digit is 0. That last part is strange */
++frac;
}
/* for very large numbers switch back to native sprintf for exponentials.
anyone want to write code to replace this? */
/*
normal printf behavior is to print EVERY whole number digit
which can be 100s of characters overflowing your buffers == bad
*/
if (value > thres_max) {
sprintf(str, "%e", neg ? -value : value);
return;
}
if (prec == 0) {
diff = value - whole;
if (diff > 0.5) {
/* greater than 0.5, round up, e.g. 1.6 -> 2 */
++whole;
} else if (diff == 0.5 && (whole & 1)) {
/* exactly 0.5 and ODD, then round up */
/* 1.5 -> 2, but 2.5 -> 2 */
++whole;
}
//vvvvvvvvvvvvvvvvvvv Diff from modp_dto2
} else if (frac) {
count = prec;
// now do fractional part, as an unsigned number
// we know it is not 0 but we can have leading zeros, these
// should be removed
while (!(frac % 10)) {
--count;
frac /= 10;
}
//^^^^^^^^^^^^^^^^^^^ Diff from modp_dto2
// now do fractional part, as an unsigned number
do {
--count;
*wstr++ = (char)(48 + (frac % 10));
} while (frac /= 10);
// add extra 0s
while (count-- > 0) *wstr++ = '0';
// add decimal
*wstr++ = '.';
}
// do whole part
// Take care of sign
// Conversion. Number is reversed.
do *wstr++ = (char)(48 + (whole % 10)); while (whole /= 10);
if (neg) {
*wstr++ = '-';
}
*wstr='\0';
strreverse(str, wstr-1);
}
//=================================================================
/* end of jWrite.c */