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.
52 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
56 BUNDLE_ERROR_NONE = TIZEN_ERROR_NONE, /**< Successful */
57 BUNDLE_ERROR_OUT_OF_MEMORY = TIZEN_ERROR_OUT_OF_MEMORY, /**< Out of memory */
58 BUNDLE_ERROR_INVALID_PARAMETER = TIZEN_ERROR_INVALID_PARAMETER, /**< Invalid parameter */
59 BUNDLE_ERROR_KEY_NOT_AVAILABLE = TIZEN_ERROR_KEY_NOT_AVAILABLE, /**< Required key not available */
60 BUNDLE_ERROR_KEY_EXISTS = TIZEN_ERROR_BUNDLE | 0x01 /**< Key exists */
64 * @brief The bundle handle.
65 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
67 typedef struct _bundle_t bundle;
70 * @brief The encoded data type.
71 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
72 * @see bundle_encode()
73 * @see bundle_decode()
75 typedef unsigned char bundle_raw;
79 * @brief Enumeration for key-value pair types.
80 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
82 enum bundle_type_property {
83 BUNDLE_TYPE_ARRAY = 0x0100, /**< Array type */
84 BUNDLE_TYPE_PRIMITIVE = 0x0200, /**< Primitive type */
85 BUNDLE_TYPE_MEASURABLE = 0x0400 /**< Measurable type */
89 * @brief Enumeration for bundle types.
90 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
93 BUNDLE_TYPE_NONE = -1, /**< None */
94 BUNDLE_TYPE_ANY = 0, /**< Any type */
95 BUNDLE_TYPE_STR = 1 | BUNDLE_TYPE_MEASURABLE, /**< String type (Default) */
96 BUNDLE_TYPE_STR_ARRAY = BUNDLE_TYPE_STR | BUNDLE_TYPE_ARRAY | BUNDLE_TYPE_MEASURABLE, /**< String array type */
97 BUNDLE_TYPE_BYTE = 2, /**< Byte type */
98 BUNDLE_TYPE_BYTE_ARRAY = BUNDLE_TYPE_BYTE | BUNDLE_TYPE_ARRAY /**< Byte array type */
102 * @brief The key-value pair handle.
103 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
104 * @see bundle_iterator_t
106 typedef struct keyval_t bundle_keyval_t;
110 * @brief Called for every key-value pair.
111 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
112 * @see bundle_foreach()
114 typedef void (*bundle_iterator_t) (
117 const bundle_keyval_t *kv,
122 * @brief Creates a bundle object.
123 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
124 * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section.
125 * @return The bundle object
126 * @retval @c NULL - Failure
127 * @exception BUNDLE_ERROR_NONE Success
128 * @exception BUNDLE_ERROR_OUT_OF_MEMORY Out of memory
133 bundle *b = bundle_create(); // Create new bundle object
134 bundle_free(b); // free bundle
137 API bundle* bundle_create(void);
140 * @brief Frees the given bundle object with key-value pairs in it.
141 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
142 * @param[in] b The bundle object to be freed
143 * @return The operation result;
144 * @retval BUNDLE_ERROR_NONE Success
145 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
146 * @pre @a b must be a valid bundle object.
147 * @see bundle_create()
150 bundle *b = bundle_create(); // Create new bundle object
151 bundle_free(b); // free bundle
154 API int bundle_free(bundle *b);
157 * @brief Adds a strings array type key-value pair into a given bundle.
158 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
159 * @param[in] b The bundle object
160 * @param[in] key The key
161 * @param[in] str_array The string type value; if @c NULL, an empty array is created; you can change an item with
162 * @param[in] len The length of the array
163 * @return The operation result
164 * @retval BUNDLE_ERROR_NONE Success
165 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
166 * @retval BUNDLE_ERROR_KEY_EXISTS Key already exists
167 * @retval BUNDLE_ERROR_OUT_OF_MEMORY Out of memory
168 * @pre @a b must be a valid bundle object.
169 * @see bundle_get_str_array()
173 char *sa = { "aaa", "bbb", "ccc" }; // A string array of length 3
174 bundle *b = bundle_create();
175 bundle_add_str_array(b, "foo", sa, 3); // add a key-val pair
179 API int bundle_add_str_array(bundle *b, const char *key, const char **str_array, const int len);
182 * @brief Deletes a key-value object with the given key.
183 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
184 * @param[in] b The bundle object
185 * @param[in] key The given key
186 * @return The operation result
187 * @retval BUNDLE_ERROR_NONE Success
188 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
189 * @retval BUNDLE_ERROR_KEY_NOT_AVAILABLE Key not available
190 * @pre @a b must be a valid bundle object.
193 bundle *b = bundle_create(); // Create new bundle object
194 bundle_add_str(b, "foo_key", "bar_val"); // add a key-val pair
195 bundle_del(b, "foo_key"); // del "foo_key" from b
200 API int bundle_del(bundle *b, const char* key);
203 * @brief Gets a string array from a given key.
204 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
205 * @remarks You MUST NOT free or modify the returned string!
206 * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section.
207 * @param[in] b The bundle object
208 * @param[in] key The key
209 * @param[out] len The array length
210 * @return The pointer to the array of strings
211 * @retval @c NULL - Key not found
212 * @exception BUNDLE_ERROR_NONE Success
213 * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
214 * @exception BUNDLE_ERROR_KEY_NOT_AVAILABLE Key not available
215 * @pre @a b must be a valid bundle object.
216 * @see bundle_add_str_array()
219 bundle *b = bundle_create();
220 char *sa = { "aaa", "bbb", "ccc" }; // A string array of length 3
221 bundle_add_str_array(b, "foo", sa, 3); // add a key-val pair
223 char **str_array = NULL;
224 int len_str_array = 0;
226 str_array=bundle_get_str_array(b, "foo", &len_str_array);
227 // str_array = { "aaa", "bbb", "ccc" }, and len_str_array = 3
232 API const char** bundle_get_str_array(bundle *b, const char *key,int *len);
235 * @brief Gets the number of bundle items.
236 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
237 * @param[in] b The bundle object
238 * @return The number of bundle items
239 * @pre @a b must be a valid bundle object.
242 bundle *b = bundle_create(); // Create new bundle object
243 bundle_add_str(b, "key1", "val1"); //add a key-val pair
244 int count = bundle_get_count(b); // count=1
245 bundle_add_str(b, "key2", "val2"); // add another key-val pair
246 count = bundle_get_count(b); // count=2
251 API int bundle_get_count(bundle *b);
254 * @brief Gets the type of a value with a given key.
255 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
256 * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section.
257 * @param[in] b A bundle
258 * @param[in] key A key in the bundle
259 * @return The type of a key in @a b
260 * @exception BUNDLE_ERROR_NONE Success
261 * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
262 * @exception BUNDLE_ERROR_KEY_NOT_AVAILABLE Key not available
263 * @pre @a b must be a valid bundle object.
268 API int bundle_get_type(bundle *b, const char *key);
271 * @brief Duplicates a given bundle object.
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_from the bundle object to be duplicated
275 * @return The new bundle object
276 * @retval @c NULL - Failure
277 * @exception BUNDLE_ERROR_NONE Success
278 * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
279 * @pre @a b must be a valid bundle object.
282 bundle *b = bundle_create(); // Create new bundle object
283 bundle_add_str(b, "foo_key", "bar_val"); // add a key-val pair
284 bundle *b_dup = bundle_dup(b); // duplicate b
290 API bundle * bundle_dup(bundle *b_from);
293 * @brief Iterates a callback function for each key-value pair in a given bundle.
294 * @details Supports all types of values.
295 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
296 * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section.
297 * @remarks This function supports all types.
298 * @param[in] b The bundle object
299 * @param[in] iter The iteration callback function
300 * @param[in] user_data The data for the callback function
301 * @exception BUNDLE_ERROR_NONE Success
302 * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
303 * @pre @a b must be a valid bundle object.
304 * @see bundle_keyval_get_type()
305 * @see bundle_keyval_type_is_array()
306 * @see bundle_keyval_get_basic_val()
307 * @see bundle_keyval_get_array_val()
311 void sample_cb(const char *key, const int type, const bundle_keyval_t *kv, void *user_data) {
312 void *basic_val = NULL;
313 size_t basic_size = 0;
314 void **array_val = NULL;
316 size_t *array_elem_size = NULL;
318 printf("Key:%s, Type:%d\n", key, type);
319 if(bundle_keyval_type_is_array(kv)) {
320 bundle_keyval_get_array_val(kv, &array_val, &array_len, &array_elem_size);
324 bundle_keyval_get_basic_val(kv, &basic_val, &basic_size);
330 bundle *b = bundle_create(); // Create new bundle object
331 bundle_add_str(b, "k1", "v1"); // add a key-val pair
332 bundle_add_byte(b, "k2", "v2", 3); // add a key-val pair
333 char *s_arr[] = {"abc", "bcd", "cde"};
334 bundle_add_str_array(b, "k3", s_arr, 3); // add a key-val pair
335 bundle_foreach(b, sample_cb, NULL); // iterate sample_cb() for each key/val
340 API void bundle_foreach(bundle *b, bundle_iterator_t iter, void *user_data);
343 * @brief Gets the type of a key-value pair.
344 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
345 * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section.
346 * @param[in] kv A bundle_keyval_t object
347 * @return The type of @a kv
348 * @retval @c -1 - Failure
349 * @exception BUNDLE_ERROR_NONE Success
350 * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
351 * @pre @a kv must be a valid bundle_keyval_t object.
352 * @see bundle_foreach()
354 API int bundle_keyval_get_type(bundle_keyval_t *kv);
357 * @brief Determines whether the type of a key-value pair is array.
358 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
359 * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section.
360 * @param[in] kv A bundle_keyval_t object
361 * @return The operation result
362 * @retval @c 1 - @a kv is an array
363 * @retval @c 0 - @a kv is not an array
364 * @exception BUNDLE_ERROR_NONE Success
365 * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
366 * @pre @a kv must be a valid bundle_keyval_t object.
367 * @see bundle_foreach()
369 API int bundle_keyval_type_is_array(bundle_keyval_t *kv);
372 * @brief Gets the value and size of the value from a key-value pair of basic type.
373 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
374 * @remarks You must not free @a val.
375 * @param[in] kv A bundle_keyval_t object
376 * @param[out] val The value
377 * @param[out] size The size of @a val
378 * @return The operation result
379 * @retval BUNDLE_ERROR_NONE Success
380 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
381 * @pre @a kv must be a valid bundle_keyval_t object.
382 * @post @a val and @a size are set.
383 * @see bundle_foreach()
385 API int bundle_keyval_get_basic_val(bundle_keyval_t *kv, void **val, size_t *size);
388 * @brief Gets the value array, length of the array, and size of each array item.
389 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
390 * @param[in] kv A bundle_keyval_t object
391 * @param[out] array_val The array pointer of values
392 * @param[out] array_len The length of @a array_val
393 * @param[out] array_element_size The array of size of each array element
394 * @return The operation result
395 * @retval BUNDLE_ERROR_NONE Success
396 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
397 * @pre @a kv must be a valid bundle_keyval_t object.
398 * @post @a array_val, @a array_len, @a array_item_size are set.
399 * @see bundle_foreach()
401 API int bundle_keyval_get_array_val(bundle_keyval_t *kv, void ***array_val, unsigned int *array_len, size_t **array_element_size);
404 * @brief Encodes a bundle to the bundle_raw format (uses base64 format).
405 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
406 * @param[in] b The bundle object
407 * @param[out] r The returned bundle_raw data(byte data)
408 * @a r MUST BE FREED by free(r)
409 * @param[out] len The size of @a r (in bytes)
410 * @return The size of the raw data
411 * @retval BUNDLE_ERROR_NONE Success
412 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
413 * @pre @a b must be a valid bundle object.
416 bundle *b = bundle_create(); // Create new bundle object
417 bundle_add_str(b, "foo_key", "bar_val"); // add a key-val pair
420 bundle_encode(b, &r, &len); // encode b
425 API int bundle_encode(bundle *b, bundle_raw **r, int *len);
428 * @brief Deserializes bundle_raw and gets the bundle object.
429 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
430 * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section.
431 * @param[in] r The bundle_raw data to be converted to bundle object
432 * @param[in] len The size of @a r
433 * @return The bundle object
434 * @retval @c NULL - Failure
435 * @exception BUNDLE_ERROR_NONE Success
436 * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
437 * @pre @a b must be a valid bundle object.
440 bundle *b = bundle_create(); // Create new bundle object
441 bundle_add_str(b, "foo_key", "bar_val"); // add a key-val pair
443 bundle_raw *encoded_b;
445 bundle_encode(b, &encoded_b, &len); // encode b
448 b_dup = bundle_decode(encoded_b, len); // decoded bundle object
455 API bundle * bundle_decode(const bundle_raw *r, const int len);
458 * @brief Adds a string type key-value pair into a bundle.
459 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
460 * @param[in] b The bundle object
461 * @param[in] key The key
462 * @param[in] str The string type value
463 * @return The operation result
464 * @retval BUNDLE_ERROR_NONE Success
465 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
466 * @retval BUNDLE_ERROR_KEY_EXISTS Key already exists
467 * @retval BUNDLE_ERROR_OUT_OF_MEMORY Out of memory
468 * @pre @a b must be a valid bundle object.
469 * @see bundle_get_str()
472 bundle *b = bundle_create(); // Create new bundle object
473 bundle_add_str(b, "foo", "bar"); // add a key-val pair
478 API int bundle_add_str(bundle *b, const char *key, const char *str);
481 * @brief Adds a byte type key-value pair into a bundle.
482 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
483 * @param[in] b The bundle object
484 * @param[in] key The key
485 * @param[in] byte The string type value
486 * @param[in] size The size of @a byte
487 * @return The operation result
488 * @retval BUNDLE_ERROR_NONE Success
489 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
490 * @retval BUNDLE_ERROR_KEY_EXISTS Key already exists
491 * @retval BUNDLE_ERROR_OUT_OF_MEMORY Out of memory
492 * @pre @a b must be a valid bundle object.
493 * @see bundle_get_byte()
496 bundle *b = bundle_create(); // Create new bundle object
497 bundle_add_byte(b, "foo", "bar\0", 4); // add a key-val pair
500 bundle_add_byte(b, "number", &number, sizeof(int));
505 API int bundle_add_byte(bundle *b, const char *key, const void *byte, const size_t size);
508 * @brief Gets the string value with the given key.
509 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
510 * @remarks You must not free str!
511 * @param[in] b The bundle object
512 * @param[in] key The key
513 * @param[out] str The returned value
514 * @return The operation result
515 * @retval BUNDLE_ERROR_NONE Success
516 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
517 * @retval BUNDLE_ERROR_KEY_NOT_AVAILABLE Key not available
518 * @pre @a b must be a valid bundle object.
519 * @see bundle_add_str()
522 bundle *b = bundle_create(); // Create new bundle object
523 bundle_add_str(b, "foo_key", "bar_val"); // add a key-val pair
526 bundle_get_str(b, "foo_key", &v); // v = "bar_val"
528 bundle_free(b); // After freeing b, v becomes a dangling pointer.
532 API int bundle_get_str(bundle *b, const char *key, char **str);
535 * @brief Gets the byte value with the given key.
536 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
537 * @remarks You must not free @a byte!
538 * @param[in] b The bundle object
539 * @param[in] key The key
540 * @param[out] byte The returned value
541 * @param[out] size The size of the byte
542 * @return The operation result
543 * @retval BUNDLE_ERROR_NONE Success
544 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
545 * @retval BUNDLE_ERROR_KEY_NOT_AVAILABLE Key not available
546 * @pre @a b must be a valid bundle object.
547 * @see bundle_add_byte()
550 bundle *b = bundle_create(); // Create new bundle object
551 bundle_add_byte(b, "foo", "bar\0", 4); // add string to bundle
553 bundle_add_byte(b, "number", (const void**)&number, sizeof(int)); // add integer to bundle
555 unsigned char *v = NULL;
557 bundle_get_byte(b, "foo", (void**)&v, &v_size); // v = "bar\0"
560 bundle_get_byte(b, "number", (void**)&n, &n_size); // number = 12345
562 bundle_free(b); // After freeing b, v and n becomes a dangling pointer.
565 API int bundle_get_byte(bundle *b, const char *key, void **byte, size_t *size);
576 #endif /* __BUNDLE_H__ */