diff --git a/library.json b/library.json index 26410ce..6ec9f81 100644 --- a/library.json +++ b/library.json @@ -12,7 +12,7 @@ "type": "git", "url": "https://git.xpablo.cz/xPablo.cz/sysvars.git" }, - "version": "0.0.3", + "version": "1.0.0", "license": "MIT", "frameworks": "arduino", "platforms": ["espressif8266", "espressif32"], diff --git a/library.properties b/library.properties index 7826aff..5bd1db5 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=sysvars -version=0.0.2 +version=1.0.0 author=Pavel Brychta maintainer=Pavel Brychta sentence=Key-Value JSON file storage library. diff --git a/src/definefs.hpp b/src/definefs.hpp index e9cb4af..5acf39d 100644 --- a/src/definefs.hpp +++ b/src/definefs.hpp @@ -2,13 +2,13 @@ #ifndef USEDFS # if defined(ESP32) # ifdef USE_LITTLEFS -# if ESP_IDF_VERSION_MAJOR < 4 -# include -# define USEDFS LITTLEFS -# else +//# if ESP_IDF_VERSION_MAJOR < 4 +//# include +//# define USEDFS LITTLEFS +//# else # include # define USEDFS LittleFS -# endif +//# endif # else # include # define USEDFS SPIFFS diff --git a/src/sysvars.cpp b/src/sysvars.cpp new file mode 100644 index 0000000..a309889 --- /dev/null +++ b/src/sysvars.cpp @@ -0,0 +1,159 @@ +/** Sysvars library + * Copyright (c) 2018-2024 Pavel Brychta, pablo@xpablo.cz + * + * Key-value store/load library with JSON file using internal filesystem + **/ + +#include "definefs.hpp" +#include "sysvars.hpp" +#include +#include +#include + +#ifndef TRACE_SYSVARS +# define TRACE_SYSVARS 0 +#endif + +#if not defined(SYSVAR_FILE) + #define SYSVAR_FILE "/cfg.json" +#endif + +template +T svGetV(const String & name, const T deflt) +{ + T rv = deflt; + + File f = USEDFS.open(F(SYSVAR_FILE), "r"); + if (f) { + JsonDocument doc; + DeserializationError err = deserializeJson(doc, f); + + if (err) { + TRACEPLUS(TRACE_SYSVARS, TRACE_ERROR, "SV: Deserialize error '%s'", err.c_str()); // wrong JSON file (malformed) + } else { + if (!doc.containsKey(name)) { + TRACEPLUS(TRACE_SYSVARS, TRACE_ERROR, "SV: Key '%s' not found", name.c_str()); + } else { + rv = doc[name].as(); + } + } + } else { + TRACEPLUS(TRACE_SYSVARS, TRACE_ERROR, "SV: File not found"); + } + return rv; +} + +template +T svGetVFile(const String & name, const String & fname, const T deflt) +{ + T rv = deflt; + File f = USEDFS.open(fname, "r"); + + if (f) { + JsonDocument doc; + DeserializationError err = deserializeJson(doc, f); + + if (err) { + TRACEPLUS(TRACE_SYSVARS, TRACE_ERROR, "SV: Deserialize error '%s'", err.c_str()); // wrong JSON file (malformed) + } else { + if (!doc.containsKey(name)) { + TRACEPLUS(TRACE_SYSVARS, TRACE_ERROR, "SV: Key '%s' not found", name.c_str()); + } else { + rv = doc[name].as(); + } + } + } else { + TRACEPLUS(TRACE_SYSVARS, TRACE_ERROR, "SV: File not found '%s'", fname.c_str()); + } + return rv; +} + +/** + * @brief Store value for the key + * + * @param[in] name Key name + * @param[in] value New value + * + * @tparam T Type of the value + * + * @return Result (SV_...) + */ +template +int svSetV(const String & name, T value) +{ + int res; + JsonDocument doc; + File f = USEDFS.open(F(SYSVAR_FILE), "r"); + + if (f) { + DeserializationError err = deserializeJson(doc, f); + if (err) { + res = SV_DESERIALIZE_ERROR; // wrong (malformed) JSON file + } + f.close(); + } + doc[name] = value; + + f = USEDFS.open(SYSVAR_FILE, "w"); + if (f) { + serializeJson(doc, f); + res = SV_OK; + } else { + res = SV_FILE_WRITE_ERROR; + } + return res; +} + +/** + * @brief Store value for the key + * + * @param[in] name Key name + * @param[in] value New value + * @param[in] fname JSON file name + * + * @tparam T Type of the value + * + * @return Result (SV_...) + */ +template +int svSetVFile(const String & name, T value, const String & fname) +{ + int res = SV_UNSPECIFIED_ERROR; + JsonDocument doc; + File f = USEDFS.open(fname, "r"); + + if (f) { + DeserializationError err = deserializeJson(doc, f); + if (err) { + res = SV_DESERIALIZE_ERROR; // wrong (malformed) JSON file + } + f.close(); + } + doc[name] = value; + + f = USEDFS.open(fname, "w"); + if (f) { + serializeJson(doc, f); + res = SV_OK; + } else { + res = SV_FILE_WRITE_ERROR; + } + + return res; +} + +template int svGetV(const String & name, const int deflt); +template String svGetV(const String & name, const String deflt); +template unsigned svGetV(const String & name, const unsigned deflt); + +template int svGetVFile(const String & name, const String & fname, const int deflt); +template String svGetVFile(const String & name, const String & fname, const String deflt); +template unsigned svGetVFile(const String & name, const String & fname, const unsigned deflt); + +template int svSetV(const String & name, int value); +template int svSetV(const String & name, unsigned value); +template int svSetV(const String & name, String value); + +template int svSetVFile(const String & name, int value, const String & fname); +template int svSetVFile(const String & name, unsigned value, const String & fname); +template int svSetVFile(const String & name, String value, const String & fname); diff --git a/src/sysvars.hpp b/src/sysvars.hpp index c1f1d12..f966674 100644 --- a/src/sysvars.hpp +++ b/src/sysvars.hpp @@ -1,14 +1,11 @@ #pragma once /** Sysvars library - * Copyright (c) 2018-2023 Pavel Brychta, pablo@xpablo.cz + * Copyright (c) 2018-2024 Pavel Brychta, pablo@xpablo.cz * * Key-value store/load library with JSON file using internal filesystem **/ -#include "definefs.hpp" #include -#include -#include enum { SV_OK = 0, @@ -20,88 +17,32 @@ enum { SV_KEY_NOT_FOUND = -6, }; -#if not defined(SYSVAR_FILE) - #define SYSVAR_FILE "/cfg.json" -#endif - /** * @brief Get value * * @param[in] name Name of the key - * @param result Status variable (SV_...) + * @param deflt Default value * * @tparam T result type * * @return Value for the key given */ template -T svGetV(const String & name, int * result = nullptr) -{ - T rv{}; - int res; - JsonDocument doc; - File f; - - f = USEDFS.open(F(SYSVAR_FILE), "r"); - if (f) { - DeserializationError err = deserializeJson(doc, f); - if (err) { - res = SV_DESERIALIZE_ERROR; // wrong JSON file (malformed) - } else { - if (!doc.containsKey(name)) { - res = SV_KEY_NOT_FOUND; // key not found - } else { - rv = doc[name].as(); - res = SV_OK; - } - } - } else { - res = SV_FILE_NOT_FOUND; // file not found - } - if (result) - *result = res; // result code if we are interested - return rv; -} +T svGetV(const String & name, const T deflt = {}); /** * @brief Get value * * @param[in] name Name of the key - * @param result Status variable (SV_....) * @param[in] fname Used JSON file name + * @param deflt Default value * * @tparam T Result type * * @return Value for the key given */ template -T svGetV(const String & name, int * result, const String & fname) -{ - T rv{}; - int res; - JsonDocument doc; - File f; - - f = USEDFS.open(fname, "r"); - if (f) { - DeserializationError err = deserializeJson(doc, f); - if (err) { - res = SV_DESERIALIZE_ERROR; // wrong (malformed) JSON file - } else { - if (!doc.containsKey(name)) { - res = SV_KEY_NOT_FOUND; // key not found - } else { - rv = doc[name].as(); - res = SV_OK; - } - } - } else { - res = SV_FILE_NOT_FOUND; // file not found - } - if (result) - *result = res; // result code if we are interested - return rv; -} +T svGetVFile(const String & name, const String & fname, const T deflt = {}); /** * @brief Store value for the key @@ -114,31 +55,7 @@ T svGetV(const String & name, int * result, const String & fname) * @return Result (SV_...) */ template -int svSetV(const String & name, T value) -{ - int res; - JsonDocument doc; - File f; - - f = USEDFS.open(F(SYSVAR_FILE), "r"); - if (f) { - DeserializationError err = deserializeJson(doc, f); - if (err) { - res = SV_DESERIALIZE_ERROR; // wrong (malformed) JSON file - } - f.close(); - } - doc[name] = value; - - f = USEDFS.open(SYSVAR_FILE, "w"); - if (f) { - serializeJson(doc, f); - res = SV_OK; - } else { - res = SV_FILE_WRITE_ERROR; - } - return res; -} +int svSetV(const String & name, T value); /** * @brief Store value for the key @@ -152,29 +69,4 @@ int svSetV(const String & name, T value) * @return Result (SV_...) */ template -int svSetV(const String & name, T value, const String & fname) -{ - int res = SV_UNSPECIFIED_ERROR; - JsonDocument doc; - File f; - - f = USEDFS.open(fname, "r"); - if (f) { - DeserializationError err = deserializeJson(doc, f); - if (err) { - res = SV_DESERIALIZE_ERROR; // wrong (malformed) JSON file - } - f.close(); - } - doc[name] = value; - - f = USEDFS.open(fname, "w"); - if (f) { - serializeJson(doc, f); - res = SV_OK; - } else { - res = SV_FILE_WRITE_ERROR; - } - - return res; -} +int svSetVFile(const String & name, T value, const String & fname);