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:
lbernstone
2020-11-03 08:03:04 -07:00
committed by GitHub
parent 56a7ae8712
commit e2452c0dfc
2 changed files with 32 additions and 2 deletions

View File

@ -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
* */