00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <string.h>
00027 #include <stdio.h>
00028 #include <math.h>
00029 #include <stdlib.h>
00030 #include <float.h>
00031 #include <limits.h>
00032 #include <ctype.h>
00033 #include "cJSON.h"
00034
00035 static const char *ep;
00036
00037 const char *cJSON_GetErrorPtr(void) {return ep;}
00038
00039 static int cJSON_strcasecmp(const char *s1,const char *s2)
00040 {
00041 if (!s1) return (s1==s2)?0:1;if (!s2) return 1;
00042 for(; tolower(*s1) == tolower(*s2); ++s1, ++s2) if(*s1 == 0) return 0;
00043 return tolower(*(const unsigned char *)s1) - tolower(*(const unsigned char *)s2);
00044 }
00045
00046 static void *(*cJSON_malloc)(size_t sz) = malloc;
00047 static void (*cJSON_free)(void *ptr) = free;
00048
00049 static char* cJSON_strdup(const char* str)
00050 {
00051 size_t len;
00052 char* copy;
00053
00054 len = strlen(str) + 1;
00055 if (!(copy = (char*)cJSON_malloc(len))) return 0;
00056 memcpy(copy,str,len);
00057 return copy;
00058 }
00059
00060 void cJSON_InitHooks(cJSON_Hooks* hooks)
00061 {
00062 if (!hooks) {
00063 cJSON_malloc = malloc;
00064 cJSON_free = free;
00065 return;
00066 }
00067
00068 cJSON_malloc = (hooks->malloc_fn)?hooks->malloc_fn:malloc;
00069 cJSON_free = (hooks->free_fn)?hooks->free_fn:free;
00070 }
00071
00072
00073 static cJSON *cJSON_New_Item(void)
00074 {
00075 cJSON* node = (cJSON*)cJSON_malloc(sizeof(cJSON));
00076 if (node) memset(node,0,sizeof(cJSON));
00077 return node;
00078 }
00079
00080
00081 void cJSON_Delete(cJSON *c)
00082 {
00083 cJSON *next;
00084 while (c)
00085 {
00086 next=c->next;
00087 if (!(c->type&cJSON_IsReference) && c->child) cJSON_Delete(c->child);
00088 if (!(c->type&cJSON_IsReference) && c->valuestring) cJSON_free(c->valuestring);
00089 if (!(c->type&cJSON_StringIsConst) && c->string) cJSON_free(c->string);
00090 cJSON_free(c);
00091 c=next;
00092 }
00093 }
00094
00095
00096 static const char *parse_number(cJSON *item,const char *num)
00097 {
00098 double n=0,sign=1,scale=0;int subscale=0,signsubscale=1;
00099
00100 if (*num=='-') sign=-1,num++;
00101 if (*num=='0') num++;
00102 if (*num>='1' && *num<='9') do n=(n*10.0)+(*num++ -'0'); while (*num>='0' && *num<='9');
00103 if (*num=='.' && num[1]>='0' && num[1]<='9') {num++; do n=(n*10.0)+(*num++ -'0'),scale--; while (*num>='0' && *num<='9');}
00104 if (*num=='e' || *num=='E')
00105 { num++;if (*num=='+') num++; else if (*num=='-') signsubscale=-1,num++;
00106 while (*num>='0' && *num<='9') subscale=(subscale*10)+(*num++ - '0');
00107 }
00108
00109 n=sign*n*pow(10.0,(scale+subscale*signsubscale));
00110
00111 item->valuedouble=n;
00112 item->valueint=(int)n;
00113 item->type=cJSON_Number;
00114 return num;
00115 }
00116
00117 static int pow2gt (int x) { --x; x|=x>>1; x|=x>>2; x|=x>>4; x|=x>>8; x|=x>>16; return x+1; }
00118
00119 typedef struct {char *buffer; int length; int offset; } printbuffer;
00120
00121 static char* ensure(printbuffer *p,int needed)
00122 {
00123 char *newbuffer;int newsize;
00124 if (!p || !p->buffer) return 0;
00125 needed+=p->offset;
00126 if (needed<=p->length) return p->buffer+p->offset;
00127
00128 newsize=pow2gt(needed);
00129 newbuffer=(char*)cJSON_malloc(newsize);
00130 if (!newbuffer) {cJSON_free(p->buffer);p->length=0,p->buffer=0;return 0;}
00131 if (newbuffer) memcpy(newbuffer,p->buffer,p->length);
00132 cJSON_free(p->buffer);
00133 p->length=newsize;
00134 p->buffer=newbuffer;
00135 return newbuffer+p->offset;
00136 }
00137
00138 static int update(printbuffer *p)
00139 {
00140 char *str;
00141 if (!p || !p->buffer) return 0;
00142 str=p->buffer+p->offset;
00143 return p->offset+strlen(str);
00144 }
00145
00146
00147 static char *print_number(cJSON *item,printbuffer *p)
00148 {
00149 char *str=0;
00150 double d=item->valuedouble;
00151 if (d==0)
00152 {
00153 if (p) str=ensure(p,2);
00154 else str=(char*)cJSON_malloc(2);
00155 if (str) strcpy(str,"0");
00156 }
00157 else if (fabs(((double)item->valueint)-d)<=DBL_EPSILON && d<=INT_MAX && d>=INT_MIN)
00158 {
00159 if (p) str=ensure(p,21);
00160 else str=(char*)cJSON_malloc(21);
00161 if (str) sprintf(str,"%d",item->valueint);
00162 }
00163 else
00164 {
00165 if (p) str=ensure(p,64);
00166 else str=(char*)cJSON_malloc(64);
00167 if (str)
00168 {
00169 if (fabs(floor(d)-d)<=DBL_EPSILON && fabs(d)<1.0e60)sprintf(str,"%.0f",d);
00170 else if (fabs(d)<1.0e-6 || fabs(d)>1.0e9) sprintf(str,"%e",d);
00171 else sprintf(str,"%f",d);
00172 }
00173 }
00174 return str;
00175 }
00176
00177 static unsigned parse_hex4(const char *str)
00178 {
00179 unsigned h=0;
00180 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;
00181 h=h<<4;str++;
00182 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;
00183 h=h<<4;str++;
00184 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;
00185 h=h<<4;str++;
00186 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;
00187 return h;
00188 }
00189
00190
00191 static const unsigned char firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
00192 static const char *parse_string(cJSON *item,const char *str)
00193 {
00194 const char *ptr=str+1;char *ptr2;char *out;int len=0;unsigned uc,uc2;
00195 if (*str!='\"') {ep=str;return 0;}
00196
00197 while (*ptr!='\"' && *ptr && ++len) if (*ptr++ == '\\') ptr++;
00198
00199 out=(char*)cJSON_malloc(len+1);
00200 if (!out) return 0;
00201
00202 ptr=str+1;ptr2=out;
00203 while (*ptr!='\"' && *ptr)
00204 {
00205 if (*ptr!='\\') *ptr2++=*ptr++;
00206 else
00207 {
00208 ptr++;
00209 switch (*ptr)
00210 {
00211 case 'b': *ptr2++='\b'; break;
00212 case 'f': *ptr2++='\f'; break;
00213 case 'n': *ptr2++='\n'; break;
00214 case 'r': *ptr2++='\r'; break;
00215 case 't': *ptr2++='\t'; break;
00216 case 'u':
00217 uc=parse_hex4(ptr+1);ptr+=4;
00218
00219 if ((uc>=0xDC00 && uc<=0xDFFF) || uc==0) break;
00220
00221 if (uc>=0xD800 && uc<=0xDBFF)
00222 {
00223 if (ptr[1]!='\\' || ptr[2]!='u') break;
00224 uc2=parse_hex4(ptr+3);ptr+=6;
00225 if (uc2<0xDC00 || uc2>0xDFFF) break;
00226 uc=0x10000 + (((uc&0x3FF)<<10) | (uc2&0x3FF));
00227 }
00228
00229 len=4;if (uc<0x80) len=1;else if (uc<0x800) len=2;else if (uc<0x10000) len=3; ptr2+=len;
00230
00231 switch (len) {
00232 case 4: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6;
00233 case 3: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6;
00234 case 2: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6;
00235 case 1: *--ptr2 =(uc | firstByteMark[len]);
00236 }
00237 ptr2+=len;
00238 break;
00239 default: *ptr2++=*ptr; break;
00240 }
00241 ptr++;
00242 }
00243 }
00244 *ptr2=0;
00245 if (*ptr=='\"') ptr++;
00246 item->valuestring=out;
00247 item->type=cJSON_String;
00248 return ptr;
00249 }
00250
00251
00252 static char *print_string_ptr(const char *str,printbuffer *p)
00253 {
00254 const char *ptr;char *ptr2,*out;int len=0,flag=0;unsigned char token;
00255
00256 for (ptr=str;*ptr;ptr++) flag|=((*ptr>0 && *ptr<32)||(*ptr=='\"')||(*ptr=='\\'))?1:0;
00257 if (!flag)
00258 {
00259 len=ptr-str;
00260 if (p) out=ensure(p,len+3);
00261 else out=(char*)cJSON_malloc(len+3);
00262 if (!out) return 0;
00263 ptr2=out;*ptr2++='\"';
00264 strcpy(ptr2,str);
00265 ptr2[len]='\"';
00266 ptr2[len+1]=0;
00267 return out;
00268 }
00269
00270 if (!str)
00271 {
00272 if (p) out=ensure(p,3);
00273 else out=(char*)cJSON_malloc(3);
00274 if (!out) return 0;
00275 strcpy(out,"\"\"");
00276 return out;
00277 }
00278 ptr=str;while ((token=*ptr) && ++len) {if (strchr("\"\\\b\f\n\r\t",token)) len++; else if (token<32) len+=5;ptr++;}
00279
00280 if (p) out=ensure(p,len+3);
00281 else out=(char*)cJSON_malloc(len+3);
00282 if (!out) return 0;
00283
00284 ptr2=out;ptr=str;
00285 *ptr2++='\"';
00286 while (*ptr)
00287 {
00288 if ((unsigned char)*ptr>31 && *ptr!='\"' && *ptr!='\\') *ptr2++=*ptr++;
00289 else
00290 {
00291 *ptr2++='\\';
00292 switch (token=*ptr++)
00293 {
00294 case '\\': *ptr2++='\\'; break;
00295 case '\"': *ptr2++='\"'; break;
00296 case '\b': *ptr2++='b'; break;
00297 case '\f': *ptr2++='f'; break;
00298 case '\n': *ptr2++='n'; break;
00299 case '\r': *ptr2++='r'; break;
00300 case '\t': *ptr2++='t'; break;
00301 default: sprintf(ptr2,"u%04x",token);ptr2+=5; break;
00302 }
00303 }
00304 }
00305 *ptr2++='\"';*ptr2++=0;
00306 return out;
00307 }
00308
00309 static char *print_string(cJSON *item,printbuffer *p) {return print_string_ptr(item->valuestring,p);}
00310
00311
00312 static const char *parse_value(cJSON *item,const char *value);
00313 static char *print_value(cJSON *item,int depth,int fmt,printbuffer *p);
00314 static const char *parse_array(cJSON *item,const char *value);
00315 static char *print_array(cJSON *item,int depth,int fmt,printbuffer *p);
00316 static const char *parse_object(cJSON *item,const char *value);
00317 static char *print_object(cJSON *item,int depth,int fmt,printbuffer *p);
00318
00319
00320 static const char *skip(const char *in) {while (in && *in && (unsigned char)*in<=32) in++; return in;}
00321
00322
00323 cJSON *cJSON_ParseWithOpts(const char *value,const char **return_parse_end,int require_null_terminated)
00324 {
00325 const char *end=0;
00326 cJSON *c=cJSON_New_Item();
00327 ep=0;
00328 if (!c) return 0;
00329
00330 end=parse_value(c,skip(value));
00331 if (!end) {cJSON_Delete(c);return 0;}
00332
00333
00334 if (require_null_terminated) {end=skip(end);if (*end) {cJSON_Delete(c);ep=end;return 0;}}
00335 if (return_parse_end) *return_parse_end=end;
00336 return c;
00337 }
00338
00339 cJSON *cJSON_Parse(const char *value) {return cJSON_ParseWithOpts(value,0,0);}
00340
00341
00342 char *cJSON_Print(cJSON *item) {return print_value(item,0,1,0);}
00343 char *cJSON_PrintUnformatted(cJSON *item) {return print_value(item,0,0,0);}
00344
00345 char *cJSON_PrintBuffered(cJSON *item,int prebuffer,int fmt)
00346 {
00347 printbuffer p;
00348 p.buffer=(char*)cJSON_malloc(prebuffer);
00349 p.length=prebuffer;
00350 p.offset=0;
00351 return print_value(item,0,fmt,&p);
00352 return p.buffer;
00353 }
00354
00355
00356
00357 static const char *parse_value(cJSON *item,const char *value)
00358 {
00359 if (!value) return 0;
00360 if (!strncmp(value,"null",4)) { item->type=cJSON_NULL; return value+4; }
00361 if (!strncmp(value,"false",5)) { item->type=cJSON_False; return value+5; }
00362 if (!strncmp(value,"true",4)) { item->type=cJSON_True; item->valueint=1; return value+4; }
00363 if (*value=='\"') { return parse_string(item,value); }
00364 if (*value=='-' || (*value>='0' && *value<='9')) { return parse_number(item,value); }
00365 if (*value=='[') { return parse_array(item,value); }
00366 if (*value=='{') { return parse_object(item,value); }
00367
00368 ep=value;return 0;
00369 }
00370
00371
00372 static char *print_value(cJSON *item,int depth,int fmt,printbuffer *p)
00373 {
00374 char *out=0;
00375 if (!item) return 0;
00376 if (p)
00377 {
00378 switch ((item->type)&255)
00379 {
00380 case cJSON_NULL: {out=ensure(p,5); if (out) strcpy(out,"null"); break;}
00381 case cJSON_False: {out=ensure(p,6); if (out) strcpy(out,"false"); break;}
00382 case cJSON_True: {out=ensure(p,5); if (out) strcpy(out,"true"); break;}
00383 case cJSON_Number: out=print_number(item,p);break;
00384 case cJSON_String: out=print_string(item,p);break;
00385 case cJSON_Array: out=print_array(item,depth,fmt,p);break;
00386 case cJSON_Object: out=print_object(item,depth,fmt,p);break;
00387 }
00388 }
00389 else
00390 {
00391 switch ((item->type)&255)
00392 {
00393 case cJSON_NULL: out=cJSON_strdup("null"); break;
00394 case cJSON_False: out=cJSON_strdup("false");break;
00395 case cJSON_True: out=cJSON_strdup("true"); break;
00396 case cJSON_Number: out=print_number(item,0);break;
00397 case cJSON_String: out=print_string(item,0);break;
00398 case cJSON_Array: out=print_array(item,depth,fmt,0);break;
00399 case cJSON_Object: out=print_object(item,depth,fmt,0);break;
00400 }
00401 }
00402 return out;
00403 }
00404
00405
00406 static const char *parse_array(cJSON *item,const char *value)
00407 {
00408 cJSON *child;
00409 if (*value!='[') {ep=value;return 0;}
00410
00411 item->type=cJSON_Array;
00412 value=skip(value+1);
00413 if (*value==']') return value+1;
00414
00415 item->child=child=cJSON_New_Item();
00416 if (!item->child) return 0;
00417 value=skip(parse_value(child,skip(value)));
00418 if (!value) return 0;
00419
00420 while (*value==',')
00421 {
00422 cJSON *new_item;
00423 if (!(new_item=cJSON_New_Item())) return 0;
00424 child->next=new_item;new_item->prev=child;child=new_item;
00425 value=skip(parse_value(child,skip(value+1)));
00426 if (!value) return 0;
00427 }
00428
00429 if (*value==']') return value+1;
00430 ep=value;return 0;
00431 }
00432
00433
00434 static char *print_array(cJSON *item,int depth,int fmt,printbuffer *p)
00435 {
00436 char **entries;
00437 char *out=0,*ptr,*ret;int len=5;
00438 cJSON *child=item->child;
00439 int numentries=0,i=0,fail=0;
00440 size_t tmplen=0;
00441
00442
00443 while (child) numentries++,child=child->next;
00444
00445 if (!numentries)
00446 {
00447 if (p) out=ensure(p,3);
00448 else out=(char*)cJSON_malloc(3);
00449 if (out) strcpy(out,"[]");
00450 return out;
00451 }
00452
00453 if (p)
00454 {
00455
00456 i=p->offset;
00457 ptr=ensure(p,1);if (!ptr) return 0; *ptr='['; p->offset++;
00458 child=item->child;
00459 while (child && !fail)
00460 {
00461 print_value(child,depth+1,fmt,p);
00462 p->offset=update(p);
00463 if (child->next) {len=fmt?2:1;ptr=ensure(p,len+1);if (!ptr) return 0;*ptr++=',';if(fmt)*ptr++=' ';*ptr=0;p->offset+=len;}
00464 child=child->next;
00465 }
00466 ptr=ensure(p,2);if (!ptr) return 0; *ptr++=']';*ptr=0;
00467 out=(p->buffer)+i;
00468 }
00469 else
00470 {
00471
00472 entries=(char**)cJSON_malloc(numentries*sizeof(char*));
00473 if (!entries) return 0;
00474 memset(entries,0,numentries*sizeof(char*));
00475
00476 child=item->child;
00477 while (child && !fail)
00478 {
00479 ret=print_value(child,depth+1,fmt,0);
00480 entries[i++]=ret;
00481 if (ret) len+=strlen(ret)+2+(fmt?1:0); else fail=1;
00482 child=child->next;
00483 }
00484
00485
00486 if (!fail) out=(char*)cJSON_malloc(len);
00487
00488 if (!out) fail=1;
00489
00490
00491 if (fail)
00492 {
00493 for (i=0;i<numentries;i++) if (entries[i]) cJSON_free(entries[i]);
00494 cJSON_free(entries);
00495 return 0;
00496 }
00497
00498
00499 *out='[';
00500 ptr=out+1;*ptr=0;
00501 for (i=0;i<numentries;i++)
00502 {
00503 tmplen=strlen(entries[i]);memcpy(ptr,entries[i],tmplen);ptr+=tmplen;
00504 if (i!=numentries-1) {*ptr++=',';if(fmt)*ptr++=' ';*ptr=0;}
00505 cJSON_free(entries[i]);
00506 }
00507 cJSON_free(entries);
00508 *ptr++=']';*ptr++=0;
00509 }
00510 return out;
00511 }
00512
00513
00514 static const char *parse_object(cJSON *item,const char *value)
00515 {
00516 cJSON *child;
00517 if (*value!='{') {ep=value;return 0;}
00518
00519 item->type=cJSON_Object;
00520 value=skip(value+1);
00521 if (*value=='}') return value+1;
00522
00523 item->child=child=cJSON_New_Item();
00524 if (!item->child) return 0;
00525 value=skip(parse_string(child,skip(value)));
00526 if (!value) return 0;
00527 child->string=child->valuestring;child->valuestring=0;
00528 if (*value!=':') {ep=value;return 0;}
00529 value=skip(parse_value(child,skip(value+1)));
00530 if (!value) return 0;
00531
00532 while (*value==',')
00533 {
00534 cJSON *new_item;
00535 if (!(new_item=cJSON_New_Item())) return 0;
00536 child->next=new_item;new_item->prev=child;child=new_item;
00537 value=skip(parse_string(child,skip(value+1)));
00538 if (!value) return 0;
00539 child->string=child->valuestring;child->valuestring=0;
00540 if (*value!=':') {ep=value;return 0;}
00541 value=skip(parse_value(child,skip(value+1)));
00542 if (!value) return 0;
00543 }
00544
00545 if (*value=='}') return value+1;
00546 ep=value;return 0;
00547 }
00548
00549
00550 static char *print_object(cJSON *item,int depth,int fmt,printbuffer *p)
00551 {
00552 char **entries=0,**names=0;
00553 char *out=0,*ptr,*ret,*str;int len=7,i=0,j;
00554 cJSON *child=item->child;
00555 int numentries=0,fail=0;
00556 size_t tmplen=0;
00557
00558 while (child) numentries++,child=child->next;
00559
00560 if (!numentries)
00561 {
00562 if (p) out=ensure(p,fmt?depth+4:3);
00563 else out=(char*)cJSON_malloc(fmt?depth+4:3);
00564 if (!out) return 0;
00565 ptr=out;*ptr++='{';
00566 if (fmt) {*ptr++='\n';for (i=0;i<depth-1;i++) *ptr++='\t';}
00567 *ptr++='}';*ptr++=0;
00568 return out;
00569 }
00570 if (p)
00571 {
00572
00573 i=p->offset;
00574 len=fmt?2:1; ptr=ensure(p,len+1); if (!ptr) return 0;
00575 *ptr++='{'; if (fmt) *ptr++='\n'; *ptr=0; p->offset+=len;
00576 child=item->child;depth++;
00577 while (child)
00578 {
00579 if (fmt)
00580 {
00581 ptr=ensure(p,depth); if (!ptr) return 0;
00582 for (j=0;j<depth;j++) *ptr++='\t';
00583 p->offset+=depth;
00584 }
00585 print_string_ptr(child->string,p);
00586 p->offset=update(p);
00587
00588 len=fmt?2:1;
00589 ptr=ensure(p,len); if (!ptr) return 0;
00590 *ptr++=':';if (fmt) *ptr++='\t';
00591 p->offset+=len;
00592
00593 print_value(child,depth,fmt,p);
00594 p->offset=update(p);
00595
00596 len=(fmt?1:0)+(child->next?1:0);
00597 ptr=ensure(p,len+1); if (!ptr) return 0;
00598 if (child->next) *ptr++=',';
00599 if (fmt) *ptr++='\n';*ptr=0;
00600 p->offset+=len;
00601 child=child->next;
00602 }
00603 ptr=ensure(p,fmt?(depth+1):2); if (!ptr) return 0;
00604 if (fmt) for (i=0;i<depth-1;i++) *ptr++='\t';
00605 *ptr++='}';*ptr=0;
00606 out=(p->buffer)+i;
00607 }
00608 else
00609 {
00610
00611 entries=(char**)cJSON_malloc(numentries*sizeof(char*));
00612 if (!entries) return 0;
00613 names=(char**)cJSON_malloc(numentries*sizeof(char*));
00614 if (!names) {cJSON_free(entries);return 0;}
00615 memset(entries,0,sizeof(char*)*numentries);
00616 memset(names,0,sizeof(char*)*numentries);
00617
00618
00619 child=item->child;depth++;if (fmt) len+=depth;
00620 while (child)
00621 {
00622 names[i]=str=print_string_ptr(child->string,0);
00623 entries[i++]=ret=print_value(child,depth,fmt,0);
00624 if (str && ret) len+=strlen(ret)+strlen(str)+2+(fmt?2+depth:0); else fail=1;
00625 child=child->next;
00626 }
00627
00628
00629 if (!fail) out=(char*)cJSON_malloc(len);
00630 if (!out) fail=1;
00631
00632
00633 if (fail)
00634 {
00635 for (i=0;i<numentries;i++) {if (names[i]) cJSON_free(names[i]);if (entries[i]) cJSON_free(entries[i]);}
00636 cJSON_free(names);cJSON_free(entries);
00637 return 0;
00638 }
00639
00640
00641 *out='{';ptr=out+1;if (fmt)*ptr++='\n';*ptr=0;
00642 for (i=0;i<numentries;i++)
00643 {
00644 if (fmt) for (j=0;j<depth;j++) *ptr++='\t';
00645 tmplen=strlen(names[i]);memcpy(ptr,names[i],tmplen);ptr+=tmplen;
00646 *ptr++=':';if (fmt) *ptr++='\t';
00647 strcpy(ptr,entries[i]);ptr+=strlen(entries[i]);
00648 if (i!=numentries-1) *ptr++=',';
00649 if (fmt) *ptr++='\n';*ptr=0;
00650 cJSON_free(names[i]);cJSON_free(entries[i]);
00651 }
00652
00653 cJSON_free(names);cJSON_free(entries);
00654 if (fmt) for (i=0;i<depth-1;i++) *ptr++='\t';
00655 *ptr++='}';*ptr++=0;
00656 }
00657 return out;
00658 }
00659
00660
00661 int cJSON_GetArraySize(cJSON *array) {cJSON *c=array->child;int i=0;while(c)i++,c=c->next;return i;}
00662 cJSON *cJSON_GetArrayItem(cJSON *array,int item) {cJSON *c=array->child; while (c && item>0) item--,c=c->next; return c;}
00663 cJSON *cJSON_GetObjectItem(cJSON *object,const char *string) {cJSON *c=object->child; while (c && cJSON_strcasecmp(c->string,string)) c=c->next; return c;}
00664
00665
00666 static void suffix_object(cJSON *prev,cJSON *item) {prev->next=item;item->prev=prev;}
00667
00668 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;}
00669
00670
00671 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);}}
00672 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);}
00673 void cJSON_AddItemToObjectCS(cJSON *object,const char *string,cJSON *item) {if (!item) return; if (!(item->type&cJSON_StringIsConst) && item->string) cJSON_free(item->string);item->string=(char*)string;item->type|=cJSON_StringIsConst;cJSON_AddItemToArray(object,item);}
00674 void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item) {cJSON_AddItemToArray(array,create_reference(item));}
00675 void cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item) {cJSON_AddItemToObject(object,string,create_reference(item));}
00676
00677 cJSON *cJSON_DetachItemFromArray(cJSON *array,int which) {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) return 0;
00678 if (c->prev) c->prev->next=c->next;if (c->next) c->next->prev=c->prev;if (c==array->child) array->child=c->next;c->prev=c->next=0;return c;}
00679 void cJSON_DeleteItemFromArray(cJSON *array,int which) {cJSON_Delete(cJSON_DetachItemFromArray(array,which));}
00680 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;}
00681 void cJSON_DeleteItemFromObject(cJSON *object,const char *string) {cJSON_Delete(cJSON_DetachItemFromObject(object,string));}
00682
00683
00684 void cJSON_InsertItemInArray(cJSON *array,int which,cJSON *newitem) {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) {cJSON_AddItemToArray(array,newitem);return;}
00685 newitem->next=c;newitem->prev=c->prev;c->prev=newitem;if (c==array->child) array->child=newitem; else newitem->prev->next=newitem;}
00686 void cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem) {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) return;
00687 newitem->next=c->next;newitem->prev=c->prev;if (newitem->next) newitem->next->prev=newitem;
00688 if (c==array->child) array->child=newitem; else newitem->prev->next=newitem;c->next=c->prev=0;cJSON_Delete(c);}
00689 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);}}
00690
00691
00692 cJSON *cJSON_CreateNull(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_NULL;return item;}
00693 cJSON *cJSON_CreateTrue(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_True;return item;}
00694 cJSON *cJSON_CreateFalse(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_False;return item;}
00695 cJSON *cJSON_CreateBool(int b) {cJSON *item=cJSON_New_Item();if(item)item->type=b?cJSON_True:cJSON_False;return item;}
00696 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;}
00697 cJSON *cJSON_CreateString(const char *string) {cJSON *item=cJSON_New_Item();if(item){item->type=cJSON_String;item->valuestring=cJSON_strdup(string);}return item;}
00698 cJSON *cJSON_CreateArray(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_Array;return item;}
00699 cJSON *cJSON_CreateObject(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_Object;return item;}
00700
00701
00702 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;}
00703 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;}
00704 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;}
00705 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;}
00706
00707
00708 cJSON *cJSON_Duplicate(cJSON *item,int recurse)
00709 {
00710 cJSON *newitem,*cptr,*nptr=0,*newchild;
00711
00712 if (!item) return 0;
00713
00714 newitem=cJSON_New_Item();
00715 if (!newitem) return 0;
00716
00717 newitem->type=item->type&(~cJSON_IsReference),newitem->valueint=item->valueint,newitem->valuedouble=item->valuedouble;
00718 if (item->valuestring) {newitem->valuestring=cJSON_strdup(item->valuestring); if (!newitem->valuestring) {cJSON_Delete(newitem);return 0;}}
00719 if (item->string) {newitem->string=cJSON_strdup(item->string); if (!newitem->string) {cJSON_Delete(newitem);return 0;}}
00720
00721 if (!recurse) return newitem;
00722
00723 cptr=item->child;
00724 while (cptr)
00725 {
00726 newchild=cJSON_Duplicate(cptr,1);
00727 if (!newchild) {cJSON_Delete(newitem);return 0;}
00728 if (nptr) {nptr->next=newchild,newchild->prev=nptr;nptr=newchild;}
00729 else {newitem->child=newchild;nptr=newchild;}
00730 cptr=cptr->next;
00731 }
00732 return newitem;
00733 }
00734
00735 void cJSON_Minify(char *json)
00736 {
00737 char *into=json;
00738 while (*json)
00739 {
00740 if (*json==' ') json++;
00741 else if (*json=='\t') json++;
00742 else if (*json=='\r') json++;
00743 else if (*json=='\n') json++;
00744 else if (*json=='/' && json[1]=='/') while (*json && *json!='\n') json++;
00745 else if (*json=='/' && json[1]=='*') {while (*json && !(*json=='*' && json[1]=='/')) json++;json+=2;}
00746 else if (*json=='\"'){*into++=*json++;while (*json && *json!='\"'){if (*json=='\\') *into++=*json++;*into++=*json++;}*into++=*json++;}
00747 else *into++=*json++;
00748 }
00749 *into=0;
00750 }