summaryrefslogtreecommitdiff
path: root/include/bundle.h
blob: c1dc415cd3edbb4cff852dca40f1c000fb74a364 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
/*
 * bundle
 *
 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>,
 * Jaeho Lee <jaeho81.lee@samsung.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */


#ifndef __BUNDLE_H__
#define __BUNDLE_H__

/**
 * @file bundle.h
 * @version 0.1
 * @brief    This file declares API of bundle library
 */

/**
 * @addtogroup APPLICATION_FRAMEWORK
 * @{
 *
 * @defgroup bundle
 * @version    0.1
 *
 * @section    Header to use them:
 * @code
 * #include <bundle.h>
 * @endcode
 *
 * @addtogroup bundle
 * @{
 */

#include <errno.h>
#include <stddef.h>

#ifdef __cplusplus
extern "C" {
# endif

#define API 	__attribute__((visibility("default")))
#define likely(x) __builtin_expect(x,1)
#define unlikely(x) __builtin_expect(x,0)

/**
 * bundle is an opaque type pointing a bundle object
 */
typedef struct _bundle_t bundle;

/**
 * bundle_raw is an encoded data type
 * @see bundle_encode()
 * @see bundle_decode()
 */
typedef unsigned char bundle_raw;


/**
 * Each bundle keyval have a type.
 */
enum bundle_type_property {
	BUNDLE_TYPE_ARRAY = 0x0100,
	BUNDLE_TYPE_PRIMITIVE = 0x0200,
	BUNDLE_TYPE_MEASURABLE = 0x0400
};

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
};

/**
 * A keyval object in a bundle.
 * @see bundle_iterator_t
 */
typedef struct keyval_t bundle_keyval_t;


/**
 * bundle_iterator is a new iterator function type for bundle_foreach()
 * @see bundle_foreach()
 */
typedef void (*bundle_iterator_t) (
		const char *key,
		const int type,
		const bundle_keyval_t *kv,
		void *user_data
);


