2 * Copyright (c) 2020 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.
20 #include "include/bundle.h"
21 #include "include/bundle_internal.h"
23 #include "bundle-internal.h"
24 #include "exception-internal.h"
25 #include "export-api-internal.h"
26 #include "json-internal.h"
28 using namespace tizen_base::internal;
30 extern "C" EXPORT_API bundle* bundle_create(void) {
31 auto* h = new (std::nothrow) Bundle();
33 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
37 set_last_result(BUNDLE_ERROR_NONE);
38 return reinterpret_cast<bundle*>(h);
41 extern "C" EXPORT_API int bundle_free(bundle* b) {
43 return BUNDLE_ERROR_INVALID_PARAMETER;
45 auto* h = reinterpret_cast<Bundle*>(b);
47 return BUNDLE_ERROR_NONE;
50 extern "C" EXPORT_API int bundle_add_str(bundle* b,
51 const char* key, const char* str) {
52 if (b == nullptr || key == nullptr || str == nullptr)
53 return BUNDLE_ERROR_INVALID_PARAMETER;
55 auto* h = reinterpret_cast<Bundle*>(b);
56 std::vector<unsigned char> value(str, str + (strlen(str) + 1));
57 std::shared_ptr<KeyInfo> key_info(
58 new (std::nothrow) KeyInfo(BUNDLE_TYPE_STR, key, value));
59 if (key_info.get() == nullptr)
60 return BUNDLE_ERROR_OUT_OF_MEMORY;
64 } catch (Exception& e) {
65 return e.GetErrorCode();
68 return BUNDLE_ERROR_NONE;
71 extern "C" EXPORT_API int bundle_get_str(bundle* b,
72 const char* key, char** str) {
73 if (b == nullptr || key == nullptr || str == nullptr)
74 return BUNDLE_ERROR_INVALID_PARAMETER;
76 auto* h = reinterpret_cast<Bundle*>(b);
77 std::shared_ptr<KeyInfo> key_info;
79 key_info = h->Get(key);
80 } catch (Exception& e) {
81 return e.GetErrorCode();
84 if (key_info->GetType() != BUNDLE_TYPE_STR)
85 return BUNDLE_ERROR_INVALID_PARAMETER;
87 auto& values = key_info->GetValues();
88 auto& value = const_cast<std::unique_ptr<unsigned char[]>&>(values[0]);
89 *str = reinterpret_cast<char*>(&value[0]);
91 return BUNDLE_ERROR_NONE;
94 extern "C" EXPORT_API int bundle_add(bundle* b, const char* key,
96 return bundle_add_str(b, key, val);
99 extern "C" EXPORT_API int bundle_del(bundle* b, const char* key) {
100 if (b == nullptr || key == nullptr)
101 return BUNDLE_ERROR_INVALID_PARAMETER;
103 auto* h = reinterpret_cast<Bundle*>(b);
106 } catch (Exception& e) {
107 return e.GetErrorCode();
110 return BUNDLE_ERROR_NONE;
113 extern "C" EXPORT_API const char* bundle_get_val(bundle* b, const char* key) {
115 int ret = bundle_get_str(b, key, &val);
116 set_last_result(ret);
120 extern "C" EXPORT_API int bundle_get_count(bundle* b) {
124 auto* h = reinterpret_cast<Bundle*>(b);
125 set_last_result(BUNDLE_ERROR_NONE);
129 extern "C" EXPORT_API void bundle_iterate(bundle* b,
130 bundle_iterate_cb_t callback, void* user_data) {
131 if (b == nullptr || callback == nullptr) {
132 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
136 auto* h = reinterpret_cast<Bundle*>(b);
137 for (const auto& kv : h->GetMap()) {
138 auto& key_info = kv.second;
139 auto& values = key_info->GetValues();
140 auto& value = const_cast<std::unique_ptr<unsigned char[]>&>(values[0]);
141 auto* val = reinterpret_cast<char*>(&value[0]);
142 callback(key_info->GetKey().c_str(), val, user_data);
145 set_last_result(BUNDLE_ERROR_NONE);
148 extern "C" EXPORT_API void bundle_foreach(bundle* b,
149 bundle_iterator_t callback, void* user_data) {
150 if (b == nullptr || callback == nullptr) {
151 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
155 auto* h = reinterpret_cast<Bundle*>(b);
156 for (const auto& kv : h->GetMap()) {
157 auto& key_info = kv.second;
158 callback(key_info->GetKey().c_str(), key_info->GetType(),
159 reinterpret_cast<bundle_keyval_t*>(key_info.get()), user_data);
162 set_last_result(BUNDLE_ERROR_NONE);
165 extern "C" EXPORT_API int bundle_keyval_get_type(bundle_keyval_t* kv) {
167 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
171 auto* key_info = reinterpret_cast<KeyInfo*>(kv);
172 set_last_result(BUNDLE_ERROR_NONE);
173 return key_info->GetType();
176 extern "C" EXPORT_API int bundle_keyval_type_is_array(bundle_keyval_t* kv) {
177 int type = bundle_keyval_get_type(kv);
181 set_last_result(BUNDLE_ERROR_NONE);
182 if (type & BUNDLE_TYPE_ARRAY)
188 extern "C" EXPORT_API int bundle_keyval_type_is_measurable(
189 bundle_keyval_t* kv) {
190 int type = bundle_keyval_get_type(kv);
194 set_last_result(BUNDLE_ERROR_NONE);
195 if (type & BUNDLE_TYPE_MEASURABLE)
201 extern "C" EXPORT_API int bundle_keyval_get_basic_val(bundle_keyval_t* kv,
202 void** val, size_t* size) {
203 if (kv == nullptr || val == nullptr || size == nullptr)
204 return BUNDLE_ERROR_INVALID_PARAMETER;
206 auto* key_info = reinterpret_cast<KeyInfo*>(kv);
207 auto& values = key_info->GetValues();
208 auto& value = const_cast<std::unique_ptr<unsigned char[]>&>(values[0]);
209 *val = reinterpret_cast<void*>(&value[0]);
210 auto& values_size = key_info->GetValuesSize();
211 *size = reinterpret_cast<size_t>(values_size[0]);
212 return BUNDLE_ERROR_NONE;
215 extern "C" EXPORT_API int bundle_keyval_get_array_val(bundle_keyval_t* kv,
216 void*** array_val, unsigned int* array_len, size_t** array_item_size) {
217 if (kv == nullptr || array_val == nullptr || array_len == nullptr ||
218 array_item_size == nullptr)
219 return BUNDLE_ERROR_INVALID_PARAMETER;
221 auto* key_info = reinterpret_cast<KeyInfo*>(kv);
222 auto& values = const_cast<std::vector<std::unique_ptr<unsigned char[]>>&>(
223 key_info->GetValues());
224 auto& values_size = const_cast<std::vector<std::size_t>&>(
225 key_info->GetValuesSize());
226 *array_val = reinterpret_cast<void**>(&values[0]);
227 *array_len = static_cast<unsigned int>(values.size());
228 *array_item_size = reinterpret_cast<size_t*>(&values_size[0]);
229 return BUNDLE_ERROR_NONE;
232 extern "C" EXPORT_API bundle_keyval_t* bundle_keyval_dup(
233 const bundle_keyval_t* kv) {
235 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
239 auto* keyval = const_cast<bundle_keyval_t*>(kv);
240 auto* key_info = reinterpret_cast<KeyInfo*>(keyval);
241 auto* k = new (std::nothrow) KeyInfo(*key_info);
243 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
247 set_last_result(BUNDLE_ERROR_NONE);
248 return reinterpret_cast<bundle_keyval_t*>(k);
251 extern "C" EXPORT_API int bundle_keyval_free(bundle_keyval_t* kv) {
253 return BUNDLE_ERROR_INVALID_PARAMETER;
255 auto* key_info = reinterpret_cast<KeyInfo*>(kv);
257 return BUNDLE_ERROR_NONE;
260 extern "C" EXPORT_API bundle* bundle_dup(bundle* b_from) {
261 if (b_from == nullptr) {
262 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
266 auto* h = reinterpret_cast<Bundle*>(b_from);
267 auto* b = new (std::nothrow) Bundle(*h);
269 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
273 set_last_result(BUNDLE_ERROR_NONE);
274 return reinterpret_cast<bundle*>(b);
277 extern "C" EXPORT_API int bundle_encode(bundle *b, bundle_raw** raw, int* len) {
278 if (b == nullptr || raw == nullptr || len == nullptr)
279 return BUNDLE_ERROR_INVALID_PARAMETER;
281 auto* h = reinterpret_cast<Bundle*>(b);
282 *raw = reinterpret_cast<bundle_raw*>(h->Encode());
283 *len = strlen(reinterpret_cast<char*>(*raw));
284 return BUNDLE_ERROR_NONE;
287 extern "C" EXPORT_API int bundle_free_encoded_rawdata(bundle_raw **r) {
288 if (r == nullptr || *r == nullptr)
289 return BUNDLE_ERROR_INVALID_PARAMETER;
293 return BUNDLE_ERROR_NONE;
296 extern "C" EXPORT_API bundle* bundle_decode(const bundle_raw* r,
297 const int data_size) {
299 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
305 auto* raw = const_cast<bundle_raw*>(r);
306 b = new (std::nothrow) Bundle(static_cast<unsigned char*>(raw), data_size);
307 } catch (Exception& e) {
308 set_last_result(e.GetErrorCode());
312 set_last_result(BUNDLE_ERROR_NONE);
313 return reinterpret_cast<bundle*>(b);
316 extern "C" EXPORT_API int bundle_encode_raw(bundle* b, bundle_raw** r,
318 return bundle_encode(b, r, len);
321 extern "C" EXPORT_API bundle* bundle_decode_raw(const bundle_raw* r,
322 const int data_size) {
323 return bundle_decode(r, data_size);
326 extern "C" EXPORT_API int bundle_get_type(bundle* b, const char* key) {
327 if (b == nullptr || key == nullptr) {
328 set_last_result(BUNDLE_ERROR_KEY_NOT_AVAILABLE);
329 return BUNDLE_TYPE_NONE;
332 auto* h = reinterpret_cast<Bundle*>(b);
333 std::shared_ptr<KeyInfo> key_info;
335 key_info = h->Get(key);
336 } catch (Exception& e) {
337 set_last_result(e.GetErrorCode());
338 return BUNDLE_TYPE_NONE;
341 set_last_result(BUNDLE_ERROR_NONE);
342 return key_info->GetType();
345 extern "C" EXPORT_API int bundle_add_str_array(bundle* b, const char* key,
346 const char** str_array, const int len) {
347 if (b == nullptr || key == nullptr || len <= 0)
348 return BUNDLE_ERROR_INVALID_PARAMETER;
350 auto* h = reinterpret_cast<Bundle*>(b);
351 std::vector<std::vector<unsigned char>> values(len);
353 for (int i = 0; i < len; ++i) {
354 std::vector<unsigned char> value(str_array[i],
355 str_array[i] + (strlen(str_array[i]) + 1));
356 values[i] = std::move(value);
360 std::shared_ptr<KeyInfo> key_info(
361 new (std::nothrow) KeyInfo(
362 (BUNDLE_TYPE_STR_ARRAY | BUNDLE_TYPE_ARRAY), key, values));
363 if (key_info.get() == nullptr)
364 return BUNDLE_ERROR_OUT_OF_MEMORY;
367 h->Add(std::move(key_info));
368 } catch (Exception& e) {
369 return e.GetErrorCode();
372 return BUNDLE_ERROR_NONE;
375 extern "C" EXPORT_API const char** bundle_get_str_array(bundle* b,
376 const char* key, int* len) {
377 if (b == nullptr || key == nullptr || len == nullptr) {
378 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
382 auto* h = reinterpret_cast<Bundle*>(b);
383 std::shared_ptr<KeyInfo> key_info;
385 key_info = h->Get(key);
386 } catch (Exception& e) {
387 set_last_result(e.GetErrorCode());
391 if (key_info->GetType() != BUNDLE_TYPE_STR_ARRAY) {
392 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
396 auto& raw_values = const_cast<std::vector<std::unique_ptr<unsigned char[]>>&>(
397 key_info->GetValues());
398 auto** values = reinterpret_cast<unsigned char**>(&raw_values[0]);
399 *len = static_cast<int>(raw_values.size());
401 set_last_result(BUNDLE_ERROR_NONE);
402 return const_cast<const char**>(reinterpret_cast<char**>(values));
405 extern "C" EXPORT_API int bundle_add_byte(bundle* b, const char* key,
406 const void* bytes, const size_t size) {
407 if (b == nullptr || key == nullptr || bytes == nullptr || size == 0)
408 return BUNDLE_ERROR_INVALID_PARAMETER;
410 auto* h = reinterpret_cast<Bundle*>(b);
411 auto* p = reinterpret_cast<const unsigned char*>(bytes);
412 std::vector<unsigned char> value(p, p + size);
414 std::shared_ptr<KeyInfo> key_info(
415 new (std::nothrow) KeyInfo(BUNDLE_TYPE_BYTE, key, value));
416 if (key_info.get() == nullptr)
417 return BUNDLE_ERROR_OUT_OF_MEMORY;
420 h->Add(std::move(key_info));
421 } catch (Exception& e) {
422 return e.GetErrorCode();
425 return BUNDLE_ERROR_NONE;
428 extern "C" EXPORT_API int bundle_get_byte(bundle* b, const char* key,
429 void** bytes, size_t* size) {
430 if (b == nullptr || key == nullptr || bytes == nullptr || size == 0)
431 return BUNDLE_ERROR_INVALID_PARAMETER;
433 auto* h = reinterpret_cast<Bundle*>(b);
434 std::shared_ptr<KeyInfo> key_info;
436 key_info = h->Get(key);
437 } catch (Exception& e) {
438 return e.GetErrorCode();
441 if (key_info->GetType() != BUNDLE_TYPE_BYTE)
442 return BUNDLE_ERROR_INVALID_PARAMETER;
444 auto& values = const_cast<std::vector<std::unique_ptr<unsigned char[]>>&>(
445 key_info->GetValues());
446 auto& value = values[0];
447 *bytes = reinterpret_cast<void*>(&value[0]);
448 auto& values_size = key_info->GetValuesSize();
449 *size = reinterpret_cast<size_t>(values_size[0]);
451 return BUNDLE_ERROR_NONE;
454 extern "C" EXPORT_API int bundle_export_to_argv(bundle* b, char*** argv) {
455 if (b == nullptr || argv == nullptr) {
456 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
460 auto* h = reinterpret_cast<Bundle*>(b);
461 std::vector<std::string> exported_vt;
463 exported_vt = h->Export();
464 } catch (Exception& e) {
465 set_last_result(e.GetErrorCode());
469 int argc = exported_vt.size();
470 auto** exported_argv = reinterpret_cast<char**>(
471 calloc(argc + 1, sizeof(char*)));
472 if (exported_argv == nullptr) {
473 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
477 exported_argv[1] = const_cast<char*>(TAG_IMPORT_EXPORT_CHECK);
478 for (unsigned int idx = 2; idx < exported_vt.size(); idx += 2) {
479 exported_argv[idx] = strdup(exported_vt[idx].c_str());
480 if (exported_argv[idx] == nullptr) {
481 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
482 bundle_free_exported_argv(argc, &exported_argv);
486 exported_argv[idx + 1] = strdup(exported_vt[idx + 1].c_str());
487 if (exported_argv[idx + 1] == nullptr) {
488 set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
489 bundle_free_exported_argv(argc, &exported_argv);
494 *argv = exported_argv;
495 set_last_result(BUNDLE_ERROR_NONE);
499 extern "C" EXPORT_API int bundle_free_exported_argv(int argc, char*** argv) {
500 if (argc < 2 || !argv || !*argv)
501 return BUNDLE_ERROR_INVALID_PARAMETER;
503 for (int i = 2; i < argc; i++)
504 std::free((*argv)[i]);
509 return BUNDLE_ERROR_NONE;
512 extern "C" EXPORT_API bundle* bundle_import_from_argv(int argc, char** argv) {
513 if (argv == nullptr) {
514 set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
520 b = new (std::nothrow) Bundle(argc, argv);
521 } catch (Exception& e) {
522 set_last_result(e.GetErrorCode());
526 set_last_result(BUNDLE_ERROR_NONE);
527 return reinterpret_cast<bundle*>(b);
530 extern "C" EXPORT_API int bundle_compare(bundle* b1, bundle* b2) {
531 if (b1 == nullptr || b2 == nullptr)
534 auto* h1 = reinterpret_cast<Bundle*>(b1);
535 auto* h2 = reinterpret_cast<Bundle*>(b2);
542 extern "C" EXPORT_API int bundle_set_str_array_element(bundle* b,
543 const char* key, const unsigned int idx, const char* val) {
544 if (b == nullptr || key == nullptr || val == nullptr)
545 return BUNDLE_ERROR_INVALID_PARAMETER;
547 auto len = strlen(val) + 1;
548 auto* p = reinterpret_cast<unsigned char*>(const_cast<char*>(val));
549 std::vector<unsigned char> value(p, p + len);
551 auto* h = reinterpret_cast<Bundle*>(b);
553 h->Set(key, idx, value);
554 } catch (Exception& e) {
555 return e.GetErrorCode();
558 return BUNDLE_ERROR_NONE;
561 extern "C" EXPORT_API int bundle_add_byte_array(bundle* b,
562 const char* key, const unsigned int len) {
563 return bundle_init_byte_array(b, key, len);
566 extern "C" EXPORT_API int bundle_init_byte_array(bundle* b,
567 const char* key, const unsigned int len) {
568 if (b == nullptr || key == nullptr || len <= 0)
569 return BUNDLE_ERROR_INVALID_PARAMETER;
571 auto* h = reinterpret_cast<Bundle*>(b);
572 std::vector<std::vector<unsigned char>> values(len);
573 auto key_info = std::shared_ptr<KeyInfo>(
574 new (std::nothrow) KeyInfo(Bundle::Type::ByteArray, key, values));
575 if (key_info.get() == nullptr)
576 return BUNDLE_ERROR_OUT_OF_MEMORY;
580 } catch (Exception& e) {
581 return e.GetErrorCode();
584 return BUNDLE_ERROR_NONE;
587 extern "C" EXPORT_API int bundle_get_byte_array(bundle* b,
588 const char* key, void*** bytes_array, unsigned int* len,
589 unsigned int** array_element_size) {
590 if (b == nullptr || key == nullptr || bytes_array == nullptr ||
591 len == nullptr || array_element_size == nullptr)
592 return BUNDLE_ERROR_INVALID_PARAMETER;
594 auto* h = reinterpret_cast<Bundle*>(b);
595 std::shared_ptr<KeyInfo> key_info;
597 key_info = h->Get(key);
598 } catch (Exception& e) {
599 return e.GetErrorCode();
602 if (key_info->GetType() != BUNDLE_TYPE_BYTE_ARRAY)
603 return BUNDLE_ERROR_INVALID_PARAMETER;
605 auto& raw_values = const_cast<std::vector<std::unique_ptr<unsigned char[]>>&>(
606 key_info->GetValues());
607 auto** values = reinterpret_cast<unsigned char**>(&raw_values[0]);
608 *bytes_array = reinterpret_cast<void**>(values);
609 *len = static_cast<unsigned int>(raw_values.size());
611 auto& raw_values_size = const_cast<std::vector<unsigned int>&>(
612 key_info->GetUValuesSize());
613 *array_element_size = reinterpret_cast<unsigned int*>(&raw_values_size[0]);
615 return BUNDLE_ERROR_NONE;
618 extern "C" EXPORT_API int bundle_set_byte_array_element(bundle* b,
619 const char* key, const unsigned int idx,
620 const void* bytes, const size_t size) {
621 if (b == nullptr || key == nullptr || bytes == nullptr || size <= 0)
622 return BUNDLE_ERROR_INVALID_PARAMETER;
624 auto* p = reinterpret_cast<unsigned char*>(const_cast<void*>(bytes));
625 std::vector<unsigned char> value(p, p + size);
627 auto* h = reinterpret_cast<Bundle*>(b);
629 h->Set(key, idx, value);
630 } catch (Exception& e) {
631 return e.GetErrorCode();
634 return BUNDLE_ERROR_NONE;
637 extern "C" EXPORT_API int bundle_to_json(bundle* b, char** json) {
638 if (b == nullptr || json == nullptr)
639 return BUNDLE_ERROR_INVALID_PARAMETER;
641 auto* h = reinterpret_cast<Bundle*>(b);
644 *json = strdup(js.ToString().c_str());
645 } catch (Exception& e) {
646 return e.GetErrorCode();
649 return BUNDLE_ERROR_NONE;
652 extern "C" EXPORT_API int bundle_from_json(const char* json, bundle** b) {
653 if (json == nullptr || b == nullptr)
654 return BUNDLE_ERROR_INVALID_PARAMETER;
658 *b = reinterpret_cast<bundle*>(js.ToBundle());
659 } catch (Exception& e) {
660 return e.GetErrorCode();
663 return BUNDLE_ERROR_NONE;