* limitations under the License.
*/
+#include "include/app_preference.h"
+#include "preference/exception-internal.hh"
+#include "preference/log-internal.hh"
#include "preference/preference-internal.hh"
namespace preference {
int Preference::GetInt(const std::string& key) {
auto d = backend_->Get(key);
if (d == nullptr)
- return -1;
+ THROW(PREFERENCE_ERROR_IO_ERROR);
+
+ auto type = static_cast<preference_type_e>(d->GetType());
+ if (type == PREFERENCE_TYPE_INT) {
+ set_last_result(PREFERENCE_ERROR_NONE);
+ } else {
+ _E("The type(%d) of keynode(%s) is not integer",
+ static_cast<int>(type), key.c_str());
+ set_last_result(PREFERENCE_ERROR_INVALID_PARAMETER);
+ }
return d->GetInt();
}
double Preference::GetDouble(const std::string& key) {
auto d = backend_->Get(key);
if (d == nullptr)
- return -1;
+ THROW(PREFERENCE_ERROR_IO_ERROR);
+
+ auto type = static_cast<preference_type_e>(d->GetType());
+ if (type == PREFERENCE_TYPE_DOUBLE) {
+ set_last_result(PREFERENCE_ERROR_NONE);
+ } else {
+ _E("The type(%d) of keynode(%s) is not double",
+ static_cast<int>(type), key.c_str());
+ set_last_result(PREFERENCE_ERROR_INVALID_PARAMETER);
+ }
return d->GetDouble();
}
std::string Preference::GetString(const std::string& key) {
auto d = backend_->Get(key);
if (d == nullptr)
- return std::string("");
+ THROW(PREFERENCE_ERROR_IO_ERROR);
+
+ auto type = static_cast<preference_type_e>(d->GetType());
+ if (type == PREFERENCE_TYPE_STRING) {
+ set_last_result(PREFERENCE_ERROR_NONE);
+ } else {
+ _E("The type(%d) of keynode(%s) is not string",
+ static_cast<int>(type), key.c_str());
+ set_last_result(PREFERENCE_ERROR_INVALID_PARAMETER);
+ }
return d->GetString();
}
bool Preference::GetBoolean(const std::string& key) {
auto d = backend_->Get(key);
if (d == nullptr)
- return false;
+ THROW(PREFERENCE_ERROR_IO_ERROR);
+
+ auto type = static_cast<preference_type_e>(d->GetType());
+ if (type == PREFERENCE_TYPE_BOOLEAN) {
+ set_last_result(PREFERENCE_ERROR_NONE);
+ } else {
+ _E("The type(%d) of keynode(%s) is not boolean",
+ static_cast<int>(type), key.c_str());
+ set_last_result(PREFERENCE_ERROR_INVALID_PARAMETER);
+ }
return d->GetBoolean();
}
#include <thread>
#include "include/app_preference.h"
+#include "preference/exception-internal.hh"
#include "preference/log-internal.hh"
#include "preference/preference-internal.hh"
if (!pref.IsExisting(key))
return PREFERENCE_ERROR_NO_KEY;
- *intval = pref.GetInt(key);
+ try {
+ *intval = pref.GetInt(key);
+ } catch (const preference::internal::Exception& e) {
+ return e.GetErrorCode();
+ }
+
SECURE_LOGD("key(%s), value(%d)", key, *intval);
- return PREFERENCE_ERROR_NONE;
+ return get_last_result();
}
extern "C" API int preference_get_boolean(const char* key, bool* boolval) {
if (!pref.IsExisting(key))
return PREFERENCE_ERROR_NO_KEY;
- *boolval = pref.GetBoolean(key);
+ try {
+ *boolval = pref.GetBoolean(key);
+ } catch (const preference::internal::Exception& e) {
+ return e.GetErrorCode();
+ }
+
SECURE_LOGD("key(%s), value(%d)", key, *boolval);
- return PREFERENCE_ERROR_NONE;
+ return get_last_result();
}
extern "C" API int preference_get_double(const char* key, double* dblval) {
if (!pref.IsExisting(key))
return PREFERENCE_ERROR_NO_KEY;
- *dblval = pref.GetDouble(key);
+ try {
+ *dblval = pref.GetDouble(key);
+ } catch (const preference::internal::Exception& e) {
+ return e.GetErrorCode();
+ }
+
SECURE_LOGD("key(%s), value(%lf)", key, *dblval);
- return PREFERENCE_ERROR_NONE;
+ return get_last_result();
}
extern "C" API int preference_get_string(const char* key, char** value) {
if (!pref.IsExisting(key))
return PREFERENCE_ERROR_NO_KEY;
- *value = strdup(pref.GetString(key).c_str());
+ try {
+ *value = strdup(pref.GetString(key).c_str());
+ } catch (const preference::internal::Exception& e) {
+ return e.GetErrorCode();
+ }
+
SECURE_LOGD("key(%s), value(%s)", key, *value);
- return PREFERENCE_ERROR_NONE;
+ return get_last_result();
}
extern "C" API int preference_remove(const char* key) {
TEST_F(PreferenceUnitTest, preference_get_int_N) {
int ret = preference_get_int(nullptr, nullptr);
ASSERT_EQ(ret, PREFERENCE_ERROR_INVALID_PARAMETER);
+
+ ret = preference_set_string("key:string", "value");
+ ASSERT_EQ(ret, PREFERENCE_ERROR_NONE);
+ int value;
+ ret = preference_get_int("key:string", &value);
+ ASSERT_EQ(ret, PREFERENCE_ERROR_INVALID_PARAMETER);
}
TEST_F(PreferenceUnitTest, preference_set_double_P) {
TEST_F(PreferenceUnitTest, preference_get_double_N) {
int ret = preference_get_double(nullptr, nullptr);
ASSERT_EQ(ret, PREFERENCE_ERROR_INVALID_PARAMETER);
+
+ ret = preference_set_int("key:int", 100);
+ ASSERT_EQ(ret, PREFERENCE_ERROR_NONE);
+ double value;
+ ret = preference_get_double("key:int", &value);
+ ASSERT_EQ(ret, PREFERENCE_ERROR_INVALID_PARAMETER);
}
TEST_F(PreferenceUnitTest, preference_set_string_P) {
TEST_F(PreferenceUnitTest, preference_get_string_N) {
int ret = preference_get_string(nullptr, nullptr);
ASSERT_EQ(ret, PREFERENCE_ERROR_INVALID_PARAMETER);
+
+ ret = preference_set_int("key:int", 101);
+ ASSERT_EQ(ret, PREFERENCE_ERROR_NONE);
+ char* value = nullptr;
+ ret = preference_get_string("key:int", &value);
+ free(value);
+ ASSERT_EQ(ret, PREFERENCE_ERROR_INVALID_PARAMETER);
}
TEST_F(PreferenceUnitTest, preference_set_boolean_P) {
TEST_F(PreferenceUnitTest, preference_get_boolean_N) {
int ret = preference_get_boolean(nullptr, nullptr);
ASSERT_EQ(ret, PREFERENCE_ERROR_INVALID_PARAMETER);
+
+ ret = preference_set_int("key:int", 102);
+ ASSERT_EQ(ret, PREFERENCE_ERROR_NONE);
+ bool value;
+ ret = preference_get_boolean("key:int", &value);
+ ASSERT_EQ(ret, PREFERENCE_ERROR_INVALID_PARAMETER);
+
}
TEST_F(PreferenceUnitTest, preference_remove_P) {