Bundle::Bundle(const Bundle& b) {
map_ = b.map_;
+ list_ = b.list_;
}
Bundle& Bundle::operator = (const Bundle& b) {
- if (this != &b)
+ if (this != &b) {
map_ = b.map_;
+ list_ = b.list_;
+ }
return *this;
}
Bundle::Bundle(Bundle&& b) noexcept {
map_ = std::move(b.map_);
+ list_ = std::move(b.list_);
}
Bundle& Bundle::operator = (Bundle&& b) noexcept {
- if (this != &b)
+ if (this != &b) {
map_ = std::move(b.map_);
+ list_ = std::move(b.list_);
+ }
return *this;
}
if (iter == map_.end())
THROW(BUNDLE_ERROR_KEY_NOT_AVAILABLE);
+ list_.remove(iter->second);
map_.erase(iter);
}
THROW(BUNDLE_ERROR_KEY_EXISTS);
map_[key_info->GetKey()] = key_info;
+ list_.push_back(key_info);
}
void Bundle::Set(const std::string& key, int index,
if (iter == map_.end())
THROW(BUNDLE_ERROR_KEY_NOT_AVAILABLE);
- iter->second->SetValue(index, value);
+ int ret = iter->second->SetValue(index, value);
+ if (ret != BUNDLE_ERROR_NONE)
+ THROW(ret);
}
std::shared_ptr<KeyInfo>& Bundle::Get(const std::string& key) {
unsigned char* Bundle::Encode() {
std::vector<unsigned char> bytes;
- for (const auto& i : map_) {
- auto& key_info = i.second;
+ for (const auto& key_info : list_) {
auto encoded_bytes = key_info->Encode();
bytes.insert(bytes.end(), encoded_bytes.begin(), encoded_bytes.end());
}
std::back_inserter(encoded_bytes));
reader += total_size;
- auto key_info = std::shared_ptr<KeyInfo>(
- new (std::nothrow) KeyInfo(encoded_bytes));
- if (key_info.get() == nullptr)
+ KeyInfo* new_key_info;
+ try {
+ new_key_info = new KeyInfo(encoded_bytes);
+ } catch (Exception& e) {
+ return e.GetErrorCode();
+ } catch (const std::bad_alloc& ba) {
return BUNDLE_ERROR_OUT_OF_MEMORY;
+ }
+ auto key_info = std::shared_ptr<KeyInfo>(new_key_info);
auto iter = map_.find(key_info->GetKey());
if (iter != map_.end())
continue;
map_[key_info->GetKey()] = key_info;
+ list_.push_back(key_info);
}
return BUNDLE_ERROR_NONE;
std::vector<std::string> Bundle::Export() {
std::vector<std::string> argv(2);
- for (const auto& kv : map_) {
- auto& key_info = kv.second;
+ for (const auto& key_info : list_) {
argv.push_back(key_info->GetKey());
auto encoded_bytes = key_info->Encode();
auto len = strlen(argv[idx + 1]) + 1;
std::vector<unsigned char> value;
std::copy(p, p + len, std::back_inserter(value));
- auto key_info = std::shared_ptr<KeyInfo>(
- new (std::nothrow) KeyInfo(Type::String, argv[idx], value));
- if (key_info.get() == nullptr)
+
+ KeyInfo* new_key_info;
+ try {
+ new_key_info = new KeyInfo(Type::String, argv[idx], value);
+ } catch (Exception& e) {
+ return e.GetErrorCode();
+ } catch (const std::bad_alloc& ba) {
return BUNDLE_ERROR_OUT_OF_MEMORY;
+ }
+ auto key_info = std::shared_ptr<KeyInfo>(new_key_info);
auto iter = map_.find(key_info->GetKey());
if (iter != map_.end())
continue;
map_[key_info->GetKey()] = key_info;
+ list_.push_back(key_info);
}
return BUNDLE_ERROR_NONE;
}
auto* p = reinterpret_cast<unsigned char*>(bytes);
std::vector<unsigned char> decoded_bytes(p, p + (out_len + 1));
- auto key_info = std::shared_ptr<KeyInfo>(
- new (std::nothrow) KeyInfo(decoded_bytes));
- if (key_info.get() == nullptr)
+
+ KeyInfo* new_key_info;
+ try {
+ new_key_info = new KeyInfo(decoded_bytes);
+ } catch (Exception& e) {
+ return e.GetErrorCode();
+ } catch (const std::bad_alloc& ba) {
return BUNDLE_ERROR_OUT_OF_MEMORY;
+ }
+ auto key_info = std::shared_ptr<KeyInfo>(new_key_info);
auto iter = map_.find(key_info->GetKey());
if (iter != map_.end())
continue;
map_[key_info->GetKey()] = key_info;
+ list_.push_back(key_info);
}
return BUNDLE_ERROR_NONE;
#ifndef BUNDLE_INTERNAL_H_
#define BUNDLE_INTERNAL_H_
+#include <list>
#include <map>
#include <memory>
#include <string>
private:
std::map<std::string, std::shared_ptr<KeyInfo>> map_;
+ std::list<std::shared_ptr<KeyInfo>> list_;
};
} // namespace internal
void Json::OnJsonObjectMember(JsonObject* object, const char* key,
JsonNode* node, gpointer user_data) {
auto* b = static_cast<Bundle*>(user_data);
- std::shared_ptr<KeyInfo> key_info;
+ KeyInfo* key_info;
JsonNodeType node_type = JSON_NODE_TYPE(node);
if (node_type == JSON_NODE_ARRAY) {
JsonArray* json_arr = json_node_get_array(node);
std::copy(p, p + (strlen(val) + 1), std::back_inserter(value));
values.push_back(value);
}
- key_info.reset(
- new (std::nothrow) KeyInfo(Bundle::Type::StringArray, key, values));
+
+ try {
+ key_info = new KeyInfo(Bundle::Type::StringArray, key, values);
+ } catch (Exception& e) {
+ _E("Error(%d)", e.GetErrorCode());
+ return;
+ } catch (const std::bad_alloc& ba) {
+ _E("bad alloc exception");
+ return;
+ }
} else {
auto* val = json_node_get_string(node);
if (val == nullptr)
auto* p = reinterpret_cast<unsigned char*>(const_cast<char*>(val));
std::vector<unsigned char> value;
std::copy(p, p + (strlen(val) + 1), std::back_inserter(value));
- key_info.reset(
- new (std::nothrow) KeyInfo(Bundle::Type::String, key, value));
- }
- if (key_info.get() == nullptr) {
- _E("Out of memory");
- return;
+ try {
+ key_info = new KeyInfo(Bundle::Type::String, key, value);
+ } catch (Exception& e) {
+ _E("Error(%d)", e.GetErrorCode());
+ return;
+ } catch (const std::bad_alloc& ba) {
+ _E("bad alloc exception");
+ return;
+ }
}
try {
- b->Add(key_info);
+ b->Add(std::shared_ptr<KeyInfo>(key_info));
} catch (Exception& e) {
_W("Add() is failed. error(%d)", e.GetErrorCode());
return;
#include <cstring>
+#include "include/bundle.h"
+
+#include "exception-internal.h"
#include "key-info-internal.h"
-#include "log-private.h"
namespace tizen_base {
namespace internal {
std::vector<unsigned char> value)
: type_(type),
key_(std::move(key)) {
- SetValue(value);
+ int ret = SetValue(value);
+ if (ret != BUNDLE_ERROR_NONE)
+ THROW(ret);
}
KeyInfo::KeyInfo(int type, std::string key,
std::vector<std::vector<unsigned char>> values)
: type_(type),
key_(std::move(key)) {
- SetValues(values);
+ int ret = SetValues(values);
+ if (ret != BUNDLE_ERROR_NONE)
+ THROW(ret);
}
KeyInfo::KeyInfo(std::vector<unsigned char> encoded_bytes) {
- Decode(encoded_bytes);
+ int ret = Decode(encoded_bytes);
+ if (ret != BUNDLE_ERROR_NONE)
+ THROW(ret);
}
KeyInfo::~KeyInfo() = default;
uvalues_size_ = key_info.uvalues_size_;
for (unsigned int i = 0; i < key_info.values_.size(); ++i) {
auto* new_value = new (std::nothrow) unsigned char[values_size_[i]];
+ if (new_value == nullptr)
+ THROW(BUNDLE_ERROR_OUT_OF_MEMORY);
+
std::copy(key_info.values_[i].get(),
key_info.values_[i].get() + values_size_[i], new_value);
values_.emplace_back(new_value);
uvalues_size_ = key_info.uvalues_size_;
for (unsigned int i = 0; i < key_info.values_.size(); ++i) {
auto* new_value = new (std::nothrow) unsigned char[values_size_[i]];
+ if (new_value == nullptr)
+ THROW(BUNDLE_ERROR_OUT_OF_MEMORY);
+
std::copy(key_info.values_[i].get(),
key_info.values_[i].get() + values_size_[i], new_value);
values_.emplace_back(new_value);
}
bool KeyInfo::IsArray() const {
- if (values_.size() == 1)
+ if (type_ & BUNDLE_TYPE_ARRAY)
return true;
return false;
return uvalues_size_;
}
-void KeyInfo::SetValue(const std::vector<unsigned char>& value) {
+int KeyInfo::SetValue(const std::vector<unsigned char>& value) {
auto* new_value = new (std::nothrow) unsigned char[value.size()];
+ if (new_value == nullptr)
+ return BUNDLE_ERROR_OUT_OF_MEMORY;
+
std::copy(value.begin(), value.end(), new_value);
values_.emplace_back(new_value);
values_size_.push_back(value.size());
uvalues_size_.push_back(static_cast<unsigned int>(value.size()));
+ return BUNDLE_ERROR_NONE;
}
-void KeyInfo::SetValues(const std::vector<std::vector<unsigned char>>& values) {
+int KeyInfo::SetValues(const std::vector<std::vector<unsigned char>>& values) {
for (unsigned int i = 0; i< values.size(); ++i) {
auto* new_value = new (std::nothrow) unsigned char[values[i].size()];
+ if (new_value == nullptr)
+ return BUNDLE_ERROR_OUT_OF_MEMORY;
+
std::copy(values[i].begin(), values[i].end(), new_value);
values_.emplace_back(new_value);
values_size_.push_back(values[i].size());
uvalues_size_.push_back(values[i].size());
}
+ return BUNDLE_ERROR_NONE;
}
std::vector<unsigned char> KeyInfo::Encode() {
// key
std::copy(key_.begin(), key_.end() + 1, std::back_inserter(bytes));
- // values size
- std::size_t values_size = values_.size();
- p = reinterpret_cast<unsigned char*>(&values_size);
- std::copy(p , p + sizeof(values_size), std::back_inserter(bytes));
+ if (type_ & BUNDLE_TYPE_ARRAY) {
+ // values size
+ std::size_t values_size = values_.size();
+ p = reinterpret_cast<unsigned char*>(&values_size);
+ std::copy(p , p + sizeof(values_size), std::back_inserter(bytes));
+ }
// values
for (unsigned int i = 0; i < values_.size(); i++) {
encoded_size += key_.length() + 1;
- // values size
- if ((encoded_size + sizeof(std::size_t)) < encoded_size)
- return 0;
+ if (type_ & BUNDLE_TYPE_ARRAY) {
+ // values size
+ if ((encoded_size + sizeof(std::size_t)) < encoded_size)
+ return 0;
- encoded_size += sizeof(std::size_t);
+ encoded_size += sizeof(std::size_t);
+ }
// values
std::size_t values_size = 0;
return encoded_size;
}
-void KeyInfo::Decode(const std::vector<unsigned char>& bytes) {
+int KeyInfo::Decode(const std::vector<unsigned char>& bytes) {
unsigned int reader = 0;
// total size
std::size_t total_size = 0;
if ((reader + sizeof(total_size)) > bytes.size())
- return;
+ return BUNDLE_ERROR_INVALID_PARAMETER;
unsigned char* p = reinterpret_cast<unsigned char*>(&total_size);
std::copy(&bytes[reader], &bytes[reader] + sizeof(total_size), p);
// type
if ((reader + sizeof(type_)) > bytes.size())
- return;
+ return BUNDLE_ERROR_INVALID_PARAMETER;
p = reinterpret_cast<unsigned char*>(&type_);
std::copy(&bytes[reader], &bytes[reader] + sizeof(type_), p);
std::size_t key_size = 0;
if ((reader + sizeof(key_size)) > bytes.size())
- return;
+ return BUNDLE_ERROR_INVALID_PARAMETER;
p = reinterpret_cast<unsigned char*>(&key_size);
std::copy(&bytes[reader], &bytes[reader] + sizeof(key_size), p);
reader += sizeof(key_size);
if ((reader + key_size) > bytes.size())
- return;
+ return BUNDLE_ERROR_INVALID_PARAMETER;
// key
std::vector<unsigned char> key(&bytes[reader], &bytes[reader] + key_size);
key_ = std::string(reinterpret_cast<char*>(p));
reader += key_size;
- // values size
std::size_t values_size = 0;
- if ((reader + sizeof(values_size)) > bytes.size())
- return;
-
- p = reinterpret_cast<unsigned char*>(&values_size);
- std::copy(&bytes[reader], &bytes[reader] + sizeof(values_size), p);
- reader += sizeof(values_size);
+ if (type_ & BUNDLE_TYPE_ARRAY) {
+ // values size
+ if ((reader + sizeof(values_size)) > bytes.size())
+ return BUNDLE_ERROR_INVALID_PARAMETER;
+
+ p = reinterpret_cast<unsigned char*>(&values_size);
+ std::copy(&bytes[reader], &bytes[reader] + sizeof(values_size), p);
+ reader += sizeof(values_size);
+ } else {
+ values_size = 1;
+ }
// values
for (std::size_t i = 0; i < values_size; ++i) {
// value_size
std::size_t value_size = 0;
if ((reader + sizeof(value_size)) > bytes.size())
- return;
+ return BUNDLE_ERROR_INVALID_PARAMETER;
p = reinterpret_cast<unsigned char*>(&value_size);
std::copy(&bytes[reader], &bytes[reader] + sizeof(value_size), p);
// value
if ((reader + value_size) > bytes.size())
- return;
+ return BUNDLE_ERROR_INVALID_PARAMETER;
+
+ auto* new_value = new (std::nothrow) unsigned char[value_size];
+ if (new_value == nullptr)
+ return BUNDLE_ERROR_OUT_OF_MEMORY;
- auto* new_value = new unsigned char[value_size];
std::copy(&bytes[reader], &bytes[reader] + value_size, new_value);
reader += value_size;
values_size_.push_back(value_size);
uvalues_size_.push_back(static_cast<unsigned int>(value_size));
}
+
+ return BUNDLE_ERROR_NONE;
}
-void KeyInfo::SetValue(int index, const std::vector<unsigned char>& value) {
+int KeyInfo::SetValue(int index, const std::vector<unsigned char>& value) {
if (index > GetSize() && index < 0)
- return;
+ return BUNDLE_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS;
+
+ if (!IsArray())
+ return BUNDLE_ERROR_INVALID_PARAMETER;
+
+ auto* new_value = new (std::nothrow) unsigned char[value.size()];
+ if (new_value == nullptr)
+ return BUNDLE_ERROR_OUT_OF_MEMORY;
+
+ if (value.size() != 0)
+ std::copy(value.begin(), value.end(), new_value);
- auto* new_value = new unsigned char[value.size()];
- std::copy(value.begin(), value.end(), new_value);
values_[index].reset(new_value);
values_size_[index] = value.size();
uvalues_size_[index] = static_cast<unsigned int>(value.size());
+ return BUNDLE_ERROR_NONE;
}
int KeyInfo::GetSize() const {
const std::vector<unsigned int>& GetUValuesSize();
std::vector<unsigned char> Encode();
- void SetValue(int index, const std::vector<unsigned char>& value);
+ int SetValue(int index, const std::vector<unsigned char>& value);
int GetSize() const;
private:
std::size_t GetEncodedSize();
- void SetValuesSize();
- void SetValue(const std::vector<unsigned char>& value);
- void SetValues(const std::vector<std::vector<unsigned char>>& values);
- void Decode(const std::vector<unsigned char>& bytes);
+ int SetValuesSize();
+ int SetValue(const std::vector<unsigned char>& value);
+ int SetValues(const std::vector<std::vector<unsigned char>>& values);
+ int Decode(const std::vector<unsigned char>& bytes);
private:
int type_;
extern "C" EXPORT_API int bundle_add_str(bundle* b,
const char* key, const char* str) {
- if (b == nullptr || key == nullptr || str == nullptr)
+ if (b == nullptr || key == nullptr || strlen(key) == 0 || str == nullptr)
return BUNDLE_ERROR_INVALID_PARAMETER;
auto* h = reinterpret_cast<Bundle*>(b);
std::vector<unsigned char> value(str, str + (strlen(str) + 1));
- std::shared_ptr<KeyInfo> key_info(
- new (std::nothrow) KeyInfo(BUNDLE_TYPE_STR, key, value));
- if (key_info.get() == nullptr)
+
+ KeyInfo* key_info;
+ try {
+ key_info = new KeyInfo(BUNDLE_TYPE_STR, key, value);
+ } catch (Exception& e) {
+ return e.GetErrorCode();
+ } catch (const std::bad_alloc& ba) {
return BUNDLE_ERROR_OUT_OF_MEMORY;
+ }
try {
- h->Add(key_info);
+ h->Add(std::shared_ptr<KeyInfo>(key_info));
} catch (Exception& e) {
return e.GetErrorCode();
}
extern "C" EXPORT_API int bundle_get_str(bundle* b,
const char* key, char** str) {
- if (b == nullptr || key == nullptr || str == nullptr)
+ if (b == nullptr || key == nullptr)
return BUNDLE_ERROR_INVALID_PARAMETER;
auto* h = reinterpret_cast<Bundle*>(b);
if (key_info->GetType() != BUNDLE_TYPE_STR)
return BUNDLE_ERROR_INVALID_PARAMETER;
- auto& values = key_info->GetValues();
- auto& value = const_cast<std::unique_ptr<unsigned char[]>&>(values[0]);
- *str = reinterpret_cast<char*>(&value[0]);
+ if (str) {
+ auto& values = key_info->GetValues();
+ auto& value = const_cast<std::unique_ptr<unsigned char[]>&>(values[0]);
+ *str = reinterpret_cast<char*>(&value[0]);
+ }
return BUNDLE_ERROR_NONE;
}
}
extern "C" EXPORT_API int bundle_del(bundle* b, const char* key) {
- if (b == nullptr || key == nullptr)
+ if (b == nullptr || key == nullptr || strlen(key) == 0)
return BUNDLE_ERROR_INVALID_PARAMETER;
auto* h = reinterpret_cast<Bundle*>(b);
extern "C" EXPORT_API int bundle_keyval_get_basic_val(bundle_keyval_t* kv,
void** val, size_t* size) {
- if (kv == nullptr || val == nullptr || size == nullptr)
+ if (kv == nullptr)
return BUNDLE_ERROR_INVALID_PARAMETER;
auto* key_info = reinterpret_cast<KeyInfo*>(kv);
- auto& values = key_info->GetValues();
- auto& value = const_cast<std::unique_ptr<unsigned char[]>&>(values[0]);
- *val = reinterpret_cast<void*>(&value[0]);
- auto& values_size = key_info->GetValuesSize();
- *size = reinterpret_cast<size_t>(values_size[0]);
+ if (key_info->IsArray())
+ return BUNDLE_ERROR_INVALID_PARAMETER;
+
+ if (val) {
+ auto& values = key_info->GetValues();
+ auto& value = const_cast<std::unique_ptr<unsigned char[]>&>(values[0]);
+ *val = reinterpret_cast<void*>(&value[0]);
+ }
+
+ if (size) {
+ auto& values_size = key_info->GetValuesSize();
+ *size = reinterpret_cast<size_t>(values_size[0]);
+ }
+
return BUNDLE_ERROR_NONE;
}
extern "C" EXPORT_API int bundle_keyval_get_array_val(bundle_keyval_t* kv,
void*** array_val, unsigned int* array_len, size_t** array_item_size) {
- if (kv == nullptr || array_val == nullptr || array_len == nullptr ||
- array_item_size == nullptr)
+ if (kv == nullptr)
return BUNDLE_ERROR_INVALID_PARAMETER;
auto* key_info = reinterpret_cast<KeyInfo*>(kv);
+ if (!key_info->IsArray())
+ return BUNDLE_ERROR_INVALID_PARAMETER;
+
auto& values = const_cast<std::vector<std::unique_ptr<unsigned char[]>>&>(
key_info->GetValues());
- auto& values_size = const_cast<std::vector<std::size_t>&>(
+
+ if (array_val)
+ *array_val = reinterpret_cast<void**>(&values[0]);
+
+ if (array_len)
+ *array_len = static_cast<unsigned int>(values.size());
+
+ if (array_item_size) {
+ auto& values_size = const_cast<std::vector<std::size_t>&>(
key_info->GetValuesSize());
- *array_val = reinterpret_cast<void**>(&values[0]);
- *array_len = static_cast<unsigned int>(values.size());
- *array_item_size = reinterpret_cast<size_t*>(&values_size[0]);
+ *array_item_size = reinterpret_cast<size_t*>(&values_size[0]);
+ }
+
return BUNDLE_ERROR_NONE;
}
auto* keyval = const_cast<bundle_keyval_t*>(kv);
auto* key_info = reinterpret_cast<KeyInfo*>(keyval);
- auto* k = new (std::nothrow) KeyInfo(*key_info);
- if (k == nullptr) {
+
+ KeyInfo* k;
+ try {
+ k = new KeyInfo(*key_info);
+ } catch (Exception& e) {
+ set_last_result(e.GetErrorCode());
+ return nullptr;
+ } catch (const std::bad_alloc& ba) {
set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
return nullptr;
}
Bundle* b = nullptr;
try {
auto* raw = const_cast<bundle_raw*>(r);
- b = new (std::nothrow) Bundle(static_cast<unsigned char*>(raw), data_size);
+ b = new Bundle(static_cast<unsigned char*>(raw), data_size);
} catch (Exception& e) {
set_last_result(e.GetErrorCode());
return nullptr;
+ } catch (const std::bad_alloc& ba) {
+ set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
+ return nullptr;
}
set_last_result(BUNDLE_ERROR_NONE);
extern "C" EXPORT_API int bundle_add_str_array(bundle* b, const char* key,
const char** str_array, const int len) {
- if (b == nullptr || key == nullptr || len <= 0)
+ if (b == nullptr || key == nullptr || strlen(key) == 0)
return BUNDLE_ERROR_INVALID_PARAMETER;
auto* h = reinterpret_cast<Bundle*>(b);
}
}
- std::shared_ptr<KeyInfo> key_info(
- new (std::nothrow) KeyInfo(
- (BUNDLE_TYPE_STR_ARRAY | BUNDLE_TYPE_ARRAY), key, values));
- if (key_info.get() == nullptr)
- return BUNDLE_ERROR_OUT_OF_MEMORY;
+ KeyInfo* key_info;
+ try {
+ key_info = new KeyInfo((BUNDLE_TYPE_STR_ARRAY | BUNDLE_TYPE_ARRAY),
+ key, values);
+ } catch (Exception& e) {
+ return e.GetErrorCode();
+ }
try {
- h->Add(std::move(key_info));
+ h->Add(std::shared_ptr<KeyInfo>(key_info));
} catch (Exception& e) {
return e.GetErrorCode();
}
extern "C" EXPORT_API const char** bundle_get_str_array(bundle* b,
const char* key, int* len) {
- if (b == nullptr || key == nullptr || len == nullptr) {
+ if (b == nullptr || key == nullptr) {
set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
return nullptr;
}
return nullptr;
}
+ set_last_result(BUNDLE_ERROR_NONE);
+
auto& raw_values = const_cast<std::vector<std::unique_ptr<unsigned char[]>>&>(
key_info->GetValues());
- auto** values = reinterpret_cast<unsigned char**>(&raw_values[0]);
- *len = static_cast<int>(raw_values.size());
- set_last_result(BUNDLE_ERROR_NONE);
+ if (len)
+ *len = static_cast<int>(raw_values.size());
+
+ if (raw_values.size() == 0)
+ return nullptr;
+
+ auto** values = reinterpret_cast<unsigned char**>(&raw_values[0]);
return const_cast<const char**>(reinterpret_cast<char**>(values));
}
extern "C" EXPORT_API int bundle_add_byte(bundle* b, const char* key,
const void* bytes, const size_t size) {
- if (b == nullptr || key == nullptr || bytes == nullptr || size == 0)
+ if (b == nullptr || key == nullptr || strlen(key) == 0)
return BUNDLE_ERROR_INVALID_PARAMETER;
auto* h = reinterpret_cast<Bundle*>(b);
auto* p = reinterpret_cast<const unsigned char*>(bytes);
- std::vector<unsigned char> value(p, p + size);
+ std::vector<unsigned char> value;
+ if (bytes)
+ std::copy(p, p + size, std::back_inserter(value));
- std::shared_ptr<KeyInfo> key_info(
- new (std::nothrow) KeyInfo(BUNDLE_TYPE_BYTE, key, value));
- if (key_info.get() == nullptr)
+ KeyInfo* key_info;
+ try {
+ key_info = new KeyInfo(BUNDLE_TYPE_BYTE, key, value);
+ } catch (Exception& e) {
+ return e.GetErrorCode();
+ } catch (const std::bad_alloc& ba) {
return BUNDLE_ERROR_OUT_OF_MEMORY;
+ }
try {
- h->Add(std::move(key_info));
+ h->Add(std::shared_ptr<KeyInfo>(key_info));
} catch (Exception& e) {
return e.GetErrorCode();
}
extern "C" EXPORT_API int bundle_get_byte(bundle* b, const char* key,
void** bytes, size_t* size) {
- if (b == nullptr || key == nullptr || bytes == nullptr || size == 0)
+ if (b == nullptr || key == nullptr)
return BUNDLE_ERROR_INVALID_PARAMETER;
auto* h = reinterpret_cast<Bundle*>(b);
if (key_info->GetType() != BUNDLE_TYPE_BYTE)
return BUNDLE_ERROR_INVALID_PARAMETER;
- auto& values = const_cast<std::vector<std::unique_ptr<unsigned char[]>>&>(
- key_info->GetValues());
- auto& value = values[0];
- *bytes = reinterpret_cast<void*>(&value[0]);
- auto& values_size = key_info->GetValuesSize();
- *size = reinterpret_cast<size_t>(values_size[0]);
+ if (bytes) {
+ auto& values = const_cast<std::vector<std::unique_ptr<unsigned char[]>>&>(
+ key_info->GetValues());
+ if (values.size() == 0) {
+ *bytes = nullptr;
+ } else {
+ auto& value = values[0];
+ *bytes = reinterpret_cast<void*>(&value[0]);
+ }
+ }
+
+ if (size) {
+ auto& values_size = key_info->GetValuesSize();
+ *size = reinterpret_cast<size_t>(values_size[0]);
+ }
return BUNDLE_ERROR_NONE;
}
extern "C" EXPORT_API int bundle_init_byte_array(bundle* b,
const char* key, const unsigned int len) {
- if (b == nullptr || key == nullptr || len <= 0)
+ if (b == nullptr || key == nullptr || strlen(key) == 0)
return BUNDLE_ERROR_INVALID_PARAMETER;
auto* h = reinterpret_cast<Bundle*>(b);
- std::vector<std::vector<unsigned char>> values(len);
- auto key_info = std::shared_ptr<KeyInfo>(
- new (std::nothrow) KeyInfo(Bundle::Type::ByteArray, key, values));
- if (key_info.get() == nullptr)
+
+ KeyInfo* key_info;
+ try {
+ std::vector<std::vector<unsigned char>> values(len);
+ key_info = new KeyInfo(Bundle::Type::ByteArray, key, values);
+ } catch (Exception& e) {
+ return e.GetErrorCode();
+ } catch (const std::bad_alloc& ba) {
return BUNDLE_ERROR_OUT_OF_MEMORY;
+ }
try {
- h->Add(key_info);
+ h->Add(std::shared_ptr<KeyInfo>(key_info));
} catch (Exception& e) {
return e.GetErrorCode();
}
extern "C" EXPORT_API int bundle_get_byte_array(bundle* b,
const char* key, void*** bytes_array, unsigned int* len,
unsigned int** array_element_size) {
- if (b == nullptr || key == nullptr || bytes_array == nullptr ||
- len == nullptr || array_element_size == nullptr)
+ if (b == nullptr || key == nullptr)
return BUNDLE_ERROR_INVALID_PARAMETER;
auto* h = reinterpret_cast<Bundle*>(b);
auto& raw_values = const_cast<std::vector<std::unique_ptr<unsigned char[]>>&>(
key_info->GetValues());
- auto** values = reinterpret_cast<unsigned char**>(&raw_values[0]);
- *bytes_array = reinterpret_cast<void**>(values);
- *len = static_cast<unsigned int>(raw_values.size());
- auto& raw_values_size = const_cast<std::vector<unsigned int>&>(
- key_info->GetUValuesSize());
- *array_element_size = reinterpret_cast<unsigned int*>(&raw_values_size[0]);
+ if (bytes_array) {
+ auto** values = reinterpret_cast<unsigned char**>(&raw_values[0]);
+ *bytes_array = reinterpret_cast<void**>(values);
+ }
+
+ if (len)
+ *len = static_cast<unsigned int>(raw_values.size());
+
+ if (array_element_size) {
+ auto& raw_values_size = const_cast<std::vector<unsigned int>&>(
+ key_info->GetUValuesSize());
+ *array_element_size = reinterpret_cast<unsigned int*>(&raw_values_size[0]);
+ }
return BUNDLE_ERROR_NONE;
}
extern "C" EXPORT_API int bundle_set_byte_array_element(bundle* b,
const char* key, const unsigned int idx,
const void* bytes, const size_t size) {
- if (b == nullptr || key == nullptr || bytes == nullptr || size <= 0)
+ if (b == nullptr || key == nullptr || size <= 0)
return BUNDLE_ERROR_INVALID_PARAMETER;
- auto* p = reinterpret_cast<unsigned char*>(const_cast<void*>(bytes));
- std::vector<unsigned char> value(p, p + size);
+ std::vector<unsigned char> value;
+ if (bytes) {
+ auto* p = reinterpret_cast<unsigned char*>(const_cast<void*>(bytes));
+ std::copy(p, p + size, std::back_inserter(value));
+ }
auto* h = reinterpret_cast<Bundle*>(b);
try {