| 2351 | } |
| 2352 | |
| 2353 | CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement) |
| 2354 | { |
| 2355 | if ((parent == NULL) || (parent->child == NULL) || (replacement == NULL) || (item == NULL)) |
| 2356 | { |
| 2357 | return false; |
| 2358 | } |
| 2359 | |
| 2360 | if (replacement == item) |
| 2361 | { |
| 2362 | return true; |
| 2363 | } |
| 2364 | |
| 2365 | replacement->next = item->next; |
| 2366 | replacement->prev = item->prev; |
| 2367 | |
| 2368 | if (replacement->next != NULL) |
| 2369 | { |
| 2370 | replacement->next->prev = replacement; |
| 2371 | } |
| 2372 | if (parent->child == item) |
| 2373 | { |
| 2374 | if (parent->child->prev == parent->child) |
| 2375 | { |
| 2376 | replacement->prev = replacement; |
| 2377 | } |
| 2378 | parent->child = replacement; |
| 2379 | } |
| 2380 | else |
| 2381 | { /* |
| 2382 | * To find the last item in array quickly, we use prev in array. |
| 2383 | * We can't modify the last item's next pointer where this item was the parent's child |
| 2384 | */ |
| 2385 | if (replacement->prev != NULL) |
| 2386 | { |
| 2387 | replacement->prev->next = replacement; |
| 2388 | } |
| 2389 | if (replacement->next == NULL) |
| 2390 | { |
| 2391 | parent->child->prev = replacement; |
| 2392 | } |
| 2393 | } |
| 2394 | |
| 2395 | item->next = NULL; |
| 2396 | item->prev = NULL; |
| 2397 | cJSON_Delete(item); |
| 2398 | |
| 2399 | return true; |
| 2400 | } |
| 2401 | |
| 2402 | CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem) |
| 2403 | { |
searching dependent graphs…