Debian Codegraph @

wxcrafter/wxcLib/cJSON.cpp

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