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