#include <sys/types.h>
#include <unistd.h>
+#include <new>
+
#include "preference/file-internal.hh"
#include "preference/log-internal.hh"
#include "preference/path-internal.hh"
std::copy(&raw_data_[index], &raw_data_[index] + sizeof(int), p);
index += sizeof(int);
- auto data = std::make_unique<Data>(key);
- if (data.get() == nullptr) {
- _E("Out of memory");
- return nullptr;
- }
+ try {
+ auto data = std::make_unique<Data>(key);
+ Data::Type data_type = static_cast<Data::Type>(type);
+ if (data_type == Data::Type::STRING) {
+ if (index == raw_data_.size() || raw_data_[raw_data_.size() - 1] != '\0')
+ raw_data_.push_back(0x00);
+
+ const char* str = reinterpret_cast<const char*>(&raw_data_[index]);
+ data->SetString(str);
+ } else if (data_type == Data::Type::INT) {
+ if (index + sizeof(int) > raw_data_.size())
+ return nullptr;
+
+ int i = 0;
+ p = reinterpret_cast<uint8_t*>(&i);
+ std::copy(&raw_data_[index], &raw_data_[index] + sizeof(int), p);
+ data->SetInt(i);
+ } else if (data_type == Data::Type::DOUBLE) {
+ if (index + sizeof(double) > raw_data_.size())
+ return nullptr;
+
+ double d = 0;
+ p = reinterpret_cast<uint8_t*>(&d);
+ std::copy(&raw_data_[index], &raw_data_[index] + sizeof(double), p);
+ data->SetDouble(d);
+ } else if (data_type == Data::Type::BOOLEAN) {
+ if (index + sizeof(bool) > raw_data_.size())
+ return nullptr;
+
+ bool b = 0;
+ p = reinterpret_cast<uint8_t*>(&b);
+ std::copy(&raw_data_[index], &raw_data_[index] + sizeof(bool), p);
+ data->SetBoolean(b);
+ }
- Data::Type data_type = static_cast<Data::Type>(type);
- if (data_type == Data::Type::STRING) {
- if (index == raw_data_.size() || raw_data_[raw_data_.size() - 1] != '\0')
- raw_data_.push_back(0x00);
-
- const char* str = reinterpret_cast<const char*>(&raw_data_[index]);
- data->SetString(str);
- } else if (data_type == Data::Type::INT) {
- if (index + sizeof(int) > raw_data_.size())
- return nullptr;
-
- int i = 0;
- p = reinterpret_cast<uint8_t*>(&i);
- std::copy(&raw_data_[index], &raw_data_[index] + sizeof(int), p);
- data->SetInt(i);
- } else if (data_type == Data::Type::DOUBLE) {
- if (index + sizeof(double) > raw_data_.size())
- return nullptr;
-
- double d = 0;
- p = reinterpret_cast<uint8_t*>(&d);
- std::copy(&raw_data_[index], &raw_data_[index] + sizeof(double), p);
- data->SetDouble(d);
- } else if (data_type == Data::Type::BOOLEAN) {
- if (index + sizeof(bool) > raw_data_.size())
- return nullptr;
-
- bool b = 0;
- p = reinterpret_cast<uint8_t*>(&b);
- std::copy(&raw_data_[index], &raw_data_[index] + sizeof(bool), p);
- data->SetBoolean(b);
+ return data;
+ } catch (const std::bad_alloc& e) {
+ _E("Exception occurs. error=%s", e.what());
+ return nullptr;
}
-
- return data;
}
int File::Write() {