From d3538b3997c79c049a18ae59daebfad7aaec0cc8 Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Wed, 26 Feb 2014 23:59:08 +0100 Subject: [PATCH 2/2] Import object_delete --- include/ucl.h | 15 +++++++++++++++ src/ucl_util.c | 23 +++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/include/ucl.h b/include/ucl.h index 0a2fce9..ff52c26 100644 --- a/include/ucl.h +++ b/include/ucl.h @@ -369,6 +369,21 @@ UCL_EXTERN ucl_object_t* ucl_object_replace_key (ucl_object_t *top, ucl_object_t const char *key, size_t keylen, bool copy_key) UCL_WARN_UNUSED_RESULT; /** + * Delete a object associated with key 'key', old object will be unrefered, + * @param top object + * @param key key associated to the object to remove + * @param keylen length of the key (or 0 for NULL terminated keys) + */ +UCL_EXTERN bool ucl_object_delete_keyl (ucl_object_t *top, const char *key, size_t keylen); + +/** + * Delete a object associated with key 'key', old object will be unrefered, + * @param top object + * @param key key associated to the object to remove + */ +UCL_EXTERN bool ucl_object_delete_key (ucl_object_t *top, const char *key); + +/** * Insert a object 'elt' to the hash 'top' and associate it with key 'key', if the specified key exist, * try to merge its content * @param top destination object (will be created automatically if top is NULL) diff --git a/src/ucl_util.c b/src/ucl_util.c index cbe525d..5b9dca6 100644 --- a/src/ucl_util.c +++ b/src/ucl_util.c @@ -1147,6 +1147,29 @@ ucl_object_insert_key_common (ucl_object_t *top, ucl_object_t *elt, return top; } +bool +ucl_object_delete_keyl(ucl_object_t *top, const char *key, size_t keylen) +{ + ucl_object_t *found; + + found = ucl_object_find_keyl(top, key, keylen); + + if (found == NULL) + return false; + + ucl_hash_delete(top->value.ov, found); + ucl_object_unref (found); + top->len --; + + return true; +} + +bool +ucl_object_delete_key(ucl_object_t *top, const char *key) +{ + return ucl_object_delete_keyl(top, key, 0); +} + ucl_object_t * ucl_object_insert_key (ucl_object_t *top, ucl_object_t *elt, const char *key, size_t keylen, bool copy_key) -- 1.8.5.2