Lines Matching +full:copy +full:- +full:item
2 Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
4 Permission is hereby granted, free of charge, to any person obtaining a copy
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
74 #define isinf(d) (isnan((d - d)) && !isnan(d))
82 #define NAN sqrt(-1.0)
99 CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item) in cJSON_GetStringValue() argument
101 if (!cJSON_IsString(item)) in cJSON_GetStringValue()
106 return item->valuestring; in cJSON_GetStringValue()
110 CJSON_PUBLIC(long long *) cJSON_GetInt64NumberValue(cJSON * const item) in cJSON_GetInt64NumberValue() argument
112 if (!cJSON_IsInt64Number(item)) in cJSON_GetInt64NumberValue()
117 return &(item->valueint); in cJSON_GetInt64NumberValue()
121 CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item) in cJSON_GetNumberValue() argument
123 if (!cJSON_IsNumber(item)) in cJSON_GetNumberValue()
128 return item->valuedouble; in cJSON_GetNumberValue()
131 /* This is a safeguard to prevent copy-pasters from using incompatible C and header files */
165 return tolower(*string1) - tolower(*string2); in case_insensitive_strcmp()
196 #define static_strlen(string_literal) (sizeof(string_literal) - sizeof(""))
203 unsigned char *copy = NULL; in cJSON_strdup() local
211 copy = (unsigned char*)hooks->allocate(length); in cJSON_strdup()
212 if (copy == NULL) in cJSON_strdup()
216 memcpy(copy, string, length); in cJSON_strdup()
218 return copy; in cJSON_strdup()
233 if (hooks->malloc_fn != NULL) in cJSON_InitHooks()
235 global_hooks.allocate = hooks->malloc_fn; in cJSON_InitHooks()
239 if (hooks->free_fn != NULL) in cJSON_InitHooks()
241 global_hooks.deallocate = hooks->free_fn; in cJSON_InitHooks()
255 cJSON* node = (cJSON*)hooks->allocate(sizeof(cJSON)); in cJSON_New_Item()
265 CJSON_PUBLIC(void) cJSON_Delete(cJSON *item) in cJSON_Delete() argument
268 while (item != NULL) in cJSON_Delete()
270 next = item->next; in cJSON_Delete()
271 if (!(item->type & cJSON_IsReference) && (item->child != NULL)) in cJSON_Delete()
273 cJSON_Delete(item->child); in cJSON_Delete()
275 if (!(item->type & cJSON_IsReference) && (item->valuestring != NULL)) in cJSON_Delete()
277 global_hooks.deallocate(item->valuestring); in cJSON_Delete()
278 item->valuestring = NULL; in cJSON_Delete()
280 if (!(item->type & cJSON_StringIsConst) && (item->string != NULL)) in cJSON_Delete()
282 global_hooks.deallocate(item->string); in cJSON_Delete()
283 item->string = NULL; in cJSON_Delete()
285 if (next == item) in cJSON_Delete()
289 global_hooks.deallocate(item); in cJSON_Delete()
290 item = next; in cJSON_Delete()
299 return (unsigned char) lconv->decimal_point[0]; in get_decimal_point()
315 #define can_read(buffer, size) ((buffer != NULL) && (((buffer)->offset + size) <= (buffer)->length))
317 …access_at_index(buffer, index) ((buffer != NULL) && (((buffer)->offset + index) < (buffer)->length…
320 #define buffer_at_offset(buffer) ((buffer)->content + (buffer)->offset)
323 /* Parse the input text to generate a number, and populate the result into item. */
324 static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_buffer) in parse_number() argument
334 if ((input_buffer == NULL) || (input_buffer->content == NULL)) in parse_number()
339 /* copy the number into a temporary buffer and replace '.' with the decimal point in parse_number()
342 for (i = 0; (i < (sizeof(number_c_string) - 1)) && can_access_at_index(input_buffer, i); i++) in parse_number()
357 case '-': in parse_number()
382 item->type = cJSON_Number | cJSON_IsInt64; in parse_number()
388 item->valueint = integer; in parse_number()
389 item->valuedouble = (double)integer; in parse_number()
399 item->valuedouble = number; in parse_number()
404 item->valueint = LLONG_MAX; in parse_number()
408 item->valueint = LLONG_MIN; in parse_number()
412 item->valueint = (long long)number; in parse_number()
415 item->type = cJSON_Number; in parse_number()
418 input_buffer->offset += (size_t)(after_end - number_c_string); in parse_number()
422 /* Parse the input text to generate a number, and populate the result into item. */
423 static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_buffer) in parse_number() argument
431 if ((input_buffer == NULL) || (input_buffer->content == NULL)) in parse_number()
436 /* copy the number into a temporary buffer and replace '.' with the decimal point in parse_number()
439 for (i = 0; (i < (sizeof(number_c_string) - 1)) && can_access_at_index(input_buffer, i); i++) in parse_number()
454 case '-': in parse_number()
477 item->valuedouble = number; in parse_number()
482 item->valueint = INT_MAX; in parse_number()
486 item->valueint = INT_MIN; in parse_number()
490 item->valueint = (int)number; in parse_number()
493 item->type = cJSON_Number; in parse_number()
495 input_buffer->offset += (size_t)(after_end - number_c_string); in parse_number()
509 if (!(object->type & cJSON_Number) || !(object->type & cJSON_IsInt64)) in cJSON_SetInt64NumberValue()
514 object->valueint = integer; in cJSON_SetInt64NumberValue()
515 object->valuedouble = (double)integer; in cJSON_SetInt64NumberValue()
527 object->valueint = LLONG_MAX; in cJSON_SetNumberHelper()
531 object->valueint = LLONG_MIN; in cJSON_SetNumberHelper()
535 object->valueint = (long long)number; in cJSON_SetNumberHelper()
538 return object->valuedouble = number; in cJSON_SetNumberHelper()
546 object->valueint = INT_MAX; in cJSON_SetNumberHelper()
550 object->valueint = INT_MIN; in cJSON_SetNumberHelper()
554 object->valueint = (int)number; in cJSON_SetNumberHelper()
557 return object->valuedouble = number; in cJSON_SetNumberHelper()
563 char *copy = NULL; in cJSON_SetValuestring() local
565 if ((object == NULL) || !(object->type & cJSON_String) || (object->type & cJSON_IsReference)) in cJSON_SetValuestring()
570 if (object->valuestring == NULL || valuestring == NULL) in cJSON_SetValuestring()
574 if (strlen(valuestring) <= strlen(object->valuestring)) in cJSON_SetValuestring()
576 strcpy(object->valuestring, valuestring); in cJSON_SetValuestring()
577 return object->valuestring; in cJSON_SetValuestring()
579 copy = (char*) cJSON_strdup((const unsigned char*)valuestring, &global_hooks); in cJSON_SetValuestring()
580 if (copy == NULL) in cJSON_SetValuestring()
584 if (object->valuestring != NULL) in cJSON_SetValuestring()
586 cJSON_free(object->valuestring); in cJSON_SetValuestring()
588 object->valuestring = copy; in cJSON_SetValuestring()
590 return copy; in cJSON_SetValuestring()
610 if ((p == NULL) || (p->buffer == NULL)) in ensure()
615 if ((p->length > 0) && (p->offset >= p->length)) in ensure()
627 needed += p->offset + 1; in ensure()
628 if (needed <= p->length) in ensure()
630 return p->buffer + p->offset; in ensure()
633 if (p->noalloc) { in ensure()
655 if (p->hooks.reallocate != NULL) in ensure()
658 newbuffer = (unsigned char*)p->hooks.reallocate(p->buffer, newsize); in ensure()
661 p->hooks.deallocate(p->buffer); in ensure()
662 p->length = 0; in ensure()
663 p->buffer = NULL; in ensure()
671 newbuffer = (unsigned char*)p->hooks.allocate(newsize); in ensure()
674 p->hooks.deallocate(p->buffer); in ensure()
675 p->length = 0; in ensure()
676 p->buffer = NULL; in ensure()
681 memcpy(newbuffer, p->buffer, p->offset + 1); in ensure()
682 p->hooks.deallocate(p->buffer); in ensure()
684 p->length = newsize; in ensure()
685 p->buffer = newbuffer; in ensure()
687 return newbuffer + p->offset; in ensure()
694 if ((buffer == NULL) || (buffer->buffer == NULL)) in update_offset()
698 buffer_pointer = buffer->buffer + buffer->offset; in update_offset()
700 buffer->offset += strlen((const char*)buffer_pointer); in update_offset()
703 /* securely comparison of floating-point variables */
707 return (fabs(a - b) <= maxVal * DBL_EPSILON); in compare_double()
711 /* Render the number nicely from the given item into a string. */
712 static cJSON_bool print_number(const cJSON * const item, printbuffer * const output_buffer) in print_number() argument
715 double d = item->valuedouble; in print_number()
716 long long integer = item->valueint; in print_number()
728 if (item->type & cJSON_IsInt64) in print_number()
733 else /* item->type == cJSON_Number */ in print_number()
755 if ((length < 0) || (length > (int)(sizeof(number_buffer) - 1))) in print_number()
767 /* copy the printed number to the output and replace locale in print_number()
781 output_buffer->offset += (size_t)length; in print_number()
786 /* Render the number nicely from the given item into a string. */
787 static cJSON_bool print_number(const cJSON * const item, printbuffer * const output_buffer) in print_number() argument
790 double d = item->valuedouble; in print_number()
807 else if(d == (double)item->valueint) in print_number()
809 length = sprintf((char*)number_buffer, "%d", item->valueint); in print_number()
825 if ((length < 0) || (length > (int)(sizeof(number_buffer) - 1))) in print_number()
837 /* copy the printed number to the output and replace locale in print_number()
851 output_buffer->offset += (size_t)length; in print_number()
868 h += (unsigned int) input[i] - '0'; in parse_hex4()
872 h += (unsigned int) 10 + input[i] - 'A'; in parse_hex4()
876 h += (unsigned int) 10 + input[i] - 'a'; in parse_hex4()
893 /* converts a UTF-16 literal to UTF-8
905 if ((input_end - first_sequence) < 6) in utf16_literal_to_utf8()
927 if ((input_end - second_sequence) < 6) in utf16_literal_to_utf8()
958 /* encode as UTF-8 in utf16_literal_to_utf8()
991 for (utf8_position = (unsigned char)(utf8_length - 1); utf8_position > 0; utf8_position--) in utf16_literal_to_utf8()
1015 /* Parse the input text into an unescaped cinput, and populate item. */
1016 static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_buffer) in parse_string() argument
1033 …while (((size_t)(input_end - input_buffer->content) < input_buffer->length) && (*input_end != '\"'… in parse_string()
1038 if ((size_t)(input_end + 1 - input_buffer->content) >= input_buffer->length) in parse_string()
1048 … if (((size_t)(input_end - input_buffer->content) >= input_buffer->length) || (*input_end != '\"')) in parse_string()
1054 allocation_length = (size_t) (input_end - buffer_at_offset(input_buffer)) - skipped_bytes; in parse_string()
1055 output = (unsigned char*)input_buffer->hooks.allocate(allocation_length + sizeof("")); in parse_string()
1074 if ((input_end - input_pointer) < 1) in parse_string()
1102 /* UTF-16 literal */ in parse_string()
1107 /* failed to convert UTF16-literal to UTF-8 */ in parse_string()
1122 item->type = cJSON_String; in parse_string()
1123 item->valuestring = (char*)output; in parse_string()
1125 input_buffer->offset = (size_t) (input_end - input_buffer->content); in parse_string()
1126 input_buffer->offset++; in parse_string()
1133 input_buffer->hooks.deallocate(output); in parse_string()
1138 input_buffer->offset = (size_t)(input_pointer - input_buffer->content); in parse_string()
1190 /* UTF-16 escape sequence uXXXX */ in print_string_ptr()
1196 output_length = (size_t)(input_pointer - input) + escape_characters; in print_string_ptr()
1217 /* copy the string */ in print_string_ptr()
1222 /* normal character, copy */ in print_string_ptr()
1266 /* Invoke print_string_ptr (which is useful) on an item. */
1267 static cJSON_bool print_string(const cJSON * const item, printbuffer * const p) in print_string() argument
1269 return print_string_ptr((unsigned char*)item->valuestring, p); in print_string()
1273 static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer);
1274 static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer);
1275 static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer);
1276 static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer);
1277 static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer);
1278 static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer);
1283 if ((buffer == NULL) || (buffer->content == NULL)) in buffer_skip_whitespace()
1295 buffer->offset++; in buffer_skip_whitespace()
1298 if (buffer->offset == buffer->length) in buffer_skip_whitespace()
1300 buffer->offset--; in buffer_skip_whitespace()
1306 /* skip the UTF-8 BOM (byte order mark) if it is at the beginning of a buffer */
1309 if ((buffer == NULL) || (buffer->content == NULL) || (buffer->offset != 0)) in skip_utf8_bom()
1316 buffer->offset += 3; in skip_utf8_bom()
1337 /* Parse an object - create a new root, and populate. */
1341 cJSON *item = NULL; in cJSON_ParseWithLengthOpts() local
1357 item = cJSON_New_Item(&global_hooks); in cJSON_ParseWithLengthOpts()
1358 if (item == NULL) /* memory fail */ in cJSON_ParseWithLengthOpts()
1363 if (!parse_value(item, buffer_skip_whitespace(skip_utf8_bom(&buffer)))) in cJSON_ParseWithLengthOpts()
1369 …/* if we require null-terminated JSON without appended garbage, skip and then check for a null ter… in cJSON_ParseWithLengthOpts()
1383 return item; in cJSON_ParseWithLengthOpts()
1386 if (item != NULL) in cJSON_ParseWithLengthOpts()
1388 cJSON_Delete(item); in cJSON_ParseWithLengthOpts()
1403 local_error.position = buffer.length - 1; in cJSON_ParseWithLengthOpts()
1430 static unsigned char *print(const cJSON * const item, cJSON_bool format, const internal_hooks * con… in print() argument
1439 buffer->buffer = (unsigned char*) hooks->allocate(default_buffer_size); in print()
1440 buffer->length = default_buffer_size; in print()
1441 buffer->format = format; in print()
1442 buffer->hooks = *hooks; in print()
1443 if (buffer->buffer == NULL) in print()
1449 if (!print_value(item, buffer)) in print()
1456 if (hooks->reallocate != NULL) in print()
1458 printed = (unsigned char*) hooks->reallocate(buffer->buffer, buffer->offset + 1); in print()
1462 buffer->buffer = NULL; in print()
1464 else /* otherwise copy the JSON over to a new buffer */ in print()
1466 printed = (unsigned char*) hooks->allocate(buffer->offset + 1); in print()
1471 memcpy(printed, buffer->buffer, cjson_min(buffer->length, buffer->offset + 1)); in print()
1472 printed[buffer->offset] = '\0'; /* just to be sure */ in print()
1475 hooks->deallocate(buffer->buffer); in print()
1481 if (buffer->buffer != NULL) in print()
1483 hooks->deallocate(buffer->buffer); in print()
1488 hooks->deallocate(printed); in print()
1494 /* Render a cJSON item/entity/structure to text. */
1495 CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item) in cJSON_Print() argument
1497 return (char*)print(item, true, &global_hooks); in cJSON_Print()
1500 CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item) in cJSON_PrintUnformatted() argument
1502 return (char*)print(item, false, &global_hooks); in cJSON_PrintUnformatted()
1505 CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt) in cJSON_PrintBuffered() argument
1526 if (!print_value(item, &p)) in cJSON_PrintBuffered()
1535 CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const… in cJSON_PrintPreallocated() argument
1551 return print_value(item, &p); in cJSON_PrintPreallocated()
1554 /* Parser core - when encountering text, process appropriately. */
1555 static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer) in parse_value() argument
1557 if ((input_buffer == NULL) || (input_buffer->content == NULL)) in parse_value()
1566 item->type = cJSON_NULL; in parse_value()
1567 input_buffer->offset += 4; in parse_value()
1573 item->type = cJSON_False; in parse_value()
1574 input_buffer->offset += 5; in parse_value()
1580 item->type = cJSON_True; in parse_value()
1581 item->valueint = 1; in parse_value()
1582 input_buffer->offset += 4; in parse_value()
1588 return parse_string(item, input_buffer); in parse_value()
1591 …if (can_access_at_index(input_buffer, 0) && ((buffer_at_offset(input_buffer)[0] == '-') || ((buffe… in parse_value()
1593 return parse_number(item, input_buffer); in parse_value()
1598 return parse_array(item, input_buffer); in parse_value()
1603 return parse_object(item, input_buffer); in parse_value()
1610 static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer) in print_value() argument
1614 if ((item == NULL) || (output_buffer == NULL)) in print_value()
1619 switch ((item->type) & 0xFF) in print_value()
1649 return print_number(item, output_buffer); in print_value()
1654 if (item->valuestring == NULL) in print_value()
1659 raw_length = strlen(item->valuestring) + sizeof(""); in print_value()
1665 memcpy(output, item->valuestring, raw_length); in print_value()
1670 return print_string(item, output_buffer); in print_value()
1673 return print_array(item, output_buffer); in print_value()
1676 return print_object(item, output_buffer); in print_value()
1684 static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer) in parse_array() argument
1689 if (input_buffer->depth >= CJSON_NESTING_LIMIT) in parse_array()
1693 input_buffer->depth++; in parse_array()
1701 input_buffer->offset++; in parse_array()
1712 input_buffer->offset--; in parse_array()
1717 input_buffer->offset--; in parse_array()
1721 /* allocate next item */ in parse_array()
1722 cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks)); in parse_array()
1728 /* attach next item to list */ in parse_array()
1737 current_item->next = new_item; in parse_array()
1738 new_item->prev = current_item; in parse_array()
1743 input_buffer->offset++; in parse_array()
1759 input_buffer->depth--; in parse_array()
1762 head->prev = current_item; in parse_array()
1765 item->type = cJSON_Array; in parse_array()
1766 item->child = head; in parse_array()
1768 input_buffer->offset++; in parse_array()
1782 static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer) in print_array() argument
1786 cJSON *current_element = item->child; in print_array()
1802 output_buffer->offset++; in print_array()
1803 output_buffer->depth++; in print_array()
1812 if (current_element->next) in print_array()
1814 length = (size_t) (output_buffer->format ? 2 : 1); in print_array()
1821 if(output_buffer->format) in print_array()
1826 output_buffer->offset += length; in print_array()
1828 current_element = current_element->next; in print_array()
1838 output_buffer->depth--; in print_array()
1844 static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer) in parse_object() argument
1849 if (input_buffer->depth >= CJSON_NESTING_LIMIT) in parse_object()
1853 input_buffer->depth++; in parse_object()
1860 input_buffer->offset++; in parse_object()
1870 input_buffer->offset--; in parse_object()
1875 input_buffer->offset--; in parse_object()
1879 /* allocate next item */ in parse_object()
1880 cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks)); in parse_object()
1886 /* attach next item to list */ in parse_object()
1895 current_item->next = new_item; in parse_object()
1896 new_item->prev = current_item; in parse_object()
1906 input_buffer->offset++; in parse_object()
1915 current_item->string = current_item->valuestring; in parse_object()
1916 current_item->valuestring = NULL; in parse_object()
1924 input_buffer->offset++; in parse_object()
1940 input_buffer->depth--; in parse_object()
1943 head->prev = current_item; in parse_object()
1946 item->type = cJSON_Object; in parse_object()
1947 item->child = head; in parse_object()
1949 input_buffer->offset++; in parse_object()
1962 static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer) in print_object() argument
1966 cJSON *current_item = item->child; in print_object()
1974 length = (size_t) (output_buffer->format ? 2 : 1); /* fmt: {\n */ in print_object()
1982 output_buffer->depth++; in print_object()
1983 if (output_buffer->format) in print_object()
1987 output_buffer->offset += length; in print_object()
1991 if (output_buffer->format) in print_object()
1994 output_pointer = ensure(output_buffer, output_buffer->depth); in print_object()
1999 for (i = 0; i < output_buffer->depth; i++) in print_object()
2003 output_buffer->offset += output_buffer->depth; in print_object()
2007 if (!print_string_ptr((unsigned char*)current_item->string, output_buffer)) in print_object()
2013 length = (size_t) (output_buffer->format ? 2 : 1); in print_object()
2020 if (output_buffer->format) in print_object()
2024 output_buffer->offset += length; in print_object()
2034 length = ((size_t)(output_buffer->format ? 1 : 0) + (size_t)(current_item->next ? 1 : 0)); in print_object()
2040 if (current_item->next) in print_object()
2045 if (output_buffer->format) in print_object()
2050 output_buffer->offset += length; in print_object()
2052 current_item = current_item->next; in print_object()
2055 output_pointer = ensure(output_buffer, output_buffer->format ? (output_buffer->depth + 1) : 2); in print_object()
2060 if (output_buffer->format) in print_object()
2063 for (i = 0; i < (output_buffer->depth - 1); i++) in print_object()
2070 output_buffer->depth--; in print_object()
2075 /* Get Array size/item / object item. */
2086 child = array->child; in cJSON_GetArraySize()
2091 child = child->next; in cJSON_GetArraySize()
2108 current_child = array->child; in get_array_item()
2111 index--; in get_array_item()
2112 current_child = current_child->next; in get_array_item()
2137 current_element = object->child; in get_object_item()
2140 …hile ((current_element != NULL) && (current_element->string != NULL) && (strcmp(name, current_elem… in get_object_item()
2142 current_element = current_element->next; in get_object_item()
2147 …ensitive_strcmp((const unsigned char*)name, (const unsigned char*)(current_element->string)) != 0)) in get_object_item()
2149 current_element = current_element->next; in get_object_item()
2153 if ((current_element == NULL) || (current_element->string == NULL)) { in get_object_item()
2176 static void suffix_object(cJSON *prev, cJSON *item) in suffix_object() argument
2178 prev->next = item; in suffix_object()
2179 item->prev = prev; in suffix_object()
2183 static cJSON *create_reference(const cJSON *item, const internal_hooks * const hooks) in create_reference() argument
2186 if (item == NULL) in create_reference()
2197 memcpy(reference, item, sizeof(cJSON)); in create_reference()
2198 reference->string = NULL; in create_reference()
2199 reference->type |= cJSON_IsReference; in create_reference()
2200 reference->next = reference->prev = NULL; in create_reference()
2204 static cJSON_bool add_item_to_array(cJSON *array, cJSON *item) in add_item_to_array() argument
2208 if ((item == NULL) || (array == NULL) || (array == item)) in add_item_to_array()
2213 child = array->child; in add_item_to_array()
2215 * To find the last item in array quickly, we use prev in array in add_item_to_array()
2220 array->child = item; in add_item_to_array()
2221 item->prev = item; in add_item_to_array()
2222 item->next = NULL; in add_item_to_array()
2227 if (child->prev) in add_item_to_array()
2229 suffix_object(child->prev, item); in add_item_to_array()
2230 array->child->prev = item; in add_item_to_array()
2237 /* Add item to array/object. */
2238 CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item) in cJSON_AddItemToArray() argument
2240 return add_item_to_array(array, item); in cJSON_AddItemToArray()
2247 #pragma GCC diagnostic ignored "-Wcast-qual"
2259 …_to_object(cJSON * const object, const char * const string, cJSON * const item, const internal_hoo… in add_item_to_object() argument
2264 if ((object == NULL) || (string == NULL) || (item == NULL) || (object == item)) in add_item_to_object()
2272 new_type = item->type | cJSON_StringIsConst; in add_item_to_object()
2282 new_type = item->type & ~cJSON_StringIsConst; in add_item_to_object()
2285 if (!(item->type & cJSON_StringIsConst) && (item->string != NULL)) in add_item_to_object()
2287 hooks->deallocate(item->string); in add_item_to_object()
2290 item->string = new_key; in add_item_to_object()
2291 item->type = new_type; in add_item_to_object()
2293 return add_item_to_array(object, item); in add_item_to_object()
2296 CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item) in cJSON_AddItemToObject() argument
2298 return add_item_to_object(object, string, item, &global_hooks, false); in cJSON_AddItemToObject()
2301 /* Add an item to an object with constant string as key */
2302 CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item) in cJSON_AddItemToObjectCS() argument
2304 return add_item_to_object(object, string, item, &global_hooks, true); in cJSON_AddItemToObjectCS()
2307 CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item) in cJSON_AddItemReferenceToArray() argument
2314 return add_item_to_array(array, create_reference(item, &global_hooks)); in cJSON_AddItemReferenceToArray()
2317 …N_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item) in cJSON_AddItemReferenceToObject() argument
2324 …return add_item_to_object(object, string, create_reference(item, &global_hooks), &global_hooks, fa… in cJSON_AddItemReferenceToObject()
2449 CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item) in cJSON_DetachItemViaPointer() argument
2451 if ((parent == NULL) || (item == NULL) || (item != parent->child && item->prev == NULL)) in cJSON_DetachItemViaPointer()
2456 if (item != parent->child) in cJSON_DetachItemViaPointer()
2459 item->prev->next = item->next; in cJSON_DetachItemViaPointer()
2461 if (item->next != NULL) in cJSON_DetachItemViaPointer()
2464 item->next->prev = item->prev; in cJSON_DetachItemViaPointer()
2467 if (item == parent->child) in cJSON_DetachItemViaPointer()
2470 parent->child = item->next; in cJSON_DetachItemViaPointer()
2472 else if (item->next == NULL) in cJSON_DetachItemViaPointer()
2475 parent->child->prev = item->prev; in cJSON_DetachItemViaPointer()
2478 /* make sure the detached item doesn't point anywhere anymore */ in cJSON_DetachItemViaPointer()
2479 item->prev = NULL; in cJSON_DetachItemViaPointer()
2480 item->next = NULL; in cJSON_DetachItemViaPointer()
2482 return item; in cJSON_DetachItemViaPointer()
2540 if (after_inserted != array->child && after_inserted->prev == NULL) { in cJSON_InsertItemInArray()
2541 /* return false if after_inserted is a corrupted array item */ in cJSON_InsertItemInArray()
2545 newitem->next = after_inserted; in cJSON_InsertItemInArray()
2546 newitem->prev = after_inserted->prev; in cJSON_InsertItemInArray()
2547 after_inserted->prev = newitem; in cJSON_InsertItemInArray()
2548 if (after_inserted == array->child) in cJSON_InsertItemInArray()
2550 array->child = newitem; in cJSON_InsertItemInArray()
2554 newitem->prev->next = newitem; in cJSON_InsertItemInArray()
2559 CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSO… in cJSON_ReplaceItemViaPointer() argument
2561 if ((parent == NULL) || (parent->child == NULL) || (replacement == NULL) || (item == NULL)) in cJSON_ReplaceItemViaPointer()
2566 if (replacement == item) in cJSON_ReplaceItemViaPointer()
2571 replacement->next = item->next; in cJSON_ReplaceItemViaPointer()
2572 replacement->prev = item->prev; in cJSON_ReplaceItemViaPointer()
2574 if (replacement->next != NULL) in cJSON_ReplaceItemViaPointer()
2576 replacement->next->prev = replacement; in cJSON_ReplaceItemViaPointer()
2578 if (parent->child == item) in cJSON_ReplaceItemViaPointer()
2580 if (parent->child->prev == parent->child) in cJSON_ReplaceItemViaPointer()
2582 replacement->prev = replacement; in cJSON_ReplaceItemViaPointer()
2584 parent->child = replacement; in cJSON_ReplaceItemViaPointer()
2588 * To find the last item in array quickly, we use prev in array. in cJSON_ReplaceItemViaPointer()
2589 * We can't modify the last item's next pointer where this item was the parent's child in cJSON_ReplaceItemViaPointer()
2591 if (replacement->prev != NULL) in cJSON_ReplaceItemViaPointer()
2593 replacement->prev->next = replacement; in cJSON_ReplaceItemViaPointer()
2595 if (replacement->next == NULL) in cJSON_ReplaceItemViaPointer()
2597 parent->child->prev = replacement; in cJSON_ReplaceItemViaPointer()
2601 item->next = NULL; in cJSON_ReplaceItemViaPointer()
2602 item->prev = NULL; in cJSON_ReplaceItemViaPointer()
2603 cJSON_Delete(item); in cJSON_ReplaceItemViaPointer()
2626 if (!(replacement->type & cJSON_StringIsConst) && (replacement->string != NULL)) in replace_item_in_object()
2628 cJSON_free(replacement->string); in replace_item_in_object()
2630 replacement->string = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks); in replace_item_in_object()
2631 if (replacement->string == NULL) in replace_item_in_object()
2636 replacement->type &= ~cJSON_StringIsConst; in replace_item_in_object()
2654 cJSON *item = cJSON_New_Item(&global_hooks); in cJSON_CreateNull() local
2655 if(item) in cJSON_CreateNull()
2657 item->type = cJSON_NULL; in cJSON_CreateNull()
2660 return item; in cJSON_CreateNull()
2665 cJSON *item = cJSON_New_Item(&global_hooks); in cJSON_CreateTrue() local
2666 if(item) in cJSON_CreateTrue()
2668 item->type = cJSON_True; in cJSON_CreateTrue()
2671 return item; in cJSON_CreateTrue()
2676 cJSON *item = cJSON_New_Item(&global_hooks); in cJSON_CreateFalse() local
2677 if(item) in cJSON_CreateFalse()
2679 item->type = cJSON_False; in cJSON_CreateFalse()
2682 return item; in cJSON_CreateFalse()
2687 cJSON *item = cJSON_New_Item(&global_hooks); in cJSON_CreateBool() local
2688 if(item) in cJSON_CreateBool()
2690 item->type = boolean ? cJSON_True : cJSON_False; in cJSON_CreateBool()
2693 return item; in cJSON_CreateBool()
2699 cJSON *item = cJSON_New_Item(&global_hooks); in cJSON_CreateInt64Number() local
2700 if(item) in cJSON_CreateInt64Number()
2702 item->type = cJSON_Number | cJSON_IsInt64; in cJSON_CreateInt64Number()
2703 item->valueint = integer; in cJSON_CreateInt64Number()
2704 item->valuedouble = (double)integer; in cJSON_CreateInt64Number()
2707 return item; in cJSON_CreateInt64Number()
2714 cJSON *item = cJSON_New_Item(&global_hooks); in cJSON_CreateNumber() local
2715 if(item) in cJSON_CreateNumber()
2717 item->type = cJSON_Number; in cJSON_CreateNumber()
2718 item->valuedouble = num; in cJSON_CreateNumber()
2724 item->valueint = LLONG_MAX; in cJSON_CreateNumber()
2728 item->valueint = LLONG_MIN; in cJSON_CreateNumber()
2732 item->valueint = (long long)num; in cJSON_CreateNumber()
2736 return item; in cJSON_CreateNumber()
2741 cJSON *item = cJSON_New_Item(&global_hooks); in cJSON_CreateNumber() local
2742 if(item) in cJSON_CreateNumber()
2744 item->type = cJSON_Number; in cJSON_CreateNumber()
2745 item->valuedouble = num; in cJSON_CreateNumber()
2750 item->valueint = INT_MAX; in cJSON_CreateNumber()
2754 item->valueint = INT_MIN; in cJSON_CreateNumber()
2758 item->valueint = (int)num; in cJSON_CreateNumber()
2762 return item; in cJSON_CreateNumber()
2768 cJSON *item = cJSON_New_Item(&global_hooks); in cJSON_CreateString() local
2769 if(item) in cJSON_CreateString()
2771 item->type = cJSON_String; in cJSON_CreateString()
2772 item->valuestring = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks); in cJSON_CreateString()
2773 if(!item->valuestring) in cJSON_CreateString()
2775 cJSON_Delete(item); in cJSON_CreateString()
2780 return item; in cJSON_CreateString()
2785 cJSON *item = cJSON_New_Item(&global_hooks); in cJSON_CreateStringReference() local
2786 if (item != NULL) in cJSON_CreateStringReference()
2788 item->type = cJSON_String | cJSON_IsReference; in cJSON_CreateStringReference()
2789 item->valuestring = (char*)cast_away_const(string); in cJSON_CreateStringReference()
2792 return item; in cJSON_CreateStringReference()
2797 cJSON *item = cJSON_New_Item(&global_hooks); in cJSON_CreateObjectReference() local
2798 if (item != NULL) { in cJSON_CreateObjectReference()
2799 item->type = cJSON_Object | cJSON_IsReference; in cJSON_CreateObjectReference()
2800 item->child = (cJSON*)cast_away_const(child); in cJSON_CreateObjectReference()
2803 return item; in cJSON_CreateObjectReference()
2807 cJSON *item = cJSON_New_Item(&global_hooks); in cJSON_CreateArrayReference() local
2808 if (item != NULL) { in cJSON_CreateArrayReference()
2809 item->type = cJSON_Array | cJSON_IsReference; in cJSON_CreateArrayReference()
2810 item->child = (cJSON*)cast_away_const(child); in cJSON_CreateArrayReference()
2813 return item; in cJSON_CreateArrayReference()
2818 cJSON *item = cJSON_New_Item(&global_hooks); in cJSON_CreateRaw() local
2819 if(item) in cJSON_CreateRaw()
2821 item->type = cJSON_Raw; in cJSON_CreateRaw()
2822 item->valuestring = (char*)cJSON_strdup((const unsigned char*)raw, &global_hooks); in cJSON_CreateRaw()
2823 if(!item->valuestring) in cJSON_CreateRaw()
2825 cJSON_Delete(item); in cJSON_CreateRaw()
2830 return item; in cJSON_CreateRaw()
2835 cJSON *item = cJSON_New_Item(&global_hooks); in cJSON_CreateArray() local
2836 if(item) in cJSON_CreateArray()
2838 item->type=cJSON_Array; in cJSON_CreateArray()
2841 return item; in cJSON_CreateArray()
2846 cJSON *item = cJSON_New_Item(&global_hooks); in cJSON_CreateObject() local
2847 if (item) in cJSON_CreateObject()
2849 item->type = cJSON_Object; in cJSON_CreateObject()
2852 return item; in cJSON_CreateObject()
2880 a->child = n; in cJSON_CreateIntArray()
2889 if (a && a->child) { in cJSON_CreateIntArray()
2890 a->child->prev = n; in cJSON_CreateIntArray()
2920 a->child = n; in cJSON_CreateFloatArray()
2929 if (a && a->child) { in cJSON_CreateFloatArray()
2930 a->child->prev = n; in cJSON_CreateFloatArray()
2960 a->child = n; in cJSON_CreateDoubleArray()
2969 if (a && a->child) { in cJSON_CreateDoubleArray()
2970 a->child->prev = n; in cJSON_CreateDoubleArray()
3000 a->child = n; in cJSON_CreateStringArray()
3009 if (a && a->child) { in cJSON_CreateStringArray()
3010 a->child->prev = n; in cJSON_CreateStringArray()
3017 CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse) in cJSON_Duplicate() argument
3025 if (!item) in cJSON_Duplicate()
3029 /* Create new item */ in cJSON_Duplicate()
3035 /* Copy over all vars */ in cJSON_Duplicate()
3036 newitem->type = item->type & (~cJSON_IsReference); in cJSON_Duplicate()
3037 newitem->valueint = item->valueint; in cJSON_Duplicate()
3038 newitem->valuedouble = item->valuedouble; in cJSON_Duplicate()
3039 if (item->valuestring) in cJSON_Duplicate()
3041 … newitem->valuestring = (char*)cJSON_strdup((unsigned char*)item->valuestring, &global_hooks); in cJSON_Duplicate()
3042 if (!newitem->valuestring) in cJSON_Duplicate()
3047 if (item->string) in cJSON_Duplicate()
3049 …newitem->string = (item->type&cJSON_StringIsConst) ? item->string : (char*)cJSON_strdup((unsigned … in cJSON_Duplicate()
3050 if (!newitem->string) in cJSON_Duplicate()
3055 /* If non-recursive, then we're done! */ in cJSON_Duplicate()
3060 /* Walk the ->next chain for the child. */ in cJSON_Duplicate()
3061 child = item->child; in cJSON_Duplicate()
3064 …newchild = cJSON_Duplicate(child, true); /* Duplicate (with recurse) each item in the ->next chain… in cJSON_Duplicate()
3071 /* If newitem->child already set, then crosswire ->prev and ->next and move on */ in cJSON_Duplicate()
3072 next->next = newchild; in cJSON_Duplicate()
3073 newchild->prev = next; in cJSON_Duplicate()
3078 /* Set newitem->child and move to it */ in cJSON_Duplicate()
3079 newitem->child = newchild; in cJSON_Duplicate()
3082 child = child->next; in cJSON_Duplicate()
3084 if (newitem && newitem->child) in cJSON_Duplicate()
3086 newitem->child->prev = newchild; in cJSON_Duplicate()
3193 /* and null-terminate. */ in cJSON_Minify()
3197 CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item) in cJSON_IsInvalid() argument
3199 if (item == NULL) in cJSON_IsInvalid()
3204 return (item->type & 0xFF) == cJSON_Invalid; in cJSON_IsInvalid()
3207 CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item) in cJSON_IsFalse() argument
3209 if (item == NULL) in cJSON_IsFalse()
3214 return (item->type & 0xFF) == cJSON_False; in cJSON_IsFalse()
3217 CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item) in cJSON_IsTrue() argument
3219 if (item == NULL) in cJSON_IsTrue()
3224 return (item->type & 0xff) == cJSON_True; in cJSON_IsTrue()
3228 CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item) in cJSON_IsBool() argument
3230 if (item == NULL) in cJSON_IsBool()
3235 return (item->type & (cJSON_True | cJSON_False)) != 0; in cJSON_IsBool()
3237 CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item) in cJSON_IsNull() argument
3239 if (item == NULL) in cJSON_IsNull()
3244 return (item->type & 0xFF) == cJSON_NULL; in cJSON_IsNull()
3248 CJSON_PUBLIC(cJSON_bool) cJSON_IsInt64Number(const cJSON * const item) in cJSON_IsInt64Number() argument
3250 if (item == NULL) in cJSON_IsInt64Number()
3255 return cJSON_IsNumber(item) && (item->type & cJSON_IsInt64); in cJSON_IsInt64Number()
3259 CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item) in cJSON_IsNumber() argument
3261 if (item == NULL) in cJSON_IsNumber()
3266 return (item->type & 0xFF) == cJSON_Number; in cJSON_IsNumber()
3269 CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item) in cJSON_IsString() argument
3271 if (item == NULL) in cJSON_IsString()
3276 return (item->type & 0xFF) == cJSON_String; in cJSON_IsString()
3279 CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item) in cJSON_IsArray() argument
3281 if (item == NULL) in cJSON_IsArray()
3286 return (item->type & 0xFF) == cJSON_Array; in cJSON_IsArray()
3289 CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item) in cJSON_IsObject() argument
3291 if (item == NULL) in cJSON_IsObject()
3296 return (item->type & 0xFF) == cJSON_Object; in cJSON_IsObject()
3299 CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item) in cJSON_IsRaw() argument
3301 if (item == NULL) in cJSON_IsRaw()
3306 return (item->type & 0xFF) == cJSON_Raw; in cJSON_IsRaw()
3311 if ((a == NULL) || (b == NULL) || ((a->type & 0xFF) != (b->type & 0xFF))) in cJSON_Compare()
3317 switch (a->type & 0xFF) in cJSON_Compare()
3339 switch (a->type & 0xFF) in cJSON_Compare()
3349 if (!compare_double(a->valuedouble, b->valuedouble)) in cJSON_Compare()
3354 if ((a->type & cJSON_IsInt64) != (b->type & cJSON_IsInt64)) in cJSON_Compare()
3360 if ((a->type & cJSON_IsInt64) && (b->type & cJSON_IsInt64)) in cJSON_Compare()
3363 return a->valueint == b->valueint; in cJSON_Compare()
3369 if (compare_double(a->valuedouble, b->valuedouble)) in cJSON_Compare()
3378 if ((a->valuestring == NULL) || (b->valuestring == NULL)) in cJSON_Compare()
3382 if (strcmp(a->valuestring, b->valuestring) == 0) in cJSON_Compare()
3391 cJSON *a_element = a->child; in cJSON_Compare()
3392 cJSON *b_element = b->child; in cJSON_Compare()
3401 a_element = a_element->next; in cJSON_Compare()
3402 b_element = b_element->next; in cJSON_Compare()
3420 b_element = get_object_item(b, a_element->string, case_sensitive); in cJSON_Compare()
3436 a_element = get_object_item(a, b_element->string, case_sensitive); in cJSON_Compare()