Verze 0.0.2 - komentare anglicky a upravy, doporucene clang-tidy

This commit is contained in:
Pavel Brychta 2023-06-16 16:28:55 +02:00
parent dd5efb3345
commit 69d97d4df3
3 changed files with 55 additions and 57 deletions

View File

@ -18,7 +18,7 @@
"version":"https://github.com/bblanchon/ArduinoJson.git" "version":"https://github.com/bblanchon/ArduinoJson.git"
} }
], ],
"version": "0.0.1", "version": "0.0.2",
"license": "MIT", "license": "MIT",
"frameworks": "arduino", "frameworks": "arduino",
"platforms": ["espressif8266", "espressif32"], "platforms": ["espressif8266", "espressif32"],

View File

@ -1,5 +1,5 @@
name=sysvars name=sysvars
version=0.0.1 version=0.0.2
author=Pavel Brychta author=Pavel Brychta
maintainer=Pavel Brychta <pablo@xpablo.cz> maintainer=Pavel Brychta <pablo@xpablo.cz>
sentence=Key-Value JSON file storage library. sentence=Key-Value JSON file storage library.

View File

@ -1,14 +1,14 @@
#pragma once #pragma once
/** Knihovna sysvars /** Sysvars library
* Copyright (c) 2018-2021 Pavel Brychta, pablo@xpablo.cz * 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 <Arduino.h> #include <Arduino.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <FS.h> #include <FS.h>
#include "definefs.hpp"
enum { enum {
SV_OK = 0, SV_OK = 0,
@ -21,28 +21,28 @@ enum {
}; };
#if not defined(SYSVAR_FILE) #if not defined(SYSVAR_FILE)
# define SYSVAR_FILE "/cfg.json" #define SYSVAR_FILE "/cfg.json"
#endif #endif
#if not defined(SYSVAR_DOCUMENT_SIZE) #if not defined(SYSVAR_DOCUMENT_SIZE)
# define SYSVAR_DOCUMENT_SIZE (4096) #define SYSVAR_DOCUMENT_SIZE (4096)
#endif #endif
/** /**
* @brief Ziskani hodnoty * @brief Get value
* *
* @param[in] name Jmeno klice * @param[in] name Name of the key
* @param result Promenna pro ulozeni vysledku operace (SV_...) * @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 <typename T> template <typename T>
T svGetV(const String& name, int* result = nullptr) T svGetV(const String & name, int * result = nullptr)
{ {
T retval{}; T rv{};
int res = SV_UNSPECIFIED_ERROR; // predpokladame chybu int res;
DynamicJsonDocument doc(SYSVAR_DOCUMENT_SIZE); DynamicJsonDocument doc(SYSVAR_DOCUMENT_SIZE);
File f; File f;
@ -50,40 +50,39 @@ T svGetV(const String& name, int* result = nullptr)
if (f) { if (f) {
DeserializationError err = deserializeJson(doc, f); DeserializationError err = deserializeJson(doc, f);
if (err) { if (err) {
res = SV_DESERIALIZE_ERROR; // spatny JSON soubor res = SV_DESERIALIZE_ERROR; // wrong JSON file (malformed)
} else { } else {
if (!doc.containsKey(name)) if (!doc.containsKey(name)) {
{ res = SV_KEY_NOT_FOUND; // key not found
res = SV_KEY_NOT_FOUND; // chyba - nenalezeny klic
} else { } else {
retval = doc[name].as<T>(); rv = doc[name].as<T>();
res = SV_OK; res = SV_OK;
} }
} }
} else { } else {
res = SV_FILE_NOT_FOUND; // chyba - nenalezeny soubor res = SV_FILE_NOT_FOUND; // file not found
} }
if (result) if (result)
*result = res; // navratovy kod, pokud nas zajima *result = res; // result code if we are interested
return retval; return rv;
} }
/** /**
* @brief Ziskani hodnoty klice * @brief Get value
* *
* @param[in] name Jmeno klice * @param[in] name Name of the key
* @param result Promenna pro ulozeni vysledku operace * @param result Status variable (SV_....)
* @param[in] fname Jmeno souboru s JSON parametry * @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 <typename T> template <typename T>
T svGetV(const String& name, int* result, const String& fname) T svGetV(const String & name, int * result, const String & fname)
{ {
T retval{}; T rv{};
int res = SV_UNSPECIFIED_ERROR; // predpokladame chybu int res;
DynamicJsonDocument doc(SYSVAR_DOCUMENT_SIZE); DynamicJsonDocument doc(SYSVAR_DOCUMENT_SIZE);
File f; File f;
@ -91,38 +90,37 @@ T svGetV(const String& name, int* result, const String& fname)
if (f) { if (f) {
DeserializationError err = deserializeJson(doc, f); DeserializationError err = deserializeJson(doc, f);
if (err) { if (err) {
res = SV_DESERIALIZE_ERROR; // spatny JSON soubor res = SV_DESERIALIZE_ERROR; // wrong (malformed) JSON file
} else { } else {
if (!doc.containsKey(name)) if (!doc.containsKey(name)) {
{ res = SV_KEY_NOT_FOUND; // key not found
res = SV_KEY_NOT_FOUND; // chyba - nenalezeny klic
} else { } else {
retval = doc[name].as<T>(); rv = doc[name].as<T>();
res = SV_OK; res = SV_OK;
} }
} }
} else { } else {
res = SV_FILE_NOT_FOUND; // chyba - nenalezeny soubor res = SV_FILE_NOT_FOUND; // file not found
} }
if (result) if (result)
*result = res; // navratovy kod, pokud nas zajima *result = res; // result code if we are interested
return retval; return rv;
} }
/** /**
* @brief Ulozeni hodnoty do klice * @brief Store value for the key
* *
* @param[in] name Jmeno klice * @param[in] name Key name
* @param[in] value Ukladana hodnota * @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 <typename T> template <typename T>
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); DynamicJsonDocument doc(SYSVAR_DOCUMENT_SIZE);
File f; File f;
@ -130,7 +128,7 @@ int svSetV(const String& name, T value)
if (f) { if (f) {
DeserializationError err = deserializeJson(doc, f); DeserializationError err = deserializeJson(doc, f);
if (err) { if (err) {
res = SV_DESERIALIZE_ERROR; // spatny JSON soubor res = SV_DESERIALIZE_ERROR; // wrong (malformed) JSON file
} }
f.close(); 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] name Key name
* @param[in] value Ukladana hodnota * @param[in] value New value
* @param[in] fname Jmeno souboru s JSON daty * @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 <typename T> template <typename T>
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; int res = SV_UNSPECIFIED_ERROR;
DynamicJsonDocument doc(SYSVAR_DOCUMENT_SIZE); DynamicJsonDocument doc(SYSVAR_DOCUMENT_SIZE);
@ -168,7 +166,7 @@ int svSetV(const String& name, T value, const String& fname)
if (f) { if (f) {
DeserializationError err = deserializeJson(doc, f); DeserializationError err = deserializeJson(doc, f);
if (err) { if (err) {
res = SV_DESERIALIZE_ERROR; // spatny JSON soubor res = SV_DESERIALIZE_ERROR; // wrong (malformed) JSON file
} }
f.close(); f.close();
} }