diff options
| author | 2014-03-19 02:26:09 +0100 | |
|---|---|---|
| committer | 2014-03-19 02:26:09 +0100 | |
| commit | f9299fa80a7530fea4b829ea851972a664edf1fe (patch) | |
| tree | 94cf6141a7ef3c4661f05d33cb26e05d6e1b2410 | |
| parent | 6dcc8c48bb372ad372ffb1848bd24c6b416fd16e (diff) | |
| download | libplist-f9299fa80a7530fea4b829ea851972a664edf1fe.tar.gz libplist-f9299fa80a7530fea4b829ea851972a664edf1fe.tar.bz2 | |
plist_dict_set_item: insert key/value pair if key not already present
| -rw-r--r-- | include/plist/plist.h | 9 | ||||
| -rw-r--r-- | src/plist.c | 21 |
2 files changed, 16 insertions, 14 deletions
diff --git a/include/plist/plist.h b/include/plist/plist.h index 41588a8..b482199 100644 --- a/include/plist/plist.h +++ b/include/plist/plist.h | |||
| @@ -310,20 +310,21 @@ extern "C" | |||
| 310 | 310 | ||
| 311 | /** | 311 | /** |
| 312 | * Set item identified by key in a #PLIST_DICT node. | 312 | * Set item identified by key in a #PLIST_DICT node. |
| 313 | * The previous item at index n will be freed using #plist_free | 313 | * The previous item identified by key will be freed using #plist_free. |
| 314 | * If there is no item for the given key a new item will be inserted. | ||
| 314 | * | 315 | * |
| 315 | * @param node the node of type #PLIST_DICT | 316 | * @param node the node of type #PLIST_DICT |
| 316 | * @param item the new item associated to key | 317 | * @param item the new item associated to key |
| 317 | * @param key the identifier of the item to get. Assert if identifier is not present. | 318 | * @param key the identifier of the item to set. |
| 318 | */ | 319 | */ |
| 319 | PLIST_API void plist_dict_set_item(plist_t node, const char* key, plist_t item); | 320 | PLIST_API void plist_dict_set_item(plist_t node, const char* key, plist_t item); |
| 320 | 321 | ||
| 321 | /** | 322 | /** |
| 322 | * Insert a new item at position n in a #PLIST_DICT node. | 323 | * Insert a new item into a #PLIST_DICT node. |
| 323 | * | 324 | * |
| 324 | * @param node the node of type #PLIST_DICT | 325 | * @param node the node of type #PLIST_DICT |
| 325 | * @param item the new item to insert | 326 | * @param item the new item to insert |
| 326 | * @param key The identifier of the item to insert. Assert if identifier already present. | 327 | * @param key The identifier of the item to insert. |
| 327 | */ | 328 | */ |
| 328 | PLIST_API void plist_dict_insert_item(plist_t node, const char* key, plist_t item); | 329 | PLIST_API void plist_dict_insert_item(plist_t node, const char* key, plist_t item); |
| 329 | 330 | ||
diff --git a/src/plist.c b/src/plist.c index e077ad9..f33de0a 100644 --- a/src/plist.c +++ b/src/plist.c | |||
| @@ -405,17 +405,18 @@ plist_t plist_dict_get_item(plist_t node, const char* key) | |||
| 405 | 405 | ||
| 406 | void plist_dict_set_item(plist_t node, const char* key, plist_t item) | 406 | void plist_dict_set_item(plist_t node, const char* key, plist_t item) |
| 407 | { | 407 | { |
| 408 | if (node && PLIST_DICT == plist_get_node_type(node)) | 408 | if (node && PLIST_DICT == plist_get_node_type(node)) { |
| 409 | { | ||
| 410 | node_t* old_item = plist_dict_get_item(node, key); | 409 | node_t* old_item = plist_dict_get_item(node, key); |
| 411 | if (old_item) | 410 | if (old_item) { |
| 412 | { | ||
| 413 | int idx = plist_free_node(old_item); | 411 | int idx = plist_free_node(old_item); |
| 414 | if (idx < 0) { | 412 | if (idx < 0) { |
| 415 | node_attach(node, item); | 413 | node_attach(node, item); |
| 416 | } else { | 414 | } else { |
| 417 | node_insert(node, idx, item); | 415 | node_insert(node, idx, item); |
| 418 | } | 416 | } |
| 417 | } else { | ||
| 418 | node_attach(node, plist_new_key(key)); | ||
| 419 | node_attach(node, item); | ||
| 419 | } | 420 | } |
| 420 | } | 421 | } |
| 421 | return; | 422 | return; |
| @@ -466,7 +467,7 @@ void plist_dict_merge(plist_t *target, plist_t source) | |||
| 466 | if (plist_dict_get_item(*target, key) != NULL) | 467 | if (plist_dict_get_item(*target, key) != NULL) |
| 467 | plist_dict_remove_item(*target, key); | 468 | plist_dict_remove_item(*target, key); |
| 468 | 469 | ||
| 469 | plist_dict_insert_item(*target, key, plist_copy(subnode)); | 470 | plist_dict_set_item(*target, key, plist_copy(subnode)); |
| 470 | free(key); | 471 | free(key); |
| 471 | key = NULL; | 472 | key = NULL; |
| 472 | } while (1); | 473 | } while (1); |
