| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#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) { |
| 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 |
|
| 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 |
|
| 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 |
|
| 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++; |
| 102 |
if (*num=='0') num++; |
| 103 |
if (*num>='1' && *num<='9') do n=(n*10.0)+(*num++ -'0'); while (*num>='0' && *num<='9'); |
| 104 |
if (*num=='.' && num[1]>='0' && num[1]<='9') {num++; do n=(n*10.0)+(*num++ -'0'),scale--; while (*num>='0' && *num<='9');} |
| 105 |
if (*num=='e' || *num=='E') |
| 106 |
{ num++;if (*num=='+') num++; else if (*num=='-') signsubscale=-1,num++; |
| 107 |
while (*num>='0' && *num<='9') subscale=(subscale*10)+(*num++ - '0'); |
| 108 |
} |
| 109 |
|
| 110 |
n=sign*n*pow(10.0,(scale+subscale*signsubscale)); |
| 111 |
|
| 112 |
item->valuedouble=n; |
| 113 |
item->valueint=(int)n; |
| 114 |
item->type=cJSON_Number; |
| 115 |
return num; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 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); |
| 126 |
if (str) sprintf(str,"%d",item->valueint); |
| 127 |
} |
| 128 |
else |
| 129 |
{ |
| 130 |
str=(char*)cJSON_malloc(64); |
| 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 |
|
| 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;} |
| 160 |
|
| 161 |
while (*ptr!='\"' && *ptr && ++len) if (*ptr++ == '\\') ptr++; |
| 162 |
|
| 163 |
out=(char*)cJSON_malloc(len+1); |
| 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': |
| 181 |
uc=parse_hex4(ptr+1);ptr+=4; |
| 182 |
|
| 183 |
if ((uc>=0xDC00 && uc<=0xDFFF) || uc==0) break; |
| 184 |
|
| 185 |
if (uc>=0xD800 && uc<=0xDBFF) |
| 186 |
{ |
| 187 |
if (ptr[1]!='\\' || ptr[2]!='u') break; |
| 188 |
uc2=parse_hex4(ptr+3);ptr+=6; |
| 189 |
if (uc2<0xDC00 || uc2>0xDFFF) break; |
| 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 |
|
| 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; |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
*ptr2++='\"';*ptr2++=0; |
| 248 |
return out; |
| 249 |
} |
| 250 |
|
| 251 |
static char *print_string(cJSON *item) {return print_string_ptr(item->valuestring);} |
| 252 |
|
| 253 |
|
| 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 |
|
| 262 |
static const char *skip(const char *in) {while (in && *in && (unsigned char)*in<=32) in++; return in;} |
| 263 |
|
| 264 |
|
| 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; |
| 271 |
|
| 272 |
end=parse_value(c,skip(value)); |
| 273 |
if (!end) {cJSON_Delete(c);return 0;} |
| 274 |
|
| 275 |
|
| 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 |
|
| 281 |
cJSON *cJSON_Parse(const char *value) {return cJSON_ParseWithOpts(value,0,0);} |
| 282 |
|
| 283 |
|
| 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 |
|
| 288 |
static const char *parse_value(cJSON *item,const char *value) |
| 289 |
{ |
| 290 |
if (!value) return 0; |
| 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; |
| 300 |
} |
| 301 |
|
| 302 |
|
| 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 |
|
| 321 |
static const char *parse_array(cJSON *item,const char *value) |
| 322 |
{ |
| 323 |
cJSON *child; |
| 324 |
if (*value!='[') {ep=value;return 0;} |
| 325 |
|
| 326 |
item->type=cJSON_Array; |
| 327 |
value=skip(value+1); |
| 328 |
if (*value==']') return value+1; |
| 329 |
|
| 330 |
item->child=child=cJSON_New_Item(); |
| 331 |
if (!item->child) return 0; |
| 332 |
value=skip(parse_value(child,skip(value))); |
| 333 |
if (!value) return 0; |
| 334 |
|
| 335 |
while (*value==',') |
| 336 |
{ |
| 337 |
cJSON *new_item; |
| 338 |
if (!(new_item=cJSON_New_Item())) return 0; |
| 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; |
| 342 |
} |
| 343 |
|
| 344 |
if (*value==']') return value+1; |
| 345 |
ep=value;return 0; |
| 346 |
} |
| 347 |
|
| 348 |
|
| 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 |
|
| 357 |
while (child) numentries++,child=child->next; |
| 358 |
|
| 359 |
if (!numentries) |
| 360 |
{ |
| 361 |
out=(char*)cJSON_malloc(3); |
| 362 |
if (out) strcpy(out,"[]"); |
| 363 |
return out; |
| 364 |
} |
| 365 |
|
| 366 |
entries=(char**)cJSON_malloc(numentries*sizeof(char*)); |
| 367 |
if (!entries) return 0; |
| 368 |
memset(entries,0,numentries*sizeof(char*)); |
| 369 |
|
| 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 |
|
| 380 |
if (!fail) out=(char*)cJSON_malloc(len); |
| 381 |
|
| 382 |
if (!out) fail=1; |
| 383 |
|
| 384 |
|
| 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 |
|
| 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 |
|
| 407 |
static const char *parse_object(cJSON *item,const char *value) |
| 408 |
{ |
| 409 |
cJSON *child; |
| 410 |
if (*value!='{') {ep=value;return 0;} |
| 411 |
|
| 412 |
item->type=cJSON_Object; |
| 413 |
value=skip(value+1); |
| 414 |
if (*value=='}') return value+1; |
| 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;} |
| 422 |
value=skip(parse_value(child,skip(value+1))); |
| 423 |
if (!value) return 0; |
| 424 |
|
| 425 |
while (*value==',') |
| 426 |
{ |
| 427 |
cJSON *new_item; |
| 428 |
if (!(new_item=cJSON_New_Item())) return 0; |
| 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;} |
| 434 |
value=skip(parse_value(child,skip(value+1))); |
| 435 |
if (!value) return 0; |
| 436 |
} |
| 437 |
|
| 438 |
if (*value=='}') return value+1; |
| 439 |
ep=value;return 0; |
| 440 |
} |
| 441 |
|
| 442 |
|
| 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 |
|
| 450 |
while (child) numentries++,child=child->next; |
| 451 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 480 |
if (!fail) out=(char*)cJSON_malloc(len); |
| 481 |
if (!out) fail=1; |
| 482 |
|
| 483 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 518 |
static void suffix_object(cJSON *prev,cJSON *item) {prev->next=item;item->prev=prev;} |
| 519 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 561 |
cJSON *cJSON_Duplicate(cJSON *item,int recurse) |
| 562 |
{ |
| 563 |
cJSON *newitem,*cptr,*nptr=0,*newchild; |
| 564 |
|
| 565 |
if (!item) return 0; |
| 566 |
|
| 567 |
newitem=cJSON_New_Item(); |
| 568 |
if (!newitem) return 0; |
| 569 |
|
| 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 |
|
| 574 |
if (!recurse) return newitem; |
| 575 |
|
| 576 |
cptr=item->child; |
| 577 |
while (cptr) |
| 578 |
{ |
| 579 |
newchild=cJSON_Duplicate(cptr,1); |
| 580 |
if (!newchild) {cJSON_Delete(newitem);return 0;} |
| 581 |
if (nptr) {nptr->next=newchild,newchild->prev=nptr;nptr=newchild;} |
| 582 |
else {newitem->child=newchild;nptr=newchild;} |
| 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++; |
| 595 |
else if (*json=='\r') json++; |
| 596 |
else if (*json=='\n') json++; |
| 597 |
else if (*json=='/' && json[1]=='/') while (*json && *json!='\n') json++; |
| 598 |
else if (*json=='/' && json[1]=='*') {while (*json && !(*json=='*' && json[1]=='/')) json++;json+=2;} |
| 599 |
else if (*json=='\"'){*into++=*json++;while (*json && *json!='\"'){if (*json=='\\') *into++=*json++;*into++=*json++;}*into++=*json++;} |
| 600 |
else *into++=*json++; |
| 601 |
} |
| 602 |
*into=0; |
| 603 |
} |