refactor cJSONUtils_PatchDetach

This commit is contained in:
Max Bruckner 2017-04-30 13:01:04 +02:00
parent 4ba6bafe34
commit 2040ce9004

View File

@ -354,52 +354,55 @@ static cJSON *detach_item_from_array(cJSON *array, size_t which)
static cJSON *cJSONUtils_PatchDetach(cJSON *object, const unsigned char *path) static cJSON *cJSONUtils_PatchDetach(cJSON *object, const unsigned char *path)
{ {
unsigned char *parentptr = NULL; unsigned char *parent_pointer = NULL;
unsigned char *childptr = NULL; unsigned char *child_pointer = NULL;
cJSON *parent = NULL; cJSON *parent = NULL;
cJSON *ret = NULL; cJSON *detached_item = NULL;
/* copy path and split it in parent and child */ /* copy path and split it in parent and child */
parentptr = cJSONUtils_strdup(path); parent_pointer = cJSONUtils_strdup(path);
if (parentptr == NULL) { if (parent_pointer == NULL) {
return NULL; goto cleanup;
} }
childptr = (unsigned char*)strrchr((char*)parentptr, '/'); /* last '/' */ child_pointer = (unsigned char*)strrchr((char*)parent_pointer, '/'); /* last '/' */
if (childptr == NULL) if (child_pointer == NULL)
{ {
cJSON_free(parentptr); goto cleanup;
return NULL;
} }
/* split strings */ /* split strings */
*childptr++ = '\0'; child_pointer[0] = '\0';
child_pointer++;
parent = cJSONUtils_GetPointer(object, (char*)parentptr); parent = cJSONUtils_GetPointer(object, (char*)parent_pointer);
cJSONUtils_InplaceDecodePointerString(childptr); cJSONUtils_InplaceDecodePointerString(child_pointer);
if (!parent) if (cJSON_IsArray(parent))
{
/* Couldn't find object to remove child from. */
ret = NULL;
}
else if (cJSON_IsArray(parent))
{ {
size_t index = 0; size_t index = 0;
if (!decode_array_index_from_pointer(childptr, &index)) if (!decode_array_index_from_pointer(child_pointer, &index))
{ {
cJSON_free(parentptr); goto cleanup;
return NULL;
} }
ret = detach_item_from_array(parent, index); detached_item = detach_item_from_array(parent, index);
} }
else if (cJSON_IsObject(parent)) else if (cJSON_IsObject(parent))
{ {
ret = cJSON_DetachItemFromObject(parent, (char*)childptr); detached_item = cJSON_DetachItemFromObject(parent, (char*)child_pointer);
}
else
{
/* Couldn't find object to remove child from. */
goto cleanup;
} }
cJSON_free(parentptr);
/* return the detachted item */ cleanup:
return ret; if (parent_pointer != NULL)
{
cJSON_free(parent_pointer);
}
return detached_item;
} }
static int cJSONUtils_Compare(cJSON *a, cJSON *b) static int cJSONUtils_Compare(cJSON *a, cJSON *b)