From 69d97d4df336127c922cc26c6094b6f42ef7b047 Mon Sep 17 00:00:00 2001 From: pablo2048 Date: Fri, 16 Jun 2023 16:28:55 +0200 Subject: [PATCH] Verze 0.0.2 - komentare anglicky a upravy, doporucene clang-tidy --- library.json | 2 +- library.properties | 2 +- src/sysvars.hpp | 108 ++++++++++++++++++++++----------------------- 3 files changed, 55 insertions(+), 57 deletions(-) diff --git a/library.json b/library.json index 01cf550..d3cc8db 100644 --- a/library.json +++ b/library.json @@ -18,7 +18,7 @@ "version":"https://github.com/bblanchon/ArduinoJson.git" } ], - "version": "0.0.1", + "version": "0.0.2", "license": "MIT", "frameworks": "arduino", "platforms": ["espressif8266", "espressif32"], diff --git a/library.properties b/library.properties index 4bab057..7826aff 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=sysvars -version=0.0.1 +version=0.0.2 author=Pavel Brychta maintainer=Pavel Brychta sentence=Key-Value JSON file storage library. diff --git a/src/sysvars.hpp b/src/sysvars.hpp index 972d8c2..5dd5eb9 100644 --- a/src/sysvars.hpp +++ b/src/sysvars.hpp @@ -1,14 +1,14 @@ #pragma once -/** Knihovna sysvars - * Copyright (c) 2018-2021 Pavel Brychta, pablo@xpablo.cz +/** Sysvars library + * Copyright (c) 2018-2023 Pavel Brychta, pablo@xpablo.cz * - * Knihovna umoznuje ukladani a vycitani paru hodnot typu klic-hodnota do souboru ve formatu JSON + * Key-value store/load library with JSON file using internal filesystem **/ +#include "definefs.hpp" #include #include #include -#include "definefs.hpp" enum { SV_OK = 0, @@ -21,28 +21,28 @@ enum { }; #if not defined(SYSVAR_FILE) -# define SYSVAR_FILE "/cfg.json" + #define SYSVAR_FILE "/cfg.json" #endif #if not defined(SYSVAR_DOCUMENT_SIZE) -# define SYSVAR_DOCUMENT_SIZE (4096) + #define SYSVAR_DOCUMENT_SIZE (4096) #endif /** - * @brief Ziskani hodnoty + * @brief Get value * - * @param[in] name Jmeno klice - * @param result Promenna pro ulozeni vysledku operace (SV_...) + * @param[in] name Name of the key + * @param result Status variable (SV_...) * - * @tparam T Typ vysledne hodnoty + * @tparam T result type * - * @return Vracena hodnota zadaneho klice + * @return Value for the key given */ template -T svGetV(const String& name, int* result = nullptr) +T svGetV(const String & name, int * result = nullptr) { - T retval{}; - int res = SV_UNSPECIFIED_ERROR; // predpokladame chybu + T rv{}; + int res; DynamicJsonDocument doc(SYSVAR_DOCUMENT_SIZE); File f; @@ -50,40 +50,39 @@ T svGetV(const String& name, int* result = nullptr) if (f) { DeserializationError err = deserializeJson(doc, f); if (err) { - res = SV_DESERIALIZE_ERROR; // spatny JSON soubor + res = SV_DESERIALIZE_ERROR; // wrong JSON file (malformed) } else { - if (!doc.containsKey(name)) - { - res = SV_KEY_NOT_FOUND; // chyba - nenalezeny klic + if (!doc.containsKey(name)) { + res = SV_KEY_NOT_FOUND; // key not found } else { - retval = doc[name].as(); + rv = doc[name].as(); res = SV_OK; } } } else { - res = SV_FILE_NOT_FOUND; // chyba - nenalezeny soubor + res = SV_FILE_NOT_FOUND; // file not found } if (result) - *result = res; // navratovy kod, pokud nas zajima - return retval; + *result = res; // result code if we are interested + return rv; } /** - * @brief Ziskani hodnoty klice + * @brief Get value * - * @param[in] name Jmeno klice - * @param result Promenna pro ulozeni vysledku operace - * @param[in] fname Jmeno souboru s JSON parametry + * @param[in] name Name of the key + * @param result Status variable (SV_....) + * @param[in] fname Used JSON file name * - * @tparam T Typ vysledne hodnoty + * @tparam T Result type * - * @return Hodnota zadaneho klice + * @return Value for the key given */ template -T svGetV(const String& name, int* result, const String& fname) +T svGetV(const String & name, int * result, const String & fname) { - T retval{}; - int res = SV_UNSPECIFIED_ERROR; // predpokladame chybu + T rv{}; + int res; DynamicJsonDocument doc(SYSVAR_DOCUMENT_SIZE); File f; @@ -91,38 +90,37 @@ T svGetV(const String& name, int* result, const String& fname) if (f) { DeserializationError err = deserializeJson(doc, f); if (err) { - res = SV_DESERIALIZE_ERROR; // spatny JSON soubor + res = SV_DESERIALIZE_ERROR; // wrong (malformed) JSON file } else { - if (!doc.containsKey(name)) - { - res = SV_KEY_NOT_FOUND; // chyba - nenalezeny klic + if (!doc.containsKey(name)) { + res = SV_KEY_NOT_FOUND; // key not found } else { - retval = doc[name].as(); + rv = doc[name].as(); res = SV_OK; } } } else { - res = SV_FILE_NOT_FOUND; // chyba - nenalezeny soubor + res = SV_FILE_NOT_FOUND; // file not found } if (result) - *result = res; // navratovy kod, pokud nas zajima - return retval; + *result = res; // result code if we are interested + return rv; } /** - * @brief Ulozeni hodnoty do klice + * @brief Store value for the key * - * @param[in] name Jmeno klice - * @param[in] value Ukladana hodnota + * @param[in] name Key name + * @param[in] value New value * - * @tparam T Typ ukladane hodnoty + * @tparam T Type of the value * - * @return Chybovy stav operace (SV_...) + * @return Result (SV_...) */ template -int svSetV(const String& name, T value) +int svSetV(const String & name, T value) { - int res = SV_UNSPECIFIED_ERROR; + int res; DynamicJsonDocument doc(SYSVAR_DOCUMENT_SIZE); File f; @@ -130,7 +128,7 @@ int svSetV(const String& name, T value) if (f) { DeserializationError err = deserializeJson(doc, f); if (err) { - res = SV_DESERIALIZE_ERROR; // spatny JSON soubor + res = SV_DESERIALIZE_ERROR; // wrong (malformed) JSON file } f.close(); } @@ -147,18 +145,18 @@ int svSetV(const String& name, T value) } /** - * @brief Ulozeni hodnoty klice + * @brief Store value for the key * - * @param[in] name Jmeno klice - * @param[in] value Ukladana hodnota - * @param[in] fname Jmeno souboru s JSON daty + * @param[in] name Key name + * @param[in] value New value + * @param[in] fname JSON file name * - * @tparam T Typ ukladane hodnoty + * @tparam T Type of the value * - * @return Chybovy stav operace (SV_...) + * @return Result (SV_...) */ template -int svSetV(const String& name, T value, const String& fname) +int svSetV(const String & name, T value, const String & fname) { int res = SV_UNSPECIFIED_ERROR; DynamicJsonDocument doc(SYSVAR_DOCUMENT_SIZE); @@ -168,7 +166,7 @@ int svSetV(const String& name, T value, const String& fname) if (f) { DeserializationError err = deserializeJson(doc, f); if (err) { - res = SV_DESERIALIZE_ERROR; // spatny JSON soubor + res = SV_DESERIALIZE_ERROR; // wrong (malformed) JSON file } f.close(); }