From 0b921c6c1c2090803e2bbd16aa4dae739c7737f0 Mon Sep 17 00:00:00 2001 From: Jiwoong Im Date: Tue, 3 Mar 2015 10:13:07 +0900 Subject: [PATCH] Update source from tizen 2.3 - modify error return to use tizen error code. - fix coding style JIRA ticket : TC-2468 Change-Id: I7352f02d226249474b86e763bbe6be263fd2fc75 Signed-off-by: Jiwoong Im --- CMakeLists.txt | 2 +- bundle.pc.in | 1 + include/bundle.h | 913 +++++++++++++++++++++++++------------------------ include/keyval_array.h | 1 + packaging/bundle.spec | 1 + src/bundle.c | 638 +++++++++++++++++++--------------- src/keyval.c | 128 +++---- src/keyval_array.c | 226 ++++++------ src/keyval_type.c | 30 +- 9 files changed, 1032 insertions(+), 908 deletions(-) mode change 100755 => 100644 src/bundle.c diff --git a/CMakeLists.txt b/CMakeLists.txt index edc88ca..bbab736 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,7 @@ set (VERSION "0.1.31") ### Required packages INCLUDE(FindPkgConfig) -pkg_check_modules(pkgs REQUIRED glib-2.0 dlog) +pkg_check_modules(pkgs REQUIRED glib-2.0 dlog capi-base-common) FOREACH(flag ${pkgs_CFLAGS}) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") ENDFOREACH(flag) diff --git a/bundle.pc.in b/bundle.pc.in index f1d9d42..399ea0b 100644 --- a/bundle.pc.in +++ b/bundle.pc.in @@ -8,5 +8,6 @@ includedir=${prefix}/include Name: bundle Description: Simple string key/val dictionary library Version: @VERSION@ +Requires: capi-base-common Libs: -L${libdir} -lbundle Cflags: -I${includedir} diff --git a/include/bundle.h b/include/bundle.h index c1dc415..03b970f 100755 --- a/include/bundle.h +++ b/include/bundle.h @@ -26,28 +26,17 @@ /** * @file bundle.h - * @version 0.1 - * @brief This file declares API of bundle library + * @brief This file declares has API of the bundle library */ /** - * @addtogroup APPLICATION_FRAMEWORK - * @{ - * - * @defgroup bundle - * @version 0.1 - * - * @section Header to use them: - * @code - * #include - * @endcode - * - * @addtogroup bundle + * @addtogroup CORE_LIB_BUNDLE_MODULE * @{ */ #include #include +#include #ifdef __cplusplus extern "C" { @@ -58,12 +47,27 @@ extern "C" { #define unlikely(x) __builtin_expect(x,0) /** - * bundle is an opaque type pointing a bundle object + * @brief Enumeration for error code of Bundle. + * + * @since_tizen 2.3 + */ +typedef enum { + BUNDLE_ERROR_NONE = TIZEN_ERROR_NONE, /**< Successful */ + BUNDLE_ERROR_OUT_OF_MEMORY = TIZEN_ERROR_OUT_OF_MEMORY, /**< Out of memory */ + BUNDLE_ERROR_INVALID_PARAMETER = TIZEN_ERROR_INVALID_PARAMETER, /**< Invalid parameter */ + BUNDLE_ERROR_KEY_NOT_AVAILABLE = TIZEN_ERROR_KEY_NOT_AVAILABLE, /**< Required key not available */ + BUNDLE_ERROR_KEY_EXISTS = TIZEN_ERROR_FILE_EXISTS /**< Key exists */ +} bundle_error_e; + +/** + * @brief The bundle handle. + * @since_tizen 2.3 */ typedef struct _bundle_t bundle; /** - * bundle_raw is an encoded data type + * @brief The encoded data type. + * @since_tizen 2.3 * @see bundle_encode() * @see bundle_decode() */ @@ -71,32 +75,39 @@ typedef unsigned char bundle_raw; /** - * Each bundle keyval have a type. + * @brief Enumeration for key-value pair types. + * @since_tizen 2.3 */ enum bundle_type_property { - BUNDLE_TYPE_ARRAY = 0x0100, - BUNDLE_TYPE_PRIMITIVE = 0x0200, - BUNDLE_TYPE_MEASURABLE = 0x0400 + BUNDLE_TYPE_ARRAY = 0x0100, /**< Array type */ + BUNDLE_TYPE_PRIMITIVE = 0x0200, /**< Primitive type */ + BUNDLE_TYPE_MEASURABLE = 0x0400 /**< Measurable type */ }; +/** + * @brief Enumeration for bundle types. + * @since_tizen 2.3 + */ enum bundle_type { - BUNDLE_TYPE_NONE = -1, - BUNDLE_TYPE_ANY = 0, - BUNDLE_TYPE_STR = 1 | BUNDLE_TYPE_MEASURABLE, /* Default */ - BUNDLE_TYPE_STR_ARRAY = BUNDLE_TYPE_STR | BUNDLE_TYPE_ARRAY | BUNDLE_TYPE_MEASURABLE, - BUNDLE_TYPE_BYTE = 2, - BUNDLE_TYPE_BYTE_ARRAY = BUNDLE_TYPE_BYTE | BUNDLE_TYPE_ARRAY + BUNDLE_TYPE_NONE = -1, /**< None */ + BUNDLE_TYPE_ANY = 0, /**< Any type */ + BUNDLE_TYPE_STR = 1 | BUNDLE_TYPE_MEASURABLE, /**< String type (Default) */ + BUNDLE_TYPE_STR_ARRAY = BUNDLE_TYPE_STR | BUNDLE_TYPE_ARRAY | BUNDLE_TYPE_MEASURABLE, /**< String array type */ + BUNDLE_TYPE_BYTE = 2, /**< Byte type */ + BUNDLE_TYPE_BYTE_ARRAY = BUNDLE_TYPE_BYTE | BUNDLE_TYPE_ARRAY /**< Byte array type */ }; /** - * A keyval object in a bundle. + * @brief The key-value pair handle. + * @since_tizen 2.3 * @see bundle_iterator_t */ typedef struct keyval_t bundle_keyval_t; /** - * bundle_iterator is a new iterator function type for bundle_foreach() + * @brief Called for every key-value pair. + * @since_tizen 2.3 * @see bundle_foreach() */ typedef void (*bundle_iterator_t) ( @@ -108,22 +119,24 @@ typedef void (*bundle_iterator_t) ( /** - * bundle_iterate_cb_t is an iterator function type for bundle_iterate() + * @internal + * @brief Called for every key-value pair. + * @since_tizen 2.3 + * @remarks This type is obsolete. You must not use this type any more. * @see bundle_iterate() - * @remark This type is obsolete. Do not use this type any more. */ typedef void (*bundle_iterate_cb_t) (const char *key, const char *val, void *data); -/** - * @brief Create a bundle object. - * @pre None - * @post None +/** + * @brief Creates a bundle object. + * @since_tizen 2.3 + * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section. + * @return The bundle object + * @retval @c NULL - Failure + * @exception BUNDLE_ERROR_NONE Success + * @exception BUNDLE_ERROR_OUT_OF_MEMORY Out of memory * @see bundle_free() - * @return bundle object - * @retval NULL on failure creating an object - * @remark When NULL is returned, errno is set to one of the following values; \n - * ENOMEM : No memory to create an object * @code #include @@ -134,15 +147,14 @@ typedef void (*bundle_iterate_cb_t) (const char *key, const char *val, void *dat API bundle* bundle_create(void); /** - * @brief Free given bundle object with key/values in it - * @pre b must be a valid bundle object. - * @post None + * @brief Frees the given bundle object with key-value pairs in it. + * @since_tizen 2.3 + * @param[in] b The bundle object to be freed + * @return The operation result; + * @retval BUNDLE_ERROR_NONE Success + * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter + * @pre @a b must be a valid bundle object. * @see bundle_create() - * @param[in] b bundle object to be freed - * @return Operation result; - * @retval 0 success - * @retval -1 failure - * @remark None @code #include bundle *b = bundle_create(); // Create new bundle object @@ -150,24 +162,22 @@ API bundle* bundle_create(void); @endcode */ API int bundle_free(bundle *b); + /** - * @brief Add a string array type key-value pair into bundle. - * @pre b must be a valid bundle object. - * @post None + * @brief Adds a strings array type key-value pair into a given bundle. + * @since_tizen 2.3 + * @param[in] b The bundle object + * @param[in] key The key + * @param[in] str_array The string type value; if @c NULL, an empty array is created; you can change an item with + * @param[in] len The length of the array + * @return The operation result + * @retval BUNDLE_ERROR_NONE Success + * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter + * @retval BUNDLE_ERROR_KEY_EXISTS Key already exists + * @retval BUNDLE_ERROR_OUT_OF_MEMORY Out of memory + * @pre @a b must be a valid bundle object. * @see bundle_get_str_array() - * @see bundle_set_str_array_element() - * @param[in] b bundle object - * @param[in] key key - * @param[in] str_array string type value. If NULL, empty array is created. You can change an item with - * @param[in] len Length of array. - * @return Operation result - * @retval 0 success - * @retval -1 failure * - * @remark When -1 is returned, errno is set to one of the following values; \n - EKEYREJECTED : key is rejected (NULL or sth) \n - EPERM : key is already exist, not permitted to overwrite value \n - EINVAL : b or val is not valid (NULL or sth) \n @code #include char *sa = { "aaa", "bbb", "ccc" }; // A string array of length 3 @@ -177,22 +187,21 @@ API int bundle_free(bundle *b); @endcode */ API int bundle_add_str_array(bundle *b, const char *key, const char **str_array, const int len); + /** - * @brief Add a string type key-value pair into bundle. - * @pre b must be a valid bundle object. - * @post None + * @internal + * @brief Adds a string type key-value pair into a given bundle. + * @since_tizen 2.3 + * @param[in] b The bundle object + * @param[in] key The key + * @param[in] val The value + * @return The operation result + * @retval BUNDLE_ERROR_NONE Success + * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter + * @retval BUNDLE_ERROR_KEY_EXISTS Key already exists + * @retval BUNDLE_ERROR_OUT_OF_MEMORY Out of memory + * @pre @a b must be a valid bundle object. * @see bundle_add_str() - * @param[in] b bundle object - * @param[in] key key - * @param[in] val value - * @return Operation result - * @retval 0 success - * @retval -1 failure - * - * @remark When -1 is returned, errno is set to one of the following values; \n - EKEYREJECTED : key is rejected (NULL or sth) \n - EPERM : key is already exist, not permitted to overwrite value \n - EINVAL : b or val is not valid (NULL or sth) \n @code #include bundle *b = bundle_create(); // Create new bundle object @@ -204,53 +213,46 @@ API int bundle_add_str_array(bundle *b, const char *key, const char **str_array, API int bundle_add(bundle *b, const char *key, const char *val); /** - * @brief Delete val with given key - * @pre b must be a valid bundle object. - * @post None - * @see None - * @param[in] b bundle object - * @param[in] key given key - * @return Operation result - * @retval 0 Success - * @retval -1 Failure - * - * @remark When -1 is returned, errno is set to one of the following values; \n - EINVAL : b is invalid (NULL or sth) \n - ENOKEY : No key exist \n - EKEYREJECTED : key is invalid (NULL or sth) \n + * @brief Deletes a key-value object with the given key. + * @since_tizen 2.3 + * @param[in] b The bundle object + * @param[in] key The given key + * @return The operation result + * @retval BUNDLE_ERROR_NONE Success + * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter + * @retval BUNDLE_ERROR_KEY_NOT_AVAILABLE Key not available + * @pre @a b must be a valid bundle object. @code #include bundle *b = bundle_create(); // Create new bundle object - bundle_add(b, "foo_key", "bar_val"); // add a key-val pair + bundle_add_str(b, "foo_key", "bar_val"); // add a key-val pair bundle_del(b, "foo_key"); // del "foo_key" from b bundle_free(b); @endcode */ API int bundle_del(bundle *b, const char* key); + /** - * @brief Get string array value from key - * @pre b must be a valid bundle object. - * @post None + * @brief Gets a string array from a given key. + * @since_tizen 2.3 + * @remarks You MUST free or modify the returned string! + * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section. + * @param[in] b The bundle object + * @param[in] key The key + * @param[out] len The array length + * @return The pointer to the array of strings + * @retval @c NULL - Key not found + * @exception BUNDLE_ERROR_NONE Success + * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter + * @exception BUNDLE_ERROR_KEY_NOT_AVAILABLE Key not available + * @pre @a b must be a valid bundle object. * @see bundle_add_str_array() - * @see bundle_set_str_array_element() - * @param[in] b bundle object - * @param[in] key key - * @param[out] len array length - * @return Pointer to array of string - * @retval NULL If key is not found, returns NULL. - * @remark DO NOT free or modify returned string! - When NULL is returned, errno is set to one of the following values; \n - EINVAL : b is invalid \n - ENOKEY : No key exists \n - EKEYREJECTED : invalid key (NULL or sth) \n @code #include bundle *b = bundle_create(); - bundle_add_str_array(b, "foo", NULL, 3); // add a key-val pair - bundle_set_str_array_element(b, "foo", 0, "aaa"); - bundle_set_str_array_element(b, "foo", 1, "bbb"); - bundle_set_str_array_element(b, "foo", 2, "ccc"); + char *sa = { "aaa", "bbb", "ccc" }; // A string array of length 3 + bundle_add_str_array(b, "foo", sa, 3); // add a key-val pair char **str_array = NULL; int len_str_array = 0; @@ -261,26 +263,27 @@ API int bundle_del(bundle *b, const char* key); bundle_free(b); @endcode */ - API const char** bundle_get_str_array(bundle *b, const char *key,int *len); + /** - * @brief Get value from key - * @pre b must be a valid bundle object. - * @post None + * @internal + * @brief Gets a value with a given key. + * @since_tizen 2.3 + * @remarks You MUST free or modify the returned string! + * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section. + * @param[in] b The bundle object + * @param[in] key The key + * @return The pointer for the value string + * @retval @c NULL - Key not found + * @exception BUNDLE_ERROR_NONE Success + * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter + * @exception BUNDLE_ERROR_KEY_NOT_AVAILABLE Key not available + * @pre @a b must be a valid bundle object. * @see bundle_get_str() - * @param[in] b bundle object - * @param[in] key key - * @return Pointer for value string - * @retval NULL If key is not found, returns NULL. - * @remark DO NOT free or modify returned string! - When NULL is returned, errno is set to one of the following values; \n - EINVAL : b is invalid \n - ENOKEY : No key exists \n - EKEYREJECTED : invalid key (NULL or sth) \n @code #include bundle *b = bundle_create(); // Create new bundle object - bundle_add(b, "foo_key", "bar_val"); // add a key-val pair + bundle_add_str(b, "foo", "bar"); //add a key-val pair char *val = bundle_get_val(b, "foo_key"); // val = "bar_val" bundle_free(b); // After freeing b, val becomes a dangling pointer. @@ -290,19 +293,17 @@ API const char** bundle_get_str_array(bundle *b, const char *key,int *len); API const char* bundle_get_val(bundle *b, const char *key); /** - * @brief Get the number of bundle items - * @pre b must be a valid bundle object. - * @post None - * @see None - * @param[in] b bundle object - * @return Number of bundle items - * @remark None + * @brief Gets the number of bundle items. + * @since_tizen 2.3 + * @param[in] b The bundle object + * @return The number of bundle items + * @pre @a b must be a valid bundle object. @code #include bundle *b = bundle_create(); // Create new bundle object - bundle_add(b, "key1", "val1"); // add a key-val pair + bundle_add_str(b, "key1", "val1"); //add a key-val pair int count = bundle_get_count(b); // count=1 - bundle_add(b, "key2", "val2"); // add another key-val pair + bundle_add_str(b, "key2", "val2"); // add another key-val pair count = bundle_get_count(b); // count=2 bundle_free(b); @@ -310,36 +311,39 @@ API const char* bundle_get_val(bundle *b, const char *key); */ API int bundle_get_count(bundle *b); - /** - * @brief Get a type of a value with certain key - * @pre b must be a valid bundle object - * @post None - * @see bundle_type_t + * @brief Gets the type of a value with a given key. + * @since_tizen 2.3 + * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section. * @param[in] b A bundle - * @param[in] key A key in bundle - * @return Type of a key in b - * @remark + * @param[in] key A key in the bundle + * @return The type of a key in @a b + * @exception BUNDLE_ERROR_NONE Success + * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter + * @exception BUNDLE_ERROR_KEY_NOT_AVAILABLE Key not available + * @pre @a b must be a valid bundle object. + * @see bundle_type_t @code @endcode */ API int bundle_get_type(bundle *b, const char *key); - /** - * @brief Duplicate given bundle object - * @pre b must be a valid bundle object. - * @post None - * @see None - * @param[in] b_from bundle object to be duplicated - * @return New bundle object - * @retval NULL Failure - * @remark None + * @internal + * @brief Duplicates a given bundle object. + * @since_tizen 2.3 + * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section. + * @param[in] b_from the bundle object to be duplicated + * @return The new bundle object + * @retval @c NULL - Failure + * @exception BUNDLE_ERROR_NONE Success + * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter + * @pre @a b must be a valid bundle object. @code #include bundle *b = bundle_create(); // Create new bundle object - bundle_add(b, "foo_key", "bar_val"); // add a key-val pair - bundle *b_dup = bundle_dup(b); // duplicate b + bundle_add_str(b, "foo_key", "bar_val"); // add a key-val pair + bundle *b_dup = bundle_dup(b); // duplicate b bundle_free(b); bundle_free(b_dup); @@ -348,16 +352,20 @@ API int bundle_get_type(bundle *b, const char *key); API bundle * bundle_dup(bundle *b_from); /** - * @brief iterate callback function with each key/val pairs in bundle. (NOTE: Only BUNDLE_TYPE_STR type values come!) - * @pre b must be a valid bundle object. - * @post None - * @see None - * @param[in] b bundle object - * @param[in] callback iteration callback function - * @param[in] data data for callback function - * @remark This function is obsolete, and does not give values whose types are not BUNDLE_TYPE_STR. + * @internal + * @brief Iterates a callback function for each key-value pairs in a given bundle. + * @details (NOTE: Only BUNDLE_TYPE_STR type values come!) + * @since_tizen 2.3 + * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section. + * @remarks This function is obsolete and does not give values whose types are not BUNDLE_TYPE_STR. + * @param[in] b The bundle object + * @param[in] callback The iteration callback function + * @param[in] cb_data The data for callback function + * @exception BUNDLE_ERROR_NONE Success + * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter + * @pre @a b must be a valid bundle object. @code - @include + #include #include void sample_cb(const char *k, const char *v, void *data) { printf("%s -> %s\n", k, v); @@ -365,28 +373,34 @@ API bundle * bundle_dup(bundle *b_from); int main(void) { bundle *b = bundle_create(); // Create new bundle object - bundle_add(b, "k1", "v1"); // add a key-val pair - bundle_add(b, "k2", "v2"); // add a key-val pair - bundle_add(b, "k3", "v3"); // add a key-val pair - bundle_iterate(b, sample_cb, NULL); // iterate sample_cb for each key/val + bundle_add_str(b, "k1", "v1"); // add a key-val pair + bundle_add_str(b, "k2", "v2"); // add a key-val pair + bundle_add_str(b, "k3", "v3"); // add a key-val pair + bundle_iterate(b, sample_cb, NULL); // iterate sample_cb() for each key/val return 0; - } + } @endcode */ API void bundle_iterate(bundle *b, bundle_iterate_cb_t callback, void *cb_data); - /** - * @brief iterate callback function with each key/val pairs in bundle. (Supports all types of value) - * @pre b must be a valid bundle object. - * @post None - * @see bundle_keyval_get_type bundle_keyval_type_is_array bundle_keyval_get_basic_val bundle_keyval_get_array_val - * @param[in] b bundle object - * @param[in] iter iteration callback function - * @param[in] user_data data for callback function - * @remark This function supports all types. + * @brief Iterates a callback function for each key-value pair in a given bundle. + * @details Supports all types of values. + * @since_tizen 2.3 + * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section. + * @remarks This function supports all types. + * @param[in] b The bundle object + * @param[in] iter The iteration callback function + * @param[in] user_data The data for the callback function + * @exception BUNDLE_ERROR_NONE Success + * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter + * @pre @a b must be a valid bundle object. + * @see bundle_keyval_get_type() + * @see bundle_keyval_type_is_array() + * @see bundle_keyval_get_basic_val() + * @see bundle_keyval_get_array_val() @code - @include + #include #include void sample_cb(const char *key, const int type, const bundle_keyval_t *kv, void *user_data) { void *basic_val = NULL; @@ -401,152 +415,153 @@ API void bundle_iterate(bundle *b, bundle_iterate_cb_t callback, void *cb_data // Do something... } else { - bundle_keyval_get_basic_val(kv, &basic_val, &size); + bundle_keyval_get_basic_val(kv, &basic_val, &basic_size); // Do something... } } - + int main(void) { bundle *b = bundle_create(); // Create new bundle object bundle_add_str(b, "k1", "v1"); // add a key-val pair bundle_add_byte(b, "k2", "v2", 3); // add a key-val pair char *s_arr[] = {"abc", "bcd", "cde"}; bundle_add_str_array(b, "k3", s_arr, 3); // add a key-val pair - bundle_iterate(b, sample_cb, NULL); // iterate sample_cb for each key/val + bundle_foreach(b, sample_cb, NULL); // iterate sample_cb() for each key/val return 0; - } + } @endcode */ API void bundle_foreach(bundle *b, bundle_iterator_t iter, void *user_data); - /** - * @brief Get type for a bundle_keyval_t object. - * @pre kv must be a valid bundle_keyval_t object. - * @post None - * @see bundle_foreach + * @brief Gets the type of a key-value pair. + * @since_tizen 2.3 + * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section. * @param[in] kv A bundle_keyval_t object - * @return Type of kv - * @retval -1 Operation failure. errno is set. - * @remark + * @return The type of @a kv + * @retval @c -1 - Failure + * @exception BUNDLE_ERROR_NONE Success + * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter + * @pre @a kv must be a valid bundle_keyval_t object. + * @see bundle_foreach() */ API int bundle_keyval_get_type(bundle_keyval_t *kv); - /** - * @brief Determine if kv is array type or not. - * @pre kv must be a valid bundle_keyval_t object. - * @post None - * @see bundle_foreach + * @brief Determines whether the type of a key-value pair is array. + * @since_tizen 2.3 + * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section. * @param[in] kv A bundle_keyval_t object - * @return Operation result - * @retval 1 kv is an array. - * @retval 0 kv is not an array. - * @remark + * @return The operation result + * @retval @c 1 - @a kv is an array + * @retval @c 0 - @a kv is not an array + * @exception BUNDLE_ERROR_NONE Success + * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter + * @pre @a kv must be a valid bundle_keyval_t object. + * @see bundle_foreach() */ API int bundle_keyval_type_is_array(bundle_keyval_t *kv); - /** - * @brief Determine if kv is measurable type or not. - * @pre kv must be a valid bundle_keyval_t object. - * @post None - * @see bundle_foreach + * @internal + * @brief Determines whether the type of a key-value pair is measurable. + * @since_tizen 2.3 + * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section. * @param[in] kv A bundle_keyval_t object - * @return Operation result - * @retval 1 kv is an measurable. - * @retval 0 kv is not an measurable. - * @remark + * @return The operation result + * @retval @c 1 - @a kv is an measurable + * @retval @c 0 - @a kv is not an measurable + * @exception BUNDLE_ERROR_NONE Success + * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter + * @pre @a kv must be a valid bundle_keyval_t object. + * @see bundle_foreach() */ API int bundle_keyval_type_is_measurable(bundle_keyval_t *kv); - /** - * @brief Get value and size of the value from kv of basic type. - * @pre kv must be a valid bundle_keyval_t object. - * @post val, size are set. - * @see bundle_foreach + * @brief Gets the value and size of the value from a key-value pair of basic type. + * @since_tizen 2.3 + * @remarks You must not free @a val. * @param[in] kv A bundle_keyval_t object - * @param[out] val Value - * @param[out] size Size of val - * @return Operation result - * @retval 0 Success - * @remark Do not free val. + * @param[out] val The value + * @param[out] size The size of @a val + * @return The operation result + * @retval BUNDLE_ERROR_NONE Success + * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter + * @pre @a kv must be a valid bundle_keyval_t object. + * @post @a val and @a size are set. + * @see bundle_foreach() */ API int bundle_keyval_get_basic_val(bundle_keyval_t *kv, void **val, size_t *size); - /** - * @brief Get value array, length of array, and size of each array item - * @pre kv must be a valid bundle_keyval_t object. - * @post array_val, array_len, array_item_size are set. - * @see bundle_foreach + * @brief Gets the value array, length of the array, and size of each array item. + * @since_tizen 2.3 * @param[in] kv A bundle_keyval_t object - * @param[out] array_val Array pointer of values - * @param[out] array_len Length of array_val - * @param[out] array_element_size Array of size of each array element - * @return Operation result - * @retval 0 Success - * @retval 0 Failure - * @remark + * @param[out] array_val The array pointer of values + * @param[out] array_len The length of @a array_val + * @param[out] array_element_size The array of size of each array element + * @return The operation result + * @retval BUNDLE_ERROR_NONE Success + * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter + * @pre @a kv must be a valid bundle_keyval_t object. + * @post @a array_val, @a array_len, @a array_item_size are set. + * @see bundle_foreach() */ API int bundle_keyval_get_array_val(bundle_keyval_t *kv, void ***array_val, unsigned int *array_len, size_t **array_element_size); - /** - * @brief Encode bundle to bundle_raw format (uses base64 format) - * @pre b must be a valid bundle object. - * @post None - * @see None - * @param[in] b bundle object - * @param[out] r returned bundle_raw data(byte data) - * r MUST BE FREED by free(r). - * @param[out] len size of r (in bytes) - * @return size of raw data - * @retval 0 Success - * @retval -1 Failure - * @remark None + * @brief Encodes a bundle to the bundle_raw format (uses base64 format). + * @since_tizen 2.3 + * @param[in] b The bundle object + * @param[out] r The returned bundle_raw data(byte data) + * @a r MUST BE FREED by free(r) + * @param[out] len The size of @a r (in bytes) + * @return The size of the raw data + * @retval BUNDLE_ERROR_NONE Success + * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter + * @pre @a b must be a valid bundle object. @code #include bundle *b = bundle_create(); // Create new bundle object - bundle_add(b, "foo_key", "bar_val"); // add a key-val pair + bundle_add_str(b, "foo_key", "bar_val"); // add a key-val pair bundle_raw *r; int len; bundle_encode(b, &r, &len); // encode b - bundle_free_encoded_rawdata(r); bundle_free(b); @endcode */ API int bundle_encode(bundle *b, bundle_raw **r, int *len); /** - * @brief Free encoded rawdata from memory - * @pre r is a valid rawdata generated by bundle_encode(). - * @post None - * @see bundle_encode - * @param[in] r is a rawdata - * @return Operation result - * @retval 0 Success - * @retval -1 Failure - * @reamark None + * @internal + * @brief Frees the encoded rawdata. + * @since_tizen 2.3 + * @param[in] r The rawdata + * @return The operation result + * @retval BUNDLE_ERROR_NONE Success + * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter + * @pre @a r is a valid rawdata generated by bundle_encode(). + * @see bundle_encode() */ API int bundle_free_encoded_rawdata(bundle_raw **r); /** - * @brief deserialize bundle_raw, and get bundle object - * @pre b must be a valid bundle object. - * @post None - * @see None - * @param[in] r bundle_raw data to be converted to bundle object - * @param[in] len size of r - * @return bundle object - * @retval NULL Failure - * @remark None + * @brief Deserializes bundle_raw and gets the bundle object. + * @since_tizen 2.3 + * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section. + * @param[in] r The bundle_raw data to be converted to bundle object + * @param[in] len The size of @a r + * @return The bundle object + * @retval @c NULL - Failure + * @exception BUNDLE_ERROR_NONE Success + * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter + * @pre @a b must be a valid bundle object. @code #include bundle *b = bundle_create(); // Create new bundle object - bundle_add(b, "foo_key", "bar_val"); // add a key-val pair + bundle_add_str(b, "foo_key", "bar_val"); // add a key-val pair bundle_raw *encoded_b; int len; @@ -563,46 +578,47 @@ API int bundle_free_encoded_rawdata(bundle_raw **r); API bundle * bundle_decode(const bundle_raw *r, const int len); /** - * @brief Encode bundle to bundle_raw format - * @pre b must be a valid bundle object. - * @post None - * @see None - * @param[in] b bundle object - * @param[out] r returned bundle_raw data(byte data) - * r MUST BE FREED by free(r). - * @param[out] len size of r (in bytes) - * @return size of raw data - * @retval 0 Success - * @retval -1 Failure - * @remark None + * @internal + * @brief Encodes a bundle to the bundle_raw format. + * @since_tizen 2.3 + * @param[in] b The bundle object + * @param[out] r The returned bundle_raw data(byte data) + * @a r MUST BE FREED by free(r) + * @param[out] len The size of @a r (in bytes) + * @return The size of the raw data + * @retval BUNDLE_ERROR_NONE Success + * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter + * @retval BUNDLE_ERROR_OUT_OF_MEMORY Out of memory + * @pre @a b must be a valid bundle object. @code #include bundle *b = bundle_create(); // Create new bundle object - bundle_add(b, "foo_key", "bar_val"); // add a key-val pair + bundle_add_str(b, "foo_key", "bar_val"); // add a key-val pair bundle_raw *r; int len; bundle_encode_raw(b, &r, &len); // encode b - bundle_free_encoded_rawdata(r); bundle_free(b); @endcode */ API int bundle_encode_raw(bundle *b, bundle_raw **r, int *len); /** - * @brief deserialize bundle_raw, and get bundle object - * @pre b must be a valid bundle object. - * @post None - * @see None - * @param[in] r bundle_raw data to be converted to bundle object - * @param[in] len size of r - * @return bundle object - * @retval NULL Failure - * @remark None + * @internal + * @brief Deserializes bundle_raw and gets a bundle object. + * @since_tizen 2.3 + * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section. + * @param[in] r The bundle_raw data to be converted to a bundle object + * @param[in] len The size of @a r + * @return The bundle object + * @retval @c NULL - Failure + * @exception BUNDLE_ERROR_NONE Success + * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter + * @pre @a b must be a valid bundle object. @code #include bundle *b = bundle_create(); // Create new bundle object - bundle_add(b, "foo_key", "bar_val"); // add a key-val pair + bundle_add_str(b, "foo_key", "bar_val"); // add a key-val pair bundle_raw *encoded_b; int len; @@ -619,31 +635,36 @@ API int bundle_encode_raw(bundle *b, bundle_raw **r, int *len); API bundle * bundle_decode_raw(const bundle_raw *r, const int len); /** - * @brief Export bundle to argv - * @pre b is a valid bundle object. - * @post argv is a pointer of newly allocated memory. It must be freed. - * Each item of argv points the string in the bundle object b. If b is freed, argv will have garbage pointers. DO NOT FREE b BEFORE ACCESSING argv!! - * @see bundle_import_from_argv - * @param[in] b bundle object - * @param[out] argv Pointer of string array. - * This array has NULL values for first and last item. - * First NULL is for argv[0], and last NULL is a terminator for execv(). - * @return Number of item in argv. This value is equal to actual count of argv - 1. (Last NULL terminator is not counted.) - * @retval -1 Function failure. Check errno to get the reason. - * @remark None + * @internal + * @brief Exports bundle to @a argv. + * @since_tizen 2.3 + * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section. + * @param[in] b The bundle object + * @param[out] argv The pointer of the string array; \n + * This array has NULL values for the first and last item; \n + * First NULL is for argv[0], and last NULL is a terminator for execv() \n + * @return The number of item in @a argv. This value is equal to the actual count of argv - 1. (Last NULL terminator is not counted.) + * @retval @c -1 - Failure + * @exception BUNDLE_ERROR_NONE Success + * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter + * @exception BUNDLE_ERROR_OUT_OF_MEMORY Out of memory + * @pre @a b is a valid bundle object. + * @post @a argv is a pointer of newly allocated memory. It must be freed. + * Each item of @a argv points to the string in the bundle object @a b. If @a b is freed, @a argv will have garbage pointers. DO NOT FREE @a b BEFORE ACCESSING @a argv!! + * @see bundle_import_from_argv() @code #include bundle *b = bundle_create(); // Create new bundle object - bundle_add(b, "foo_key", "bar_val"); // add a key-val pair + bundle_add_str(b, "foo_key", "bar_val"); // add a key-val pair int argc = 0; char **argv = NULL; argc = bundle_export_to_argv(b, &argv); // export to argv if(0 > argc) error("export failure"); - + int i; for(i=0; i < argc; i++) { - printf("%s\n", argv[i]); // print argv + printf("%s\n", argv[i]); // print argv } bundle_free_exported_argv(argc, argv); // argv must be freed after being used. @@ -653,20 +674,21 @@ API bundle * bundle_decode_raw(const bundle_raw *r, const int len); API int bundle_export_to_argv(bundle *b, char ***argv); /** - * @brief Free exported argv - * @pre argv is a valid string array generated from bundle_export_to_argv(). - * @post None - * @see bundle_export_to_argv - * @param[in] argc number of args, which is the return value of bundle_export_to_argv(). - * @param[in] argv array from bundle_export_to_argv(). - * @return Operation result. - * @retval 0 on success - * @retval -1 on failure - * @remark You must not use this API when you use global argv. + * @internal + * @brief Frees the exported @a argv. + * @since_tizen 2.3 + * @remarks You must not use this API when you use global @a argv. + * @param[in] argc The number of args, which is the return value of bundle_export_to_argv() + * @param[in] argv The array from bundle_export_to_argv() + * @return The operation result + * @retval BUNDLE_ERROR_NONE Success + * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter + * @pre @a argv is a valid string array generated from bundle_export_to_argv(). + * @see bundle_export_to_argv() @code bundle *b = bundle_create(); bundle_add_str(b, "foo", "bar"); - + int argc = 0; char **argv = NULL; argc = bundle_export_to_argv(b, &argv); @@ -674,7 +696,7 @@ API int bundle_export_to_argv(bundle *b, char ***argv); // Use argv... - bundle_free_export_argv(argc, argv); + bundle_free_exported_argv(argc, argv); argv = NULL; bundle_free(b); @@ -683,15 +705,20 @@ API int bundle_export_to_argv(bundle *b, char ***argv); API int bundle_free_exported_argv(int argc, char ***argv); /** - * @brief import a bundle from argv - * @pre argv is a valid string array, which is created by bundle_export_to_argv(). - * @post Returned bundle b must be freed. - * @see bundle_export_to_argv - * @param[in] argc argument count - * @param[in] argv argument vector - * @return New bundle object - * @retval NULL Function failure - * @remark None + * @internal + * @brief Imports a bundle from @a argv. + * @since_tizen 2.3 + * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section. + * @param[in] argc The argument count + * @param[in] argv The argument vector + * @return The new bundle object + * @retval @c NULL - Failure + * @exception BUNDLE_ERROR_NONE Success + * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter + * @exception BUNDLE_ERROR_OUT_OF_MEMORY Out of memory + * @pre @a argv is a valid string array, which is created by bundle_export_to_argv(). + * @post The returned bundle @a b must be freed. + * @see bundle_export_to_argv() @code #include @@ -707,21 +734,18 @@ API int bundle_free_exported_argv(int argc, char ***argv); API bundle * bundle_import_from_argv(int argc, char **argv); /** - * @brief Add a string type key-value pair into bundle. - * @pre b must be a valid bundle object. - * @post None + * @brief Adds a string type key-value pair into a bundle. + * @since_tizen 2.3 + * @param[in] b The bundle object + * @param[in] key The key + * @param[in] str The string type value + * @return The operation result + * @retval BUNDLE_ERROR_NONE Success + * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter + * @retval BUNDLE_ERROR_KEY_EXISTS Key already exists + * @retval BUNDLE_ERROR_OUT_OF_MEMORY Out of memory + * @pre @a b must be a valid bundle object. * @see bundle_get_str() - * @param[in] b bundle object - * @param[in] key key - * @param[in] str string type value - * @return Operation result - * @retval 0 success - * @retval -1 failure - * - * @remark When -1 is returned, errno is set to one of the following values; \n - EKEYREJECTED : key is rejected (NULL or sth) \n - EPERM : key is already exist, not permitted to overwrite value \n - EINVAL : b or val is not valid (NULL or sth) \n @code #include bundle *b = bundle_create(); // Create new bundle object @@ -733,23 +757,19 @@ API bundle * bundle_import_from_argv(int argc, char **argv); API int bundle_add_str(bundle *b, const char *key, const char *str); /** - * @brief Set a value of string array element - * @pre b must be a valid bundle object. - * @post None + * @internal + * @brief Sets a value of string array elements. + * @since_tizen 2.3 + * @param[in] b The bundle object + * @param[in] key The key + * @param[in] idx The index of the array element to be changed + * @param[in] val The string type value; if @c NULL, an empty array is created; you can change an item with + * @return The operation result + * @retval BUNDLE_ERROR_NONE Success + * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter + * @pre @a b must be a valid bundle object. * @see bundle_add_str_array() * @see bundle_get_str_array() - * @param[in] b bundle object - * @param[in] key key - * @param[in] idx index of array element to be changed - * @param[in] val string type value. If NULL, empty array is created. You can change an item with - * @return Operation result - * @retval 0 success - * @retval -1 failure - * - * @remark When -1 is returned, errno is set to one of the following values; \n - EKEYREJECTED : key is rejected (NULL or sth) \n - EPERM : key is already exist, not permitted to overwrite value \n - EINVAL : b or val is not valid (NULL or sth) \n @code #include bundle *b = bundle_create(); @@ -770,51 +790,48 @@ API int bundle_add_str(bundle *b, const char *key, const char *str); API int bundle_set_str_array_element(bundle *b, const char *key, const unsigned int idx, const char *val); /** - * @brief Add a byte type key-value pair into bundle. - * @pre b must be a valid bundle object. - * @post None + * @brief Adds a byte type key-value pair into a bundle. + * @since_tizen 2.3 + * @param[in] b The bundle object + * @param[in] key The key + * @param[in] byte The string type value + * @param[in] size The size of @a byte + * @return The operation result + * @retval BUNDLE_ERROR_NONE Success + * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter + * @retval BUNDLE_ERROR_KEY_EXISTS Key already exists + * @retval BUNDLE_ERROR_OUT_OF_MEMORY Out of memory + * @pre @a b must be a valid bundle object. * @see bundle_get_byte() - * @param[in] b bundle object - * @param[in] key key - * @param[in] byte string type value - * @param[in] size size of byte - * @return Operation result - * @retval 0 success - * @retval -1 failure - * - * @remark When -1 is returned, errno is set to one of the following values; \n - EKEYREJECTED : key is rejected (NULL or sth) \n - EPERM : key is already exist, not permitted to overwrite value \n - EINVAL : b or val is not valid (NULL or sth) \n @code #include bundle *b = bundle_create(); // Create new bundle object bundle_add_byte(b, "foo", "bar\0", 4); // add a key-val pair + int number = 12345; + bundle_add_byte(b, "number", &number, sizeof(int)); + bundle_free(b); @endcode */ - API int bundle_add_byte(bundle *b, const char *key, const void *byte, const size_t size); /** - * @brief Add a byte array type key-value pair into bundle. - * @pre b must be a valid bundle object. - * @post None - * @see bundle_get_str_array() + * @internal + * @brief Adds a byte array type key-value pair into a bundle. + * @since_tizen 2.3 + * @param[in] b The bundle object + * @param[in] key The key + * @param[in] byte_array Not used + * @param[in] len The length of the array to be created + * @return The operation result + * @retval BUNDLE_ERROR_NONE Success + * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter + * @retval BUNDLE_ERROR_KEY_EXISTS Key already exists + * @retval BUNDLE_ERROR_OUT_OF_MEMORY Out of memory + * @pre @a b must be a valid bundle object. + * @see bundle_get_byte_array() * @see bundle_set_byte_array_element() - * @param[in] b bundle object - * @param[in] key key - * @param[in] byte_array Not used. - * @param[in] len Length of array to be created - * @return Operation result - * @retval 0 success - * @retval -1 failure - * - * @remark When -1 is returned, errno is set to one of the following values; \n - EKEYREJECTED : key is rejected (NULL or sth) \n - EPERM : key is already exist, not permitted to overwrite value \n - EINVAL : b or val is not valid (NULL or sth) \n @code #include bundle *b = bundle_create(); @@ -830,28 +847,26 @@ API int bundle_add_byte(bundle *b, const char *key, const void *byte, const size API int bundle_add_byte_array(bundle *b, const char *key, void **byte_array, const unsigned int len); /** - * @brief Set a value of byte array element - * @pre b must be a valid bundle object. - * @post None - * @see bundle_add_str_array() - * @see bundle_get_str_array() - * @param[in] b bundle object - * @param[in] key key - * @param[in] idx index of array element to be changed - * @param[in] val string type value. If NULL, empty array is created. You can change an item with - * @param[in] size Size of value in byte + * @internal + * @brief Sets the value of the byte array element. + * @since_tizen 2.3 + * @param[in] b The bundle object + * @param[in] key The key + * @param[in] idx The index of the array element to be changed + * @param[in] val The string type value; if @c NULL, an empty array is created; you can change an item with + * @param[in] size The size of the value in bytes * @return Operation result - * @retval 0 success - * @retval -1 failure - * - * @remark When -1 is returned, errno is set to one of the following values; \n - EKEYREJECTED : key is rejected (NULL or sth) \n - EPERM : key is already exist, not permitted to overwrite value \n - EINVAL : b or val is not valid (NULL or sth) \n + * @retval BUNDLE_ERROR_NONE Success + * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter + * @retval BUNDLE_ERROR_KEY_NOT_AVAILABLE Key not available + * @pre @a b must be a valid bundle object. + * @see bundle_add_byte_array() + * @see bundle_get_byte_array() @code #include bundle *b = bundle_create(); bundle_add_byte_array(b, "foo", NULL, 3); // add a key-val pair + bundle_set_byte_array_element(b, "foo", 0, "aaa\0", 4); bundle_set_byte_array_element(b, "foo", 1, "bbb\0", 4); bundle_set_byte_array_element(b, "foo", 2, "ccc\0", 4); @@ -859,7 +874,7 @@ API int bundle_add_byte_array(bundle *b, const char *key, void **byte_array, con unsigned char **byte_array = NULL; int len_byte_array = 0; - byte_array=bundle_get_str_array(b, "foo", &len_byte_array); + bundle_get_byte_array(b, "foo", &byte_array, &len_byte_array, &size_byte_array); // byte_array = { "aaa\0", "bbb\0", "ccc\0" }, and len_byte_array = 3 bundle_free(b); @@ -868,21 +883,18 @@ API int bundle_add_byte_array(bundle *b, const char *key, void **byte_array, con API int bundle_set_byte_array_element(bundle *b, const char *key, const unsigned int idx, const void *val, const size_t size); /** - * @brief Get string value from key - * @pre b must be a valid bundle object. - * @post None + * @brief Gets the string value with the given key. + * @since_tizen 2.3 + * @remarks You must not free str! + * @param[in] b The bundle object + * @param[in] key The key + * @param[out] str The returned value + * @return The operation result + * @retval BUNDLE_ERROR_NONE Success + * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter + * @retval BUNDLE_ERROR_KEY_NOT_AVAILABLE Key not available + * @pre @a b must be a valid bundle object. * @see bundle_add_str() - * @param[in] b bundle object - * @param[in] key key - * @param[out] str returned value - * @return Operation result - * @retval 0 on success - * @retval -1 on failure - * @remark Do not free str! - When -1 is returned, errno is set to one of the following values; \n - EINVAL : b is invalid \n - ENOKEY : No key exists \n - EKEYREJECTED : invalid key (NULL or sth) \n @code #include bundle *b = bundle_create(); // Create new bundle object @@ -898,54 +910,55 @@ API int bundle_set_byte_array_element(bundle *b, const char *key, const unsigned API int bundle_get_str(bundle *b, const char *key, char **str); /** - * @brief Get byte value from key - * @pre b must be a valid bundle object. - * @post None + * @brief Gets the byte value with the given key. + * @since_tizen 2.3 + * @remarks You must not free @a byte! + * @param[in] b The bundle object + * @param[in] key The key + * @param[out] byte The returned value + * @param[out] size The size of the byte + * @return The operation result + * @retval BUNDLE_ERROR_NONE Success + * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter + * @retval BUNDLE_ERROR_KEY_NOT_AVAILABLE Key not available + * @pre @a b must be a valid bundle object. * @see bundle_add_byte() - * @param[in] b bundle object - * @param[in] key key - * @param[out] byte returned value - * @param[out] size Size of byte - * @return Operation result - * @retval 0 on success - * @retval -1 on failure - * @remark Do not free str! - When -1 is returned, errno is set to one of the following values; \n - EINVAL : b is invalid \n - ENOKEY : No key exists \n - EKEYREJECTED : invalid key (NULL or sth) \n @code #include bundle *b = bundle_create(); // Create new bundle object - bundle_add_byte(b, "foo", "bar\0", 4); // add a key-val pair + bundle_add_byte(b, "foo", "bar\0", 4); // add string to bundle + int number = 12345; + bundle_add_byte(b, "number", (const void**)&number, sizeof(int)); // add integer to bundle unsigned char *v = NULL; - bundle_get_str(b, "foo", &v); // v = "bar\0" + size_t v_size; + bundle_get_byte(b, "foo", (void**)&v, &v_size); // v = "bar\0" + int *n = NULL; + size_t n_size; + bundle_get_byte(b, "number", (void**)&n, &n_size); // number = 12345 - bundle_free(b); // After freeing b, v becomes a dangling pointer. + bundle_free(b); // After freeing b, v and n becomes a dangling pointer. @endcode */ API int bundle_get_byte(bundle *b, const char *key, void **byte, size_t *size); /** - * @brief Get byte array value from key - * @pre b must be a valid bundle object. - * @post None - * @see bundle_add_str_array() - * @see bundle_set_str_array_element() - * @param[in] b bundle object - * @param[in] key key - * @param[out] byte_array returned value - * @param[out] len array length - * @param[out] array_element_size an array of sizes of each byte_array element - * @return Operation result - * @retval 0 on success - * @retval -1 on failure - * @remark Do not free str! - When -1 is returned, errno is set to one of the following values; \n - EINVAL : b is invalid \n - ENOKEY : No key exists \n - EKEYREJECTED : invalid key (NULL or sth) \n + * @internal + * @brief Gets the byte array value with the given key. + * @since_tizen 2.3 + * @remarks You must not free str! + * @param[in] b The bundle object + * @param[in] key The key + * @param[out] byte_array The returned value + * @param[out] len The array length + * @param[out] array_element_size an array of sizes of each @a byte_array element + * @return The operation result + * @retval BUNDLE_ERROR_NONE Success + * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter + * @retval BUNDLE_ERROR_KEY_NOT_AVAILABLE Key not available + * @pre @a b must be a valid bundle object. + * @see bundle_add_byte_array() + * @see bundle_set_byte_array_element() @code #include bundle *b = bundle_create(); @@ -958,7 +971,7 @@ API int bundle_get_byte(bundle *b, const char *key, void **byte, size_t *size); int len_byte_array = 0; size_t *size_byte_array = NULL; - byte_array = bundle_get_str_array(b, "foo", &len_byte_array, &size_byte_array); + bundle_get_byte_array(b, "foo", &byte_array, &len_byte_array, &size_byte_array); // byte_array = { "aaa\0", "bbb\0", "ccc\0" }, len_byte_array = 3, and size_byte_array = { 4, 4, 4 } bundle_free(b); diff --git a/include/keyval_array.h b/include/keyval_array.h index afdf52c..66cf31d 100755 --- a/include/keyval_array.h +++ b/include/keyval_array.h @@ -50,3 +50,4 @@ size_t keyval_array_decode(void *byte, keyval_array_t **kva); int keyval_array_copy_array(keyval_array_t *kva, void **array_val, unsigned int array_len, size_t (*measure_val_len)(void * val)); int keyval_array_get_data(keyval_array_t *kva, int *type,void ***array_val, unsigned int *len, size_t **array_element_size); int keyval_array_set_element(keyval_array_t *kva, int idx, void *val, size_t size); +int keyval_array_is_idx_valid(keyval_array_t *kva, int idx); diff --git a/packaging/bundle.spec b/packaging/bundle.spec index 36d1dff..a4c8270 100644 --- a/packaging/bundle.spec +++ b/packaging/bundle.spec @@ -9,6 +9,7 @@ Source1001: bundle.manifest BuildRequires: cmake BuildRequires: pkgconfig(glib-2.0) BuildRequires: pkgconfig(dlog) +BuildRequires: pkgconfig(capi-base-common) %description Simple string key-val dictionary ADT diff --git a/src/bundle.c b/src/bundle.c old mode 100755 new mode 100644 index 69b37dd..6f8618d --- a/src/bundle.c +++ b/src/bundle.c @@ -35,13 +35,11 @@ #include /* calloc, free */ #include /* strdup */ -#include #define CHECKSUM_LENGTH 32 #define TAG_IMPORT_EXPORT_CHECK "`zaybxcwdveuftgsh`" /* ADT */ -struct _bundle_t -{ +struct _bundle_t { keyval_t *kv_head; }; @@ -53,17 +51,23 @@ static keyval_t * _bundle_find_kv(bundle *b, const char *key) { keyval_t *kv; - - if(NULL == b) { errno = EINVAL; return NULL; } - if(NULL == key) { errno = EKEYREJECTED; return NULL; } + if (NULL == b) { + set_last_result(BUNDLE_ERROR_INVALID_PARAMETER); + return NULL; + } + if (NULL == key) { + set_last_result(BUNDLE_ERROR_INVALID_PARAMETER); + return NULL; + } kv = b->kv_head; while (kv != NULL) { - if(0 == strcmp(key, kv->key)) return kv; + if (0 == strcmp(key, kv->key)) + return kv; kv = kv->next; - } + } /* Not found */ - errno = ENOKEY; + set_last_result(BUNDLE_ERROR_KEY_NOT_AVAILABLE); return NULL; } @@ -75,73 +79,73 @@ _bundle_append_kv(bundle *b, keyval_t *new_kv) { keyval_t *kv; - if (NULL == b->kv_head) b->kv_head = new_kv; + if (NULL == b->kv_head) + b->kv_head = new_kv; else { kv = b->kv_head; - while (NULL != kv->next) kv = kv->next; + while (NULL != kv->next) + kv = kv->next; kv->next = new_kv; } - return 0; + return BUNDLE_ERROR_NONE; } static int -_bundle_add_kv(bundle *b, const char *key, const void *val, const size_t size, const int type, const unsigned int len) +_bundle_add_kv(bundle *b, const char *key, const void *val, + const size_t size, const int type, const unsigned int len) { /* basic value check */ - if(NULL == b) { errno = EINVAL; return -1; } - if(NULL == key) { errno = EKEYREJECTED; return -1; } - if(0 == strlen(key)) { errno = EKEYREJECTED; return -1; } + if (NULL == b) + return BUNDLE_ERROR_INVALID_PARAMETER; + if (NULL == key) + return BUNDLE_ERROR_INVALID_PARAMETER; + if (0 == strlen(key)) + return BUNDLE_ERROR_INVALID_PARAMETER; keyval_t *kv = _bundle_find_kv(b, key); - if(kv) { /* Key already exists */ - errno = EPERM; - return -1; + if (kv) { /* Key already exists */ + return BUNDLE_ERROR_KEY_EXISTS; } - errno = 0; keyval_t *new_kv = NULL; - if(keyval_type_is_array(type)) { - // array type - keyval_array_t *kva = keyval_array_new(NULL, key, type, (const void **) val, len); + if (keyval_type_is_array(type)) { + /* array type */ + keyval_array_t *kva = keyval_array_new(NULL, key, type, + (const void **) val, len); new_kv = (keyval_t *)kva; - } - else { - // normal type + } else { + /* normal type */ new_kv = keyval_new(NULL, key, type, val, size); } - if(!new_kv) { - // NOTE: errno is already set. (ENOMEM, ...) - return -1; - } + if (!new_kv) + return BUNDLE_ERROR_OUT_OF_MEMORY; _bundle_append_kv(b, new_kv); - return 0; + return BUNDLE_ERROR_NONE; } static int -_bundle_get_val(bundle *b, const char *key, const int type, void **val, size_t *size, unsigned int *len, size_t **array_element_size) +_bundle_get_val(bundle *b, const char *key, const int type, void **val, + size_t *size, unsigned int *len, size_t **array_element_size) { keyval_t *kv = _bundle_find_kv(b, key); - if(!kv) { /* Key doesn't exist */ - /* NOTE: errno is already set. */ - return -1; - } - if(BUNDLE_TYPE_ANY != type && type != kv->type) { - errno = ENOTSUP; - return -1; - } + if (!kv) /* Key doesn't exist */ + return get_last_result(); - if(keyval_type_is_array(type)) { + if (BUNDLE_TYPE_ANY != type && type != kv->type) + return BUNDLE_ERROR_INVALID_PARAMETER; + + if (keyval_type_is_array(type)) { keyval_array_t *kva = (keyval_array_t *)kv; - keyval_array_get_data(kva, NULL, (void ***)val, len, array_element_size); - } - else { + keyval_array_get_data(kva, NULL, (void ***)val, + len, array_element_size); + } else { keyval_get_data(kv, NULL, val, size); } - return 0; + return BUNDLE_ERROR_NONE; } /** global initialization @@ -150,10 +154,12 @@ _bundle_get_val(bundle *b, const char *key, const int type, void **val, size_t * static void _bundle_global_init(void) { - static int _is_done = 0; - if(_is_done) return; - - // Run init functions + static int _is_done; + + if (_is_done) + return; + + /* Run init functions */ keyval_type_init(); _is_done = 1; @@ -166,16 +172,17 @@ bundle * bundle_create(void) { bundle *b = NULL; - + _bundle_global_init(); b = calloc(1, sizeof(bundle)); /* fill mem with NULL */ - if(NULL == b) { + if (NULL == b) { BUNDLE_EXCEPTION_PRINT("Unable to allocate memory for bundle\n"); - errno = ENOMEM; + set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY); goto EXCEPTION; } + set_last_result(BUNDLE_ERROR_NONE); return b; EXCEPTION: @@ -185,17 +192,16 @@ EXCEPTION: int bundle_free(bundle *b) { - keyval_t *kv, *tmp_kv; + keyval_t *kv, *tmp_kv; - if(NULL == b) { + if (NULL == b) { BUNDLE_EXCEPTION_PRINT("Bundle is already freed\n"); - errno = EINVAL; - return -1; + return BUNDLE_ERROR_INVALID_PARAMETER; } /* Free keyval list */ kv = b->kv_head; - while(kv != NULL) { + while (kv != NULL) { tmp_kv = kv; kv = kv->next; tmp_kv->method->free(tmp_kv, 1); @@ -204,20 +210,22 @@ bundle_free(bundle *b) /* free bundle */ free(b); - return 0; + return BUNDLE_ERROR_NONE; } -// str type + int bundle_add_str(bundle *b, const char *key, const char *str) { - if(!str) { errno = EINVAL; return -1; } + if (!str) + return BUNDLE_ERROR_INVALID_PARAMETER; return _bundle_add_kv(b, key, str, strlen(str)+1, BUNDLE_TYPE_STR, 1); } int bundle_get_str(bundle *b, const char *key, char **str) { - return _bundle_get_val(b, key, BUNDLE_TYPE_STR, (void **) str, NULL, NULL, NULL); + return _bundle_get_val(b, key, BUNDLE_TYPE_STR, (void **) str, + NULL, NULL, NULL); } int @@ -232,25 +240,30 @@ bundle_del(bundle *b, const char *key) keyval_t *kv = NULL, *prev_kv = NULL; /* basic value check */ - if(NULL == b) { errno = EINVAL; return -1; } - if(NULL == key) { errno = EKEYREJECTED; return -1; } - if(0 == strlen(key)) { errno = EKEYREJECTED; return -1; } + if (NULL == b) + return BUNDLE_ERROR_INVALID_PARAMETER; + if (NULL == key) + return BUNDLE_ERROR_INVALID_PARAMETER; + if (0 == strlen(key)) + return BUNDLE_ERROR_INVALID_PARAMETER; kv = b->kv_head; while (kv != NULL) { - if(0 == strcmp(key, kv->key)) break; + if (0 == strcmp(key, kv->key)) + break; prev_kv = kv; kv = kv->next; - } - if (NULL == kv) { errno = ENOKEY; return -1; } + } + if (NULL == kv) + return BUNDLE_ERROR_KEY_NOT_AVAILABLE; else { - if(NULL != prev_kv) { + if (NULL != prev_kv) prev_kv->next = kv->next; - } - if(kv == b->kv_head) b->kv_head = kv->next; + if (kv == b->kv_head) + b->kv_head = kv->next; kv->method->free(kv, 1); } - return 0; + return BUNDLE_ERROR_NONE; } @@ -258,9 +271,11 @@ const char * bundle_get_val(bundle *b, const char *key) { char *val = NULL; - int r = 0; + int ret = 0; + + ret = bundle_get_str(b, key, &val); + set_last_result(ret); - r = bundle_get_str(b, key, &val); return val; } @@ -269,17 +284,19 @@ bundle_get_val(bundle *b, const char *key) * @brief used by bundle_get_count() API, to count number of items in a bundle */ static void -_bundle_get_count_iter(const char *k, const int type, const bundle_keyval_t *kv, void *user_data) +_bundle_get_count_iter(const char *k, const int type, + const bundle_keyval_t *kv, void *user_data) { int *count = (int *)user_data; *count += 1; } int -bundle_get_count (bundle *b) +bundle_get_count(bundle *b) { int count = 0; - if (NULL == b) return count; + if (NULL == b) + return count; bundle_foreach(b, _bundle_get_count_iter, &count); return count; } @@ -287,60 +304,83 @@ bundle_get_count (bundle *b) void bundle_iterate(bundle *b, bundle_iterate_cb_t callback, void *data) { - keyval_t *kv = b->kv_head; - if(callback) { - while(NULL != kv) { - callback(kv->key, kv->val, data); - kv = kv->next; - } + keyval_t *kv; + + if (NULL == b || NULL == callback) { + set_last_result(BUNDLE_ERROR_INVALID_PARAMETER); + return; } + + kv = b->kv_head; + while (NULL != kv) { + callback(kv->key, kv->val, data); + kv = kv->next; + } + set_last_result(BUNDLE_ERROR_NONE); } void bundle_foreach(bundle *b, bundle_iterator_t iter, void *user_data) { - if(NULL==b) - { + if (NULL == b || NULL == iter) { + set_last_result(BUNDLE_ERROR_INVALID_PARAMETER); return; /*TC_FIX if b=NULL- error handling */ } + keyval_t *kv = b->kv_head; - if(iter) { - while(NULL != kv) { - iter(kv->key, kv->type, kv, user_data); - kv = kv->next; - } + while (NULL != kv) { + iter(kv->key, kv->type, kv, user_data); + kv = kv->next; } + set_last_result(BUNDLE_ERROR_NONE); } /* keyval functions */ -int +int bundle_keyval_get_type(bundle_keyval_t *kv) { + if (NULL == kv) { + set_last_result(BUNDLE_ERROR_INVALID_PARAMETER); + return -1; + } + set_last_result(BUNDLE_ERROR_NONE); return kv->type; } -int +int bundle_keyval_type_is_array(bundle_keyval_t *kv) { + if (NULL == kv) { + set_last_result(BUNDLE_ERROR_INVALID_PARAMETER); + return -1; + } + set_last_result(BUNDLE_ERROR_NONE); return keyval_type_is_array(kv->type); } -int +int bundle_keyval_type_is_measurable(bundle_keyval_t *kv) { + if (NULL == kv) { + set_last_result(BUNDLE_ERROR_INVALID_PARAMETER); + return -1; + } + set_last_result(BUNDLE_ERROR_NONE); return keyval_type_is_measurable(kv->type); } -int +int bundle_keyval_get_basic_val(bundle_keyval_t *kv, void **val, size_t *size) { return keyval_get_data(kv, NULL, val, size); } -int -bundle_keyval_get_array_val(bundle_keyval_t *kv, void ***array_val, unsigned int *array_len, size_t **array_item_size) +int +bundle_keyval_get_array_val(bundle_keyval_t *kv, void ***array_val, + unsigned int *array_len, size_t **array_item_size) { - return keyval_array_get_data((keyval_array_t *)kv, NULL, array_val, array_len, array_item_size); + return keyval_array_get_data((keyval_array_t *)kv, NULL, + array_val, array_len, array_item_size); } @@ -360,26 +400,35 @@ bundle_dup(bundle *b_from) bundle *b_to = NULL; int i; - if(NULL == b_from) { errno = EINVAL; return NULL; } + if (NULL == b_from) { + set_last_result(BUNDLE_ERROR_INVALID_PARAMETER); + return NULL; + } b_to = bundle_create(); - if(NULL == b_to) return NULL; + if (NULL == b_to) + return NULL; keyval_t *kv_from = b_from->kv_head; keyval_t *kv_to = NULL; - while(kv_from != NULL) { - if(keyval_type_is_array(kv_from->type)) { + while (kv_from != NULL) { + if (keyval_type_is_array(kv_from->type)) { keyval_array_t *kva_from = (keyval_array_t *)kv_from; - kv_to = (keyval_t *) keyval_array_new(NULL, kv_from->key, kv_from->type, NULL, kva_from->len); - if(!kv_to) goto ERR_CLEANUP; - for(i=0; i < kva_from->len; i++) { - if(((keyval_array_t *)kv_from)->array_val[i]) { - keyval_array_set_element((keyval_array_t*)kv_to, i, ((keyval_array_t *)kv_from)->array_val[i], ((keyval_array_t *)kv_from)->array_element_size[i]); + kv_to = (keyval_t *) keyval_array_new(NULL, kv_from->key, + kv_from->type, NULL, kva_from->len); + if (!kv_to) + goto ERR_CLEANUP; + for (i = 0; i < kva_from->len; i++) { + if (((keyval_array_t *)kv_from)->array_val[i]) { + keyval_array_set_element((keyval_array_t *)kv_to, i, + ((keyval_array_t *)kv_from)->array_val[i], + ((keyval_array_t *)kv_from)->array_element_size[i]); } } _bundle_append_kv(b_to, kv_to); - } - else { - if(_bundle_add_kv(b_to, kv_from->key, kv_from->val, kv_from->size, kv_from->type, 0)) goto ERR_CLEANUP; + } else { + if (_bundle_add_kv(b_to, kv_from->key, kv_from->val, + kv_from->size, kv_from->type, 0)) + goto ERR_CLEANUP; } kv_from = kv_from->next; @@ -402,26 +451,25 @@ bundle_encode(bundle *b, bundle_raw **r, int *len) size_t byte_len; gchar *chksum_val; - if(NULL == b) { - errno = EINVAL; - return -1; - } + if (NULL == b || NULL == r || NULL == len) + return BUNDLE_ERROR_INVALID_PARAMETER; /* calculate memory size */ - size_t msize = 0; // Sum of required size + size_t msize = 0; /* Sum of required size */ kv = b->kv_head; - while(kv != NULL) { + while (kv != NULL) { msize += kv->method->get_encoded_size(kv); kv = kv->next; } - m = calloc(msize+CHECKSUM_LENGTH, sizeof(unsigned char)); - if(unlikely(NULL == m )) { errno = ENOMEM; return -1; } + m = calloc(msize + CHECKSUM_LENGTH, sizeof(unsigned char)); + if (unlikely(NULL == m)) + return BUNDLE_ERROR_OUT_OF_MEMORY; - p_m = m+CHECKSUM_LENGTH; /* temporary pointer */ + p_m = m + CHECKSUM_LENGTH; /* temporary pointer */ kv = b->kv_head; - while(kv != NULL) { + while (kv != NULL) { byte = NULL; byte_len = 0; @@ -435,28 +483,32 @@ bundle_encode(bundle *b, bundle_raw **r, int *len) } /*compute checksum from the data*/ - chksum_val = g_compute_checksum_for_string(G_CHECKSUM_MD5,m+CHECKSUM_LENGTH,msize); + chksum_val = g_compute_checksum_for_string(G_CHECKSUM_MD5, + (const char *)(m+CHECKSUM_LENGTH), msize); /*prefix checksum to the data */ - memcpy(m,chksum_val,CHECKSUM_LENGTH); - if ( NULL != r ) { + memcpy(m, chksum_val, CHECKSUM_LENGTH); + if (NULL != r) { /*base64 encode for whole string checksum and data*/ - *r =(unsigned char*)g_base64_encode(m,msize+CHECKSUM_LENGTH); - if ( NULL != len ) *len = strlen((char*)*r); + *r = (unsigned char *)g_base64_encode(m, msize + CHECKSUM_LENGTH); + if (NULL != len) + *len = strlen((char *)*r); } free(m); g_free(chksum_val);/*free checksum string */ - return 0; + return BUNDLE_ERROR_NONE; } int bundle_free_encoded_rawdata(bundle_raw **r) { - if(!*r) return -1; /*TC_FIX - double free sigabrt handling */ + if (!*r) + return BUNDLE_ERROR_INVALID_PARAMETER; + /*TC_FIX - double free sigabrt handling */ free(*r); - *r=NULL; - return 0; + *r = NULL; + return BUNDLE_ERROR_NONE; } bundle * @@ -469,35 +521,36 @@ bundle_decode(const bundle_raw *r, const int data_size) unsigned char *d_r; unsigned int d_len; char *extract_cksum; - gchar* compute_cksum; + gchar *compute_cksum; - if(NULL == r) { - errno = EINVAL; + if (NULL == r) { + set_last_result(BUNDLE_ERROR_INVALID_PARAMETER); return NULL; } extract_cksum = calloc(CHECKSUM_LENGTH+1, sizeof(char)); - if(unlikely(NULL== extract_cksum)) - { - errno = ENOMEM; + if (unlikely(NULL == extract_cksum)) { + set_last_result(BUNDLE_ERROR_INVALID_PARAMETER); return NULL; } /* base 64 decode of input string*/ - d_str = g_base64_decode((char*)r, &d_len_raw); + d_str = g_base64_decode((char *)r, &d_len_raw); /*extract checksum from the received string */ - strncpy(extract_cksum,d_str,CHECKSUM_LENGTH); + strncpy(extract_cksum, (const char *)d_str, CHECKSUM_LENGTH); /* compute checksum for the data */ - compute_cksum = g_compute_checksum_for_string(G_CHECKSUM_MD5,d_str+CHECKSUM_LENGTH,d_len_raw-CHECKSUM_LENGTH); - /*compare checksum values- extracted from the received string and computed from the data */ - if(strcmp(extract_cksum,compute_cksum)!=0) - { + compute_cksum = g_compute_checksum_for_string(G_CHECKSUM_MD5, + (const char *)(d_str+CHECKSUM_LENGTH), d_len_raw-CHECKSUM_LENGTH); + /* compare checksum values- extracted from the received + * string and computed from the data + */ + if (strcmp(extract_cksum, compute_cksum) != 0) { free(extract_cksum); g_free(compute_cksum); return NULL; } - d_r = d_str+CHECKSUM_LENGTH; - d_len= d_len_raw-CHECKSUM_LENGTH; + d_r = d_str + CHECKSUM_LENGTH; + d_len = d_len_raw - CHECKSUM_LENGTH; /* re-construct bundle */ b = bundle_create(); @@ -507,21 +560,21 @@ bundle_decode(const bundle_raw *r, const int data_size) size_t bytes_read; keyval_t *kv; - while(p_r < d_r + d_len - 1) { - kv = NULL; // To get a new kv + while (p_r < d_r + d_len - 1) { + kv = NULL; /* To get a new kv */ - // Find type, and use decode function according to type + /* Find type, and use decode function according to type */ int type = keyval_get_type_from_encoded_byte(p_r); - if(keyval_type_is_array(type)) { + if (keyval_type_is_array(type)) bytes_read = keyval_array_decode(p_r, (keyval_array_t **) &kv); - } - else { + else bytes_read = keyval_decode(p_r, &kv); - } - if(kv) _bundle_append_kv(b, kv); - else { break; } + if (kv) + _bundle_append_kv(b, kv); + else + break; p_r += bytes_read; } @@ -529,6 +582,7 @@ bundle_decode(const bundle_raw *r, const int data_size) g_free(compute_cksum); free(d_str); + set_last_result(BUNDLE_ERROR_NONE); return b; } @@ -548,26 +602,25 @@ bundle_encode_raw(bundle *b, bundle_raw **r, int *len) size_t byte_len; gchar *chksum_val = NULL; - if(NULL == b || NULL == r) { - errno = EINVAL; - return -1; - } + if (NULL == b || NULL == r) + return BUNDLE_ERROR_INVALID_PARAMETER; /* calculate memory size */ - size_t msize = 0; // Sum of required size + size_t msize = 0; /* Sum of required size */ kv = b->kv_head; - while(kv != NULL) { + while (kv != NULL) { msize += kv->method->get_encoded_size(kv); kv = kv->next; } m = calloc(msize+CHECKSUM_LENGTH, sizeof(unsigned char)); - if(unlikely(NULL == m )) { errno = ENOMEM; return -1; } + if (unlikely(NULL == m)) + return BUNDLE_ERROR_OUT_OF_MEMORY; - p_m = m+CHECKSUM_LENGTH; /* temporary pointer */ + p_m = m + CHECKSUM_LENGTH; /* temporary pointer */ kv = b->kv_head; - while(kv != NULL) { + while (kv != NULL) { byte = NULL; byte_len = 0; @@ -581,19 +634,20 @@ bundle_encode_raw(bundle *b, bundle_raw **r, int *len) } /*compute checksum from the data*/ - chksum_val = g_compute_checksum_for_string(G_CHECKSUM_MD5,m+CHECKSUM_LENGTH,msize); + chksum_val = g_compute_checksum_for_string(G_CHECKSUM_MD5, + (const char *)(m+CHECKSUM_LENGTH), msize); /*prefix checksum to the data */ - memcpy(m,chksum_val,CHECKSUM_LENGTH); + memcpy(m, chksum_val, CHECKSUM_LENGTH); /*if ( NULL != r ) { *r =(unsigned char*)g_base64_encode(m,msize+CHECKSUM_LENGTH); if ( NULL != len ) *len = strlen((char*)*r); } free(m);*/ *r = m; - *len = msize+CHECKSUM_LENGTH; + *len = msize + CHECKSUM_LENGTH; g_free(chksum_val);/*free checksum string */ - return 0; + return BUNDLE_ERROR_NONE; } bundle * @@ -606,37 +660,40 @@ bundle_decode_raw(const bundle_raw *r, const int data_size) unsigned char *d_r = NULL; unsigned int d_len; char *extract_cksum = NULL; - gchar* compute_cksum = NULL; + gchar *compute_cksum = NULL; - if(NULL == r) { - errno = EINVAL; + if (NULL == r) { + set_last_result(BUNDLE_ERROR_INVALID_PARAMETER); return NULL; } - extract_cksum = calloc(CHECKSUM_LENGTH+1, sizeof(char)); - if(unlikely(NULL== extract_cksum)) - { - errno = ENOMEM; + extract_cksum = calloc(CHECKSUM_LENGTH + 1, sizeof(char)); + if (unlikely(NULL == extract_cksum)) { + set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY); return NULL; } /* base 64 decode of input string*/ - //d_str = g_base64_decode((char*)r, &d_len_raw); - d_str = r; + /* d_str = g_base64_decode((char*)r, &d_len_raw); */ + d_str = (unsigned char *)r; d_len_raw = data_size; - /*extract checksum from the received string */ - strncpy(extract_cksum,d_str,CHECKSUM_LENGTH); + /* extract checksum from the received string */ + strncpy(extract_cksum, (const char *)d_str, CHECKSUM_LENGTH); /* compute checksum for the data */ - compute_cksum = g_compute_checksum_for_string(G_CHECKSUM_MD5,d_str+CHECKSUM_LENGTH,d_len_raw-CHECKSUM_LENGTH); - /*compare checksum values- extracted from the received string and computed from the data */ - if(strcmp(extract_cksum,compute_cksum)!=0) - { + compute_cksum = g_compute_checksum_for_string(G_CHECKSUM_MD5, + (const char *)(d_str+CHECKSUM_LENGTH), + d_len_raw-CHECKSUM_LENGTH); + /*compare checksum values- extracted from the received + * string and computed from the data + */ + if (strcmp(extract_cksum, compute_cksum) != 0) { free(extract_cksum); g_free(compute_cksum); + set_last_result(BUNDLE_ERROR_INVALID_PARAMETER); return NULL; } - d_r = d_str+CHECKSUM_LENGTH; - d_len= d_len_raw-CHECKSUM_LENGTH; + d_r = d_str + CHECKSUM_LENGTH; + d_len = d_len_raw - CHECKSUM_LENGTH; /* re-construct bundle */ b = bundle_create(); @@ -646,34 +703,35 @@ bundle_decode_raw(const bundle_raw *r, const int data_size) size_t bytes_read; keyval_t *kv; - while(p_r < d_r + d_len - 1) { - kv = NULL; // To get a new kv + while (p_r < d_r + d_len - 1) { + kv = NULL; /* To get a new kv */ - // Find type, and use decode function according to type + /* Find type, and use decode function according to type */ int type = keyval_get_type_from_encoded_byte(p_r); - if(keyval_type_is_array(type)) { + if (keyval_type_is_array(type)) bytes_read = keyval_array_decode(p_r, (keyval_array_t **) &kv); - } - else { + else bytes_read = keyval_decode(p_r, &kv); - } - if(kv) _bundle_append_kv(b, kv); - else { break; } + if (kv) + _bundle_append_kv(b, kv); + else + break; p_r += bytes_read; } free(extract_cksum); g_free(compute_cksum); - //free(d_str); + set_last_result(BUNDLE_ERROR_NONE); return b; } -void -_iter_export_to_argv(const char *key, const int type, const keyval_t *kv, void *user_data) +void +_iter_export_to_argv(const char *key, const int type, const keyval_t *kv, + void *user_data) { struct _argv_idx *vi = (struct _argv_idx *)user_data; @@ -682,20 +740,19 @@ _iter_export_to_argv(const char *key, const int type, const keyval_t *kv, void * unsigned char *byte = NULL, *encoded_byte = NULL; size_t byte_len = 0; - if(0 == kv->method->encode((struct keyval_t *)kv, &byte, &byte_len)) { - // TODO: encode FAILED! + if (0 == kv->method->encode((struct keyval_t *)kv, &byte, &byte_len)) { + /* TODO: encode FAILED! */ BUNDLE_EXCEPTION_PRINT("bundle: FAILED to encode keyval: %s\n", key); return; } - // bas64 encode - encoded_byte =(unsigned char *) g_base64_encode(byte, byte_len); - if(NULL == encoded_byte) { + encoded_byte = (unsigned char *)g_base64_encode(byte, byte_len); + if (NULL == encoded_byte) { BUNDLE_EXCEPTION_PRINT("bundle: failed to encode byte\n"); return; } - - vi->argv[vi->idx + 1] =(char*)encoded_byte; + + vi->argv[vi->idx + 1] = (char *)encoded_byte; (vi->idx) += 2; free(byte); @@ -706,44 +763,63 @@ bundle_export_to_argv(bundle *b, char ***argv) { int argc, item_count; + if (b == NULL || argv == NULL) { + set_last_result(BUNDLE_ERROR_INVALID_PARAMETER); + return -1; + } + item_count = bundle_get_count(b); - argc = 2 * item_count + 2; /* 2 more count for argv[0] and arv[1] = encoded */ + argc = 2 * item_count + 2; + /* 2 more count for argv[0] and arv[1] = encoded */ *argv = calloc(argc + 1, sizeof(char *)); - if(!*argv) return -1; + if (!*argv) { + set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY); + return -1; + } struct _argv_idx vi; vi.argc = argc; vi.argv = *argv; vi.idx = 2; /* start from index 2*/ - vi.argv[1]=TAG_IMPORT_EXPORT_CHECK; /* set argv[1] as encoded*/ + vi.argv[1] = TAG_IMPORT_EXPORT_CHECK; /* set argv[1] as encoded*/ /*BUNDLE_LOG_PRINT("\nargument 1 is %s",vi.argv[1]);*/ bundle_foreach(b, _iter_export_to_argv, &vi); + set_last_result(BUNDLE_ERROR_NONE); return argc; } int bundle_free_exported_argv(int argc, char ***argv) { - if(!*argv) return -1; /*TC_FIX : fix for double free- sigabrt */ - + if (argc < 1) + return BUNDLE_ERROR_INVALID_PARAMETER; + + if (!*argv || argc < 2) + return BUNDLE_ERROR_INVALID_PARAMETER; + int i; - for(i=1; i < argc; i+=2) { - free((*argv)[i+1]); + for (i = 3; i < argc; i += 2) { + free((*argv)[i]); + /* need to free value from g_base64_encode() */ } free(*argv); - *argv= NULL; - return 0; + *argv = NULL; + return BUNDLE_ERROR_NONE; } bundle * bundle_import_from_argv(int argc, char **argv) { - if(!argv) return NULL; /* TC_FIX error handling for argv =NULL*/ + if (!argv) { + set_last_result(BUNDLE_ERROR_INVALID_PARAMETER); + return NULL; /* TC_FIX error handling for argv =NULL*/ + } bundle *b = bundle_create(); - if(!b) return NULL; + if (!b) + return NULL; /* @@ -753,11 +829,11 @@ bundle_import_from_argv(int argc, char **argv) } */ - if(!argv[1]||strcmp(argv[1],TAG_IMPORT_EXPORT_CHECK)) - { + if (!argv[1] || strcmp(argv[1], TAG_IMPORT_EXPORT_CHECK)) { /*BUNDLE_LOG_PRINT("\nit is not encoded");*/ int idx; - for (idx = 1; idx + 1 < argc; idx = idx + 2) { /*start idx from one as argv[1] is user given argument*/ + /*start idx from one as argv[1] is user given argument*/ + for (idx = 1; idx + 1 < argc; idx = idx + 2) { bundle_add(b, argv[idx], argv[idx + 1]); } return b; @@ -769,30 +845,31 @@ bundle_import_from_argv(int argc, char **argv) unsigned char *byte = NULL; char *encoded_byte; unsigned int byte_size; - for(idx = 2; idx + 1 < argc; idx = idx+2) { // start idx from 2 as argv[1] is encoded + /* start idx from 2 as argv[1] is encoded */ + for (idx = 2; idx + 1 < argc; idx = idx + 2) { kv = NULL; kva = NULL; encoded_byte = argv[idx+1]; - // base64_decode + /* base64_decode */ byte = g_base64_decode(encoded_byte, &byte_size); - if(NULL == byte) { - if(b) bundle_free(b); + if (NULL == byte) { + if (b) + set_last_result(bundle_free(b)); return NULL; } type = keyval_get_type_from_encoded_byte(byte); - if(keyval_type_is_array(type)) { - if(0 == keyval_array_decode(byte, &kva)) { - // TODO: error! + if (keyval_type_is_array(type)) { + if (0 == keyval_array_decode(byte, &kva)) { + /* TODO: error! */ BUNDLE_EXCEPTION_PRINT("Unable to Decode array\n"); } kv = (keyval_t *)kva; - } - else { - if(0 == keyval_decode(byte, &kv)) { - // TODO: error! + } else { + if (0 == keyval_decode(byte, &kv)) { + /* TODO: error! */ BUNDLE_EXCEPTION_PRINT("Unable to Decode\n"); } } @@ -801,6 +878,7 @@ bundle_import_from_argv(int argc, char **argv) free(byte); byte = NULL; } + set_last_result(BUNDLE_ERROR_NONE); return b; } @@ -809,20 +887,20 @@ bundle_import_from_argv(int argc, char **argv) bundle_get_type(bundle *b, const char *key) { keyval_t *kv = _bundle_find_kv(b, key); - if(kv) return kv->type; + if (kv) + return kv->type; else { - errno = ENOKEY; + set_last_result(BUNDLE_ERROR_KEY_NOT_AVAILABLE); return BUNDLE_TYPE_NONE; } } -// array functions /** Get length of an array */ unsigned int bundle_get_array_len(bundle *b, const char *key) { - return 0; + return BUNDLE_ERROR_NONE; } /** Get size of an item in byte, of given pointer @@ -830,44 +908,39 @@ bundle_get_array_len(bundle *b, const char *key) size_t bundle_get_array_val_size(bundle *b, const char *key, const void *val_ptr) { - return 0; + return BUNDLE_ERROR_NONE; } static int -bundle_set_array_val(bundle *b, const char *key, const int type, const unsigned int idx, const void *val, const size_t size) +bundle_set_array_val(bundle *b, const char *key, const int type, + const unsigned int idx, const void *val, const size_t size) { -//void **array = NULL; - keyval_t *kv = _bundle_find_kv(b, key); - if(NULL == kv) return -1; + if (NULL == kv) + return get_last_result(); - if(type != kv->type) { - errno = EINVAL; - return -1; - } + if (type != kv->type) + return BUNDLE_ERROR_INVALID_PARAMETER; - if(! keyval_type_is_array(kv->type)) { // TODO: Is this needed? - errno = EINVAL; - return -1; - } + /* TODO: Is this needed? */ + if (!keyval_type_is_array(kv->type)) + return BUNDLE_ERROR_INVALID_PARAMETER; keyval_array_t *kva = (keyval_array_t *)kv; - if(! keyval_array_is_idx_valid(kva, idx)) { - errno = EINVAL; - return -1; - } + if (!keyval_array_is_idx_valid(kva, idx)) + return BUNDLE_ERROR_INVALID_PARAMETER; - if(!kva->array_val) { // NULL value test (TODO: is this needed?) - errno = ENOMEM; - return -1; - } + /* NULL value test (TODO: is this needed?) */ + if (!kva->array_val) + return BUNDLE_ERROR_INVALID_PARAMETER; - return keyval_array_set_element(kva, idx, val, size); + return keyval_array_set_element(kva, idx, (void *)val, size); } int -bundle_add_str_array(bundle *b, const char *key, const char **str_array, const int len) +bundle_add_str_array(bundle *b, const char *key, const char **str_array, + const int len) { return _bundle_add_kv(b, key, str_array, 0, BUNDLE_TYPE_STR_ARRAY, len); } @@ -876,34 +949,38 @@ bundle_add_str_array(bundle *b, const char *key, const char **str_array, const i int bundle_get_val_array(bundle *b, const char *key, char ***str_array, int *len) { - return _bundle_get_val(b, key, BUNDLE_TYPE_STR_ARRAY, (void **) str_array, NULL,(unsigned int *)len, NULL); + return _bundle_get_val(b, key, BUNDLE_TYPE_STR_ARRAY, + (void **)str_array, NULL, (unsigned int *)len, NULL); } -const char ** bundle_get_str_array(bundle *b, const char *key,int *len) +const char **bundle_get_str_array(bundle *b, const char *key, int *len) { + int ret = BUNDLE_ERROR_NONE; const char **arr_val = NULL; - int r = 0; - r = bundle_get_val_array(b,key,(char***)&arr_val,len); - return arr_val; + ret = bundle_get_val_array(b, key, (char ***)&arr_val, len); + set_last_result(ret); + return arr_val; } int bundle_compare(bundle *b1, bundle *b2) { - if(!b1 || !b2) return -1; + if (!b1 || !b2) + return -1; keyval_t *kv1, *kv2; - //keyval_array_t *kva1, *kva2; - //char *key; - if(bundle_get_count(b1) != bundle_get_count(b2)) return 1; - for(kv1 = b1->kv_head; kv1 != NULL; kv1 = kv1->next) { + if (bundle_get_count(b1) != bundle_get_count(b2)) + return 1; + for (kv1 = b1->kv_head; kv1 != NULL; kv1 = kv1->next) { kv2 = _bundle_find_kv(b2, kv1->key); - if(!kv2) return 1; - if(kv1->method->compare(kv1, kv2)) return 1; + if (!kv2) + return 1; + if (kv1->method->compare(kv1, kv2)) + return 1; } return 0; } @@ -911,19 +988,20 @@ bundle_compare(bundle *b1, bundle *b2) int -bundle_set_str_array_element(bundle *b, const char *key, const unsigned int idx, const char *val) +bundle_set_str_array_element(bundle *b, const char *key, + const unsigned int idx, const char *val) { - if(!val) { - errno = EINVAL; - return -1; - } - return bundle_set_array_val(b, key, BUNDLE_TYPE_STR_ARRAY, idx, val, strlen(val)+1); + if (!val) + return BUNDLE_ERROR_INVALID_PARAMETER; + + return bundle_set_array_val(b, key, BUNDLE_TYPE_STR_ARRAY, + idx, val, strlen(val)+1); } -// byte type int -bundle_add_byte(bundle *b, const char *key, const void *byte, const size_t size) +bundle_add_byte(bundle *b, const char *key, const void *byte, + const size_t size) { return _bundle_add_kv(b, key, byte, size, BUNDLE_TYPE_BYTE, 1); } @@ -931,25 +1009,31 @@ bundle_add_byte(bundle *b, const char *key, const void *byte, const size_t size) int bundle_get_byte(bundle *b, const char *key, void **byte, size_t *size) { - return _bundle_get_val(b, key, BUNDLE_TYPE_BYTE, (void **) byte, size, NULL, NULL); + return _bundle_get_val(b, key, BUNDLE_TYPE_BYTE, (void **) byte, + size, NULL, NULL); } int -bundle_add_byte_array(bundle *b, const char *key, void **byte_array, const unsigned int len) +bundle_add_byte_array(bundle *b, const char *key, void **byte_array, + const unsigned int len) { return _bundle_add_kv(b, key, byte_array, 0, BUNDLE_TYPE_BYTE_ARRAY, len); } int -bundle_get_byte_array(bundle *b, const char *key, void ***byte_array, unsigned int *len, unsigned int **array_element_size) +bundle_get_byte_array(bundle *b, const char *key, void ***byte_array, + unsigned int *len, unsigned int **array_element_size) { - return _bundle_get_val(b, key, BUNDLE_TYPE_BYTE_ARRAY, (void **)byte_array, NULL, len, array_element_size); + return _bundle_get_val(b, key, BUNDLE_TYPE_BYTE_ARRAY, + (void **)byte_array, NULL, len, array_element_size); } int -bundle_set_byte_array_element(bundle *b, const char *key, const unsigned int idx, const void *val, const size_t size) +bundle_set_byte_array_element(bundle *b, const char *key, + const unsigned int idx, const void *val, const size_t size) { - return bundle_set_array_val(b, key, BUNDLE_TYPE_BYTE_ARRAY, idx, val, size); + return bundle_set_array_val(b, key, BUNDLE_TYPE_BYTE_ARRAY, + idx, val, size); } diff --git a/src/keyval.c b/src/keyval.c index 5397f90..35472c6 100755 --- a/src/keyval.c +++ b/src/keyval.c @@ -29,9 +29,8 @@ #include "keyval_type.h" #include "keyval.h" #include "bundle_log.h" +#include "bundle.h" #include -#include -extern int errno; static keyval_method_collection_t method = { keyval_free, @@ -42,48 +41,45 @@ static keyval_method_collection_t method = { }; keyval_t * -keyval_new(keyval_t *kv, const char *key, const int type, const void *val, const size_t size) +keyval_new(keyval_t *kv, const char *key, const int type, const void *val, + const size_t size) { int must_free_obj; must_free_obj = kv ? 0 : 1; - if(!kv) { + if (!kv) { kv = calloc(1, sizeof(keyval_t)); - if(!kv) { - //errno = ENOMEM; // set by calloc + if (!kv) { + set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY); return NULL; } } - // key - if(kv->key) { + if (kv->key) { keyval_free(kv, must_free_obj); return NULL; } kv->key = strdup(key); - if(!kv->key) { - //errno = ENOMEM; // set by strdup + if (!kv->key) { + set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY); keyval_free(kv, must_free_obj); return NULL; } - // elementa of primitive types kv->type = type; kv->size = size; - - if(size) { - kv->val = calloc(1, size); // allocate memory unconditionally ! - if(!kv->val) { - errno = ENOMEM; + + if (size) { + kv->val = calloc(1, size); + if (!kv->val) { + set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY); keyval_free(kv, 1); return NULL; } - if(val) { + if (val) memcpy(kv->val, val, size); - } } - // Set methods kv->method = &method; return kv; @@ -92,21 +88,21 @@ keyval_new(keyval_t *kv, const char *key, const int type, const void *val, const void keyval_free(keyval_t *kv, int do_free_object) { - //int i; - - if(NULL == kv) return; + if (NULL == kv) + return; - if(kv->key) { + if (kv->key) { free(kv->key); kv->key = NULL; } - if(NULL != kv->val) { + if (NULL != kv->val) { free(kv->val); kv->val = NULL; } - if(do_free_object) free(kv); + if (do_free_object) + free(kv); return; } @@ -114,12 +110,17 @@ keyval_free(keyval_t *kv, int do_free_object) int keyval_get_data(keyval_t *kv, int *type, void **val, size_t *size) { - if(!kv) return -EINVAL; - if(keyval_type_is_array(kv->type)) return -EINVAL; - - if(type) *type = kv->type; - if(val) *val = kv->val; - if(size) *size = kv->size; + if (!kv) + return BUNDLE_ERROR_INVALID_PARAMETER; + if (keyval_type_is_array(kv->type)) + return BUNDLE_ERROR_INVALID_PARAMETER; + + if (type) + *type = kv->type; + if (val) + *val = kv->val; + if (size) + *size = kv->size; return 0; } @@ -127,15 +128,22 @@ keyval_get_data(keyval_t *kv, int *type, void **val, size_t *size) int keyval_compare(keyval_t *kv1, keyval_t *kv2) { - if(!kv1 || !kv2) return -1; - - if(0 != strcmp(kv1->key, kv2->key)) return 1; - if(kv1->type != kv2->type) return 1; - if(kv1->size != kv2->size) return 1; - - if(kv1->val == NULL && kv2->val == NULL) return 0; - if(kv1->val == NULL || kv2->val == NULL) return 1; - if(0 != memcmp(kv1->val, kv2->val, kv1->size)) return 1; + if (!kv1 || !kv2) + return -1; + + if (0 != strcmp(kv1->key, kv2->key)) + return 1; + if (kv1->type != kv2->type) + return 1; + if (kv1->size != kv2->size) + return 1; + + if (kv1->val == NULL && kv2->val == NULL) + return 0; + if (kv1->val == NULL || kv2->val == NULL) + return 1; + if (0 != memcmp(kv1->val, kv2->val, kv1->size)) + return 1; return 0; } @@ -143,15 +151,16 @@ keyval_compare(keyval_t *kv1, keyval_t *kv2) size_t keyval_get_encoded_size(keyval_t *kv) { - if(!kv) return 0; + if (!kv) + return 0; - size_t encoded_size - = sizeof(size_t) // total size - + sizeof(int) // type - + sizeof(size_t) // key size - + strlen(kv->key) + 1 // key (+ null byte) - + sizeof(size_t) // size - + kv->size; // val + size_t encoded_size + = sizeof(size_t) /* total size */ + + sizeof(int) /* type */ + + sizeof(size_t) /* key size */ + + strlen(kv->key) + 1 /* key (+ null byte) */ + + sizeof(size_t) /* size */ + + kv->size; /* val */ return encoded_size; } @@ -169,14 +178,6 @@ keyval_get_encoded_size(keyval_t *kv) size_t keyval_encode(keyval_t *kv, unsigned char **byte, size_t *byte_len) { - /* - * type - * key size - * key - * val size - * val - */ - static const size_t sz_type = sizeof(int); static const size_t sz_keysize = sizeof(size_t); size_t sz_key = strlen(kv->key) + 1; @@ -186,10 +187,11 @@ keyval_encode(keyval_t *kv, unsigned char **byte, size_t *byte_len) *byte_len = keyval_get_encoded_size(kv); *byte = calloc(1, *byte_len); - if(!*byte) return 0; + if (!*byte) + return 0; unsigned char *p = *byte; - + memcpy(p, byte_len, sizeof(size_t)); p += sizeof(size_t); memcpy(p, &(kv->type), sz_type); p += sz_type; memcpy(p, &sz_key, sz_keysize); p += sz_keysize; @@ -226,7 +228,8 @@ keyval_decode(unsigned char *byte, keyval_t **kv) size_t size = *((size_t *)p); p += sz_size; void *val = (void *)p; p += size; - if(kv) *kv = keyval_new(*kv, key, type, val, size); // If *kv != NULL, use given kv + if (kv) + *kv = keyval_new(*kv, key, type, val, size); return byte_len; } @@ -235,14 +238,11 @@ keyval_decode(unsigned char *byte, keyval_t **kv) int keyval_get_type_from_encoded_byte(unsigned char *byte) { - // skip total size (== sizeof(size_t)) static const size_t sz_byte_len = sizeof(size_t); - unsigned char *p=byte; + unsigned char *p = byte; p += sz_byte_len; int type = *((int *)p); - return type; - - //return (int )*(byte + sizeof(size_t)); + return type; } diff --git a/src/keyval_array.c b/src/keyval_array.c index af2aa12..8666678 100755 --- a/src/keyval_array.c +++ b/src/keyval_array.c @@ -34,7 +34,6 @@ #include #include -#include static keyval_method_collection_t method = { @@ -46,24 +45,24 @@ static keyval_method_collection_t method = { }; keyval_array_t * -keyval_array_new(keyval_array_t *kva, const char *key, const int type, const void **array_val, const unsigned int len) +keyval_array_new(keyval_array_t *kva, const char *key, const int type, + const void **array_val, const unsigned int len) { int must_free_obj; must_free_obj = kva ? 0 : 1; - if(!kva) { + if (!kva) { kva = calloc(1, sizeof(keyval_array_t)); - if(unlikely(NULL==kva)) { - errno = ENOMEM; + if (unlikely(NULL == kva)) { + set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY); return NULL; } } - // keyval setting + /* keyval setting */ keyval_t *kv = keyval_new((keyval_t *)kva, key, type, NULL, 0); - if(unlikely(NULL==kv)) - { - errno = ENOMEM; + if (unlikely(NULL == kv)) { + set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY); return NULL; } @@ -71,33 +70,34 @@ keyval_array_new(keyval_array_t *kva, const char *key, const int type, const voi kva->len = len; - // Set array value, if exist - if(kva->array_val) { - errno=EINVAL; - if(must_free_obj) keyval_array_free(kva, 1); + /* Set array value, if exist */ + if (kva->array_val) { + set_last_result(BUNDLE_ERROR_INVALID_PARAMETER); + if (must_free_obj) + keyval_array_free(kva, 1); return NULL; } kva->array_val = calloc(len, sizeof(void *)); - if(!(kva->array_val)) { - errno = ENOMEM; + if (!(kva->array_val)) { + set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY); keyval_array_free(kva, 1); return NULL; } - // array_element_size + /* array_element_size */ kva->array_element_size = calloc(len, sizeof(size_t)); - if(!(kva->array_element_size)) { - errno = ENOMEM; + if (!(kva->array_element_size)) { + set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY); keyval_array_free(kva, 1); return NULL; } - // If avaliable, copy array val - if(array_val + /* If available, copy array val */ + if (array_val && keyval_type_is_measurable(type) && keyval_type_get_measure_size_func(type)) { - // array_val have original data array. copy it! + /* array_val have original data array. copy it! */ - if(keyval_array_copy_array((keyval_array_t*)kv, - (void**)array_val, + if (keyval_array_copy_array((keyval_array_t *)kv, + (void **)array_val, len, keyval_type_get_measure_size_func(type)) ) { @@ -106,7 +106,7 @@ keyval_array_new(keyval_array_t *kva, const char *key, const int type, const voi } } - // Set methods + /* Set methods */ kv->method = &method; return kva; @@ -115,70 +115,85 @@ keyval_array_new(keyval_array_t *kva, const char *key, const int type, const voi void keyval_array_free(keyval_array_t *kva, int do_free_object) { - if(!kva) return; + if (!kva) + return; - // free keyval_array elements + /* free keyval_array elements */ free(kva->array_element_size); int i; - for(i=0; ilen; i++) { - if(kva->array_val[i]) free(kva->array_val[i]); + for (i = 0; i < kva->len; i++) { + if (kva->array_val[i]) + free(kva->array_val[i]); } free(kva->array_val); - - // free parent + + /* free parent */ keyval_free((keyval_t *)kva, 0); - // free object - if(do_free_object) free(kva); + /* free object */ + if (do_free_object) + free(kva); } -int +int keyval_array_compare(keyval_array_t *kva1, keyval_array_t *kva2) { keyval_t *kv1, *kv2; - if(!kva1 || !kva2) return -1; + if (!kva1 || !kva2) + return -1; kv1 = (keyval_t *)kva1; kv2 = (keyval_t *)kva2; - if(0 != strcmp(kv1->key, kv2->key)) return 1; - if(kv1->type != kv2->type) return 1; - if(kva1->len != kva2->len) return 1; + if (0 != strcmp(kv1->key, kv2->key)) + return 1; + if (kv1->type != kv2->type) + return 1; + if (kva1->len != kva2->len) + return 1; int i; - for(i=0; ilen; i++) { - if(kva1->array_val[i] == NULL && kva2->array_val[i] == NULL) continue; - if(kva1->array_val[i] == NULL || kva2->array_val[i] == NULL) return 1; - if(0 != memcmp(kva1->array_val[i], kva2->array_val[i], kva1->array_element_size[i])) return 1; + for (i = 0; i < kva1->len; i++) { + if (kva1->array_val[i] == NULL && kva2->array_val[i] == NULL) + continue; + if (kva1->array_val[i] == NULL || kva2->array_val[i] == NULL) + return 1; + if (0 != memcmp(kva1->array_val[i], kva2->array_val[i], + kva1->array_element_size[i])) + return 1; } return 0; } int -keyval_array_copy_array(keyval_array_t *kva, void **array_val, unsigned int array_len, size_t (*measure_val_len)(void * val)) +keyval_array_copy_array(keyval_array_t *kva, void **array_val, + unsigned int array_len, size_t (*measure_val_len)(void *val)) { keyval_t *kv = (keyval_t *)kva; - - // Get measure_size function of the value type - keyval_type_measure_size_func_t measure_size = keyval_type_get_measure_size_func(kv->type); - if(!measure_size) return -1; - - // Copy each array item int i; - for(i=0; i < array_len; i++) { + + /* Get measure_size function of the value type */ + keyval_type_measure_size_func_t measure_size = + keyval_type_get_measure_size_func(kv->type); + if (!measure_size) + return -1; + + /* Copy each array item */ + for (i = 0; i < array_len; i++) { kva->array_val[i] = malloc(measure_size(array_val[i])); - if(!(kva->array_val[i])) { - errno = ENOMEM; + if (!(kva->array_val[i])) { + set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY); goto cleanup_exit; } - memcpy(kva->array_val[i], array_val[i], measure_size(array_val[i])); + memcpy(kva->array_val[i], array_val[i], + measure_size(array_val[i])); kva->array_element_size[i] = measure_size(array_val[i]); } return 0; cleanup_exit: - for(i=0; iarray_val[i]) { + for (i = 0; i < array_len; i++) { + if (kva->array_val[i]) { free(kva->array_val[i]); kva->array_val[i] = NULL; } @@ -189,57 +204,60 @@ cleanup_exit: int keyval_array_is_idx_valid(keyval_array_t *kva, int idx) { - //keyval_t *kv = (keyval_t *)kva; - if(kva && kva->len > idx && 0 <= idx) return 1; + if (kva && kva->len > idx && 0 <= idx) + return 1; return 0; } int keyval_array_set_element(keyval_array_t *kva, int idx, void *val, size_t size) { - if(kva->array_val[idx]) { // An element is already exist in the idx! - if(!val) { // val==NULL means 'Free this element!' + /* An element is already exist in the idx! */ + if (kva->array_val[idx]) { + /* val==NULL means 'Free this element!' */ + if (!val) { free(kva->array_val[idx]); kva->array_val[idx] = NULL; kva->array_element_size[idx] = 0; + } else { + /* Error case! */ + return BUNDLE_ERROR_INVALID_PARAMETER; } - else { - // Error case! - errno = EINVAL; - return -1; - } - } - else { - // Normal case. Copy value into the array. + } else { + /* Normal case. Copy value into the array. */ kva->array_val[idx] = malloc(size); - if(!(kva->array_val[idx])) { - errno = ENOMEM; - return -1; - } - if(val) { - memcpy(kva->array_val[idx], val, size); // val - kva->array_element_size[idx] = size; // size + if (!(kva->array_val[idx])) + return BUNDLE_ERROR_OUT_OF_MEMORY; + if (val) { + memcpy(kva->array_val[idx], val, size); + kva->array_element_size[idx] = size; } } - return 0; + return BUNDLE_ERROR_NONE; } int keyval_array_get_data(keyval_array_t *kva, int *type, void ***array_val, unsigned int *len, size_t **array_element_size) { - if(!kva) return -EINVAL; + if (!kva) + return BUNDLE_ERROR_INVALID_PARAMETER; keyval_t *kv = (keyval_t *)kva; - if(!keyval_type_is_array(kv->type)) return -EINVAL; - - // Return values - if(type) *type = kv->type; - if(array_val) *array_val = kva->array_val; - if(len) *len = kva->len; - if(array_element_size) *array_element_size = kva->array_element_size; - - return 0; + if (!keyval_type_is_array(kv->type)) + return BUNDLE_ERROR_INVALID_PARAMETER; + + /* Return values */ + if (type) + *type = kv->type; + if (array_val) + *array_val = kva->array_val; + if (len) + *len = kva->len; + if (array_element_size) + *array_element_size = kva->array_element_size; + + return BUNDLE_ERROR_NONE; } size_t @@ -247,16 +265,16 @@ keyval_array_get_encoded_size(keyval_array_t *kva) { size_t sum_array_element_size = 0; int i; - for(i=0; i < kva->len; i++) { + for (i = 0; i < kva->len; i++) { sum_array_element_size += kva->array_element_size[i]; } size_t encoded_size - = sizeof(size_t) // total size - + sizeof(int) // type - + sizeof(size_t) // keysize - + strlen(((keyval_t *)kva)->key) + 1 // key (+ null byte) - + sizeof(int) // len - + kva->len * sizeof(size_t) // array_element_size + = sizeof(size_t) /* total size */ + + sizeof(int) /* type */ + + sizeof(size_t) /* keysize */ + + strlen(((keyval_t *)kva)->key) + 1 /* key (+ null byte) */ + + sizeof(int) /* len */ + + kva->len * sizeof(size_t) /* array_element_size */ + sum_array_element_size; return encoded_size; @@ -268,23 +286,24 @@ keyval_array_encode(keyval_array_t *kva, void **byte, size_t *byte_len) keyval_t *kv = (keyval_t *)kva; int i; - // Calculate memory size for kva + /* Calculate memory size for kva */ static const size_t sz_type = sizeof(int); static const size_t sz_keysize = sizeof(size_t); size_t sz_key = strlen(kv->key) + 1; static const unsigned int sz_len = sizeof(int); size_t sz_array_element_size = kva->len * sizeof(size_t); size_t sz_array_val = 0; - for(i=0; i < kva->len; i++) { + for (i = 0; i < kva->len; i++) { sz_array_val += kva->array_element_size[i]; } - // Allocate memory + /* Allocate memory */ *byte_len = keyval_array_get_encoded_size(kva); *byte = calloc(1, *byte_len); - if(!*byte) return 0; - - // Copy data + if (!*byte) + return 0; + + /* Copy data */ unsigned char *p = *byte; memcpy(p, byte_len, sizeof(size_t)); p += sizeof(size_t); @@ -292,8 +311,9 @@ keyval_array_encode(keyval_array_t *kva, void **byte, size_t *byte_len) memcpy(p, &sz_key, sz_keysize); p += sz_keysize; memcpy(p, kv->key, sz_key); p += sz_key; memcpy(p, &(kva->len), sz_len); p += sz_len; - memcpy(p, kva->array_element_size, sz_array_element_size); p += sz_array_element_size; - for(i=0; i < kva->len; i++) { + memcpy(p, kva->array_element_size, sz_array_element_size); + p += sz_array_element_size; + for (i = 0; i < kva->len; i++) { memcpy(p, kva->array_val[i], kva->array_element_size[i]); p += kva->array_element_size[i]; } @@ -311,7 +331,7 @@ keyval_array_decode(void *byte, keyval_array_t **kva) unsigned char *p = byte; - // Get data + /* Get data */ size_t byte_len = *((size_t *)p); p += sz_byte_len; int type = *((int *)p); p += sz_type; size_t keysize = *((size_t *)p); p += sz_keysize; @@ -323,9 +343,11 @@ keyval_array_decode(void *byte, keyval_array_t **kva) *kva = keyval_array_new(NULL, key, type, NULL, len); int i; size_t elem_size = 0; - for(i=0; i < len; i++) { + for (i = 0; i < len; i++) { elem_size += i ? array_element_size[i-1] : 0; - if(keyval_array_set_element(*kva, i, (void *)(array_val+elem_size), array_element_size[i])) { + if (keyval_array_set_element(*kva, i, + (void *)(array_val+elem_size), + array_element_size[i])) { keyval_array_free(*kva, 1); *kva = NULL; return 0; diff --git a/src/keyval_type.c b/src/keyval_type.c index 0f69eee..78004de 100755 --- a/src/keyval_type.c +++ b/src/keyval_type.c @@ -28,46 +28,48 @@ void keyval_type_init(void) { - static int is_done = 0; + static int is_done; - if(is_done) return; + if (is_done) + return; - // Still do nothing is_done = 1; } int keyval_type_is_array(int type) { - if(type & BUNDLE_TYPE_ARRAY) return 1; + if (type & BUNDLE_TYPE_ARRAY) + return 1; return 0; } int keyval_type_is_measurable(int type) { - if(type & BUNDLE_TYPE_MEASURABLE) return 1; + if (type & BUNDLE_TYPE_MEASURABLE) + return 1; return 0; } keyval_type_measure_size_func_t keyval_type_get_measure_size_func(int type) { - switch(type) { - case BUNDLE_TYPE_STR: - case BUNDLE_TYPE_STR_ARRAY: - return keyval_type_measure_size_str; - break; - default: - return NULL; + switch (type) { + case BUNDLE_TYPE_STR: + case BUNDLE_TYPE_STR_ARRAY: + return keyval_type_measure_size_str; + default: + return NULL; } return NULL; } -size_t +size_t keyval_type_measure_size_str(void *val) { - if(!val) return 0; + if (!val) + return 0; return strlen((char *)val) + 1; } -- 2.7.4