diff options
| author | 2026-04-27 01:45:33 +0200 | |
|---|---|---|
| committer | 2026-04-27 01:45:33 +0200 | |
| commit | d35b31d0a2661b6346a1592a5eb7b70e66b2a141 (patch) | |
| tree | a65156248dfd4df7c9301daf2a786a3469586147 /include | |
| parent | dddb76d74a646a7ec41cfa6b9f7772830b7acbd2 (diff) | |
| download | libplist-d35b31d0a2661b6346a1592a5eb7b70e66b2a141.tar.gz libplist-d35b31d0a2661b6346a1592a5eb7b70e66b2a141.tar.bz2 | |
Add error handling to all modification functions
Convert all array/dict modification functions from void to plist_err_t return type:
- plist_array_set_item: replace at index n
- plist_array_append_item: append to end
- plist_array_insert_item: insert at position n
- plist_array_remove_item: remove item at index n
- plist_array_item_remove: remove item from its array parent
- plist_dict_set_item: replace or insert key/value
- plist_dict_remove_item: remove key/value pair
- plist_dict_merge: merge source dict into target
Returns:
- PLIST_ERR_SUCCESS on success
- PLIST_ERR_INVALID_ARG for invalid arguments (NULL, wrong type, out of range, etc.)
- PLIST_ERR_NO_MEM on memory allocation failure
- PLIST_ERR_UNKNOWN on unexpected internal errors
Header documentation updated with full error code semantics for each function.
Diffstat (limited to 'include')
| -rw-r--r-- | include/plist/plist.h | 81 |
1 files changed, 69 insertions, 12 deletions
diff --git a/include/plist/plist.h b/include/plist/plist.h index 94930b8..e720aac 100644 --- a/include/plist/plist.h +++ b/include/plist/plist.h | |||
| @@ -347,43 +347,77 @@ extern "C" | |||
| 347 | * | 347 | * |
| 348 | * @param node the node of type #PLIST_ARRAY | 348 | * @param node the node of type #PLIST_ARRAY |
| 349 | * @param item the new item at index n. The array is responsible for freeing item when it is no longer needed. | 349 | * @param item the new item at index n. The array is responsible for freeing item when it is no longer needed. |
| 350 | * @param n the index of the item to get. Range is [0, array_size[. Assert if n is not in range. | 350 | * @param n the index of the item to get. Range is [0, array_size[. |
| 351 | * | ||
| 352 | * @return | ||
| 353 | * - PLIST_ERR_SUCCESS on success. | ||
| 354 | * - PLIST_ERR_INVALID_ARG if node is not an array, item is NULL, item is already | ||
| 355 | * attached to a parent, or n is outside the valid range. | ||
| 356 | * - PLIST_ERR_NO_MEM if replacing the item fails due to memory allocation failure | ||
| 357 | * and the original array element was restored successfully. | ||
| 358 | * - PLIST_ERR_UNKNOWN if an unexpected internal error occurred, including failure | ||
| 359 | * to restore the original array element after insertion of the replacement failed. | ||
| 351 | */ | 360 | */ |
| 352 | PLIST_API void plist_array_set_item(plist_t node, plist_t item, uint32_t n); | 361 | PLIST_API plist_err_t plist_array_set_item(plist_t node, plist_t item, uint32_t n); |
| 353 | 362 | ||
| 354 | /** | 363 | /** |
| 355 | * Append a new item at the end of a #PLIST_ARRAY node. | 364 | * Append a new item at the end of a #PLIST_ARRAY node. |
| 356 | * | 365 | * |
| 357 | * @param node the node of type #PLIST_ARRAY | 366 | * @param node the node of type #PLIST_ARRAY |
| 358 | * @param item the new item. The array is responsible for freeing item when it is no longer needed. | 367 | * @param item the new item. The array is responsible for freeing item when it is no longer needed. |
| 368 | * | ||
| 369 | * @return | ||
| 370 | * - PLIST_ERR_SUCCESS on success. | ||
| 371 | * - PLIST_ERR_INVALID_ARG if node is not an array, item is NULL, or item is | ||
| 372 | * already attached to a parent. | ||
| 373 | * - PLIST_ERR_NO_MEM if memory allocation failed while appending the item. | ||
| 374 | * - PLIST_ERR_UNKNOWN if an unexpected internal error occurred. | ||
| 359 | */ | 375 | */ |
| 360 | PLIST_API void plist_array_append_item(plist_t node, plist_t item); | 376 | PLIST_API plist_err_t plist_array_append_item(plist_t node, plist_t item); |
| 361 | 377 | ||
| 362 | /** | 378 | /** |
| 363 | * Insert a new item at position n in a #PLIST_ARRAY node. | 379 | * Insert a new item at position n in a #PLIST_ARRAY node. |
| 364 | * | 380 | * |
| 365 | * @param node the node of type #PLIST_ARRAY | 381 | * @param node the node of type #PLIST_ARRAY |
| 366 | * @param item the new item to insert. The array is responsible for freeing item when it is no longer needed. | 382 | * @param item the new item to insert. The array is responsible for freeing item when it is no longer needed. |
| 367 | * @param n The position at which the node will be stored. Range is [0, array_size[. Assert if n is not in range. | 383 | * @param n The position at which the node will be stored. Range is [0, array_size[. |
| 384 | * | ||
| 385 | * @return | ||
| 386 | * - PLIST_ERR_SUCCESS on success. | ||
| 387 | * - PLIST_ERR_INVALID_ARG if node is not an array, item is NULL, item is already | ||
| 388 | * attached to a parent, or n is outside the valid insertion range. | ||
| 389 | * - PLIST_ERR_NO_MEM if memory allocation failed while inserting the item. | ||
| 390 | * - PLIST_ERR_UNKNOWN if an unexpected internal error occurred. | ||
| 368 | */ | 391 | */ |
| 369 | PLIST_API void plist_array_insert_item(plist_t node, plist_t item, uint32_t n); | 392 | PLIST_API plist_err_t plist_array_insert_item(plist_t node, plist_t item, uint32_t n); |
| 370 | 393 | ||
| 371 | /** | 394 | /** |
| 372 | * Remove an existing position in a #PLIST_ARRAY node. | 395 | * Remove an existing position in a #PLIST_ARRAY node. |
| 373 | * Removed position will be freed using #plist_free. | 396 | * Removed position will be freed using #plist_free. |
| 374 | * | 397 | * |
| 375 | * @param node the node of type #PLIST_ARRAY | 398 | * @param node the node of type #PLIST_ARRAY |
| 376 | * @param n The position to remove. Range is [0, array_size[. Assert if n is not in range. | 399 | * @param n The position to remove. Range is [0, array_size[. |
| 400 | * | ||
| 401 | * @return | ||
| 402 | * - PLIST_ERR_SUCCESS on success. | ||
| 403 | * - PLIST_ERR_INVALID_ARG if node is not an array or n is outside the valid range. | ||
| 404 | * - PLIST_ERR_UNKNOWN if an unexpected internal error occurred while removing | ||
| 405 | * the item. | ||
| 377 | */ | 406 | */ |
| 378 | PLIST_API void plist_array_remove_item(plist_t node, uint32_t n); | 407 | PLIST_API plist_err_t plist_array_remove_item(plist_t node, uint32_t n); |
| 379 | 408 | ||
| 380 | /** | 409 | /** |
| 381 | * Remove a node that is a child node of a #PLIST_ARRAY node. | 410 | * Remove a node that is a child node of a #PLIST_ARRAY node. |
| 382 | * node will be freed using #plist_free. | 411 | * node will be freed using #plist_free. |
| 383 | * | 412 | * |
| 384 | * @param node The node to be removed from its #PLIST_ARRAY parent. | 413 | * @param node The node to be removed from its #PLIST_ARRAY parent. |
| 414 | * | ||
| 415 | * @return | ||
| 416 | * - PLIST_ERR_SUCCESS on success. | ||
| 417 | * - PLIST_ERR_INVALID_ARG if item is NULL or not a child of a #PLIST_ARRAY | ||
| 418 | * - PLIST_ERR_UNKNOWN if an unexpected internal error occurred. | ||
| 385 | */ | 419 | */ |
| 386 | PLIST_API void plist_array_item_remove(plist_t node); | 420 | PLIST_API plist_err_t plist_array_item_remove(plist_t item); |
| 387 | 421 | ||
| 388 | /** | 422 | /** |
| 389 | * Create an iterator of a #PLIST_ARRAY node. | 423 | * Create an iterator of a #PLIST_ARRAY node. |
| @@ -489,17 +523,32 @@ extern "C" | |||
| 489 | * @param node the node of type #PLIST_DICT | 523 | * @param node the node of type #PLIST_DICT |
| 490 | * @param item the new item associated to key | 524 | * @param item the new item associated to key |
| 491 | * @param key the identifier of the item to set. | 525 | * @param key the identifier of the item to set. |
| 526 | * | ||
| 527 | * @return | ||
| 528 | * - PLIST_ERR_SUCCESS on success. | ||
| 529 | * - PLIST_ERR_INVALID_ARG if node is not a dictionary, key is NULL, item is NULL, | ||
| 530 | * or item is already attached to a parent. | ||
| 531 | * - PLIST_ERR_NO_MEM if memory allocation failed while creating or inserting the | ||
| 532 | * key/value pair. | ||
| 533 | * - PLIST_ERR_UNKNOWN if an unexpected internal error occurred. | ||
| 492 | */ | 534 | */ |
| 493 | PLIST_API void plist_dict_set_item(plist_t node, const char* key, plist_t item); | 535 | PLIST_API plist_err_t plist_dict_set_item(plist_t node, const char* key, plist_t item); |
| 494 | 536 | ||
| 495 | /** | 537 | /** |
| 496 | * Remove an existing position in a #PLIST_DICT node. | 538 | * Remove an existing position in a #PLIST_DICT node. |
| 497 | * Removed position will be freed using #plist_free | 539 | * Removed position will be freed using #plist_free |
| 498 | * | 540 | * |
| 499 | * @param node the node of type #PLIST_DICT | 541 | * @param node the node of type #PLIST_DICT |
| 500 | * @param key The identifier of the item to remove. Assert if identifier is not present. | 542 | * @param key The identifier of the item to remove |
| 543 | * | ||
| 544 | * @return | ||
| 545 | * - PLIST_ERR_SUCCESS on success. | ||
| 546 | * - PLIST_ERR_INVALID_ARG if node is not a dictionary, key is NULL, or no item | ||
| 547 | * with the given key exists. | ||
| 548 | * - PLIST_ERR_UNKNOWN if an unexpected internal error occurred while removing | ||
| 549 | * the item. | ||
| 501 | */ | 550 | */ |
| 502 | PLIST_API void plist_dict_remove_item(plist_t node, const char* key); | 551 | PLIST_API plist_err_t plist_dict_remove_item(plist_t node, const char* key); |
| 503 | 552 | ||
| 504 | /** | 553 | /** |
| 505 | * Merge a dictionary into another. This will add all key/value pairs | 554 | * Merge a dictionary into another. This will add all key/value pairs |
| @@ -508,8 +557,16 @@ extern "C" | |||
| 508 | * | 557 | * |
| 509 | * @param target pointer to an existing node of type #PLIST_DICT | 558 | * @param target pointer to an existing node of type #PLIST_DICT |
| 510 | * @param source node of type #PLIST_DICT that should be merged into target | 559 | * @param source node of type #PLIST_DICT that should be merged into target |
| 560 | * | ||
| 561 | * @return | ||
| 562 | * - PLIST_ERR_SUCCESS on success. | ||
| 563 | * - PLIST_ERR_INVALID_ARG if target is NULL, source is NULL, source is not a | ||
| 564 | * dictionary, or *target is not NULL and does not point to a dictionary. | ||
| 565 | * - PLIST_ERR_NO_MEM if memory allocation failed while creating the target | ||
| 566 | * dictionary or copying items from source. | ||
| 567 | * - PLIST_ERR_UNKNOWN if an unexpected internal error occurred. | ||
| 511 | */ | 568 | */ |
| 512 | PLIST_API void plist_dict_merge(plist_t *target, plist_t source); | 569 | PLIST_API plist_err_t plist_dict_merge(plist_t *target, plist_t source); |
| 513 | 570 | ||
| 514 | /** | 571 | /** |
| 515 | * Get a boolean value from a given #PLIST_DICT entry. | 572 | * Get a boolean value from a given #PLIST_DICT entry. |
