MCPcopy Create free account
hub / github.com/OpenIPC/firmware / cJSON_ReplaceItemViaPointer

Function cJSON_ReplaceItemViaPointer

general/package/xmdp/src/cjson/cJSON.c:2295–2342  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2293}
2294
2295CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement)
2296{
2297 if ((parent == NULL) || (replacement == NULL) || (item == NULL))
2298 {
2299 return false;
2300 }
2301
2302 if (replacement == item)
2303 {
2304 return true;
2305 }
2306
2307 replacement->next = item->next;
2308 replacement->prev = item->prev;
2309
2310 if (replacement->next != NULL)
2311 {
2312 replacement->next->prev = replacement;
2313 }
2314 if (parent->child == item)
2315 {
2316 if (parent->child->prev == parent->child)
2317 {
2318 replacement->prev = replacement;
2319 }
2320 parent->child = replacement;
2321 }
2322 else
2323 { /*
2324 * To find the last item in array quickly, we use prev in array.
2325 * We can't modify the last item's next pointer where this item was the parent's child
2326 */
2327 if (replacement->prev != NULL)
2328 {
2329 replacement->prev->next = replacement;
2330 }
2331 if (replacement->next == NULL)
2332 {
2333 parent->child->prev = replacement;
2334 }
2335 }
2336
2337 item->next = NULL;
2338 item->prev = NULL;
2339 cJSON_Delete(item);
2340
2341 return true;
2342}
2343
2344CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem)
2345{

Callers 2

cJSON_ReplaceItemInArrayFunction · 0.85
replace_item_in_objectFunction · 0.85

Calls 1

cJSON_DeleteFunction · 0.85

Tested by

no test coverage detected