diff options
| author | Mark Lobodzinski <mark@lunarg.com> | 2017-01-26 13:34:13 -0700 |
|---|---|---|
| committer | Mark Lobodzinski <mark@lunarg.com> | 2017-01-26 15:45:51 -0700 |
| commit | 85dbd823c410a2817b077e2c6d1ee0cda7e247dd (patch) | |
| tree | 0043e88c6dcea7537d836c1fdb64556b09952a6c /loader/cJSON.c | |
| parent | 5492b5039192cb9cae396a96e1fd7cff3852f6da (diff) | |
| download | usermoji-85dbd823c410a2817b077e2c6d1ee0cda7e247dd.tar.xz | |
repo: Clang-format LVL source files using Google
Switch clang-format standard from the LLVM style to the
Google style for more consistency.
Change-Id: I247c4abc275d7873a91522e1e234198adaa24033
Diffstat (limited to 'loader/cJSON.c')
| -rw-r--r-- | loader/cJSON.c | 604 |
1 files changed, 243 insertions, 361 deletions
diff --git a/loader/cJSON.c b/loader/cJSON.c index 9c800e18..a7671c4a 100644 --- a/loader/cJSON.c +++ b/loader/cJSON.c @@ -47,8 +47,7 @@ static char *cJSON_strdup(const char *str) { char *copy; len = strlen(str) + 1; - if (!(copy = (char *)cJSON_malloc(len))) - return 0; + if (!(copy = (char *)cJSON_malloc(len))) return 0; memcpy(copy, str, len); return copy; } @@ -67,8 +66,7 @@ void cJSON_InitHooks(cJSON_Hooks *hooks) { /* Internal constructor. */ static cJSON *cJSON_New_Item(void) { cJSON *node = (cJSON *)cJSON_malloc(sizeof(cJSON)); - if (node) - memset(node, 0, sizeof(cJSON)); + if (node) memset(node, 0, sizeof(cJSON)); return node; } @@ -77,12 +75,9 @@ void cJSON_Delete(cJSON *c) { cJSON *next; while (c) { next = c->next; - if (!(c->type & cJSON_IsReference) && c->child) - cJSON_Delete(c->child); - if (!(c->type & cJSON_IsReference) && c->valuestring) - cJSON_free(c->valuestring); - if (!(c->type & cJSON_StringIsConst) && c->string) - cJSON_free(c->string); + if (!(c->type & cJSON_IsReference) && c->child) cJSON_Delete(c->child); + if (!(c->type & cJSON_IsReference) && c->valuestring) cJSON_free(c->valuestring); + if (!(c->type & cJSON_StringIsConst) && c->string) cJSON_free(c->string); cJSON_free(c); c = next; } @@ -96,12 +91,9 @@ static const char *parse_number(cJSON *item, const char *num) { double n = 0, sign = 1, scale = 0; int subscale = 0, signsubscale = 1; - if (*num == '-') - sign = -1, num++; /* Has sign? */ - if (*num == '0') - num++; /* is zero */ - if (*num >= '1' && *num <= '9') - do + if (*num == '-') sign = -1, num++; /* Has sign? */ + if (*num == '0') num++; /* is zero */ + if (*num >= '1' && *num <= '9') do n = (n * 10.0) + (*num++ - '0'); while (*num >= '0' && *num <= '9'); /* Number? */ if (*num == '.' && num[1] >= '0' && num[1] <= '9') { @@ -116,9 +108,8 @@ static const char *parse_number(cJSON *item, const char *num) { if (*num == '+') num++; else if (*num == '-') - signsubscale = -1, num++; /* With sign? */ - while (*num >= '0' && *num <= '9') - subscale = (subscale * 10) + (*num++ - '0'); /* Number? */ + signsubscale = -1, num++; /* With sign? */ + while (*num >= '0' && *num <= '9') subscale = (subscale * 10) + (*num++ - '0'); /* Number? */ } n = sign * n * pow(10.0, (scale + subscale * signsubscale)); /* number = +/- @@ -150,11 +141,9 @@ typedef struct { static char *ensure(printbuffer *p, size_t needed) { char *newbuffer; size_t newsize; - if (!p || !p->buffer) - return 0; + if (!p || !p->buffer) return 0; needed += p->offset; - if (needed <= p->length) - return p->buffer + p->offset; + if (needed <= p->length) return p->buffer + p->offset; newsize = pow2gt(needed); newbuffer = (char *)cJSON_malloc(newsize); @@ -163,8 +152,7 @@ static char *ensure(printbuffer *p, size_t needed) { p->length = 0, p->buffer = 0; return 0; } - if (newbuffer) - memcpy(newbuffer, p->buffer, p->length); + if (newbuffer) memcpy(newbuffer, p->buffer, p->length); cJSON_free(p->buffer); p->length = newsize; p->buffer = newbuffer; @@ -173,8 +161,7 @@ static char *ensure(printbuffer *p, size_t needed) { static size_t update(printbuffer *p) { char *str; - if (!p || !p->buffer) - return 0; + if (!p || !p->buffer) return 0; str = p->buffer + p->offset; return p->offset + strlen(str); } @@ -188,15 +175,13 @@ static char *print_number(cJSON *item, printbuffer *p) { str = ensure(p, 2); else str = (char *)cJSON_malloc(2); /* special case for 0. */ - if (str) - strcpy(str, "0"); + if (str) strcpy(str, "0"); } else if (fabs(((double)item->valueint) - d) <= DBL_EPSILON && d <= INT_MAX && d >= INT_MIN) { if (p) str = ensure(p, 21); else str = (char *)cJSON_malloc(21); /* 2^64+1 can be represented in 21 chars. */ - if (str) - sprintf(str, "%d", item->valueint); + if (str) sprintf(str, "%d", item->valueint); } else { if (p) str = ensure(p, 64); @@ -271,12 +256,10 @@ static const char *parse_string(cJSON *item, const char *str) { } /* not a string! */ while (*ptr != '\"' && *ptr && ++len) - if (*ptr++ == '\\') - ptr++; /* Skip escaped quotes. */ + if (*ptr++ == '\\') ptr++; /* Skip escaped quotes. */ out = (char *)cJSON_malloc(len + 1); /* This is how long we need for the string, roughly. */ - if (!out) - return 0; + if (!out) return 0; ptr = str + 1; ptr2 = out; @@ -286,73 +269,69 @@ static const char *parse_string(cJSON *item, const char *str) { else { ptr++; switch (*ptr) { - case 'b': - *ptr2++ = '\b'; - break; - case 'f': - *ptr2++ = '\f'; - break; - case 'n': - *ptr2++ = '\n'; - break; - case 'r': - *ptr2++ = '\r'; - break; - case 't': - *ptr2++ = '\t'; - break; - case 'u': /* transcode utf16 to utf8. */ - uc = parse_hex4(ptr + 1); - ptr += 4; /* get the unicode char. */ - - if ((uc >= 0xDC00 && uc <= 0xDFFF) || uc == 0) - break; /* check for invalid. */ - - if (uc >= 0xD800 && uc <= 0xDBFF) /* UTF16 surrogate pairs. */ - { - if (ptr[1] != '\\' || ptr[2] != 'u') - break; /* missing second-half of surrogate. */ - uc2 = parse_hex4(ptr + 3); - ptr += 6; - if (uc2 < 0xDC00 || uc2 > 0xDFFF) - break; /* invalid second-half of surrogate. */ - uc = 0x10000 + (((uc & 0x3FF) << 10) | (uc2 & 0x3FF)); - } - - len = 4; - if (uc < 0x80) - len = 1; - else if (uc < 0x800) - len = 2; - else if (uc < 0x10000) - len = 3; - ptr2 += len; - - switch (len) { - case 4: - *--ptr2 = ((uc | 0x80) & 0xBF); - uc >>= 6; - case 3: - *--ptr2 = ((uc | 0x80) & 0xBF); - uc >>= 6; - case 2: - *--ptr2 = ((uc | 0x80) & 0xBF); - uc >>= 6; - case 1: - *--ptr2 = ((unsigned char)uc | firstByteMark[len]); - } - ptr2 += len; - break; - default: - *ptr2++ = *ptr; - break; + case 'b': + *ptr2++ = '\b'; + break; + case 'f': + *ptr2++ = '\f'; + break; + case 'n': + *ptr2++ = '\n'; + break; + case 'r': + *ptr2++ = '\r'; + break; + case 't': + *ptr2++ = '\t'; + break; + case 'u': /* transcode utf16 to utf8. */ + uc = parse_hex4(ptr + 1); + ptr += 4; /* get the unicode char. */ + + if ((uc >= 0xDC00 && uc <= 0xDFFF) || uc == 0) break; /* check for invalid. */ + + if (uc >= 0xD800 && uc <= 0xDBFF) /* UTF16 surrogate pairs. */ + { + if (ptr[1] != '\\' || ptr[2] != 'u') break; /* missing second-half of surrogate. */ + uc2 = parse_hex4(ptr + 3); + ptr += 6; + if (uc2 < 0xDC00 || uc2 > 0xDFFF) break; /* invalid second-half of surrogate. */ + uc = 0x10000 + (((uc & 0x3FF) << 10) | (uc2 & 0x3FF)); + } + + len = 4; + if (uc < 0x80) + len = 1; + else if (uc < 0x800) + len = 2; + else if (uc < 0x10000) + len = 3; + ptr2 += len; + + switch (len) { + case 4: + *--ptr2 = ((uc | 0x80) & 0xBF); + uc >>= 6; + case 3: + *--ptr2 = ((uc | 0x80) & 0xBF); + uc >>= 6; + case 2: + *--ptr2 = ((uc | 0x80) & 0xBF); + uc >>= 6; + case 1: + *--ptr2 = ((unsigned char)uc | firstByteMark[len]); + } + ptr2 += len; + break; + default: + *ptr2++ = *ptr; + break; } ptr++; } } *ptr2 = 0; - if (*ptr == '\"') - ptr++; + if (*ptr == '\"') ptr++; item->valuestring = out; item->type = cJSON_String; return ptr; @@ -366,16 +345,14 @@ static char *print_string_ptr(const char *str, printbuffer *p) { size_t len = 0, flag = 0; unsigned char token; - for (ptr = str; *ptr; ptr++) - flag |= ((*ptr > 0 && *ptr < 32) || (*ptr == '\"') || (*ptr == '\\')) ? 1 : 0; + for (ptr = str; *ptr; ptr++) flag |= ((*ptr > 0 && *ptr < 32) || (*ptr == '\"') || (*ptr == '\\')) ? 1 : 0; if (!flag) { len = ptr - str; if (p) out = ensure(p, len + 3); else out = (char *)cJSON_malloc(len + 3); - if (!out) - return 0; + if (!out) return 0; ptr2 = out; *ptr2++ = '\"'; strcpy(ptr2, str); @@ -389,8 +366,7 @@ static char *print_string_ptr(const char *str, printbuffer *p) { out = ensure(p, 3); else out = (char *)cJSON_malloc(3); - if (!out) - return 0; + if (!out) return 0; strcpy(out, "\"\""); return out; } @@ -407,8 +383,7 @@ static char *print_string_ptr(const char *str, printbuffer *p) { out = ensure(p, len + 3); else out = (char *)cJSON_malloc(len + 3); - if (!out) - return 0; + if (!out) return 0; ptr2 = out; ptr = str; @@ -418,31 +393,31 @@ static char *print_string_ptr(const char *str, printbuffer *p) { *ptr2++ = *ptr++; else { switch (token = *ptr++) { - case '\\': - *ptr2++ = '\\'; - break; - case '\"': - *ptr2++ = '\"'; - break; - case '\b': - *ptr2++ = '\b'; - break; - case '\f': - *ptr2++ = '\f'; - break; - case '\n': - *ptr2++ = '\n'; - break; - case '\r': - *ptr2++ = '\r'; - break; - case '\t': - *ptr2++ = '\t'; - break; - default: - sprintf(ptr2, "u%04x", token); - ptr2 += 5; - break; /* escape and print */ + case '\\': + *ptr2++ = '\\'; + break; + case '\"': + *ptr2++ = '\"'; + break; + case '\b': + *ptr2++ = '\b'; + break; + case '\f': + *ptr2++ = '\f'; + break; + case '\n': + *ptr2++ = '\n'; + break; + case '\r': + *ptr2++ = '\r'; + break; + case '\t': + *ptr2++ = '\t'; + break; + default: + sprintf(ptr2, "u%04x", token); + ptr2 += 5; + break; /* escape and print */ } } } @@ -463,8 +438,7 @@ static char *print_object(cJSON *item, int depth, int fmt, printbuffer *p); /* Utility to jump whitespace and cr/lf */ static const char *skip(const char *in) { - while (in && *in && (unsigned char)*in <= 32) - in++; + while (in && *in && (unsigned char)*in <= 32) in++; return in; } @@ -473,8 +447,7 @@ cJSON *cJSON_ParseWithOpts(const char *value, const char **return_parse_end, int const char *end = 0; cJSON *c = cJSON_New_Item(); ep = 0; - if (!c) - return 0; /* memory fail */ + if (!c) return 0; /* memory fail */ end = parse_value(c, skip(value)); if (!end) { @@ -492,8 +465,7 @@ cJSON *cJSON_ParseWithOpts(const char *value, const char **return_parse_end, int return 0; } } - if (return_parse_end) - *return_parse_end = end; + if (return_parse_end) *return_parse_end = end; return c; } /* Default options for cJSON_Parse */ @@ -513,8 +485,7 @@ char *cJSON_PrintBuffered(cJSON *item, int prebuffer, int fmt) { /* Parser core - when encountering text, process appropriately. */ static const char *parse_value(cJSON *item, const char *value) { - if (!value) - return 0; /* Fail on null. */ + if (!value) return 0; /* Fail on null. */ if (!strncmp(value, "null", 4)) { item->type = cJSON_NULL; return value + 4; @@ -548,64 +519,60 @@ static const char *parse_value(cJSON *item, const char *value) { /* Render a value to text. */ static char *print_value(cJSON *item, int depth, int fmt, printbuffer *p) { char *out = 0; - if (!item) - return 0; + if (!item) return 0; if (p) { switch ((item->type) & 255) { - case cJSON_NULL: { - out = ensure(p, 5); - if (out) - strcpy(out, "null"); - break; - } - case cJSON_False: { - out = ensure(p, 6); - if (out) - strcpy(out, "false"); - break; - } - case cJSON_True: { - out = ensure(p, 5); - if (out) - strcpy(out, "true"); - break; - } - case cJSON_Number: - out = print_number(item, p); - break; - case cJSON_String: - out = print_string(item, p); - break; - case cJSON_Array: - out = print_array(item, depth, fmt, p); - break; - case cJSON_Object: - out = print_object(item, depth, fmt, p); - break; + case cJSON_NULL: { + out = ensure(p, 5); + if (out) strcpy(out, "null"); + break; + } + case cJSON_False: { + out = ensure(p, 6); + if (out) strcpy(out, "false"); + break; + } + case cJSON_True: { + out = ensure(p, 5); + if (out) strcpy(out, "true"); + break; + } + case cJSON_Number: + out = print_number(item, p); + break; + case cJSON_String: + out = print_string(item, p); + break; + case cJSON_Array: + out = print_array(item, depth, fmt, p); + break; + case cJSON_Object: + out = print_object(item, depth, fmt, p); + break; } } else { switch ((item->type) & 255) { - case cJSON_NULL: - out = cJSON_strdup("null"); - break; - case cJSON_False: - out = cJSON_strdup("false"); - break; - case cJSON_True: - out = cJSON_strdup("true"); - break; - case cJSON_Number: - out = print_number(item, 0); - break; - case cJSON_String: - out = print_string(item, 0); - break; - case cJSON_Array: - out = print_array(item, depth, fmt, 0); - break; - case cJSON_Object: - out = print_object(item, depth, fmt, 0); - break; + case cJSON_NULL: + out = cJSON_strdup("null"); + break; + case cJSON_False: + out = cJSON_strdup("false"); + break; + case cJSON_True: + out = cJSON_strdup("true"); + break; + case cJSON_Number: + out = print_number(item, 0); + break; + case cJSON_String: + out = print_string(item, 0); + break; + case cJSON_Array: + out = print_array(item, depth, fmt, 0); + break; + case cJSON_Object: + out = print_object(item, depth, fmt, 0); + break; } } return out; @@ -621,30 +588,24 @@ static const char *parse_array(cJSON *item, const char *value) { item->type = cJSON_Array; value = skip(value + 1); - if (*value == ']') - return value + 1; /* empty array. */ + if (*value == ']') return value + 1; /* empty array. */ item->child = child = cJSON_New_Item(); - if (!item->child) - return 0; /* memory fail */ + if (!item->child) return 0; /* memory fail */ value = skip(parse_value(child, skip(value))); /* skip any spacing, get the value. */ - if (!value) - return 0; + if (!value) return 0; while (*value == ',') { cJSON *new_item; - if (!(new_item = cJSON_New_Item())) - return 0; /* memory fail */ + if (!(new_item = cJSON_New_Item())) return 0; /* memory fail */ child->next = new_item; new_item->prev = child; child = new_item; value = skip(parse_value(child, skip(value + 1))); - if (!value) - return 0; /* memory fail */ + if (!value) return 0; /* memory fail */ } - if (*value == ']') - return value + 1; /* end of array */ + if (*value == ']') return value + 1; /* end of array */ ep = value; return 0; /* malformed. */ } @@ -659,16 +620,14 @@ static char *print_array(cJSON *item, int depth, int fmt, printbuffer *p) { size_t tmplen = 0, i = 0; /* How many entries in the array? */ - while (child) - numentries++, child = child->next; + while (child) numentries++, child = child->next; /* Explicitly handle numentries==0 */ if (!numentries) { if (p) out = ensure(p, 3); else out = (char *)cJSON_malloc(3); - if (out) - strcpy(out, "[]"); + if (out) strcpy(out, "[]"); return out; } @@ -676,8 +635,7 @@ static char *print_array(cJSON *item, int depth, int fmt, printbuffer *p) { /* Compose the output array. */ i = p->offset; ptr = ensure(p, 1); - if (!ptr) - return 0; + if (!ptr) return 0; *ptr = '['; p->offset++; child = item->child; @@ -687,27 +645,23 @@ static char *print_array(cJSON *item, int depth, int fmt, printbuffer *p) { if (child->next) { len = fmt ? 2 : 1; ptr = ensure(p, len + 1); - if (!ptr) - return 0; + if (!ptr) return 0; *ptr++ = ','; - if (fmt) - *ptr++ = ' '; + if (fmt) *ptr++ = ' '; *ptr = 0; p->offset += len; } child = child->next; } ptr = ensure(p, 2); - if (!ptr) - return 0; + if (!ptr) return 0; *ptr++ = ']'; *ptr = 0; out = (p->buffer) + i; } else { /* Allocate an array to hold the values for each */ entries = (char **)cJSON_malloc(numentries * sizeof(char *)); - if (!entries) - return 0; + if (!entries) return 0; memset(entries, 0, numentries * sizeof(char *)); /* Retrieve all the results: */ child = item->child; @@ -722,17 +676,14 @@ static char *print_array(cJSON *item, int depth, int fmt, printbuffer *p) { } /* If we didn't fail, try to malloc the output string */ - if (!fail) - out = (char *)cJSON_malloc(len); + if (!fail) out = (char *)cJSON_malloc(len); /* If that fails, we fail. */ - if (!out) - fail = 1; + if (!out) fail = 1; /* Handle failure. */ if (fail) { for (j = 0; j < numentries; j++) - if (entries[j]) - cJSON_free(entries[j]); + if (entries[j]) cJSON_free(entries[j]); cJSON_free(entries); return 0; } @@ -747,8 +698,7 @@ static char *print_array(cJSON *item, int depth, int fmt, printbuffer *p) { ptr += tmplen; if (j != numentries - 1) { *ptr++ = ','; - if (fmt) - *ptr++ = ' '; + if (fmt) *ptr++ = ' '; *ptr = 0; } cJSON_free(entries[j]); @@ -770,15 +720,12 @@ static const char *parse_object(cJSON *item, const char *value) { item->type = cJSON_Object; value = skip(value + 1); - if (*value == '}') - return value + 1; /* empty array. */ + if (*value == '}') return value + 1; /* empty array. */ item->child = child = cJSON_New_Item(); - if (!item->child) - return 0; + if (!item->child) return 0; value = skip(parse_string(child, skip(value))); - if (!value) - return 0; + if (!value) return 0; child->string = child->valuestring; child->valuestring = 0; if (*value != ':') { @@ -786,19 +733,16 @@ static const char *parse_object(cJSON *item, const char *value) { return 0; } /* fail! */ value = skip(parse_value(child, skip(value + 1))); /* skip any spacing, get the value. */ - if (!value) - return 0; + if (!value) return 0; while (*value == ',') { cJSON *new_item; - if (!(new_item = cJSON_New_Item())) - return 0; /* memory fail */ + if (!(new_item = cJSON_New_Item())) return 0; /* memory fail */ child->next = new_item; new_item->prev = child; child = new_item; value = skip(parse_string(child, skip(value + 1))); - if (!value) - return 0; + if (!value) return 0; child->string = child->valuestring; child->valuestring = 0; if (*value != ':') { @@ -806,12 +750,10 @@ static const char *parse_object(cJSON *item, const char *value) { return 0; } /* fail! */ value = skip(parse_value(child, skip(value + 1))); /* skip any spacing, get the value. */ - if (!value) - return 0; + if (!value) return 0; } - if (*value == '}') - return value + 1; /* end of array */ + if (*value == '}') return value + 1; /* end of array */ ep = value; return 0; /* malformed. */ } @@ -825,22 +767,19 @@ static char *print_object(cJSON *item, int depth, int fmt, printbuffer *p) { int numentries = 0, fail = 0, k; size_t tmplen = 0, i = 0, len = 7; /* Count the number of entries. */ - while (child) - numentries++, child = child->next; + while (child) numentries++, child = child->next; /* Explicitly handle empty object case */ if (!numentries) { if (p) out = ensure(p, fmt ? depth + 4 : 3); else out = (char *)cJSON_malloc(fmt ? depth + 4 : 3); - if (!out) - return 0; + if (!out) return 0; ptr = out; *ptr++ = '{'; if (fmt) { *ptr++ = '\n'; - for (j = 0; j < depth - 1; j++) - *ptr++ = '\t'; + for (j = 0; j < depth - 1; j++) *ptr++ = '\t'; } *ptr++ = '}'; *ptr++ = 0; @@ -851,11 +790,9 @@ static char *print_object(cJSON *item, int depth, int fmt, printbuffer *p) { i = p->offset; len = fmt ? 2 : 1; ptr = ensure(p, len + 1); - if (!ptr) - return 0; + if (!ptr) return 0; *ptr++ = '{'; - if (fmt) - *ptr++ = '\n'; + if (fmt) *ptr++ = '\n'; *ptr = 0; p->offset += len; child = item->child; @@ -863,10 +800,8 @@ static char *print_object(cJSON *item, int depth, int fmt, printbuffer *p) { while (child) { if (fmt) { ptr = ensure(p, depth); - if (!ptr) - return 0; - for (j = 0; j < depth; j++) - *ptr++ = '\t'; + if (!ptr) return 0; + for (j = 0; j < depth; j++) *ptr++ = '\t'; p->offset += depth; } print_string_ptr(child->string, p); @@ -874,11 +809,9 @@ static char *print_object(cJSON *item, int depth, int fmt, printbuffer *p) { len = fmt ? 2 : 1; ptr = ensure(p, len); - if (!ptr) - return 0; + if (!ptr) return 0; *ptr++ = ':'; - if (fmt) - *ptr++ = '\t'; + if (fmt) *ptr++ = '\t'; p->offset += len; print_value(child, depth, fmt, p); @@ -886,30 +819,24 @@ static char *print_object(cJSON *item, int depth, int fmt, printbuffer *p) { len = (fmt ? 1 : 0) + (child->next ? 1 : 0); ptr = ensure(p, len + 1); - if (!ptr) - return 0; - if (child->next) - *ptr++ = ','; - if (fmt) - *ptr++ = '\n'; + if (!ptr) return 0; + if (child->next) *ptr++ = ','; + if (fmt) *ptr++ = '\n'; *ptr = 0; p->offset += len; child = child->next; } ptr = ensure(p, fmt ? (depth + 1) : 2); - if (!ptr) - return 0; + if (!ptr) return 0; if (fmt) - for (j = 0; j < depth - 1; j++) - *ptr++ = '\t'; + for (j = 0; j < depth - 1; j++) *ptr++ = '\t'; *ptr++ = '}'; *ptr = 0; out = (p->buffer) + i; } else { /* Allocate space for the names and the objects */ entries = (char **)cJSON_malloc(numentries * sizeof(char *)); - if (!entries) - return 0; + if (!entries) return 0; names = (char **)cJSON_malloc(numentries * sizeof(char *)); if (!names) { cJSON_free(entries); @@ -921,8 +848,7 @@ static char *print_object(cJSON *item, int depth, int fmt, printbuffer *p) { /* Collect all the results into our arrays: */ child = item->child; depth++; - if (fmt) - len += depth; + if (fmt) len += depth; while (child) { names[i] = str = print_string_ptr(child->string, 0); entries[i++] = ret = print_value(child, depth, fmt, 0); @@ -934,18 +860,14 @@ static char *print_object(cJSON *item, int depth, int fmt, printbuffer *p) { } /* Try to allocate the output string */ - if (!fail) - out = (char *)cJSON_malloc(len); - if (!out) - fail = 1; + if (!fail) out = (char *)cJSON_malloc(len); + if (!out) fail = 1; /* Handle failure */ if (fail) { for (j = 0; j < numentries; j++) { - if (names[i]) - cJSON_free(names[j]); - if (entries[j]) - cJSON_free(entries[j]); + if (names[i]) cJSON_free(names[j]); + if (entries[j]) cJSON_free(entries[j]); } cJSON_free(names); cJSON_free(entries); @@ -955,25 +877,20 @@ static char *print_object(cJSON *item, int depth, int fmt, printbuffer *p) { /* Compose the output: */ *out = '{'; ptr = out + 1; - if (fmt) - *ptr++ = '\n'; + if (fmt) *ptr++ = '\n'; *ptr = 0; for (j = 0; j < numentries; j++) { if (fmt) - for (k = 0; k < depth; k++) - *ptr++ = '\t'; + for (k = 0; k < depth; k++) *ptr++ = '\t'; tmplen = strlen(names[j]); memcpy(ptr, names[j], tmplen); ptr += tmplen; *ptr++ = ':'; - if (fmt) - *ptr++ = '\t'; + if (fmt) *ptr++ = '\t'; strcpy(ptr, entries[j]); ptr += strlen(entries[j]); - if (j != numentries - 1) - *ptr++ = ','; - if (fmt) - *ptr++ = '\n'; + if (j != numentries - 1) *ptr++ = ','; + if (fmt) *ptr++ = '\n'; *ptr = 0; cJSON_free(names[j]); cJSON_free(entries[j]); @@ -982,8 +899,7 @@ static char *print_object(cJSON *item, int depth, int fmt, printbuffer *p) { cJSON_free(names); cJSON_free(entries); if (fmt) - for (j = 0; j < depth - 1; j++) - *ptr++ = '\t'; + for (j = 0; j < depth - 1; j++) *ptr++ = '\t'; *ptr++ = '}'; *ptr++ = 0; } @@ -994,20 +910,17 @@ static char *print_object(cJSON *item, int depth, int fmt, printbuffer *p) { int cJSON_GetArraySize(cJSON *array) { cJSON *c = array->child; int i = 0; - while (c) - i++, c = c->next; + while (c) i++, c = c->next; return i; } cJSON *cJSON_GetArrayItem(cJSON *array, int item) { cJSON *c = array->child; - while (c && item > 0) - item--, c = c->next; + while (c && item > 0) item--, c = c->next; return c; } cJSON *cJSON_GetObjectItem(cJSON *object, const char *string) { cJSON *c = object->child; - while (c && strcmp(c->string, string)) - c = c->next; + while (c && strcmp(c->string, string)) c = c->next; return c; } @@ -1019,8 +932,7 @@ static void suffix_object(cJSON *prev, cJSON *item) { /* Utility for handling references. */ static cJSON *create_reference(cJSON *item) { cJSON *ref = cJSON_New_Item(); - if (!ref) - return 0; + if (!ref) return 0; memcpy(ref, item, sizeof(cJSON)); ref->string = 0; ref->type |= cJSON_IsReference; @@ -1031,29 +943,23 @@ static cJSON *create_reference(cJSON *item) { /* Add item to array/object. */ void cJSON_AddItemToArray(cJSON *array, cJSON *item) { cJSON *c = array->child; - if (!item) - return; + if (!item) return; if (!c) { array->child = item; } else { - while (c && c->next) - c = c->next; + while (c && c->next) c = c->next; suffix_object(c, item); } } void cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item) { - if (!item) - return; - if (item->string) - cJSON_free(item->string); + if (!item) return; + if (item->string) cJSON_free(item->string); item->string = cJSON_strdup(string); cJSON_AddItemToArray(object, item); } void cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item) { - if (!item) - return; - if (!(item->type & cJSON_StringIsConst) && item->string) - cJSON_free(item->string); + 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); @@ -1065,16 +971,11 @@ void cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *it cJSON *cJSON_DetachItemFromArray(cJSON *array, int which) { cJSON *c = array->child; - while (c && which > 0) - c = c->next, which--; - if (!c) - return 0; - if (c->prev) - c->prev->next = c->next; - if (c->next) - c->next->prev = c->prev; - if (c == array->child) - array->child = c->next; + while (c && which > 0) c = c->next, which--; + if (!c) return 0; + 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; } @@ -1082,10 +983,8 @@ void cJSON_DeleteItemFromArray(cJSON *array, int which) { cJSON_Delete(cJSON_Det cJSON *cJSON_DetachItemFromObject(cJSON *object, const char *string) { int i = 0; cJSON *c = object->child; - while (c && strcmp(c->string, string)) - i++, c = c->next; - if (c) - return cJSON_DetachItemFromArray(object, i); + while (c && strcmp(c->string, string)) i++, c = c->next; + if (c) return cJSON_DetachItemFromArray(object, i); return 0; } void cJSON_DeleteItemFromObject(cJSON *object, const char *string) { cJSON_Delete(cJSON_DetachItemFromObject(object, string)); } @@ -1093,8 +992,7 @@ void cJSON_DeleteItemFromObject(cJSON *object, const char *string) { cJSON_Delet /* Replace array/object items with new ones. */ void cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem) { cJSON *c = array->child; - while (c && which > 0) - c = c->next, which--; + while (c && which > 0) c = c->next, which--; if (!c) { cJSON_AddItemToArray(array, newitem); return; @@ -1109,14 +1007,11 @@ void cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem) { } void cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem) { cJSON *c = array->child; - while (c && which > 0) - c = c->next, which--; - if (!c) - return; + while (c && which > 0) c = c->next, which--; + if (!c) return; newitem->next = c->next; newitem->prev = c->prev; - if (newitem->next) - newitem->next->prev = newitem; + if (newitem->next) newitem->next->prev = newitem; if (c == array->child) array->child = newitem; else @@ -1127,8 +1022,7 @@ void cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem) { void cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem) { int i = 0; cJSON *c = object->child; - while (c && strcmp(c->string, string)) - i++, c = c->next; + while (c && strcmp(c->string, string)) i++, c = c->next; if (c) { newitem->string = cJSON_strdup(string); cJSON_ReplaceItemInArray(object, i, newitem); @@ -1138,26 +1032,22 @@ void cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem /* Create basic types: */ cJSON *cJSON_CreateNull(void) { cJSON *item = cJSON_New_Item(); - if (item) - item->type = cJSON_NULL; + if (item) item->type = cJSON_NULL; return item; } cJSON *cJSON_CreateTrue(void) { cJSON *item = cJSON_New_Item(); - if (item) - item->type = cJSON_True; + if (item) item->type = cJSON_True; return item; } cJSON *cJSON_CreateFalse(void) { cJSON *item = cJSON_New_Item(); - if (item) - item->type = cJSON_False; + if (item) item->type = cJSON_False; return item; } cJSON *cJSON_CreateBool(int b) { cJSON *item = cJSON_New_Item(); - if (item) - item->type = b ? cJSON_True : cJSON_False; + if (item) item->type = b ? cJSON_True : cJSON_False; return item; } cJSON *cJSON_CreateNumber(double num) { @@ -1179,14 +1069,12 @@ cJSON *cJSON_CreateString(const char *string) { } cJSON *cJSON_CreateArray(void) { cJSON *item = cJSON_New_Item(); - if (item) - item->type = cJSON_Array; + if (item) item->type = cJSON_Array; return item; } cJSON *cJSON_CreateObject(void) { cJSON *item = cJSON_New_Item(); - if (item) - item->type = cJSON_Object; + if (item) item->type = cJSON_Object; return item; } @@ -1248,12 +1136,10 @@ cJSON *cJSON_CreateStringArray(const char **strings, int count) { cJSON *cJSON_Duplicate(cJSON *item, int recurse) { cJSON *newitem, *cptr, *nptr = 0, *newchild; /* Bail on bad ptr */ - if (!item) - return 0; + if (!item) return 0; /* Create new item */ newitem = cJSON_New_Item(); - if (!newitem) - return 0; + if (!newitem) return 0; /* Copy over all vars */ newitem->type = item->type & (~cJSON_IsReference), newitem->valueint = item->valueint, newitem->valuedouble = item->valuedouble; if (item->valuestring) { @@ -1271,8 +1157,7 @@ cJSON *cJSON_Duplicate(cJSON *item, int recurse) { } } /* If non-recursive, then we're done! */ - if (!recurse) - return newitem; + if (!recurse) return newitem; /* Walk the ->next chain for the child. */ cptr = item->child; while (cptr) { @@ -1307,18 +1192,15 @@ void cJSON_Minify(char *json) { else if (*json == '\n') json++; else if (*json == '/' && json[1] == '/') - while (*json && *json != '\n') - json++; /* double-slash comments, to end of line. */ + while (*json && *json != '\n') json++; /* double-slash comments, to end of line. */ else if (*json == '/' && json[1] == '*') { - while (*json && !(*json == '*' && json[1] == '/')) - json++; + while (*json && !(*json == '*' && json[1] == '/')) json++; json += 2; } /* multiline comments. */ else if (*json == '\"') { *into++ = *json++; while (*json && *json != '\"') { - if (*json == '\\') - *into++ = *json++; + if (*json == '\\') *into++ = *json++; *into++ = *json++; } *into++ = *json++; |
