Add cJSON_DetachItemViaPointer

This commit is contained in:
Max Bruckner
2017-05-01 23:15:00 +02:00
parent cc3a75dd39
commit c24c3e0285
3 changed files with 71 additions and 0 deletions

30
cJSON.c
View File

@ -1814,6 +1814,36 @@ CJSON_PUBLIC(void) cJSON_AddItemReferenceToObject(cJSON *object, const char *str
cJSON_AddItemToObject(object, string, create_reference(item, &global_hooks));
}
CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item)
{
if ((parent == NULL) || (item == NULL))
{
return NULL;
}
if (item->prev != NULL)
{
/* not the first element */
item->prev->next = item->next;
}
if (item->next != NULL)
{
/* not the last element */
item->next->prev = item->prev;
}
if (item == parent->child)
{
/* first element */
parent->child = item->next;
}
/* make sure the detached item doesn't point anywhere anymore */
item->prev = NULL;
item->next = NULL;
return item;
}
static cJSON *DetachItemFromArray(cJSON *array, size_t which)
{
cJSON *c = array->child;