Added isKey method to Preferences (#4441)
Checks to see if a string is a key in the namespace. Fixes #4440
This commit is contained in:
parent
56a7ae8712
commit
e2452c0dfc
@ -15,7 +15,7 @@
|
||||
|
||||
#include "nvs.h"
|
||||
|
||||
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"};
|
||||
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"};
|
||||
#define nvs_error(e) (((e)>ESP_ERR_NVS_BASE)?nvs_errors[(e)&~(ESP_ERR_NVS_BASE)]:nvs_errors[0])
|
||||
|
||||
Preferences::Preferences()
|
||||
@ -280,6 +280,30 @@ size_t Preferences::putBytes(const char* key, const void* value, size_t len){
|
||||
return len;
|
||||
}
|
||||
|
||||
PreferenceType Preferences::getType(const char* key) {
|
||||
if(!_started || !key || strlen(key)>15){
|
||||
return PT_INVALID;
|
||||
}
|
||||
int8_t mt1; uint8_t mt2; int16_t mt3; uint16_t mt4;
|
||||
int32_t mt5; uint32_t mt6; int64_t mt7; uint64_t mt8;
|
||||
size_t len = 0;
|
||||
if(nvs_get_i8(_handle, key, &mt1) == ESP_OK) return PT_I8;
|
||||
if(nvs_get_u8(_handle, key, &mt2) == ESP_OK) return PT_U8;
|
||||
if(nvs_get_i16(_handle, key, &mt3) == ESP_OK) return PT_I16;
|
||||
if(nvs_get_u16(_handle, key, &mt4) == ESP_OK) return PT_U16;
|
||||
if(nvs_get_i32(_handle, key, &mt5) == ESP_OK) return PT_I32;
|
||||
if(nvs_get_u32(_handle, key, &mt6) == ESP_OK) return PT_U32;
|
||||
if(nvs_get_i64(_handle, key, &mt7) == ESP_OK) return PT_I64;
|
||||
if(nvs_get_u64(_handle, key, &mt8) == ESP_OK) return PT_U64;
|
||||
if(nvs_get_str(_handle, key, NULL, &len) == ESP_OK) return PT_STR;
|
||||
if(nvs_get_blob(_handle, key, NULL, &len) == ESP_OK) return PT_BLOB;
|
||||
return PT_INVALID;
|
||||
}
|
||||
|
||||
bool Preferences::isKey(const char* key) {
|
||||
return getType(key) != PT_INVALID;
|
||||
}
|
||||
|
||||
/*
|
||||
* Get a key value
|
||||
* */
|
||||
|
@ -16,6 +16,10 @@
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
typedef enum {
|
||||
PT_I8, PT_U8, PT_I16, PT_U16, PT_I32, PT_U32, PT_I64, PT_U64, PT_STR, PT_BLOB, PT_INVALID
|
||||
} PreferenceType;
|
||||
|
||||
class Preferences {
|
||||
protected:
|
||||
uint32_t _handle;
|
||||
@ -48,6 +52,8 @@ class Preferences {
|
||||
size_t putString(const char* key, String value);
|
||||
size_t putBytes(const char* key, const void* value, size_t len);
|
||||
|
||||
bool isKey(const char* key);
|
||||
PreferenceType getType(const char* key);
|
||||
int8_t getChar(const char* key, int8_t defaultValue = 0);
|
||||
uint8_t getUChar(const char* key, uint8_t defaultValue = 0);
|
||||
int16_t getShort(const char* key, int16_t defaultValue = 0);
|
||||
@ -63,7 +69,7 @@ class Preferences {
|
||||
bool getBool(const char* key, bool defaultValue = false);
|
||||
size_t getString(const char* key, char* value, size_t maxLen);
|
||||
String getString(const char* key, String defaultValue = String());
|
||||
size_t getBytesLength(const char* key);
|
||||
size_t getBytesLength(const char* key);
|
||||
size_t getBytes(const char* key, void * buf, size_t maxLen);
|
||||
size_t freeEntries();
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user