Verze 0.0.2 - komentare anglicky a upravy, doporucene clang-tidy
This commit is contained in:
parent
dd5efb3345
commit
69d97d4df3
@ -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"],
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=sysvars
|
||||
version=0.0.1
|
||||
version=0.0.2
|
||||
author=Pavel Brychta
|
||||
maintainer=Pavel Brychta <pablo@xpablo.cz>
|
||||
sentence=Key-Value JSON file storage library.
|
||||
|
108
src/sysvars.hpp
108
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 <Arduino.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <FS.h>
|
||||
#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 <typename T>
|
||||
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<T>();
|
||||
rv = doc[name].as<T>();
|
||||
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 <typename T>
|
||||
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<T>();
|
||||
rv = doc[name].as<T>();
|
||||
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 <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);
|
||||
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 <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;
|
||||
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();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user