Debian Codegraph @

3rdparty/cjson/cJSON.c

raw symbols SCIP
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(void) {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) if(*s1 == 0) return 0;
44 return tolower(*(const unsigned char *)s1) - tolower(*(const unsigned char *)s2);
45 }
46
47 static void *(*cJSON_malloc)(size_t sz) = malloc;
48 static void (*cJSON_free)(void *ptr) = free;
49
50 static char* cJSON_strdup(const char* str)
51 {
52 size_t len;
53 char* copy;
54
55 len = strlen(str) + 1;
56 if (!(copy = (char*)cJSON_malloc(len))) return 0;
57 memcpy(copy,str,len);
58 return copy;
59 }
60
61 void cJSON_InitHooks(cJSON_Hooks* hooks)
62 {
63 if (!hooks) { /* Reset hooks */
64 cJSON_malloc = malloc;
65 cJSON_free = free;
66 return;
67 }
68
69 cJSON_malloc = (hooks->malloc_fn)?hooks->malloc_fn:malloc;
70 cJSON_free = (hooks->free_fn)?hooks->free_fn:free;
71 }
72
73 /* Internal constructor. */
74 static cJSON *cJSON_New_Item(void)
75 {
76 cJSON* node = (cJSON*)cJSON_malloc(sizeof(cJSON));
77 if (node) memset(node,0,sizeof(cJSON));
78 return node;
79 }
80
81 /* Delete a cJSON structure. */
82 void cJSON_Delete(cJSON *c)
83 {
84 cJSON *next;
85 while (c)
86 {
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;int subscale=0,signsubscale=1;
100
101 if (*num=='-') sign=-1,num++; /* Has sign? */
102 if (*num=='0') num++; /* is zero */
103 if (*num>='1' && *num<='9') do n=(n*10.0)+(*num++ -'0'); while (*num>='0' && *num<='9'); /* Number? */
104 if (*num=='.' && num[1]>='0' && num[1]<='9') {num++; do n=(n*10.0)+(*num++ -'0'),scale--; while (*num>='0' && *num<='9');} /* Fractional part? */
105 if (*num=='e' || *num=='E') /* Exponent? */
106 { num++;if (*num=='+') num++; else if (*num=='-') signsubscale=-1,num++; /* With sign? */
107 while (*num>='0' && *num<='9') subscale=(subscale*10)+(*num++ - '0'); /* Number? */
108 }
109
110 n=sign*n*pow(10.0,(scale+subscale*signsubscale)); /* number = +/- number.fraction * 10^+/- exponent */
111
112 item->valuedouble=n;
113 item->valueint=(int)n;
114 item->type=cJSON_Number;
115 return num;
116 }
117
118 /* Render the number nicely from the given item into a string. */
119 static char *print_number(cJSON *item)
120 {
121 char *str;
122 double d=item->valuedouble;
123 if (fabs(((double)item->valueint)-d)<=DBL_EPSILON && d<=INT_MAX && d>=INT_MIN)
124 {
125 str=(char*)cJSON_malloc(21); /* 2^64+1 can be represented in 21 chars. */
126 if (str) sprintf(str,"%d",item->valueint);
127 }
128 else
129 {
130 str=(char*)cJSON_malloc(64); /* This is a nice tradeoff. */
131 if (str)
132 {
133 if (fabs(floor(d)-d)<=DBL_EPSILON && fabs(d)<1.0e60)sprintf(str,"%.0f",d);
134 else if (fabs(d)<1.0e-6 || fabs(d)>1.0e9) sprintf(str,"%e",d);
135 else sprintf(str,"%f",d);
136 }
137 }
138 return str;
139 }
140
141 static unsigned parse_hex4(const char *str)
142 {
143 unsigned h=0;
144 if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0;
145 h=h<<4;str++;
146 if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0;
147 h=h<<4;str++;
148 if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0;
149 h=h<<4;str++;
150 if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0;
151 return h;
152 }
153
154 /* Parse the input text into an unescaped cstring, and populate item. */
155 static const unsigned char firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
156 static const char *parse_string(cJSON *item,const char *str)
157 {
158 const char *ptr=str+1;char *ptr2;char *out;int len=0;unsigned uc,uc2;
159 if (*str!='\"') {ep=str;return 0;} /* not a string! */
160
161 while (*ptr!='\"' && *ptr && ++len) if (*ptr++ == '\\') ptr++; /* Skip escaped quotes. */
162
163 out=(char*)cJSON_malloc(len+1); /* This is how long we need for the string, roughly. */
164 if (!out) return 0;
165
166 ptr=str+1;ptr2=out;
167 while (*ptr!='\"' && *ptr)
168 {
169 if (*ptr!='\\') *ptr2++=*ptr++;
170 else
171 {
172 ptr++;
173 switch (*ptr)
174 {
175 case 'b': *ptr2++='\b'; break;
176 case 'f': *ptr2++='\f'; break;
177 case 'n': *ptr2++='\n'; break;
178 case 'r': *ptr2++='\r'; break;
179 case 't': *ptr2++='\t'; break;
180 case 'u': /* transcode utf16 to utf8. */
181 uc=parse_hex4(ptr+1);ptr+=4; /* get the unicode char. */
182
183 if ((uc>=0xDC00 && uc<=0xDFFF) || uc==0) break; /* check for invalid. */
184
185 if (uc>=0xD800 && uc<=0xDBFF) /* UTF16 surrogate pairs. */
186 {
187 if (ptr[1]!='\\' || ptr[2]!='u') break; /* missing second-half of surrogate. */
188 uc2=parse_hex4(ptr+3);ptr+=6;
189 if (uc2<0xDC00 || uc2>0xDFFF) break; /* invalid second-half of surrogate. */
190 uc=0x10000 + (((uc&0x3FF)<<10) | (uc2&0x3FF));
191 }
192
193 len=4;if (uc<0x80) len=1;else if (uc<0x800) len=2;else if (uc<0x10000) len=3; ptr2+=len;
194
195 switch (len) {
196 case 4: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6;
197 case 3: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6;
198 case 2: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6;
199 case 1: *--ptr2 =(uc | firstByteMark[len]);
200 }
201 ptr2+=len;
202 break;
203 default: *ptr2++=*ptr; break;
204 }
205 ptr++;
206 }
207 }
208 *ptr2=0;
209 if (*ptr=='\"') ptr++;
210 item->valuestring=out;
211 item->type=cJSON_String;
212 return ptr;
213 }
214
215 /* Render the cstring provided to an escaped version that can be printed. */
216 static char *print_string_ptr(const char *str)
217 {
218 const char *ptr;char *ptr2,*out;int len=0;unsigned char token;
219
220 if (!str) return cJSON_strdup("");
221 ptr=str;while ((token=*ptr) && ++len) {if (strchr("\"\\\b\f\n\r\t",token)) len++; else if (token<32) len+=5;ptr++;}
222
223 out=(char*)cJSON_malloc(len+3);
224 if (!out) return 0;
225
226 ptr2=out;ptr=str;
227 *ptr2++='\"';
228 while (*ptr)
229 {
230 if ((unsigned char)*ptr>31 && *ptr!='\"' && *ptr!='\\') *ptr2++=*ptr++;
231 else
232 {
233 *ptr2++='\\';
234 switch (token=*ptr++)
235 {
236 case '\\': *ptr2++='\\'; break;
237 case '\"': *ptr2++='\"'; break;
238 case '\b': *ptr2++='b'; break;
239 case '\f': *ptr2++='f'; break;
240 case '\n': *ptr2++='n'; break;
241 case '\r': *ptr2++='r'; break;
242 case '\t': *ptr2++='t'; break;
243 default: sprintf(ptr2,"u%04x",token);ptr2+=5; break; /* escape and print */
244 }
245 }
246 }
247 *ptr2++='\"';*ptr2++=0;
248 return out;
249 }
250 /* Invote print_string_ptr (which is useful) on an item. */
251 static char *print_string(cJSON *item) {return print_string_ptr(item->valuestring);}
252
253 /* Predeclare these prototypes. */
254 static const char *parse_value(cJSON *item,const char *value);
255 static char *print_value(cJSON *item,int depth,int fmt);
256 static const char *parse_array(cJSON *item,const char *value);
257 static char *print_array(cJSON *item,int depth,int fmt);
258 static const char *parse_object(cJSON *item,const char *value);
259 static char *print_object(cJSON *item,int depth,int fmt);
260
261 /* Utility to jump whitespace and cr/lf */
262 static const char *skip(const char *in) {while (in && *in && (unsigned char)*in<=32) in++; return in;}
263
264 /* Parse an object - create a new root, and populate. */
265 cJSON *cJSON_ParseWithOpts(const char *value,const char **return_parse_end,int require_null_terminated)
266 {
267 const char *end=0;
268 cJSON *c=cJSON_New_Item();
269 ep=0;
270 if (!c) return 0; /* memory fail */
271
272 end=parse_value(c,skip(value));
273 if (!end) {cJSON_Delete(c);return 0;} /* parse failure. ep is set. */
274
275 /* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */
276 if (require_null_terminated) {end=skip(end);if (*end) {cJSON_Delete(c);ep=end;return 0;}}
277 if (return_parse_end) *return_parse_end=end;
278 return c;
279 }
280 /* Default options for cJSON_Parse */
281 cJSON *cJSON_Parse(const char *value) {return cJSON_ParseWithOpts(value,0,0);}
282
283 /* Render a cJSON item/entity/structure to text. */
284 char *cJSON_Print(cJSON *item) {return print_value(item,0,1);}
285 char *cJSON_PrintUnformatted(cJSON *item) {return print_value(item,0,0);}
286
287 /* Parser core - when encountering text, process appropriately. */
288 static const char *parse_value(cJSON *item,const char *value)
289 {
290 if (!value) return 0; /* Fail on null. */
291 if (!strncmp(value,"null",4)) { item->type=cJSON_NULL; return value+4; }
292 if (!strncmp(value,"false",5)) { item->type=cJSON_False; return value+5; }
293 if (!strncmp(value,"true",4)) { item->type=cJSON_True; item->valueint=1; return value+4; }
294 if (*value=='\"') { return parse_string(item,value); }
295 if (*value=='-' || (*value>='0' && *value<='9')) { return parse_number(item,value); }
296 if (*value=='[') { return parse_array(item,value); }
297 if (*value=='{') { return parse_object(item,value); }
298
299 ep=value;return 0; /* failure. */
300 }
301
302 /* Render a value to text. */
303 static char *print_value(cJSON *item,int depth,int fmt)
304 {
305 char *out=0;
306 if (!item) return 0;
307 switch ((item->type)&255)
308 {
309 case cJSON_NULL: out=cJSON_strdup("null"); break;
310 case cJSON_False: out=cJSON_strdup("false");break;
311 case cJSON_True: out=cJSON_strdup("true"); break;
312 case cJSON_Number: out=print_number(item);break;
313 case cJSON_String: out=print_string(item);break;
314 case cJSON_Array: out=print_array(item,depth,fmt);break;
315 case cJSON_Object: out=print_object(item,depth,fmt);break;
316 }
317 return out;
318 }
319
320 /* Build an array from input text. */
321 static const char *parse_array(cJSON *item,const char *value)
322 {
323 cJSON *child;
324 if (*value!='[') {ep=value;return 0;} /* not an array! */
325
326 item->type=cJSON_Array;
327 value=skip(value+1);
328 if (*value==']') return value+1; /* empty array. */
329
330 item->child=child=cJSON_New_Item();
331 if (!item->child) return 0; /* memory fail */
332 value=skip(parse_value(child,skip(value))); /* skip any spacing, get the value. */
333 if (!value) return 0;
334
335 while (*value==',')
336 {
337 cJSON *new_item;
338 if (!(new_item=cJSON_New_Item())) return 0; /* memory fail */
339 child->next=new_item;new_item->prev=child;child=new_item;
340 value=skip(parse_value(child,skip(value+1)));
341 if (!value) return 0; /* memory fail */
342 }
343
344 if (*value==']') return value+1; /* end of array */
345 ep=value;return 0; /* malformed. */
346 }
347
348 /* Render an array to text */
349 static char *print_array(cJSON *item,int depth,int fmt)
350 {
351 char **entries;
352 char *out=0,*ptr,*ret;int len=5;
353 cJSON *child=item->child;
354 int numentries=0,i=0,fail=0;
355
356 /* How many entries in the array? */
357 while (child) numentries++,child=child->next;
358 /* Explicitly handle numentries==0 */
359 if (!numentries)
360 {
361 out=(char*)cJSON_malloc(3);
362 if (out) strcpy(out,"[]");
363 return out;
364 }
365 /* Allocate an array to hold the values for each */
366 entries=(char**)cJSON_malloc(numentries*sizeof(char*));
367 if (!entries) return 0;
368 memset(entries,0,numentries*sizeof(char*));
369 /* Retrieve all the results: */
370 child=item->child;
371 while (child && !fail)
372 {
373 ret=print_value(child,depth+1,fmt);
374 entries[i++]=ret;
375 if (ret) len+=strlen(ret)+2+(fmt?1:0); else fail=1;
376 child=child->next;
377 }
378
379 /* If we didn't fail, try to malloc the output string */
380 if (!fail) out=(char*)cJSON_malloc(len);
381 /* If that fails, we fail. */
382 if (!out) fail=1;
383
384 /* Handle failure. */
385 if (fail)
386 {
387 for (i=0;i<numentries;i++) if (entries[i]) cJSON_free(entries[i]);
388 cJSON_free(entries);
389 return 0;
390 }
391
392 /* Compose the output array. */
393 *out='[';
394 ptr=out+1;*ptr=0;
395 for (i=0;i<numentries;i++)
396 {
397 strcpy(ptr,entries[i]);ptr+=strlen(entries[i]);
398 if (i!=numentries-1) {*ptr++=',';if(fmt)*ptr++=' ';*ptr=0;}
399 cJSON_free(entries[i]);
400 }
401 cJSON_free(entries);
402 *ptr++=']';*ptr++=0;
403 return out;
404 }
405
406 /* Build an object from the text. */
407 static const char *parse_object(cJSON *item,const char *value)
408 {
409 cJSON *child;
410 if (*value!='{') {ep=value;return 0;} /* not an object! */
411
412 item->type=cJSON_Object;
413 value=skip(value+1);
414 if (*value=='}') return value+1; /* empty array. */
415
416 item->child=child=cJSON_New_Item();
417 if (!item->child) return 0;
418 value=skip(parse_string(child,skip(value)));
419 if (!value) return 0;
420 child->string=child->valuestring;child->valuestring=0;
421 if (*value!=':') {ep=value;return 0;} /* fail! */
422 value=skip(parse_value(child,skip(value+1))); /* skip any spacing, get the value. */
423 if (!value) return 0;
424
425 while (*value==',')
426 {
427 cJSON *new_item;
428 if (!(new_item=cJSON_New_Item())) return 0; /* memory fail */
429 child->next=new_item;new_item->prev=child;child=new_item;
430 value=skip(parse_string(child,skip(value+1)));
431 if (!value) return 0;
432 child->string=child->valuestring;child->valuestring=0;
433 if (*value!=':') {ep=value;return 0;} /* fail! */
434 value=skip(parse_value(child,skip(value+1))); /* skip any spacing, get the value. */
435 if (!value) return 0;
436 }
437
438 if (*value=='}') return value+1; /* end of array */
439 ep=value;return 0; /* malformed. */
440 }
441
442 /* Render an object to text. */
443 static char *print_object(cJSON *item,int depth,int fmt)
444 {
445 char **entries=0,**names=0;
446 char *out=0,*ptr,*ret,*str;int len=7,i=0,j;
447 cJSON *child=item->child;
448 int numentries=0,fail=0;
449 /* Count the number of entries. */
450 while (child) numentries++,child=child->next;
451 /* Explicitly handle empty object case */
452 if (!numentries)
453 {
454 out=(char*)cJSON_malloc(fmt?depth+4:3);
455 if (!out) return 0;
456 ptr=out;*ptr++='{';
457 if (fmt) {*ptr++='\n';for (i=0;i<depth-1;i++) *ptr++='\t';}
458 *ptr++='}';*ptr++=0;
459 return out;
460 }
461 /* Allocate space for the names and the objects */
462 entries=(char**)cJSON_malloc(numentries*sizeof(char*));
463 if (!entries) return 0;
464 names=(char**)cJSON_malloc(numentries*sizeof(char*));
465 if (!names) {cJSON_free(entries);return 0;}
466 memset(entries,0,sizeof(char*)*numentries);
467 memset(names,0,sizeof(char*)*numentries);
468
469 /* Collect all the results into our arrays: */
470 child=item->child;depth++;if (fmt) len+=depth;
471 while (child)
472 {
473 names[i]=str=print_string_ptr(child->string);
474 entries[i++]=ret=print_value(child,depth,fmt);
475 if (str && ret) len+=strlen(ret)+strlen(str)+2+(fmt?2+depth:0); else fail=1;
476 child=child->next;
477 }
478
479 /* Try to allocate the output string */
480 if (!fail) out=(char*)cJSON_malloc(len);
481 if (!out) fail=1;
482
483 /* Handle failure */
484 if (fail)
485 {
486 for (i=0;i<numentries;i++) {if (names[i]) cJSON_free(names[i]);if (entries[i]) cJSON_free(entries[i]);}
487 cJSON_free(names);cJSON_free(entries);
488 return 0;
489 }
490
491 /* Compose the output: */
492 *out='{';ptr=out+1;if (fmt)*ptr++='\n';*ptr=0;
493 for (i=0;i<numentries;i++)
494 {
495 if (fmt) for (j=0;j<depth;j++) *ptr++='\t';
496 strcpy(ptr,names[i]);ptr+=strlen(names[i]);
497 *ptr++=':';if (fmt) *ptr++='\t';
498 strcpy(ptr,entries[i]);ptr+=strlen(entries[i]);
499 if (i!=numentries-1) *ptr++=',';
500 if (fmt) *ptr++='\n';
501 *ptr=0;
502 cJSON_free(names[i]);
503 cJSON_free(entries[i]);
504 }
505
506 cJSON_free(names);cJSON_free(entries);
507 if (fmt) for (i=0;i<depth-1;i++) *ptr++='\t';
508 *ptr++='}';*ptr++=0;
509 return out;
510 }
511
512 /* Get Array size/item / object item. */
513 int cJSON_GetArraySize(cJSON *array) {cJSON *c=array->child;int i=0;while(c)i++,c=c->next;return i;}
514 cJSON *cJSON_GetArrayItem(cJSON *array,int item) {cJSON *c=array->child; while (c && item>0) item--,c=c->next; return c;}
515 cJSON *cJSON_GetObjectItem(cJSON *object,const char *string) {cJSON *c=object->child; while (c && cJSON_strcasecmp(c->string,string)) c=c->next; return c;}
516
517 /* Utility for array list handling. */
518 static void suffix_object(cJSON *prev,cJSON *item) {prev->next=item;item->prev=prev;}
519 /* Utility for handling references. */
520 static cJSON *create_reference(cJSON *item) {cJSON *ref=cJSON_New_Item();if (!ref) return 0;memcpy(ref,item,sizeof(cJSON));ref->string=0;ref->type|=cJSON_IsReference;ref->next=ref->prev=0;return ref;}
521
522 /* Add item to array/object. */
523 void cJSON_AddItemToArray(cJSON *array, cJSON *item) {cJSON *c=array->child;if (!item) return; if (!c) {array->child=item;} else {while (c && c->next) c=c->next; suffix_object(c,item);}}
524 void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item) {if (!item) return; if (item->string) cJSON_free(item->string);item->string=cJSON_strdup(string);cJSON_AddItemToArray(object,item);}
525 void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item) {cJSON_AddItemToArray(array,create_reference(item));}
526 void cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item) {cJSON_AddItemToObject(object,string,create_reference(item));}
527
528 cJSON *cJSON_DetachItemFromArray(cJSON *array,int which) {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) return 0;
529 if (c->prev) c->prev->next=c->next;
530 if (c->next) c->next->prev=c->prev;
531 if (c==array->child) array->child=c->next;
532 c->prev=c->next=0;
533 return c;}
534 void cJSON_DeleteItemFromArray(cJSON *array,int which) {cJSON_Delete(cJSON_DetachItemFromArray(array,which));}
535 cJSON *cJSON_DetachItemFromObject(cJSON *object,const char *string) {int i=0;cJSON *c=object->child;while (c && cJSON_strcasecmp(c->string,string)) i++,c=c->next;if (c) return cJSON_DetachItemFromArray(object,i);return 0;}
536 void cJSON_DeleteItemFromObject(cJSON *object,const char *string) {cJSON_Delete(cJSON_DetachItemFromObject(object,string));}
537
538 /* Replace array/object items with new ones. */
539 void cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem) {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) return;
540 newitem->next=c->next;newitem->prev=c->prev;if (newitem->next) newitem->next->prev=newitem;
541 if (c==array->child) array->child=newitem; else newitem->prev->next=newitem;c->next=c->prev=0;cJSON_Delete(c);}
542 void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem){int i=0;cJSON *c=object->child;while(c && cJSON_strcasecmp(c->string,string))i++,c=c->next;if(c){newitem->string=cJSON_strdup(string);cJSON_ReplaceItemInArray(object,i,newitem);}}
543
544 /* Create basic types: */
545 cJSON *cJSON_CreateNull(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_NULL;return item;}
546 cJSON *cJSON_CreateTrue(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_True;return item;}
547 cJSON *cJSON_CreateFalse(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_False;return item;}
548 cJSON *cJSON_CreateBool(int b) {cJSON *item=cJSON_New_Item();if(item)item->type=b?cJSON_True:cJSON_False;return item;}
549 cJSON *cJSON_CreateNumber(double num) {cJSON *item=cJSON_New_Item();if(item){item->type=cJSON_Number;item->valuedouble=num;item->valueint=(int)num;}return item;}
550 cJSON *cJSON_CreateString(const char *string) {cJSON *item=cJSON_New_Item();if(item){item->type=cJSON_String;item->valuestring=cJSON_strdup(string);}return item;}
551 cJSON *cJSON_CreateArray(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_Array;return item;}
552 cJSON *cJSON_CreateObject(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_Object;return item;}
553
554 /* Create Arrays: */
555 cJSON *cJSON_CreateIntArray(const int *numbers,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && i<count;i++){n=cJSON_CreateNumber(numbers[i]);if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;}
556 cJSON *cJSON_CreateFloatArray(const float *numbers,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && i<count;i++){n=cJSON_CreateNumber(numbers[i]);if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;}
557 cJSON *cJSON_CreateDoubleArray(const double *numbers,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && i<count;i++){n=cJSON_CreateNumber(numbers[i]);if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;}
558 cJSON *cJSON_CreateStringArray(const char **strings,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && i<count;i++){n=cJSON_CreateString(strings[i]);if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;}
559
560 /* Duplication */
561 cJSON *cJSON_Duplicate(cJSON *item,int recurse)
562 {
563 cJSON *newitem,*cptr,*nptr=0,*newchild;
564 /* Bail on bad ptr */
565 if (!item) return 0;
566 /* Create new item */
567 newitem=cJSON_New_Item();
568 if (!newitem) return 0;
569 /* Copy over all vars */
570 newitem->type=item->type&(~cJSON_IsReference),newitem->valueint=item->valueint,newitem->valuedouble=item->valuedouble;
571 if (item->valuestring) {newitem->valuestring=cJSON_strdup(item->valuestring); if (!newitem->valuestring) {cJSON_Delete(newitem);return 0;}}
572 if (item->string) {newitem->string=cJSON_strdup(item->string); if (!newitem->string) {cJSON_Delete(newitem);return 0;}}
573 /* If non-recursive, then we're done! */
574 if (!recurse) return newitem;
575 /* Walk the ->next chain for the child. */
576 cptr=item->child;
577 while (cptr)
578 {
579 newchild=cJSON_Duplicate(cptr,1); /* Duplicate (with recurse) each item in the ->next chain */
580 if (!newchild) {cJSON_Delete(newitem);return 0;}
581 if (nptr) {nptr->next=newchild,newchild->prev=nptr;nptr=newchild;} /* If newitem->child already set, then crosswire ->prev and ->next and move on */
582 else {newitem->child=newchild;nptr=newchild;} /* Set newitem->child and move to it */
583 cptr=cptr->next;
584 }
585 return newitem;
586 }
587
588 void cJSON_Minify(char *json)
589 {
590 char *into=json;
591 while (*json)
592 {
593 if (*json==' ') json++;
594 else if (*json=='\t') json++; // Whitespace characters.
595 else if (*json=='\r') json++;
596 else if (*json=='\n') json++;
597 else if (*json=='/' && json[1]=='/') while (*json && *json!='\n') json++; // double-slash comments, to end of line.
598 else if (*json=='/' && json[1]=='*') {while (*json && !(*json=='*' && json[1]=='/')) json++;json+=2;} // multiline comments.
599 else if (*json=='\"'){*into++=*json++;while (*json && *json!='\"'){if (*json=='\\') *into++=*json++;*into++=*json++;}*into++=*json++;} // string literals, which are \" sensitive.
600 else *into++=*json++; // All other characters.
601 }
602 *into=0; // and null-terminate.
603 }