detach an item at the given path */
| 435 | |
| 436 | /* detach an item at the given path */ |
| 437 | static cJSON *detach_path(cJSON *object, const unsigned char *path, const cJSON_bool case_sensitive) |
| 438 | { |
| 439 | unsigned char *parent_pointer = NULL; |
| 440 | unsigned char *child_pointer = NULL; |
| 441 | cJSON *parent = NULL; |
| 442 | cJSON *detached_item = NULL; |
| 443 | |
| 444 | /* copy path and split it in parent and child */ |
| 445 | parent_pointer = cJSONUtils_strdup(path); |
| 446 | if (parent_pointer == NULL) |
| 447 | { |
| 448 | goto cleanup; |
| 449 | } |
| 450 | |
| 451 | child_pointer = (unsigned char *)strrchr((char *)parent_pointer, '/'); /* last '/' */ |
| 452 | if (child_pointer == NULL) |
| 453 | { |
| 454 | goto cleanup; |
| 455 | } |
| 456 | /* split strings */ |
| 457 | child_pointer[0] = '\0'; |
| 458 | child_pointer++; |
| 459 | |
| 460 | parent = get_item_from_pointer(object, (char *)parent_pointer, case_sensitive); |
| 461 | decode_pointer_inplace(child_pointer); |
| 462 | |
| 463 | if (cJSON_IsArray(parent)) |
| 464 | { |
| 465 | size_t index = 0; |
| 466 | if (!decode_array_index_from_pointer(child_pointer, &index)) |
| 467 | { |
| 468 | goto cleanup; |
| 469 | } |
| 470 | detached_item = detach_item_from_array(parent, index); |
| 471 | } |
| 472 | else if (cJSON_IsObject(parent)) |
| 473 | { |
| 474 | detached_item = cJSON_DetachItemFromObject(parent, (char *)child_pointer); |
| 475 | } |
| 476 | else |
| 477 | { |
| 478 | /* Couldn't find object to remove child from. */ |
| 479 | goto cleanup; |
| 480 | } |
| 481 | |
| 482 | cleanup: |
| 483 | if (parent_pointer != NULL) |
| 484 | { |
| 485 | cJSON_free(parent_pointer); |
| 486 | } |
| 487 | |
| 488 | return detached_item; |
| 489 | } |
| 490 | |
| 491 | /* sort lists using mergesort */ |
| 492 | static cJSON *sort_list(cJSON *list, const cJSON_bool case_sensitive) |
no test coverage detected