MCPcopy Create free account
hub / github.com/0xShug0/audio.cpp / cJSON_ReplaceItemViaPointer

Function cJSON_ReplaceItemViaPointer

external/cJSON/cJSON.c:2368–2415  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2366}
2367
2368CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement)
2369{
2370 if ((parent == NULL) || (parent->child == NULL) || (replacement == NULL) || (item == NULL))
2371 {
2372 return false;
2373 }
2374
2375 if (replacement == item)
2376 {
2377 return true;
2378 }
2379
2380 replacement->next = item->next;
2381 replacement->prev = item->prev;
2382
2383 if (replacement->next != NULL)
2384 {
2385 replacement->next->prev = replacement;
2386 }
2387 if (parent->child == item)
2388 {
2389 if (parent->child->prev == parent->child)
2390 {
2391 replacement->prev = replacement;
2392 }
2393 parent->child = replacement;
2394 }
2395 else
2396 { /*
2397 * To find the last item in array quickly, we use prev in array.
2398 * We can't modify the last item's next pointer where this item was the parent's child
2399 */
2400 if (replacement->prev != NULL)
2401 {
2402 replacement->prev->next = replacement;
2403 }
2404 if (replacement->next == NULL)
2405 {
2406 parent->child->prev = replacement;
2407 }
2408 }
2409
2410 item->next = NULL;
2411 item->prev = NULL;
2412 cJSON_Delete(item);
2413
2414 return true;
2415}
2416
2417CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem)
2418{

Callers 2

cJSON_ReplaceItemInArrayFunction · 0.85
replace_item_in_objectFunction · 0.85

Calls 1

cJSON_DeleteFunction · 0.85

Tested by

no test coverage detected