/**
 * bundle_iterate_cb_t is an iterator function type for bundle_iterate()
 * @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
 * @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 <bundle.h>
 bundle *b = bundle_create(); // Create new bundle object
 bundle_free(b); // free bundle
 @endcode
 */
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
 * @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.h>
 bundle *b = bundle_create(); // Create new bundle object
 bundle_free(b); // free bundle
 @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
 * @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 <bundle.h>
 char *sa = { "aaa", "bbb", "ccc" };	// A string array of length 3
 bundle *b = bundle_create();
 bundle_add_str_array(b, "foo", sa, 3); // add a key-val pair
 bundle_free(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
 * @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.h>
 bundle *b = bundle_create(); // Create new bundle object
 bundle_add(b, "foo_key", "bar_val"); // add a key-val pair

 bundle_free(b);
 @endcode
 */
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
 @code
 #include <bundle.h>
 bundle *b = bundle_create(); // Create new bundle object
 bundle_add(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
 * @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.h>
 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 **str_array = NULL;
 int len_str_array = 0;

 str_array=bundle_get_str_array(b, "foo", &len_str_array);
 // str_array = { "aaa", "bbb", "ccc" }, and len_str_array = 3

 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
 * @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.h>
 bundle *b = bundle_create(); // Create new bundle object
 bundle_add(b, "foo_key", "bar_val"); // 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.
 val = NULL;
 @endcode
 */
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
 @code
 #include <bundle.h>
 bundle *b = bundle_create(); // Create new bundle object
 bundle_add(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
 count = bundle_get_count(b); // count=2

 bundle_free(b);
 @endcode
 */
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
 * @param[in]	b	A bundle
 * @param[in]	key	A key in bundle
 * @return	Type of a key in b
 * @remark
 @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
 @code
 #include <bundle.h>
 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_free(b);
 bundle_free(b_dup);
 @endcode
 */
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.
 @code
 @include <stdio.h>
 #include <bundle.h>
 void sample_cb(const char *k, const char *v, void *data) {
   printf("%s -> %s\n", k, v);
 }

 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
	 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.
 @code
 @include <stdio.h>
 #include <bundle.h>
 void sample_cb(const char *key, const int type, const bundle_keyval_t *kv, void *user_data) {
   void *basic_val = NULL;
   size_t basic_size = 0;
   void **array_val = NULL;
   int array_len = 0;
   size_t *array_elem_size = NULL;

   printf("Key:%s, Type:%d\n", key, type);
   if(bundle_keyval_type_is_array(kv)) {
     bundle_keyval_get_array_val(kv, &array_val, &array_len, &array_elem_size);
	 // Do something...
   }
   else {
     bundle_keyval_get_basic_val(kv, &basic_val, &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
	 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
 * @param[in]	kv	A bundle_keyval_t object
 * @return	Type of kv
 * @retval	-1	Operation failure.	errno is set.
 * @remark
 */
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
 * @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
 */
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
 * @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
 */
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
 * @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.
 */
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
 * @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
 */
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
 @code
 #include <bundle.h>
 bundle *b = bundle_create(); // Create new bundle object
 bundle_add(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
 */
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
 @code
 #include <bundle.h>
 bundle *b = bundle_create(); // Create new bundle object
 bundle_add(b, "foo_key", "bar_val"); // add a key-val pair

 bundle_raw *encoded_b;
 int len;
 bundle_encode(b, &encoded_b, &len);	// encode b

 bundle *b_dup;
 b_dup = bundle_decode(encoded_b, len);	// decoded bundle object

 bundle_free(b);
 free(encoded_b);
 bundle_free(b_dup);
 @endcode
 */
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
 @code
 #include <bundle.h>
 bundle *b = bundle_create(); // Create new bundle object
 bundle_add(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
 @code
 #include <bundle.h>
 bundle *b = bundle_create(); // Create new bundle object
 bundle_add(b, "foo_key", "bar_val"); // add a key-val pair

 bundle_raw *encoded_b;
 int len;
 bundle_encode(b, &encoded_b, &len);	// encode b

 bundle *b_dup;
 b_dup = bundle_decode_raw(encoded_b, len);	// decoded bundle object

 bundle_free(b);
 free(encoded_b);
 bundle_free(b_dup);
 @endcode
 */
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
 @code
 #include <bundle.h>
 bundle *b = bundle_create(); // Create new bundle object
 bundle_add(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 
 }
 bundle_free_exported_argv(argc, argv);	// argv must be freed after being used.

 bundle_free(b);
 @endcode
 */
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.
 @code
 bundle *b = bundle_create();
 bundle_add_str(b, "foo", "bar");
 
 int argc = 0;
 char **argv = NULL;
 argc = bundle_export_to_argv(b, &argv);
 if(0 > argc) error("export failure");

 // Use argv...

 bundle_free_export_argv(argc, argv);
 argv = NULL;

 bundle_free(b);
 @endcode
 */
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
 @code
 #include <bundle.h>

 int main(int argc, char **argv) {
   bundle *b = bundle_import_from_argv(argc, argv); // import from argc+argv
   char *val = bundle_get_val(b, "foo_key");	// value for "foo_key"
   // ......
   bundle_free(b);	// After freeing b, val becomes a dangling pointer.
   val = NULL;
 }
 @endcode
 */
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
 * @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.h>
 bundle *b = bundle_create(); // Create new bundle object
 bundle_add_str(b, "foo", "bar"); // add a key-val pair

 bundle_free(b);
 @endcode
 */
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
 * @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.h>
 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 **str_array = NULL;
 int len_str_array = 0;

 str_array=bundle_get_str_array(b, "foo", &len_str_array);
 // str_array = { "aaa", "bbb", "ccc" }, and len_str_array = 3

 bundle_free(b);
 @endcode
 */
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
 * @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.h>
 bundle *b = bundle_create(); // Create new bundle object
 bundle_add_byte(b, "foo", "bar\0", 4); // add a key-val pair

 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()
 * @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.h>
 bundle *b = bundle_create();
 bundle_add_byte_array(b, "foo", NULL, 3); // add a byte-array with length 3

 bundle_set_byte_array_element(b, "foo", 0, "aaa\0", 4);	array[0] = "aaa\0"
 bundle_set_byte_array_element(b, "foo", 1, "bbb\0", 4);	array[1] = "bbb\0"
 bundle_set_byte_array_element(b, "foo", 2, "ccc\0", 4);	array[2] = "ccc\0"

 bundle_free(b);
 @endcode
 */
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
 * @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.h>
 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);

 unsigned char **byte_array = NULL;
 int len_byte_array = 0;

 byte_array=bundle_get_str_array(b, "foo", &len_byte_array);
 // byte_array = { "aaa\0", "bbb\0", "ccc\0" }, and len_byte_array = 3

 bundle_free(b);
 @endcode
 */
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
 * @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.h>
 bundle *b = bundle_create(); // Create new bundle object
 bundle_add_str(b, "foo_key", "bar_val"); // add a key-val pair

 char *v = NULL;
 bundle_get_str(b, "foo_key", &v);	// v = "bar_val"

 bundle_free(b);	// After freeing b, v becomes a dangling pointer.
 v = NULL;
 @endcode
 */
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
 * @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.h>
 bundle *b = bundle_create(); // Create new bundle object
 bundle_add_byte(b, "foo", "bar\0", 4); // add a key-val pair

 unsigned char *v = NULL;
 bundle_get_str(b, "foo", &v);	// v = "bar\0"

 bundle_free(b);	// After freeing b, v 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
 @code
 #include <bundle.h>
 bundle *b = bundle_create();
 bundle_add_byte_array(b, "foo", NULL, 3);
 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);

 char **byte_array = NULL;
 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);
 // byte_array = { "aaa\0", "bbb\0", "ccc\0" }, len_byte_array = 3, and size_byte_array = { 4, 4, 4 }

 bundle_free(b);
 @endcode
 */
API int bundle_get_byte_array(bundle *b, const char *key, void ***byte_array, unsigned int *len, unsigned int **array_element_size);


#ifdef __cplusplus
}
#endif

/**
 * @}
 * @}
 */

#endif	/* __BUNDLE_H__ */