Skip to content

Commit e2452c0

Browse files
authored
Added isKey method to Preferences (#4441)
Checks to see if a string is a key in the namespace. Fixes #4440
1 parent 56a7ae8 commit e2452c0

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

libraries/Preferences/src/Preferences.cpp

+25-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
#include "nvs.h"
1717

18-
const char * nvs_errors[] = { "OTHER", "NOT_INITIALIZED", "NOT_FOUND", "TYPE_MISMATCH", "READ_ONLY", "NOT_ENOUGH_SPACE", "INVALID_NAME", "INVALID_HANDLE", "REMOVE_FAILED", "KEY_TOO_LONG", "PAGE_FULL", "INVALID_STATE", "INVALID_LENGHT"};
18+
const char * nvs_errors[] = { "OTHER", "NOT_INITIALIZED", "NOT_FOUND", "TYPE_MISMATCH", "READ_ONLY", "NOT_ENOUGH_SPACE", "INVALID_NAME", "INVALID_HANDLE", "REMOVE_FAILED", "KEY_TOO_LONG", "PAGE_FULL", "INVALID_STATE", "INVALID_LENGTH"};
1919
#define nvs_error(e) (((e)>ESP_ERR_NVS_BASE)?nvs_errors[(e)&~(ESP_ERR_NVS_BASE)]:nvs_errors[0])
2020

2121
Preferences::Preferences()
@@ -280,6 +280,30 @@ size_t Preferences::putBytes(const char* key, const void* value, size_t len){
280280
return len;
281281
}
282282

283+
PreferenceType Preferences::getType(const char* key) {
284+
if(!_started || !key || strlen(key)>15){
285+
return PT_INVALID;
286+
}
287+
int8_t mt1; uint8_t mt2; int16_t mt3; uint16_t mt4;
288+
int32_t mt5; uint32_t mt6; int64_t mt7; uint64_t mt8;
289+
size_t len = 0;
290+
if(nvs_get_i8(_handle, key, &mt1) == ESP_OK) return PT_I8;
291+
if(nvs_get_u8(_handle, key, &mt2) == ESP_OK) return PT_U8;
292+
if(nvs_get_i16(_handle, key, &mt3) == ESP_OK) return PT_I16;
293+
if(nvs_get_u16(_handle, key, &mt4) == ESP_OK) return PT_U16;
294+
if(nvs_get_i32(_handle, key, &mt5) == ESP_OK) return PT_I32;
295+
if(nvs_get_u32(_handle, key, &mt6) == ESP_OK) return PT_U32;
296+
if(nvs_get_i64(_handle, key, &mt7) == ESP_OK) return PT_I64;
297+
if(nvs_get_u64(_handle, key, &mt8) == ESP_OK) return PT_U64;
298+
if(nvs_get_str(_handle, key, NULL, &len) == ESP_OK) return PT_STR;
299+
if(nvs_get_blob(_handle, key, NULL, &len) == ESP_OK) return PT_BLOB;
300+
return PT_INVALID;
301+
}
302+
303+
bool Preferences::isKey(const char* key) {
304+
return getType(key) != PT_INVALID;
305+
}
306+
283307
/*
284308
* Get a key value
285309
* */

libraries/Preferences/src/Preferences.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
#include "Arduino.h"
1818

19+
typedef enum {
20+
PT_I8, PT_U8, PT_I16, PT_U16, PT_I32, PT_U32, PT_I64, PT_U64, PT_STR, PT_BLOB, PT_INVALID
21+
} PreferenceType;
22+
1923
class Preferences {
2024
protected:
2125
uint32_t _handle;
@@ -48,6 +52,8 @@ class Preferences {
4852
size_t putString(const char* key, String value);
4953
size_t putBytes(const char* key, const void* value, size_t len);
5054

55+
bool isKey(const char* key);
56+
PreferenceType getType(const char* key);
5157
int8_t getChar(const char* key, int8_t defaultValue = 0);
5258
uint8_t getUChar(const char* key, uint8_t defaultValue = 0);
5359
int16_t getShort(const char* key, int16_t defaultValue = 0);
@@ -63,7 +69,7 @@ class Preferences {
6369
bool getBool(const char* key, bool defaultValue = false);
6470
size_t getString(const char* key, char* value, size_t maxLen);
6571
String getString(const char* key, String defaultValue = String());
66-
size_t getBytesLength(const char* key);
72+
size_t getBytesLength(const char* key);
6773
size_t getBytes(const char* key, void * buf, size_t maxLen);
6874
size_t freeEntries();
6975
};

0 commit comments

Comments
 (0)