From d12c9a00db64ca64df8e477e9305de1fe230e80a Mon Sep 17 00:00:00 2001 From: Keegan Date: Thu, 27 Oct 2016 14:55:00 -0700 Subject: [PATCH] [WIP] - Simplify reading and writing of settings --- src/c/clay.c | 62 +++++----------------------------------------------- 1 file changed, 6 insertions(+), 56 deletions(-) diff --git a/src/c/clay.c b/src/c/clay.c index 025acdd..e5f5559 100644 --- a/src/c/clay.c +++ b/src/c/clay.c @@ -11,31 +11,7 @@ static uint32_t prv_hash_key(const char *key) { static bool prv_write(const char *key, const SimpleDictDataType type, const void *data, size_t data_size, void *context) { uint32_t persist_key = prv_hash_key(key); - - switch (type) { - case SimpleDictDataType_Raw: - persist_write_data(persist_key, data, data_size); - return true; - case SimpleDictDataType_Bool: { - const bool value = *((bool *) data); - persist_write_bool(persist_key, value); - return true; - } - case SimpleDictDataType_Int: { - const int value = *((int *) data); - persist_write_int(persist_key, value); - return true; - } - case SimpleDictDataType_String: { - const char *value = data; - persist_write_string(persist_key, value); - return true; - } - case SimpleDictDataTypeCount: - break; - } - APP_LOG(APP_LOG_LEVEL_ERROR, "Unexpected type %d", type); - return false; + return persist_write_data(persist_key, data, data_size) != E_DOES_NOT_EXIST; } static void prv_simple_app_message_received_callback(const SimpleDict *message, void *context) { @@ -48,35 +24,9 @@ static void prv_simple_app_message_received_callback(const SimpleDict *message, s_callbacks.settings_updated(context); } -static bool prv_read(SimpleDictDataType type, const char *key, void *value_out, size_t size) { +static bool prv_read(const char *key, void *value_out, size_t size) { uint32_t persist_key = prv_hash_key(key); - - if (persist_exists(persist_key)) { - - switch (type) { - case SimpleDictDataType_Raw: -// persist_write_data(persist_key, data, data_size); - return true; - case SimpleDictDataType_Bool: { - const bool value = persist_read_bool(persist_key); - memcpy(value_out, &value, size); - return true; - } - case SimpleDictDataType_Int: { - const int value = persist_read_int(persist_key); - memcpy(value_out, &value, size); - return true; - } - case SimpleDictDataType_String: { - persist_read_string(persist_key, value_out, size); - return true; - } - case SimpleDictDataTypeCount: - break; - } - } - return false; - + return persist_read_data(persist_key, value_out, size) != E_DOES_NOT_EXIST; } // ----- PUBLIC API ----- @@ -105,13 +55,13 @@ void clay_init(uint32_t inbox_size, const ClayCallbacks *callbacks, void *contex } bool clay_get_int(const char *key, int *value_out) { - return prv_read(SimpleDictDataType_Int, key, value_out, sizeof(&value_out)); + return prv_read(key, value_out, sizeof(*value_out)); } bool clay_get_bool(const char *key, bool *value_out) { - return prv_read(SimpleDictDataType_Bool, key, value_out, sizeof(&value_out)); + return prv_read(key, value_out, sizeof(*value_out)); } bool clay_get_string(const char *key, char *value_out, size_t size) { - return prv_read(SimpleDictDataType_String, key, value_out, size); + return prv_read(key, value_out, size); }