4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>,
7 * Jaeho Lee <jaeho81.lee@samsung.com>
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
29 * @brief This file declares has API of the bundle library
33 * @addtogroup CORE_LIB_BUNDLE_MODULE
39 #include <tizen_error.h>
45 #define API __attribute__((visibility("default")))
46 #define likely(x) __builtin_expect(x,1)
47 #define unlikely(x) __builtin_expect(x,0)
50 * @brief Enumeration for error code of Bundle.
55 BUNDLE_ERROR_NONE = TIZEN_ERROR_NONE, /**< Successful */
56 BUNDLE_ERROR_OUT_OF_MEMORY = TIZEN_ERROR_OUT_OF_MEMORY, /**< Out of memory */
57 BUNDLE_ERROR_INVALID_PARAMETER = TIZEN_ERROR_INVALID_PARAMETER, /**< Invalid parameter */
58 BUNDLE_ERROR_KEY_NOT_AVAILABLE = TIZEN_ERROR_KEY_NOT_AVAILABLE, /**< Required key not available */
59 BUNDLE_ERROR_KEY_EXISTS = TIZEN_ERROR_FILE_EXISTS /**< Key exists */
63 * @brief The bundle handle.
66 typedef struct _bundle_t bundle;
69 * @brief The encoded data type.
71 * @see bundle_encode()
72 * @see bundle_decode()
74 typedef unsigned char bundle_raw;
78 * @brief Enumeration for key-value pair types.
81 enum bundle_type_property {
82 BUNDLE_TYPE_ARRAY = 0x0100, /**< Array type */
83 BUNDLE_TYPE_PRIMITIVE = 0x0200, /**< Primitive type */
84 BUNDLE_TYPE_MEASURABLE = 0x0400 /**< Measurable type */
88 * @brief Enumeration for bundle types.
92 BUNDLE_TYPE_NONE = -1, /**< None */
93 BUNDLE_TYPE_ANY = 0, /**< Any type */
94 BUNDLE_TYPE_STR = 1 | BUNDLE_TYPE_MEASURABLE, /**< String type (Default) */
95 BUNDLE_TYPE_STR_ARRAY = BUNDLE_TYPE_STR | BUNDLE_TYPE_ARRAY | BUNDLE_TYPE_MEASURABLE, /**< String array type */
96 BUNDLE_TYPE_BYTE = 2, /**< Byte type */
97 BUNDLE_TYPE_BYTE_ARRAY = BUNDLE_TYPE_BYTE | BUNDLE_TYPE_ARRAY /**< Byte array type */
101 * @brief The key-value pair handle.
103 * @see bundle_iterator_t
105 typedef struct keyval_t bundle_keyval_t;
109 * @brief Called for every key-value pair.
111 * @see bundle_foreach()
113 typedef void (*bundle_iterator_t) (
116 const bundle_keyval_t *kv,
123 * @brief Called for every key-value pair.
125 * @remarks This type is obsolete. You must not use this type any more.
126 * @see bundle_iterate()
128 typedef void (*bundle_iterate_cb_t) (const char *key, const char *val, void *data);
132 * @brief Creates a bundle object.
134 * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section.
135 * @return The bundle object
136 * @retval @c NULL - Failure
137 * @exception BUNDLE_ERROR_NONE Success
138 * @exception BUNDLE_ERROR_OUT_OF_MEMORY Out of memory
143 bundle *b = bundle_create(); // Create new bundle object
144 bundle_free(b); // free bundle
147 API bundle* bundle_create(void);
150 * @brief Frees the given bundle object with key-value pairs in it.
152 * @param[in] b The bundle object to be freed
153 * @return The operation result;
154 * @retval BUNDLE_ERROR_NONE Success
155 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
156 * @pre @a b must be a valid bundle object.
157 * @see bundle_create()
160 bundle *b = bundle_create(); // Create new bundle object
161 bundle_free(b); // free bundle
164 API int bundle_free(bundle *b);
167 * @brief Adds a strings array type key-value pair into a given bundle.
169 * @param[in] b The bundle object
170 * @param[in] key The key
171 * @param[in] str_array The string type value; if @c NULL, an empty array is created; you can change an item with
172 * @param[in] len The length of the array
173 * @return The operation result
174 * @retval BUNDLE_ERROR_NONE Success
175 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
176 * @retval BUNDLE_ERROR_KEY_EXISTS Key already exists
177 * @retval BUNDLE_ERROR_OUT_OF_MEMORY Out of memory
178 * @pre @a b must be a valid bundle object.
179 * @see bundle_get_str_array()
183 char *sa = { "aaa", "bbb", "ccc" }; // A string array of length 3
184 bundle *b = bundle_create();
185 bundle_add_str_array(b, "foo", sa, 3); // add a key-val pair
189 API int bundle_add_str_array(bundle *b, const char *key, const char **str_array, const int len);
193 * @brief Adds a string type key-value pair into a given bundle.
195 * @param[in] b The bundle object
196 * @param[in] key The key
197 * @param[in] val The value
198 * @return The operation result
199 * @retval BUNDLE_ERROR_NONE Success
200 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
201 * @retval BUNDLE_ERROR_KEY_EXISTS Key already exists
202 * @retval BUNDLE_ERROR_OUT_OF_MEMORY Out of memory
203 * @pre @a b must be a valid bundle object.
204 * @see bundle_add_str()
207 bundle *b = bundle_create(); // Create new bundle object
208 bundle_add(b, "foo_key", "bar_val"); // add a key-val pair
213 API int bundle_add(bundle *b, const char *key, const char *val);
216 * @brief Deletes a key-value object with the given key.
218 * @param[in] b The bundle object
219 * @param[in] key The given key
220 * @return The operation result
221 * @retval BUNDLE_ERROR_NONE Success
222 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
223 * @retval BUNDLE_ERROR_KEY_NOT_AVAILABLE Key not available
224 * @pre @a b must be a valid bundle object.
227 bundle *b = bundle_create(); // Create new bundle object
228 bundle_add_str(b, "foo_key", "bar_val"); // add a key-val pair
229 bundle_del(b, "foo_key"); // del "foo_key" from b
234 API int bundle_del(bundle *b, const char* key);
237 * @brief Gets a string array from a given key.
239 * @remarks You MUST free or modify the returned string!
240 * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section.
241 * @param[in] b The bundle object
242 * @param[in] key The key
243 * @param[out] len The array length
244 * @return The pointer to the array of strings
245 * @retval @c NULL - Key not found
246 * @exception BUNDLE_ERROR_NONE Success
247 * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
248 * @exception BUNDLE_ERROR_KEY_NOT_AVAILABLE Key not available
249 * @pre @a b must be a valid bundle object.
250 * @see bundle_add_str_array()
253 bundle *b = bundle_create();
254 char *sa = { "aaa", "bbb", "ccc" }; // A string array of length 3
255 bundle_add_str_array(b, "foo", sa, 3); // add a key-val pair
257 char **str_array = NULL;
258 int len_str_array = 0;
260 str_array=bundle_get_str_array(b, "foo", &len_str_array);
261 // str_array = { "aaa", "bbb", "ccc" }, and len_str_array = 3
266 API const char** bundle_get_str_array(bundle *b, const char *key,int *len);
270 * @brief Gets a value with a given key.
272 * @remarks You MUST free or modify the returned string!
273 * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section.
274 * @param[in] b The bundle object
275 * @param[in] key The key
276 * @return The pointer for the value string
277 * @retval @c NULL - Key not found
278 * @exception BUNDLE_ERROR_NONE Success
279 * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
280 * @exception BUNDLE_ERROR_KEY_NOT_AVAILABLE Key not available
281 * @pre @a b must be a valid bundle object.
282 * @see bundle_get_str()
285 bundle *b = bundle_create(); // Create new bundle object
286 bundle_add_str(b, "foo", "bar"); //add a key-val pair
287 char *val = bundle_get_val(b, "foo_key"); // val = "bar_val"
289 bundle_free(b); // After freeing b, val becomes a dangling pointer.
293 API const char* bundle_get_val(bundle *b, const char *key);
296 * @brief Gets the number of bundle items.
298 * @param[in] b The bundle object
299 * @return The number of bundle items
300 * @pre @a b must be a valid bundle object.
303 bundle *b = bundle_create(); // Create new bundle object
304 bundle_add_str(b, "key1", "val1"); //add a key-val pair
305 int count = bundle_get_count(b); // count=1
306 bundle_add_str(b, "key2", "val2"); // add another key-val pair
307 count = bundle_get_count(b); // count=2
312 API int bundle_get_count(bundle *b);
315 * @brief Gets the type of a value with a given key.
317 * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section.
318 * @param[in] b A bundle
319 * @param[in] key A key in the bundle
320 * @return The type of a key in @a b
321 * @exception BUNDLE_ERROR_NONE Success
322 * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
323 * @exception BUNDLE_ERROR_KEY_NOT_AVAILABLE Key not available
324 * @pre @a b must be a valid bundle object.
329 API int bundle_get_type(bundle *b, const char *key);
333 * @brief Duplicates a given bundle object.
335 * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section.
336 * @param[in] b_from the bundle object to be duplicated
337 * @return The new bundle object
338 * @retval @c NULL - Failure
339 * @exception BUNDLE_ERROR_NONE Success
340 * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
341 * @pre @a b must be a valid bundle object.
344 bundle *b = bundle_create(); // Create new bundle object
345 bundle_add_str(b, "foo_key", "bar_val"); // add a key-val pair
346 bundle *b_dup = bundle_dup(b); // duplicate b
352 API bundle * bundle_dup(bundle *b_from);
356 * @brief Iterates a callback function for each key-value pairs in a given bundle.
357 * @details (NOTE: Only BUNDLE_TYPE_STR type values come!)
359 * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section.
360 * @remarks This function is obsolete and does not give values whose types are not BUNDLE_TYPE_STR.
361 * @param[in] b The bundle object
362 * @param[in] callback The iteration callback function
363 * @param[in] cb_data The data for callback function
364 * @exception BUNDLE_ERROR_NONE Success
365 * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
366 * @pre @a b must be a valid bundle object.
370 void sample_cb(const char *k, const char *v, void *data) {
371 printf("%s -> %s\n", k, v);
375 bundle *b = bundle_create(); // Create new bundle object
376 bundle_add_str(b, "k1", "v1"); // add a key-val pair
377 bundle_add_str(b, "k2", "v2"); // add a key-val pair
378 bundle_add_str(b, "k3", "v3"); // add a key-val pair
379 bundle_iterate(b, sample_cb, NULL); // iterate sample_cb() for each key/val
384 API void bundle_iterate(bundle *b, bundle_iterate_cb_t callback, void *cb_data);
387 * @brief Iterates a callback function for each key-value pair in a given bundle.
388 * @details Supports all types of values.
390 * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section.
391 * @remarks This function supports all types.
392 * @param[in] b The bundle object
393 * @param[in] iter The iteration callback function
394 * @param[in] user_data The data for the callback function
395 * @exception BUNDLE_ERROR_NONE Success
396 * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
397 * @pre @a b must be a valid bundle object.
398 * @see bundle_keyval_get_type()
399 * @see bundle_keyval_type_is_array()
400 * @see bundle_keyval_get_basic_val()
401 * @see bundle_keyval_get_array_val()
405 void sample_cb(const char *key, const int type, const bundle_keyval_t *kv, void *user_data) {
406 void *basic_val = NULL;
407 size_t basic_size = 0;
408 void **array_val = NULL;
410 size_t *array_elem_size = NULL;
412 printf("Key:%s, Type:%d\n", key, type);
413 if(bundle_keyval_type_is_array(kv)) {
414 bundle_keyval_get_array_val(kv, &array_val, &array_len, &array_elem_size);
418 bundle_keyval_get_basic_val(kv, &basic_val, &basic_size);
424 bundle *b = bundle_create(); // Create new bundle object
425 bundle_add_str(b, "k1", "v1"); // add a key-val pair
426 bundle_add_byte(b, "k2", "v2", 3); // add a key-val pair
427 char *s_arr[] = {"abc", "bcd", "cde"};
428 bundle_add_str_array(b, "k3", s_arr, 3); // add a key-val pair
429 bundle_foreach(b, sample_cb, NULL); // iterate sample_cb() for each key/val
434 API void bundle_foreach(bundle *b, bundle_iterator_t iter, void *user_data);
437 * @brief Gets the type of a key-value pair.
439 * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section.
440 * @param[in] kv A bundle_keyval_t object
441 * @return The type of @a kv
442 * @retval @c -1 - Failure
443 * @exception BUNDLE_ERROR_NONE Success
444 * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
445 * @pre @a kv must be a valid bundle_keyval_t object.
446 * @see bundle_foreach()
448 API int bundle_keyval_get_type(bundle_keyval_t *kv);
451 * @brief Determines whether the type of a key-value pair is array.
453 * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section.
454 * @param[in] kv A bundle_keyval_t object
455 * @return The operation result
456 * @retval @c 1 - @a kv is an array
457 * @retval @c 0 - @a kv is not an array
458 * @exception BUNDLE_ERROR_NONE Success
459 * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
460 * @pre @a kv must be a valid bundle_keyval_t object.
461 * @see bundle_foreach()
463 API int bundle_keyval_type_is_array(bundle_keyval_t *kv);
467 * @brief Determines whether the type of a key-value pair is measurable.
469 * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section.
470 * @param[in] kv A bundle_keyval_t object
471 * @return The operation result
472 * @retval @c 1 - @a kv is an measurable
473 * @retval @c 0 - @a kv is not an measurable
474 * @exception BUNDLE_ERROR_NONE Success
475 * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
476 * @pre @a kv must be a valid bundle_keyval_t object.
477 * @see bundle_foreach()
479 API int bundle_keyval_type_is_measurable(bundle_keyval_t *kv);
482 * @brief Gets the value and size of the value from a key-value pair of basic type.
484 * @remarks You must not free @a val.
485 * @param[in] kv A bundle_keyval_t object
486 * @param[out] val The value
487 * @param[out] size The size of @a val
488 * @return The operation result
489 * @retval BUNDLE_ERROR_NONE Success
490 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
491 * @pre @a kv must be a valid bundle_keyval_t object.
492 * @post @a val and @a size are set.
493 * @see bundle_foreach()
495 API int bundle_keyval_get_basic_val(bundle_keyval_t *kv, void **val, size_t *size);
498 * @brief Gets the value array, length of the array, and size of each array item.
500 * @param[in] kv A bundle_keyval_t object
501 * @param[out] array_val The array pointer of values
502 * @param[out] array_len The length of @a array_val
503 * @param[out] array_element_size The array of size of each array element
504 * @return The operation result
505 * @retval BUNDLE_ERROR_NONE Success
506 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
507 * @pre @a kv must be a valid bundle_keyval_t object.
508 * @post @a array_val, @a array_len, @a array_item_size are set.
509 * @see bundle_foreach()
511 API int bundle_keyval_get_array_val(bundle_keyval_t *kv, void ***array_val, unsigned int *array_len, size_t **array_element_size);
514 * @brief Encodes a bundle to the bundle_raw format (uses base64 format).
516 * @param[in] b The bundle object
517 * @param[out] r The returned bundle_raw data(byte data)
518 * @a r MUST BE FREED by free(r)
519 * @param[out] len The size of @a r (in bytes)
520 * @return The size of the raw data
521 * @retval BUNDLE_ERROR_NONE Success
522 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
523 * @pre @a b must be a valid bundle object.
526 bundle *b = bundle_create(); // Create new bundle object
527 bundle_add_str(b, "foo_key", "bar_val"); // add a key-val pair
530 bundle_encode(b, &r, &len); // encode b
535 API int bundle_encode(bundle *b, bundle_raw **r, int *len);
539 * @brief Frees the encoded rawdata.
541 * @param[in] r The rawdata
542 * @return The operation result
543 * @retval BUNDLE_ERROR_NONE Success
544 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
545 * @pre @a r is a valid rawdata generated by bundle_encode().
546 * @see bundle_encode()
548 API int bundle_free_encoded_rawdata(bundle_raw **r);
551 * @brief Deserializes bundle_raw and gets the bundle object.
553 * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section.
554 * @param[in] r The bundle_raw data to be converted to bundle object
555 * @param[in] len The size of @a r
556 * @return The bundle object
557 * @retval @c NULL - Failure
558 * @exception BUNDLE_ERROR_NONE Success
559 * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
560 * @pre @a b must be a valid bundle object.
563 bundle *b = bundle_create(); // Create new bundle object
564 bundle_add_str(b, "foo_key", "bar_val"); // add a key-val pair
566 bundle_raw *encoded_b;
568 bundle_encode(b, &encoded_b, &len); // encode b
571 b_dup = bundle_decode(encoded_b, len); // decoded bundle object
578 API bundle * bundle_decode(const bundle_raw *r, const int len);
582 * @brief Encodes a bundle to the bundle_raw format.
584 * @param[in] b The bundle object
585 * @param[out] r The returned bundle_raw data(byte data)
586 * @a r MUST BE FREED by free(r)
587 * @param[out] len The size of @a r (in bytes)
588 * @return The size of the raw data
589 * @retval BUNDLE_ERROR_NONE Success
590 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
591 * @retval BUNDLE_ERROR_OUT_OF_MEMORY Out of memory
592 * @pre @a b must be a valid bundle object.
595 bundle *b = bundle_create(); // Create new bundle object
596 bundle_add_str(b, "foo_key", "bar_val"); // add a key-val pair
599 bundle_encode_raw(b, &r, &len); // encode b
604 API int bundle_encode_raw(bundle *b, bundle_raw **r, int *len);
608 * @brief Deserializes bundle_raw and gets a bundle object.
610 * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section.
611 * @param[in] r The bundle_raw data to be converted to a bundle object
612 * @param[in] len The size of @a r
613 * @return The bundle object
614 * @retval @c NULL - Failure
615 * @exception BUNDLE_ERROR_NONE Success
616 * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
617 * @pre @a b must be a valid bundle object.
620 bundle *b = bundle_create(); // Create new bundle object
621 bundle_add_str(b, "foo_key", "bar_val"); // add a key-val pair
623 bundle_raw *encoded_b;
625 bundle_encode(b, &encoded_b, &len); // encode b
628 b_dup = bundle_decode_raw(encoded_b, len); // decoded bundle object
635 API bundle * bundle_decode_raw(const bundle_raw *r, const int len);
639 * @brief Exports bundle to @a argv.
641 * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section.
642 * @param[in] b The bundle object
643 * @param[out] argv The pointer of the string array; \n
644 * This array has NULL values for the first and last item; \n
645 * First NULL is for argv[0], and last NULL is a terminator for execv() \n
646 * @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.)
647 * @retval @c -1 - Failure
648 * @exception BUNDLE_ERROR_NONE Success
649 * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
650 * @exception BUNDLE_ERROR_OUT_OF_MEMORY Out of memory
651 * @pre @a b is a valid bundle object.
652 * @post @a argv is a pointer of newly allocated memory. It must be freed.
653 * 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!!
654 * @see bundle_import_from_argv()
657 bundle *b = bundle_create(); // Create new bundle object
658 bundle_add_str(b, "foo_key", "bar_val"); // add a key-val pair
662 argc = bundle_export_to_argv(b, &argv); // export to argv
663 if(0 > argc) error("export failure");
666 for(i=0; i < argc; i++) {
667 printf("%s\n", argv[i]); // print argv
669 bundle_free_exported_argv(argc, argv); // argv must be freed after being used.
674 API int bundle_export_to_argv(bundle *b, char ***argv);
678 * @brief Frees the exported @a argv.
680 * @remarks You must not use this API when you use global @a argv.
681 * @param[in] argc The number of args, which is the return value of bundle_export_to_argv()
682 * @param[in] argv The array from bundle_export_to_argv()
683 * @return The operation result
684 * @retval BUNDLE_ERROR_NONE Success
685 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
686 * @pre @a argv is a valid string array generated from bundle_export_to_argv().
687 * @see bundle_export_to_argv()
689 bundle *b = bundle_create();
690 bundle_add_str(b, "foo", "bar");
694 argc = bundle_export_to_argv(b, &argv);
695 if(0 > argc) error("export failure");
699 bundle_free_exported_argv(argc, argv);
705 API int bundle_free_exported_argv(int argc, char ***argv);
709 * @brief Imports a bundle from @a argv.
711 * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section.
712 * @param[in] argc The argument count
713 * @param[in] argv The argument vector
714 * @return The new bundle object
715 * @retval @c NULL - Failure
716 * @exception BUNDLE_ERROR_NONE Success
717 * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
718 * @exception BUNDLE_ERROR_OUT_OF_MEMORY Out of memory
719 * @pre @a argv is a valid string array, which is created by bundle_export_to_argv().
720 * @post The returned bundle @a b must be freed.
721 * @see bundle_export_to_argv()
725 int main(int argc, char **argv) {
726 bundle *b = bundle_import_from_argv(argc, argv); // import from argc+argv
727 char *val = bundle_get_val(b, "foo_key"); // value for "foo_key"
729 bundle_free(b); // After freeing b, val becomes a dangling pointer.
734 API bundle * bundle_import_from_argv(int argc, char **argv);
737 * @brief Adds a string type key-value pair into a bundle.
739 * @param[in] b The bundle object
740 * @param[in] key The key
741 * @param[in] str The string type value
742 * @return The operation result
743 * @retval BUNDLE_ERROR_NONE Success
744 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
745 * @retval BUNDLE_ERROR_KEY_EXISTS Key already exists
746 * @retval BUNDLE_ERROR_OUT_OF_MEMORY Out of memory
747 * @pre @a b must be a valid bundle object.
748 * @see bundle_get_str()
751 bundle *b = bundle_create(); // Create new bundle object
752 bundle_add_str(b, "foo", "bar"); // add a key-val pair
757 API int bundle_add_str(bundle *b, const char *key, const char *str);
761 * @brief Sets a value of string array elements.
763 * @param[in] b The bundle object
764 * @param[in] key The key
765 * @param[in] idx The index of the array element to be changed
766 * @param[in] val The string type value; if @c NULL, an empty array is created; you can change an item with
767 * @return The operation result
768 * @retval BUNDLE_ERROR_NONE Success
769 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
770 * @pre @a b must be a valid bundle object.
771 * @see bundle_add_str_array()
772 * @see bundle_get_str_array()
775 bundle *b = bundle_create();
776 bundle_add_str_array(b, "foo", NULL, 3); // add a key-val pair
777 bundle_set_str_array_element(b, "foo", 0, "aaa");
778 bundle_set_str_array_element(b, "foo", 1, "bbb");
779 bundle_set_str_array_element(b, "foo", 2, "ccc");
781 char **str_array = NULL;
782 int len_str_array = 0;
784 str_array=bundle_get_str_array(b, "foo", &len_str_array);
785 // str_array = { "aaa", "bbb", "ccc" }, and len_str_array = 3
790 API int bundle_set_str_array_element(bundle *b, const char *key, const unsigned int idx, const char *val);
793 * @brief Adds a byte type key-value pair into a bundle.
795 * @param[in] b The bundle object
796 * @param[in] key The key
797 * @param[in] byte The string type value
798 * @param[in] size The size of @a byte
799 * @return The operation result
800 * @retval BUNDLE_ERROR_NONE Success
801 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
802 * @retval BUNDLE_ERROR_KEY_EXISTS Key already exists
803 * @retval BUNDLE_ERROR_OUT_OF_MEMORY Out of memory
804 * @pre @a b must be a valid bundle object.
805 * @see bundle_get_byte()
808 bundle *b = bundle_create(); // Create new bundle object
809 bundle_add_byte(b, "foo", "bar\0", 4); // add a key-val pair
812 bundle_add_byte(b, "number", &number, sizeof(int));
817 API int bundle_add_byte(bundle *b, const char *key, const void *byte, const size_t size);
821 * @brief Adds a byte array type key-value pair into a bundle.
823 * @param[in] b The bundle object
824 * @param[in] key The key
825 * @param[in] byte_array Not used
826 * @param[in] len The length of the array to be created
827 * @return The operation result
828 * @retval BUNDLE_ERROR_NONE Success
829 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
830 * @retval BUNDLE_ERROR_KEY_EXISTS Key already exists
831 * @retval BUNDLE_ERROR_OUT_OF_MEMORY Out of memory
832 * @pre @a b must be a valid bundle object.
833 * @see bundle_get_byte_array()
834 * @see bundle_set_byte_array_element()
837 bundle *b = bundle_create();
838 bundle_add_byte_array(b, "foo", NULL, 3); // add a byte-array with length 3
840 bundle_set_byte_array_element(b, "foo", 0, "aaa\0", 4); array[0] = "aaa\0"
841 bundle_set_byte_array_element(b, "foo", 1, "bbb\0", 4); array[1] = "bbb\0"
842 bundle_set_byte_array_element(b, "foo", 2, "ccc\0", 4); array[2] = "ccc\0"
847 API int bundle_add_byte_array(bundle *b, const char *key, void **byte_array, const unsigned int len);
851 * @brief Sets the value of the byte array element.
853 * @param[in] b The bundle object
854 * @param[in] key The key
855 * @param[in] idx The index of the array element to be changed
856 * @param[in] val The string type value; if @c NULL, an empty array is created; you can change an item with
857 * @param[in] size The size of the value in bytes
858 * @return Operation result
859 * @retval BUNDLE_ERROR_NONE Success
860 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
861 * @retval BUNDLE_ERROR_KEY_NOT_AVAILABLE Key not available
862 * @pre @a b must be a valid bundle object.
863 * @see bundle_add_byte_array()
864 * @see bundle_get_byte_array()
867 bundle *b = bundle_create();
868 bundle_add_byte_array(b, "foo", NULL, 3); // add a key-val pair
870 bundle_set_byte_array_element(b, "foo", 0, "aaa\0", 4);
871 bundle_set_byte_array_element(b, "foo", 1, "bbb\0", 4);
872 bundle_set_byte_array_element(b, "foo", 2, "ccc\0", 4);
874 unsigned char **byte_array = NULL;
875 int len_byte_array = 0;
877 bundle_get_byte_array(b, "foo", &byte_array, &len_byte_array, &size_byte_array);
878 // byte_array = { "aaa\0", "bbb\0", "ccc\0" }, and len_byte_array = 3
883 API int bundle_set_byte_array_element(bundle *b, const char *key, const unsigned int idx, const void *val, const size_t size);
886 * @brief Gets the string value with the given key.
888 * @remarks You must not free str!
889 * @param[in] b The bundle object
890 * @param[in] key The key
891 * @param[out] str The returned value
892 * @return The operation result
893 * @retval BUNDLE_ERROR_NONE Success
894 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
895 * @retval BUNDLE_ERROR_KEY_NOT_AVAILABLE Key not available
896 * @pre @a b must be a valid bundle object.
897 * @see bundle_add_str()
900 bundle *b = bundle_create(); // Create new bundle object
901 bundle_add_str(b, "foo_key", "bar_val"); // add a key-val pair
904 bundle_get_str(b, "foo_key", &v); // v = "bar_val"
906 bundle_free(b); // After freeing b, v becomes a dangling pointer.
910 API int bundle_get_str(bundle *b, const char *key, char **str);
913 * @brief Gets the byte value with the given key.
915 * @remarks You must not free @a byte!
916 * @param[in] b The bundle object
917 * @param[in] key The key
918 * @param[out] byte The returned value
919 * @param[out] size The size of the byte
920 * @return The operation result
921 * @retval BUNDLE_ERROR_NONE Success
922 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
923 * @retval BUNDLE_ERROR_KEY_NOT_AVAILABLE Key not available
924 * @pre @a b must be a valid bundle object.
925 * @see bundle_add_byte()
928 bundle *b = bundle_create(); // Create new bundle object
929 bundle_add_byte(b, "foo", "bar\0", 4); // add string to bundle
931 bundle_add_byte(b, "number", (const void**)&number, sizeof(int)); // add integer to bundle
933 unsigned char *v = NULL;
935 bundle_get_byte(b, "foo", (void**)&v, &v_size); // v = "bar\0"
938 bundle_get_byte(b, "number", (void**)&n, &n_size); // number = 12345
940 bundle_free(b); // After freeing b, v and n becomes a dangling pointer.
943 API int bundle_get_byte(bundle *b, const char *key, void **byte, size_t *size);
947 * @brief Gets the byte array value with the given key.
949 * @remarks You must not free str!
950 * @param[in] b The bundle object
951 * @param[in] key The key
952 * @param[out] byte_array The returned value
953 * @param[out] len The array length
954 * @param[out] array_element_size an array of sizes of each @a byte_array element
955 * @return The operation result
956 * @retval BUNDLE_ERROR_NONE Success
957 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
958 * @retval BUNDLE_ERROR_KEY_NOT_AVAILABLE Key not available
959 * @pre @a b must be a valid bundle object.
960 * @see bundle_add_byte_array()
961 * @see bundle_set_byte_array_element()
964 bundle *b = bundle_create();
965 bundle_add_byte_array(b, "foo", NULL, 3);
966 bundle_set_byte_array_element(b, "foo", 0, "aaa\0", 4);
967 bundle_set_byte_array_element(b, "foo", 1, "bbb\0", 4);
968 bundle_set_byte_array_element(b, "foo", 2, "ccc\0", 4);
970 char **byte_array = NULL;
971 int len_byte_array = 0;
972 size_t *size_byte_array = NULL;
974 bundle_get_byte_array(b, "foo", &byte_array, &len_byte_array, &size_byte_array);
975 // byte_array = { "aaa\0", "bbb\0", "ccc\0" }, len_byte_array = 3, and size_byte_array = { 4, 4, 4 }
980 API int bundle_get_byte_array(bundle *b, const char *key, void ***byte_array, unsigned int *len, unsigned int **array_element_size);
992 #endif /* __BUNDLE_H__ */