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