Debian Codegraph @

CodeLite/cJSON.cpp

raw
1 //////////////////////////////////////////////////////////////////////////////
2 //////////////////////////////////////////////////////////////////////////////
3 //
4 // copyright : (C) 2014 Eran Ifrah
5 // file name : cJSON.cpp
6 //
7 // -------------------------------------------------------------------------
8 // A
9 // _____ _ _ _ _
10 // / __ \ | | | | (_) |
11 // | / \/ ___ __| | ___| | _| |_ ___
12 // | | / _ \ / _ |/ _ \ | | | __/ _ )
13 // | \__/\ (_) | (_| | __/ |___| | || __/
14 // \____/\___/ \__,_|\___\_____/_|\__\___|
15 //
16 // F i l e
17 //
18 // This program is free software; you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation; either version 2 of the License, or
21 // (at your option) any later version.
22 //
23 //////////////////////////////////////////////////////////////////////////////
24 //////////////////////////////////////////////////////////////////////////////
25
26 /*
27 Copyright (c) 2009 Dave Gamble
28
29 Permission is hereby granted, free of charge, to any person obtaining a copy
30 of this software and associated documentation files (the "Software"), to deal
31 in the Software without restriction, including without limitation the rights
32 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
33 copies of the Software, and to permit persons to whom the Software is
34 furnished to do so, subject to the following conditions:
35
36 The above copyright notice and this permission notice shall be included in
37 all copies or substantial portions of the Software.
38
39 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
40 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
41 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
42 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
43 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
44 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
45 THE SOFTWARE.
46 */
47
48 /* cJSON */
49 /* JSON parser in C. */
50
51 // clang-format off
52 #include <stdlib.h>
53 #include <string.h>
54 #include <stdio.h>
55 #include <math.h>
56 #include <float.h>
57 #include <limits.h>
58 #include <ctype.h>
59 #include "cJSON.h"
60 // clang-format on
61
62 static const char* ep;
63 #define FMT_WHITESPACE_CHAR ' '
64
65 const char* cJSON_GetErrorPtr() { return ep; }
66
67 static int cJSON_strcasecmp(const char* s1, const char* s2)
68 {
69 if(!s1) return (s1 == s2) ? 0 : 1;
70 if(!s2) return 1;
71 for(; tolower(*s1) == tolower(*s2); ++s1, ++s2)
72 if(*s1 == 0) return 0;
73 return tolower(*(const unsigned char*)s1) - tolower(*(const unsigned char*)s2);
74 }
75
76 static void* (*cJSON_malloc)(size_t sz) = malloc;
77 static void (*cJSON_free)(void* ptr) = free;
78
79 static char* cJSON_strdup(const char* str)
80 {
81 size_t len;
82 char* copy;
83
84 len = strlen(str) + 1;
85 if(!(copy = (char*)cJSON_malloc(len))) return 0;
86 memcpy(copy, str, len);
87 return copy;
88 }
89
90 void cJSON_InitHooks(cJSON_Hooks* hooks)
91 {
92 if(!hooks) { /* Reset hooks */
93 cJSON_malloc = malloc;
94 cJSON_free = free;
95 return;
96 }
97
98 cJSON_malloc = (hooks->malloc_fn) ? hooks->malloc_fn : malloc;
99 cJSON_free = (hooks->free_fn) ? hooks->free_fn : free;
100 }
101
102 /* Internal constructor. */
103 static cJSON* cJSON_New_Item()
104 {
105 cJSON* node = (cJSON*)cJSON_malloc(sizeof(cJSON));
106 if(node) memset(node, 0, sizeof(cJSON));
107 return node;
108 }
109
110 /* Delete a cJSON structure. */
111 void cJSON_Delete(cJSON* c)
112 {
113 cJSON* next;
114 while(c) {
115 next = c->next;
116 if(!(c->type & cJSON_IsReference) && c->child) cJSON_Delete(c->child);
117 if(!(c->type & cJSON_IsReference) && c->valuestring) cJSON_free(c->valuestring);
118 if(c->string) cJSON_free(c->string);
119 cJSON_free(c);
120 c = next;
121 }
122 }
123
124 /* Parse the input text to generate a number, and populate the result into item. */
125 static const char* parse_number(cJSON* item, const char* num)
126 {
127 double n = 0, sign = 1, scale = 0;
128 int subscale = 0, signsubscale = 1;
129
130 /* Could use sscanf for this? */
131 if(*num == '-') sign = -1, num++; /* Has sign? */
132 if(*num == '0') num++; /* is zero */
133 if(*num >= '1' && *num <= '9') do
134 n = (n * 10.0) + (*num++ - '0');
135 while(*num >= '0' && *num <= '9'); /* Number? */
136 if(*num == '.' && num[1] >= '0' && num[1] <= '9') {
137 num++;
138 do
139 n = (n * 10.0) + (*num++ - '0'), scale--;
140 while(*num >= '0' && *num <= '9');
141 } /* Fractional part? */
142 if(*num == 'e' || *num == 'E') /* Exponent? */
143 {
144 num++;
145 if(*num == '+')
146 num++;
147 else if(*num == '-')
148 signsubscale = -1, num++; /* With sign? */
149 while(*num >= '0' && *num <= '9')
150 subscale = (subscale * 10) + (*num++ - '0'); /* Number? */
151 }
152
153 n = sign * n * pow(10.0, (scale + subscale * signsubscale)); /* number = +/- number.fraction * 10^+/- exponent */
154
155 item->valuedouble = n;
156 item->valueint = (int)n;
157 item->type = cJSON_Number;
158 return num;
159 }
160
161 /* Render the number nicely from the given item into a string. */
162 static char* print_number(cJSON* item)
163 {
164 char* str;
165 double d = item->valuedouble;
166 if(fabs(((double)item->valueint) - d) <= DBL_EPSILON && d <= INT_MAX && d >= INT_MIN) {
167 str = (char*)cJSON_malloc(21); /* 2^64+1 can be represented in 21 chars. */
168 if(str) sprintf(str, "%d", item->valueint);
169 } else {
170 str = (char*)cJSON_malloc(64); /* This is a nice tradeoff. */
171 if(str) {
172 if(fabs(floor(d) - d) <= DBL_EPSILON)
173 sprintf(str, "%.0f", d);
174 else if(fabs(d) < 1.0e-6 || fabs(d) > 1.0e9)
175 sprintf(str, "%e", d);
176 else
177 sprintf(str, "%f", d);
178 }
179 }
180 return str;
181 }
182
183 /* Parse the input text into an unescaped cstring, and populate item. */
184 static const unsigned char firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
185 static const char* parse_string(cJSON* item, const char* str)
186 {
187 const char* ptr = str + 1;
188 char* ptr2;
189 char* out;
190 int len = 0;
191 unsigned uc, uc2;
192 if(*str != '\"') {
193 ep = str;
194 return 0;
195 } /* not a string! */
196
197 while(*ptr != '\"' && *ptr && ++len)
198 if(*ptr++ == '\\') ptr++; /* Skip escaped quotes. */
199
200 out = (char*)cJSON_malloc(len + 1); /* This is how long we need for the string, roughly. */
201 if(!out) return 0;
202
203 ptr = str + 1;
204 ptr2 = out;
205 while(*ptr != '\"' && *ptr) {
206 if(*ptr != '\\')
207 *ptr2++ = *ptr++;
208 else {
209 ptr++;
210 switch(*ptr) {
211 case 'b':
212 *ptr2++ = '\b';
213 break;
214 case 'f':
215 *ptr2++ = '\f';
216 break;
217 case 'n':
218 *ptr2++ = '\n';
219 break;
220 case 'r':
221 *ptr2++ = '\r';
222 break;
223 case 't':
224 *ptr2++ = '\t';
225 break;
226 case 'u': /* transcode utf16 to utf8. */
227 sscanf(ptr + 1, "%4x", &uc);
228 ptr += 4; /* get the unicode char. */
229
230 if((uc >= 0xDC00 && uc <= 0xDFFF) || uc == 0) break; // check for invalid.
231
232 if(uc >= 0xD800 && uc <= 0xDBFF) // UTF16 surrogate pairs.
233 {
234 if(ptr[1] != '\\' || ptr[2] != 'u') break; // missing second-half of surrogate.
235 sscanf(ptr + 3, "%4x", &uc2);
236 ptr += 6;
237 if(uc2 < 0xDC00 || uc2 > 0xDFFF) break; // invalid second-half of surrogate.
238 uc = 0x10000 | ((uc & 0x3FF) << 10) | (uc2 & 0x3FF);
239 }
240
241 len = 4;
242 if(uc < 0x80)
243 len = 1;
244 else if(uc < 0x800)
245 len = 2;
246 else if(uc < 0x10000)
247 len = 3;
248 ptr2 += len;
249
250 switch(len) {
251 case 4:
252 *--ptr2 = ((uc | 0x80) & 0xBF);
253 uc >>= 6;
254 case 3:
255 *--ptr2 = ((uc | 0x80) & 0xBF);
256 uc >>= 6;
257 case 2:
258 *--ptr2 = ((uc | 0x80) & 0xBF);
259 uc >>= 6;
260 case 1:
261 *--ptr2 = (uc | firstByteMark[len]);
262 }
263 ptr2 += len;
264 break;
265 default:
266 *ptr2++ = *ptr;
267 break;
268 }
269 ptr++;
270 }
271 }
272 *ptr2 = 0;
273 if(*ptr == '\"') ptr++;
274 item->valuestring = out;
275 item->type = cJSON_String;
276 return ptr;
277 }
278
279 /* Render the cstring provided to an escaped version that can be printed. */
280 static char* print_string_ptr(const char* str)
281 {
282 const char* ptr;
283 char *ptr2, *out;
284 int len = 0;
285 unsigned char token;
286
287 if(!str) return cJSON_strdup("");
288 ptr = str;
289 while((token = *ptr) && ++len) {
290 if(strchr("\"\\\b\f\n\r\t", token))
291 len++;
292 else if(token < 32)
293 len += 5;
294 ptr++;
295 }
296
297 out = (char*)cJSON_malloc(len + 3);
298 if(!out) return 0;
299
300 ptr2 = out;
301 ptr = str;
302 *ptr2++ = '\"';
303 while(*ptr) {
304 if((unsigned char)*ptr > 31 && *ptr != '\"' && *ptr != '\\')
305 *ptr2++ = *ptr++;
306 else {
307 *ptr2++ = '\\';
308 switch(token = *ptr++) {
309 case '\\':
310 *ptr2++ = '\\';
311 break;
312 case '\"':
313 *ptr2++ = '\"';
314 break;
315 case '\b':
316 *ptr2++ = 'b';
317 break;
318 case '\f':
319 *ptr2++ = 'f';
320 break;
321 case '\n':
322 *ptr2++ = 'n';
323 break;
324 case '\r':
325 *ptr2++ = 'r';
326 break;
327 case '\t':
328 *ptr2++ = 't';
329 break;
330 default:
331 sprintf(ptr2, "u%04x", token);
332 ptr2 += 5;
333 break; /* escape and print */
334 }
335 }
336 }
337 *ptr2++ = '\"';
338 *ptr2++ = 0;
339 return out;
340 }
341 /* Invote print_string_ptr (which is useful) on an item. */
342 static char* print_string(cJSON* item) { return print_string_ptr(item->valuestring); }
343
344 /* Predeclare these prototypes. */
345 static const char* parse_value(cJSON* item, const char* value);
346 static char* print_value(cJSON* item, int depth, int fmt);
347 static const char* parse_array(cJSON* item, const char* value);
348 static char* print_array(cJSON* item, int depth, int fmt);
349 static const char* parse_object(cJSON* item, const char* value);
350 static char* print_object(cJSON* item, int depth, int fmt);
351
352 /* Utility to jump whitespace and cr/lf */
353 static const char* skip(const char* in)
354 {
355 while(in && *in && (unsigned char)*in <= 32)
356 in++;
357 return in;
358 }
359
360 /* Parse an object - create a new root, and populate. */
361 cJSON* cJSON_Parse(const char* value)
362 {
363 cJSON* c = cJSON_New_Item();
364 ep = 0;
365 if(!c) return 0; /* memory fail */
366
367 if(!parse_value(c, skip(value))) {
368 cJSON_Delete(c);
369 return 0;
370 }
371 return c;
372 }
373
374 /* Render a cJSON item/entity/structure to text. */
375 char* cJSON_Print(cJSON* item) { return print_value(item, 0, 1); }
376 char* cJSON_PrintUnformatted(cJSON* item) { return print_value(item, 0, 0); }
377
378 /* Parser core - when encountering text, process appropriately. */
379 static const char* parse_value(cJSON* item, const char* value)
380 {
381 if(!value) return 0; /* Fail on null. */
382 if(!strncmp(value, "null", 4)) {
383 item->type = cJSON_NULL;
384 return value + 4;
385 }
386 if(!strncmp(value, "false", 5)) {
387 item->type = cJSON_False;
388 return value + 5;
389 }
390 if(!strncmp(value, "true", 4)) {
391 item->type = cJSON_True;
392 item->valueint = 1;
393 return value + 4;
394 }
395 if(*value == '\"') { return parse_string(item, value); }
396 if(*value == '-' || (*value >= '0' && *value <= '9')) { return parse_number(item, value); }
397 if(*value == '[') { return parse_array(item, value); }
398 if(*value == '{') { return parse_object(item, value); }
399
400 ep = value;
401 return 0; /* failure. */
402 }
403
404 /* Render a value to text. */
405 static char* print_value(cJSON* item, int depth, int fmt)
406 {
407 char* out = 0;
408 if(!item) return 0;
409 switch((item->type) & 255) {
410 case cJSON_NULL:
411 out = cJSON_strdup("null");
412 break;
413 case cJSON_False:
414 out = cJSON_strdup("false");
415 break;
416 case cJSON_True:
417 out = cJSON_strdup("true");
418 break;
419 case cJSON_Number:
420 out = print_number(item);
421 break;
422 case cJSON_String:
423 out = print_string(item);
424 break;
425 case cJSON_Array:
426 out = print_array(item, depth, fmt);
427 break;
428 case cJSON_Object:
429 out = print_object(item, depth, fmt);
430 break;
431 }
432 return out;
433 }
434
435 /* Build an array from input text. */
436 static const char* parse_array(cJSON* item, const char* value)
437 {
438 cJSON* child;
439 if(*value != '[') {
440 ep = value;
441 return 0;
442 } /* not an array! */
443
444 item->type = cJSON_Array;
445 value = skip(value + 1);
446 if(*value == ']') return value + 1; /* empty array. */
447
448 item->child = child = cJSON_New_Item();
449 if(!item->child) return 0; /* memory fail */
450 value = skip(parse_value(child, skip(value))); /* skip any spacing, get the value. */
451 if(!value) return 0;
452
453 while(*value == ',') {
454 cJSON* new_item;
455 if(!(new_item = cJSON_New_Item())) return 0; /* memory fail */
456 child->next = new_item;
457 new_item->prev = child;
458 child = new_item;
459 value = skip(parse_value(child, skip(value + 1)));
460 if(!value) return 0; /* memory fail */
461 }
462
463 if(*value == ']') return value + 1; /* end of array */
464 ep = value;
465 return 0; /* malformed. */
466 }
467
468 /* Render an array to text */
469 static char* print_array(cJSON* item, int depth, int fmt)
470 {
471 char** entries;
472 char *out = 0, *ptr, *ret;
473 int len = 5;
474 cJSON* child = item->child;
475 int numentries = 0, i = 0, fail = 0;
476
477 /* How many entries in the array? */
478 while(child)
479 numentries++, child = child->next;
480 /* Allocate an array to hold the values for each */
481 entries = (char**)cJSON_malloc(numentries * sizeof(char*));
482 if(!entries) return 0;
483 memset(entries, 0, numentries * sizeof(char*));
484 /* Retrieve all the results: */
485 child = item->child;
486 while(child && !fail) {
487 ret = print_value(child, depth + 1, fmt);
488 entries[i++] = ret;
489 if(ret)
490 len += strlen(ret) + 2 + (fmt ? 1 : 0);
491 else
492 fail = 1;
493 child = child->next;
494 }
495
496 /* If we didn't fail, try to malloc the output string */
497 if(!fail) out = (char*)cJSON_malloc(len);
498 /* If that fails, we fail. */
499 if(!out) fail = 1;
500
501 /* Handle failure. */
502 if(fail) {
503 for(i = 0; i < numentries; i++)
504 if(entries[i]) cJSON_free(entries[i]);
505 cJSON_free(entries);
506 return 0;
507 }
508
509 /* Compose the output array. */
510 *out = '[';
511 ptr = out + 1;
512 *ptr = 0;
513 for(i = 0; i < numentries; i++) {
514 strcpy(ptr, entries[i]);
515 ptr += strlen(entries[i]);
516 if(i != numentries - 1) {
517 *ptr++ = ',';
518 if(fmt) *ptr++ = ' ';
519 *ptr = 0;
520 }
521 cJSON_free(entries[i]);
522 }
523 cJSON_free(entries);
524 *ptr++ = ']';
525 *ptr++ = 0;
526 return out;
527 }
528
529 /* Build an object from the text. */
530 static const char* parse_object(cJSON* item, const char* value)
531 {
532 cJSON* child;
533 if(*value != '{') {
534 ep = value;
535 return 0;
536 } /* not an object! */
537
538 item->type = cJSON_Object;
539 value = skip(value + 1);
540 if(*value == '}') return value + 1; /* empty array. */
541
542 item->child = child = cJSON_New_Item();
543 if(!item->child) return 0;
544 value = skip(parse_string(child, skip(value)));
545 if(!value) return 0;
546 child->string = child->valuestring;
547 child->valuestring = 0;
548 if(*value != ':') {
549 ep = value;
550 return 0;
551 } /* fail! */
552 value = skip(parse_value(child, skip(value + 1))); /* skip any spacing, get the value. */
553 if(!value) return 0;
554
555 while(*value == ',') {
556 cJSON* new_item;
557 if(!(new_item = cJSON_New_Item())) return 0; /* memory fail */
558 child->next = new_item;
559 new_item->prev = child;
560 child = new_item;
561 value = skip(parse_string(child, skip(value + 1)));
562 if(!value) return 0;
563 child->string = child->valuestring;
564 child->valuestring = 0;
565 if(*value != ':') {
566 ep = value;
567 return 0;
568 } /* fail! */
569 value = skip(parse_value(child, skip(value + 1))); /* skip any spacing, get the value. */
570 if(!value) return 0;
571 }
572
573 if(*value == '}') return value + 1; /* end of array */
574 ep = value;
575 return 0; /* malformed. */
576 }
577
578 /* Render an object to text. */
579 static char* print_object(cJSON* item, int depth, int fmt)
580 {
581 char **entries = 0, **names = 0;
582 char *out = 0, *ptr, *ret, *str;
583 int len = 7, i = 0, j;
584 cJSON* child = item->child;
585 int numentries = 0, fail = 0;
586 /* Count the number of entries. */
587 while(child)
588 numentries++, child = child->next;
589 /* Allocate space for the names and the objects */
590 entries = (char**)cJSON_malloc(numentries * sizeof(char*));
591 if(!entries) return 0;
592 names = (char**)cJSON_malloc(numentries * sizeof(char*));
593 if(!names) {
594 cJSON_free(entries);
595 return 0;
596 }
597 memset(entries, 0, sizeof(char*) * numentries);
598 memset(names, 0, sizeof(char*) * numentries);
599
600 /* Collect all the results into our arrays: */
601 child = item->child;
602 depth++;
603 if(fmt) len += depth;
604 while(child) {
605 names[i] = str = print_string_ptr(child->string);
606 entries[i++] = ret = print_value(child, depth, fmt);
607 if(str && ret)
608 len += strlen(ret) + strlen(str) + 2 + (fmt ? 2 + depth : 0);
609 else
610 fail = 1;
611 child = child->next;
612 }
613
614 /* Try to allocate the output string */
615 if(!fail) out = (char*)cJSON_malloc(len);
616 if(!out) fail = 1;
617
618 /* Handle failure */
619 if(fail) {
620 for(i = 0; i < numentries; i++) {
621 if(names[i]) cJSON_free(names[i]);
622 if(entries[i]) cJSON_free(entries[i]);
623 }
624 cJSON_free(names);
625 cJSON_free(entries);
626 return 0;
627 }
628
629 /* Compose the output: */
630 *out = '{';
631 ptr = out + 1;
632 if(fmt) *ptr++ = '\n';
633 *ptr = 0;
634 for(i = 0; i < numentries; i++) {
635 if(fmt)
636 for(j = 0; j < depth; j++)
637 *ptr++ = FMT_WHITESPACE_CHAR;
638 strcpy(ptr, names[i]);
639 ptr += strlen(names[i]);
640 *ptr++ = ':';
641 if(fmt) *ptr++ = FMT_WHITESPACE_CHAR;
642 strcpy(ptr, entries[i]);
643 ptr += strlen(entries[i]);
644 if(i != numentries - 1) *ptr++ = ',';
645 if(fmt) *ptr++ = '\n';
646 *ptr = 0;
647 cJSON_free(names[i]);
648 cJSON_free(entries[i]);
649 }
650
651 cJSON_free(names);
652 cJSON_free(entries);
653 if(fmt)
654 for(i = 0; i < depth - 1; i++)
655 *ptr++ = FMT_WHITESPACE_CHAR;
656 *ptr++ = '}';
657 *ptr++ = 0;
658 return out;
659 }
660
661 /* Get Array size/item / object item. */
662 int cJSON_GetArraySize(cJSON* array)
663 {
664 cJSON* c = array->child;
665 int i = 0;
666 while(c)
667 i++, c = c->next;
668 return i;
669 }
670 cJSON* cJSON_GetArrayItem(cJSON* array, int item)
671 {
672 cJSON* c = array->child;
673 while(c && item > 0)
674 item--, c = c->next;
675 return c;
676 }
677 cJSON* cJSON_GetObjectItem(cJSON* object, const char* string)
678 {
679 cJSON* c = object->child;
680 while(c && cJSON_strcasecmp(c->string, string))
681 c = c->next;
682 return c;
683 }
684
685 /* Utility for array list handling. */
686 static void suffix_object(cJSON* prev, cJSON* item)
687 {
688 prev->next = item;
689 item->prev = prev;
690 }
691 /* Utility for handling references. */
692 static cJSON* create_reference(cJSON* item)
693 {
694 cJSON* ref = cJSON_New_Item();
695 if(!ref) return 0;
696 memcpy(ref, item, sizeof(cJSON));
697 ref->string = 0;
698 ref->type |= cJSON_IsReference;
699 ref->next = ref->prev = 0;
700 return ref;
701 }
702
703 /* Add item to array/object. */
704 void cJSON_AddItemToArray(cJSON* array, cJSON* item)
705 {
706 cJSON* c = array->child;
707 if(!item) return;
708 if(!c) {
709 array->child = item;
710 } else {
711 while(c && c->next)
712 c = c->next;
713 suffix_object(c, item);
714 }
715 }
716 void cJSON_AddItemToObject(cJSON* object, const char* string, cJSON* item)
717 {
718 if(!item) return;
719 if(item->string) cJSON_free(item->string);
720 item->string = cJSON_strdup(string);
721 cJSON_AddItemToArray(object, item);
722 }
723 void cJSON_AddItemReferenceToArray(cJSON* array, cJSON* item) { cJSON_AddItemToArray(array, create_reference(item)); }
724 void cJSON_AddItemReferenceToObject(cJSON* object, const char* string, cJSON* item)
725 {
726 cJSON_AddItemToObject(object, string, create_reference(item));
727 }
728
729 cJSON* cJSON_DetachItemFromArray(cJSON* array, int which)
730 {
731 cJSON* c = array->child;
732 while(c && which > 0)
733 c = c->next, which--;
734 if(!c) return 0;
735 if(c->prev) c->prev->next = c->next;
736 if(c->next) c->next->prev = c->prev;
737 if(c == array->child) array->child = c->next;
738 c->prev = c->next = 0;
739 return c;
740 }
741 void cJSON_DeleteItemFromArray(cJSON* array, int which) { cJSON_Delete(cJSON_DetachItemFromArray(array, which)); }
742 cJSON* cJSON_DetachItemFromObject(cJSON* object, const char* string)
743 {
744 int i = 0;
745 cJSON* c = object->child;
746 while(c && cJSON_strcasecmp(c->string, string))
747 i++, c = c->next;
748 if(c) return cJSON_DetachItemFromArray(object, i);
749 return 0;
750 }
751 void cJSON_DeleteItemFromObject(cJSON* object, const char* string)
752 {
753 cJSON_Delete(cJSON_DetachItemFromObject(object, string));
754 }
755
756 /* Replace array/object items with new ones. */
757 void cJSON_ReplaceItemInArray(cJSON* array, int which, cJSON* newitem)
758 {
759 cJSON* c = array->child;
760 while(c && which > 0)
761 c = c->next, which--;
762 if(!c) return;
763 newitem->next = c->next;
764 newitem->prev = c->prev;
765 if(newitem->next) newitem->next->prev = newitem;
766 if(c == array->child)
767 array->child = newitem;
768 else
769 newitem->prev->next = newitem;
770 c->next = c->prev = 0;
771 cJSON_Delete(c);
772 }
773 void cJSON_ReplaceItemInObject(cJSON* object, const char* string, cJSON* newitem)
774 {
775 int i = 0;
776 cJSON* c = object->child;
777 while(c && cJSON_strcasecmp(c->string, string))
778 i++, c = c->next;
779 if(c) {
780 newitem->string = cJSON_strdup(string);
781 cJSON_ReplaceItemInArray(object, i, newitem);
782 }
783 }
784
785 /* Create basic types: */
786 cJSON* cJSON_CreateNull()
787 {
788 cJSON* item = cJSON_New_Item();
789 if(item) item->type = cJSON_NULL;
790 return item;
791 }
792 cJSON* cJSON_CreateTrue()
793 {
794 cJSON* item = cJSON_New_Item();
795 if(item) item->type = cJSON_True;
796 return item;
797 }
798 cJSON* cJSON_CreateFalse()
799 {
800 cJSON* item = cJSON_New_Item();
801 if(item) item->type = cJSON_False;
802 return item;
803 }
804 cJSON* cJSON_CreateBool(int b)
805 {
806 cJSON* item = cJSON_New_Item();
807 if(item) item->type = b ? cJSON_True : cJSON_False;
808 return item;
809 }
810 cJSON* cJSON_CreateNumber(double num)
811 {
812 cJSON* item = cJSON_New_Item();
813 if(item) {
814 item->type = cJSON_Number;
815 item->valuedouble = num;
816 item->valueint = (int)num;
817 }
818 return item;
819 }
820 cJSON* cJSON_CreateString(const char* string)
821 {
822 cJSON* item = cJSON_New_Item();
823 if(item) {
824 item->type = cJSON_String;
825 item->valuestring = cJSON_strdup(string);
826 }
827 return item;
828 }
829 cJSON* cJSON_CreateArray()
830 {
831 cJSON* item = cJSON_New_Item();
832 if(item) item->type = cJSON_Array;
833 return item;
834 }
835 cJSON* cJSON_CreateObject()
836 {
837 cJSON* item = cJSON_New_Item();
838 if(item) item->type = cJSON_Object;
839 return item;
840 }
841
842 /* Create Arrays: */
843 cJSON* cJSON_CreateIntArray(int* numbers, int count)
844 {
845 int i;
846 cJSON *n = 0, *p = 0, *a = cJSON_CreateArray();
847 for(i = 0; a && i < count; i++) {
848 n = cJSON_CreateNumber(numbers[i]);
849 if(!i)
850 a->child = n;
851 else
852 suffix_object(p, n);
853 p = n;
854 }
855 return a;
856 }
857 cJSON* cJSON_CreateFloatArray(float* numbers, int count)
858 {
859 int i;
860 cJSON *n = 0, *p = 0, *a = cJSON_CreateArray();
861 for(i = 0; a && i < count; i++) {
862 n = cJSON_CreateNumber(numbers[i]);
863 if(!i)
864 a->child = n;
865 else
866 suffix_object(p, n);
867 p = n;
868 }
869 return a;
870 }
871 cJSON* cJSON_CreateDoubleArray(double* numbers, int count)
872 {
873 int i;
874 cJSON *n = 0, *p = 0, *a = cJSON_CreateArray();
875 for(i = 0; a && i < count; i++) {
876 n = cJSON_CreateNumber(numbers[i]);
877 if(!i)
878 a->child = n;
879 else
880 suffix_object(p, n);
881 p = n;
882 }
883 return a;
884 }
885 cJSON* cJSON_CreateStringArray(const char** strings, int count)
886 {
887 int i;
888 cJSON *n = 0, *p = 0, *a = cJSON_CreateArray();
889 for(i = 0; a && i < count; i++) {
890 n = cJSON_CreateString(strings[i]);
891 if(!i)
892 a->child = n;
893 else
894 suffix_object(p, n);
895 p = n;
896 }
897 return a;
898 }