2 * Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 * @brief This file declares has API of the bundle library
26 * @addtogroup CORE_LIB_BUNDLE_MODULE
32 #include <tizen_error.h>
38 #define API __attribute__((visibility("default")))
39 #define likely(x) __builtin_expect(x, 1)
40 #define unlikely(x) __builtin_expect(x, 0)
43 * @brief Enumeration for error code of Bundle.
45 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
48 BUNDLE_ERROR_NONE = TIZEN_ERROR_NONE, /**< Successful */
49 BUNDLE_ERROR_OUT_OF_MEMORY = TIZEN_ERROR_OUT_OF_MEMORY, /**< Out of memory */
50 BUNDLE_ERROR_INVALID_PARAMETER = TIZEN_ERROR_INVALID_PARAMETER, /**< Invalid parameter */
51 BUNDLE_ERROR_KEY_NOT_AVAILABLE = TIZEN_ERROR_KEY_NOT_AVAILABLE, /**< Required key not available */
52 BUNDLE_ERROR_KEY_EXISTS = TIZEN_ERROR_BUNDLE | 0x01 /**< Key exists */
56 * @brief The bundle handle.
57 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
59 typedef struct _bundle_t bundle;
62 * @brief The encoded data type.
63 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
64 * @see bundle_encode()
65 * @see bundle_decode()
67 typedef unsigned char bundle_raw;
70 * @brief Enumeration for key-value pair types.
71 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
73 enum bundle_type_property {
74 BUNDLE_TYPE_ARRAY = 0x0100, /**< Array type */
75 BUNDLE_TYPE_PRIMITIVE = 0x0200, /**< Primitive type */
76 BUNDLE_TYPE_MEASURABLE = 0x0400 /**< Measurable type */
80 * @brief Enumeration for bundle types.
81 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
84 BUNDLE_TYPE_NONE = -1, /**< None */
85 BUNDLE_TYPE_ANY = 0, /**< Any type */
86 BUNDLE_TYPE_STR = 1 | BUNDLE_TYPE_MEASURABLE, /**< String type (Default) */
87 BUNDLE_TYPE_STR_ARRAY = BUNDLE_TYPE_STR | BUNDLE_TYPE_ARRAY | BUNDLE_TYPE_MEASURABLE, /**< String array type */
88 BUNDLE_TYPE_BYTE = 2, /**< Byte type */
89 BUNDLE_TYPE_BYTE_ARRAY = BUNDLE_TYPE_BYTE | BUNDLE_TYPE_ARRAY /**< Byte array type */
93 * @brief The key-value pair handle.
94 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
95 * @see bundle_iterator_t
97 typedef struct keyval_t bundle_keyval_t;
100 * @brief Called for every key-value pair.
101 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
102 * @see bundle_foreach()
104 typedef void (*bundle_iterator_t) (
107 const bundle_keyval_t *kv,
112 * @brief Creates a bundle object.
113 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
114 * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section.
115 * @return The bundle object
116 * @retval @c NULL - Failure
117 * @exception BUNDLE_ERROR_NONE Success
118 * @exception BUNDLE_ERROR_OUT_OF_MEMORY Out of memory
123 bundle *b = bundle_create(); // Create new bundle object
124 bundle_free(b); // free bundle
127 API bundle *bundle_create(void);
130 * @brief Frees the given bundle object with key-value pairs in it.
131 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
132 * @param[in] b The bundle object to be freed
133 * @return The operation result;
134 * @retval BUNDLE_ERROR_NONE Success
135 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
136 * @pre @a b must be a valid bundle object.
137 * @see bundle_create()
140 bundle *b = bundle_create(); // Create new bundle object
141 bundle_free(b); // free bundle
144 API int bundle_free(bundle *b);
147 * @brief Adds a strings array type key-value pair into a given bundle.
148 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
149 * @param[in] b The bundle object
150 * @param[in] key The key
151 * @param[in] str_array The string type value; if @c NULL, an empty array is created; you can change an item with
152 * @param[in] len The length of the array
153 * @return The operation result
154 * @retval BUNDLE_ERROR_NONE Success
155 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
156 * @retval BUNDLE_ERROR_KEY_EXISTS Key already exists
157 * @retval BUNDLE_ERROR_OUT_OF_MEMORY Out of memory
158 * @pre @a b must be a valid bundle object.
159 * @see bundle_get_str_array()
163 char *sa = { "aaa", "bbb", "ccc" }; // A string array of length 3
164 bundle *b = bundle_create();
165 bundle_add_str_array(b, "foo", sa, 3); // add a key-val pair
169 API int bundle_add_str_array(bundle *b, const char *key, const char **str_array, const int len);
172 * @brief Deletes a key-value object with the given key.
173 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
174 * @param[in] b The bundle object
175 * @param[in] key The given key
176 * @return The operation result
177 * @retval BUNDLE_ERROR_NONE Success
178 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
179 * @retval BUNDLE_ERROR_KEY_NOT_AVAILABLE Key not available
180 * @pre @a b must be a valid bundle object.
183 bundle *b = bundle_create(); // Create new bundle object
184 bundle_add_str(b, "foo_key", "bar_val"); // add a key-val pair
185 bundle_del(b, "foo_key"); // del "foo_key" from b
190 API int bundle_del(bundle *b, const char *key);
193 * @brief Gets a string array from a given key.
194 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
195 * @remarks You MUST NOT free or modify the returned string!
196 * The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section.
197 * @param[in] b The bundle object
198 * @param[in] key The key
199 * @param[out] len The array length
200 * @return The pointer to the array of strings
201 * @retval @c NULL - Key not found
202 * @exception BUNDLE_ERROR_NONE Success
203 * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
204 * @exception BUNDLE_ERROR_KEY_NOT_AVAILABLE Key not available
205 * @pre @a b must be a valid bundle object.
206 * @see bundle_add_str_array()
209 bundle *b = bundle_create();
210 char *sa = { "aaa", "bbb", "ccc" }; // A string array of length 3
211 bundle_add_str_array(b, "foo", sa, 3); // add a key-val pair
213 char **str_array = NULL;
214 int len_str_array = 0;
216 str_array=bundle_get_str_array(b, "foo", &len_str_array);
217 // str_array = { "aaa", "bbb", "ccc" }, and len_str_array = 3
222 API const char **bundle_get_str_array(bundle *b, const char *key, int *len);
225 * @brief Gets the number of bundle items.
226 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
227 * @param[in] b The bundle object
228 * @return The number of bundle items
229 * @pre @a b must be a valid bundle object.
232 bundle *b = bundle_create(); // Create new bundle object
233 bundle_add_str(b, "key1", "val1"); //add a key-val pair
234 int count = bundle_get_count(b); // count=1
235 bundle_add_str(b, "key2", "val2"); // add another key-val pair
236 count = bundle_get_count(b); // count=2
241 API int bundle_get_count(bundle *b);
244 * @brief Gets the type of a value with a given key.
245 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
246 * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section.
247 * @param[in] b A bundle
248 * @param[in] key A key in the bundle
249 * @return The type of a key in @a b
250 * @exception BUNDLE_ERROR_NONE Success
251 * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
252 * @exception BUNDLE_ERROR_KEY_NOT_AVAILABLE Key not available
253 * @pre @a b must be a valid bundle object.
258 API int bundle_get_type(bundle *b, const char *key);
261 * @brief Duplicates a given bundle object.
262 * @since_tizen @if MOBILE 2.4 @elseif WEARABLE 3.0 @endif
263 * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section.
264 * @param[in] b_from the bundle object to be duplicated
265 * @return The new bundle object
266 * @retval @c NULL - Failure
267 * @exception BUNDLE_ERROR_NONE Success
268 * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
269 * @pre @a b must be a valid bundle object.
272 bundle *b = bundle_create(); // Create new bundle object
273 bundle_add_str(b, "foo_key", "bar_val"); // add a key-val pair
274 bundle *b_dup = bundle_dup(b); // duplicate b
280 API bundle *bundle_dup(bundle *b_from);
283 * @brief Iterates a callback function for each key-value pair in a given bundle.
284 * @details Supports all types of values.
285 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
286 * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section.
287 * @remarks This function supports all types.
288 * @param[in] b The bundle object
289 * @param[in] iter The iteration callback function
290 * @param[in] user_data The data for the callback function
291 * @exception BUNDLE_ERROR_NONE Success
292 * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
293 * @pre @a b must be a valid bundle object.
294 * @see bundle_keyval_get_type()
295 * @see bundle_keyval_type_is_array()
296 * @see bundle_keyval_get_basic_val()
297 * @see bundle_keyval_get_array_val()
301 void sample_cb(const char *key, const int type, const bundle_keyval_t *kv, void *user_data) {
302 void *basic_val = NULL;
303 size_t basic_size = 0;
304 void **array_val = NULL;
306 size_t *array_elem_size = NULL;
308 printf("Key:%s, Type:%d\n", key, type);
309 if(bundle_keyval_type_is_array(kv)) {
310 bundle_keyval_get_array_val(kv, &array_val, &array_len, &array_elem_size);
314 bundle_keyval_get_basic_val(kv, &basic_val, &basic_size);
320 bundle *b = bundle_create(); // Create new bundle object
321 bundle_add_str(b, "k1", "v1"); // add a key-val pair
322 bundle_add_byte(b, "k2", "v2", 3); // add a key-val pair
323 char *s_arr[] = {"abc", "bcd", "cde"};
324 bundle_add_str_array(b, "k3", s_arr, 3); // add a key-val pair
325 bundle_foreach(b, sample_cb, NULL); // iterate sample_cb() for each key/val
330 API void bundle_foreach(bundle *b, bundle_iterator_t iter, void *user_data);
333 * @brief Gets the type of a key-value pair.
334 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
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] kv A bundle_keyval_t object
337 * @return The type of @a kv
338 * @retval @c -1 - Failure
339 * @exception BUNDLE_ERROR_NONE Success
340 * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
341 * @pre @a kv must be a valid bundle_keyval_t object.
342 * @see bundle_foreach()
344 API int bundle_keyval_get_type(bundle_keyval_t *kv);
347 * @brief Determines whether the type of a key-value pair is array.
348 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
349 * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section.
350 * @param[in] kv A bundle_keyval_t object
351 * @return The operation result
352 * @retval @c 1 - @a kv is an array
353 * @retval @c 0 - @a kv is not an array
354 * @exception BUNDLE_ERROR_NONE Success
355 * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
356 * @pre @a kv must be a valid bundle_keyval_t object.
357 * @see bundle_foreach()
359 API int bundle_keyval_type_is_array(bundle_keyval_t *kv);
362 * @brief Gets the value and size of the value from a key-value pair of basic type.
363 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
364 * @remarks You must not free @a val.
365 * @param[in] kv A bundle_keyval_t object
366 * @param[out] val The value
367 * @param[out] size The size of @a val
368 * @return The operation result
369 * @retval BUNDLE_ERROR_NONE Success
370 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
371 * @pre @a kv must be a valid bundle_keyval_t object.
372 * @post @a val and @a size are set.
373 * @see bundle_foreach()
375 API int bundle_keyval_get_basic_val(bundle_keyval_t *kv, void **val, size_t *size);
378 * @brief Gets the value array, length of the array, and size of each array item.
379 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
380 * @param[in] kv A bundle_keyval_t object
381 * @param[out] array_val The array pointer of values
382 * @param[out] array_len The length of @a array_val
383 * @param[out] array_element_size The array of size of each array element
384 * @return The operation result
385 * @retval BUNDLE_ERROR_NONE Success
386 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
387 * @pre @a kv must be a valid bundle_keyval_t object.
388 * @post @a array_val, @a array_len, @a array_item_size are set.
389 * @see bundle_foreach()
391 API int bundle_keyval_get_array_val(bundle_keyval_t *kv, void ***array_val, unsigned int *array_len, size_t **array_element_size);
394 * @brief Encodes a bundle to the bundle_raw format (uses base64 format).
395 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
396 * @param[in] b The bundle object
397 * @param[out] r The returned bundle_raw data(byte data)
398 * @a r MUST BE FREED by free(r)
399 * @param[out] len The size of @a r (in bytes)
400 * @return The size of the raw data
401 * @retval BUNDLE_ERROR_NONE Success
402 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
403 * @pre @a b must be a valid bundle object.
406 bundle *b = bundle_create(); // Create new bundle object
407 bundle_add_str(b, "foo_key", "bar_val"); // add a key-val pair
410 bundle_encode(b, &r, &len); // encode b
415 API int bundle_encode(bundle *b, bundle_raw **r, int *len);
418 * @brief Deserializes bundle_raw and gets the bundle object.
419 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
420 * @remarks The specific error code can be obtained using the get_last_result() method. Error codes are described in Exception section.
421 * @param[in] r The bundle_raw data to be converted to bundle object
422 * @param[in] len The size of @a r
423 * @return The bundle object
424 * @retval @c NULL - Failure
425 * @exception BUNDLE_ERROR_NONE Success
426 * @exception BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
427 * @pre @a b must be a valid bundle object.
430 bundle *b = bundle_create(); // Create new bundle object
431 bundle_add_str(b, "foo_key", "bar_val"); // add a key-val pair
433 bundle_raw *encoded_b;
435 bundle_encode(b, &encoded_b, &len); // encode b
438 b_dup = bundle_decode(encoded_b, len); // decoded bundle object
445 API bundle *bundle_decode(const bundle_raw *r, const int len);
448 * @brief Adds a string type key-value pair into a bundle.
449 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
450 * @param[in] b The bundle object
451 * @param[in] key The key
452 * @param[in] str The string type value
453 * @return The operation result
454 * @retval BUNDLE_ERROR_NONE Success
455 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
456 * @retval BUNDLE_ERROR_KEY_EXISTS Key already exists
457 * @retval BUNDLE_ERROR_OUT_OF_MEMORY Out of memory
458 * @pre @a b must be a valid bundle object.
459 * @see bundle_get_str()
462 bundle *b = bundle_create(); // Create new bundle object
463 bundle_add_str(b, "foo", "bar"); // add a key-val pair
468 API int bundle_add_str(bundle *b, const char *key, const char *str);
471 * @brief Adds a byte type key-value pair into a bundle.
472 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
473 * @param[in] b The bundle object
474 * @param[in] key The key
475 * @param[in] byte The string type value
476 * @param[in] size The size of @a byte
477 * @return The operation result
478 * @retval BUNDLE_ERROR_NONE Success
479 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
480 * @retval BUNDLE_ERROR_KEY_EXISTS Key already exists
481 * @retval BUNDLE_ERROR_OUT_OF_MEMORY Out of memory
482 * @pre @a b must be a valid bundle object.
483 * @see bundle_get_byte()
486 bundle *b = bundle_create(); // Create new bundle object
487 bundle_add_byte(b, "foo", "bar\0", 4); // add a key-val pair
490 bundle_add_byte(b, "number", &number, sizeof(int));
495 API int bundle_add_byte(bundle *b, const char *key, const void *byte, const size_t size);
498 * @brief Gets the string value with the given key.
499 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
500 * @remarks You must not free str!
501 * @param[in] b The bundle object
502 * @param[in] key The key
503 * @param[out] str The returned value
504 * @return The operation result
505 * @retval BUNDLE_ERROR_NONE Success
506 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
507 * @retval BUNDLE_ERROR_KEY_NOT_AVAILABLE Key not available
508 * @pre @a b must be a valid bundle object.
509 * @see bundle_add_str()
512 bundle *b = bundle_create(); // Create new bundle object
513 bundle_add_str(b, "foo_key", "bar_val"); // add a key-val pair
516 bundle_get_str(b, "foo_key", &v); // v = "bar_val"
518 bundle_free(b); // After freeing b, v becomes a dangling pointer.
522 API int bundle_get_str(bundle *b, const char *key, char **str);
525 * @brief Gets the byte value with the given key.
526 * @since_tizen @if MOBILE 2.3 @elseif WEARABLE 2.3.1 @endif
527 * @remarks You must not free @a byte!
528 * @param[in] b The bundle object
529 * @param[in] key The key
530 * @param[out] byte The returned value
531 * @param[out] size The size of the byte
532 * @return The operation result
533 * @retval BUNDLE_ERROR_NONE Success
534 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
535 * @retval BUNDLE_ERROR_KEY_NOT_AVAILABLE Key not available
536 * @pre @a b must be a valid bundle object.
537 * @see bundle_add_byte()
540 bundle *b = bundle_create(); // Create new bundle object
541 bundle_add_byte(b, "foo", "bar\0", 4); // add string to bundle
543 bundle_add_byte(b, "number", (const void**)&number, sizeof(int)); // add integer to bundle
545 unsigned char *v = NULL;
547 bundle_get_byte(b, "foo", (void**)&v, &v_size); // v = "bar\0"
550 bundle_get_byte(b, "number", (void**)&n, &n_size); // number = 12345
552 bundle_free(b); // After freeing b, v and n becomes a dangling pointer.
555 API int bundle_get_byte(bundle *b, const char *key, void **byte, size_t *size);
566 #endif /* __BUNDLE_H__ */