mirror of
https://github.com/DaveGamble/cJSON.git
synced 2023-08-10 21:13:26 +03:00
Internal function get_array_item
This commit is contained in:
parent
acb0ca88df
commit
33193ea557
87
cJSON.c
87
cJSON.c
@ -1665,16 +1665,33 @@ CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array)
|
||||
return (int)i;
|
||||
}
|
||||
|
||||
CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int item)
|
||||
static cJSON* get_array_item(const cJSON *array, size_t index)
|
||||
{
|
||||
cJSON *c = array ? array->child : NULL;
|
||||
while (c && item > 0)
|
||||
cJSON *current_child = NULL;
|
||||
|
||||
if (array == NULL)
|
||||
{
|
||||
item--;
|
||||
c = c->next;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return c;
|
||||
current_child = array->child;
|
||||
while ((current_child != NULL) && (index > 0))
|
||||
{
|
||||
index--;
|
||||
current_child = current_child->next;
|
||||
}
|
||||
|
||||
return current_child;
|
||||
}
|
||||
|
||||
CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index)
|
||||
{
|
||||
if (index < 0)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return get_array_item(array, (size_t)index);
|
||||
}
|
||||
|
||||
static cJSON *get_object_item(const cJSON * const object, const char * const name, const cJSON_bool case_sensitive)
|
||||
@ -1844,17 +1861,6 @@ CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const it
|
||||
return item;
|
||||
}
|
||||
|
||||
static cJSON *DetachItemFromArray(cJSON *array, size_t which)
|
||||
{
|
||||
cJSON *c = array->child;
|
||||
while (c && (which > 0))
|
||||
{
|
||||
c = c->next;
|
||||
which--;
|
||||
}
|
||||
|
||||
return cJSON_DetachItemViaPointer(array, c);
|
||||
}
|
||||
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which)
|
||||
{
|
||||
if (which < 0)
|
||||
@ -1862,7 +1868,7 @@ CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return DetachItemFromArray(array, (size_t)which);
|
||||
return cJSON_DetachItemViaPointer(array, get_array_item(array, (size_t)which));
|
||||
}
|
||||
|
||||
CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which)
|
||||
@ -1889,21 +1895,24 @@ CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string)
|
||||
/* Replace array/object items with new ones. */
|
||||
CJSON_PUBLIC(void) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem)
|
||||
{
|
||||
cJSON *c = array->child;
|
||||
while (c && (which > 0))
|
||||
cJSON *after_inserted = NULL;
|
||||
|
||||
if (which < 0)
|
||||
{
|
||||
c = c->next;
|
||||
which--;
|
||||
return;
|
||||
}
|
||||
if (!c)
|
||||
|
||||
after_inserted = get_array_item(array, (size_t)which);
|
||||
if (after_inserted == NULL)
|
||||
{
|
||||
cJSON_AddItemToArray(array, newitem);
|
||||
return;
|
||||
}
|
||||
newitem->next = c;
|
||||
newitem->prev = c->prev;
|
||||
c->prev = newitem;
|
||||
if (c == array->child)
|
||||
|
||||
newitem->next = after_inserted;
|
||||
newitem->prev = after_inserted->prev;
|
||||
after_inserted->prev = newitem;
|
||||
if (after_inserted == array->child)
|
||||
{
|
||||
array->child = newitem;
|
||||
}
|
||||
@ -1915,23 +1924,20 @@ CJSON_PUBLIC(void) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newit
|
||||
|
||||
static void ReplaceItemInArray(cJSON *array, size_t which, cJSON *newitem)
|
||||
{
|
||||
cJSON *c = array->child;
|
||||
while (c && (which > 0))
|
||||
{
|
||||
c = c->next;
|
||||
which--;
|
||||
}
|
||||
if (!c)
|
||||
cJSON *replaced = get_array_item(array, which);
|
||||
|
||||
if (replaced == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
newitem->next = c->next;
|
||||
newitem->prev = c->prev;
|
||||
|
||||
newitem->next = replaced->next;
|
||||
newitem->prev = replaced->prev;
|
||||
if (newitem->next)
|
||||
{
|
||||
newitem->next->prev = newitem;
|
||||
}
|
||||
if (c == array->child)
|
||||
if (replaced == array->child)
|
||||
{
|
||||
array->child = newitem;
|
||||
}
|
||||
@ -1939,8 +1945,11 @@ static void ReplaceItemInArray(cJSON *array, size_t which, cJSON *newitem)
|
||||
{
|
||||
newitem->prev->next = newitem;
|
||||
}
|
||||
c->next = c->prev = NULL;
|
||||
cJSON_Delete(c);
|
||||
|
||||
replaced->next = NULL;
|
||||
replaced->prev = NULL;
|
||||
|
||||
cJSON_Delete(replaced);
|
||||
}
|
||||
CJSON_PUBLIC(void) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem)
|
||||
{
|
||||
|
2
cJSON.h
2
cJSON.h
@ -153,7 +153,7 @@ CJSON_PUBLIC(void) cJSON_Delete(cJSON *c);
|
||||
/* Returns the number of items in an array (or object). */
|
||||
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array);
|
||||
/* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int item);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index);
|
||||
/* Get item "string" from object. Case insensitive. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON *object, const char *string);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string);
|
||||
|
Loading…
Reference in New Issue
Block a user