esp-fs-webserver
Loading...
Searching...
No Matches
linked_list.h
Go to the documentation of this file.
1#define VAR_NAME(name) #name
2
3#include <ArduinoJson.h>
4#include <FS.h>
5
6#ifndef linked_list_H
7#define linked_list_H
8
9
10#if DEBUG_MODE
11#define DebugPrint(x) DBG_OUTPUT_PORT.print(x)
12#define DebugPrintln(x) DBG_OUTPUT_PORT.println(x)
13#define DebugPrintf(fmt, ...) DBG_OUTPUT_PORT.printf(fmt, ##__VA_ARGS__)
14#define DebugPrintf_P(fmt, ...) DBG_OUTPUT_PORT.printf_P(fmt, ##__VA_ARGS__)
15#else
16#define DebugPrint(x)
17#define DebugPrintln(x)
18#define DebugPrintf(x, ...)
19#define DebugPrintf_P(x, ...)
20#endif
21
22
24
26struct VarNode
27{
28 uint8_t type;
29 void* var;
30 const char* key;
31 VarNode* next = nullptr;
32};
35{
36 private:
37 VarNode* first = nullptr;
38 VarNode* last = nullptr;
39
40 public:
42
43 template <class T>
44 void addItem(T& var, const char* key)
45 {
46 VarNode* node = new VarNode();
47 node->var = &var;
48 // node->key = VAR_NAME(var);
49 node->key = key;
50 node->type = TypeOf(var);
51
52 if (first == nullptr) {
53 first = node;
54 last = node;
55 return;
56 }
57 last->next = node;
58 last = node;
59 }
60
61 VarNode* getNode(const char* key)
62 {
63 VarNode* current = first;
64 while ( current != nullptr )
65 {
66 if (strcmp(current->key, key) == 0) {
67 return current;
68 }
69 current = current->next;
70 }
71 return nullptr;
72 }
73
74
75 bool saveValues(fs::FS *fs, const char* filename) {
76 File file = fs->open("/config.json", "w");
77 int sz = file.size() * 1.33;
78 int docSize = max(sz, 2048);
79 DynamicJsonDocument root((size_t)docSize);
80 if (!file)
81 {
82 DebugPrintln(F("File not found, will be created new configuration file"));
83 return false;
84 }
85
86 VarNode* current = first;
87 while ( current != nullptr )
88 {
89 switch (current->type) {
90 case BOOL: root[current->key] = *(bool*)current->var; break;
91 case CHAR: root[current->key] = *(signed char*)current->var; break;
92 case INT: root[current->key] = *(int*)current->var; break;
93 case LONG: root[current->key] = *(long*)current->var; break;
94 case UCHAR: root[current->key] = *(unsigned char*)current->var; break;
95 case UINT: root[current->key] = *(unsigned int*)current->var; break;
96 case ULONG: root[current->key] = *(unsigned long*)current->var; break;
97 case CHAR_PT: root[current->key] = (unsigned char*)current->var; break;
98 case FLOAT: root[current->key] = *(float*)current->var; break;
99 case DOUBLE: root[current->key] = *(double*)current->var; break;
100 case STRING: root[current->key] = *(String*)current->var; break;
101 }
102 current = current->next;
103 }
104
105 serializeJson(root, file);
106 file.close();
107 return true;
108 }
109
110 bool loadValues(fs::FS *fs, const char* filename) {
111 File file = fs->open("/config.json", "r");
112 DynamicJsonDocument doc(file.size() * 1.33);
113 if (file)
114 {
115 DeserializationError error = deserializeJson(doc, file);
116 if (error)
117 {
118 DebugPrintln(F("Failed to deserialize file, may be corrupted"));
119 DebugPrintln(error.c_str());
120 file.close();
121 return false;
122 }
123 file.close();
124 }
125 else
126 return false;
127
128 VarNode* current = first;
129 while ( current != nullptr )
130 {
131 switch (current->type) {
132 case BOOL: *(bool*)current->var = doc[current->key]; break;
133 case CHAR: *(signed char*)current->var = doc[current->key]; break;
134 case INT: *(int*)current->var = doc[current->key]; break;
135 case LONG: *(long*)current->var = doc[current->key]; break;
136 case UCHAR: *(unsigned char*)current->var = doc[current->key]; break;
137 case UINT: *(unsigned int*)current->var = doc[current->key]; break;
138 case ULONG: *(unsigned long*)current->var = doc[current->key]; break;
139 case CHAR_PT: strcpy((char*)current->var, doc[current->key]); break;
140 case FLOAT: *(float*)current->var = doc[current->key]; break;
141 case DOUBLE: *(double*)current->var = doc[current->key]; break;
142 case STRING: *(String*)current->var = doc[current->key].as<String>(); break;
143 }
144 current = current->next;
145 }
146
147 return true;
148 }
149
150
151 virtual inline uint8_t TypeOf(const bool&) { return BOOL; }
152 virtual inline uint8_t TypeOf(const char&) { return CHAR; }
153 virtual inline uint8_t TypeOf(const char*) { return CHAR_PT; }
154 virtual inline uint8_t TypeOf(const double&) { return DOUBLE; }
155 virtual inline uint8_t TypeOf(const float&) { return FLOAT; }
156 virtual inline uint8_t TypeOf(const int&) { return INT; }
157 virtual inline uint8_t TypeOf(const unsigned int&) { return UINT; }
158 virtual inline uint8_t TypeOf(const unsigned long&) { return ULONG; }
159 virtual inline uint8_t TypeOf(const unsigned char&) { return UCHAR; }
160 virtual inline uint8_t TypeOf(const String&) { return STRING; }
161};
162
163#endif
VarNode * getNode(const char *key)
Definition: linked_list.h:61
bool loadValues(fs::FS *fs, const char *filename)
Definition: linked_list.h:110
virtual uint8_t TypeOf(const String &)
Definition: linked_list.h:160
virtual uint8_t TypeOf(const char *)
Definition: linked_list.h:153
virtual uint8_t TypeOf(const double &)
Definition: linked_list.h:154
virtual uint8_t TypeOf(const float &)
Definition: linked_list.h:155
virtual uint8_t TypeOf(const unsigned long &)
Definition: linked_list.h:158
void addItem(T &var, const char *key)
Definition: linked_list.h:44
virtual uint8_t TypeOf(const char &)
Definition: linked_list.h:152
virtual uint8_t TypeOf(const bool &)
Definition: linked_list.h:151
virtual uint8_t TypeOf(const int &)
Definition: linked_list.h:156
virtual uint8_t TypeOf(const unsigned char &)
Definition: linked_list.h:159
bool saveValues(fs::FS *fs, const char *filename)
Definition: linked_list.h:75
virtual uint8_t TypeOf(const unsigned int &)
Definition: linked_list.h:157
#define DebugPrintln(x)
Definition: linked_list.h:17
@ DOUBLE
Definition: linked_list.h:23
@ CHAR_PT
Definition: linked_list.h:23
@ CHAR
Definition: linked_list.h:23
@ UINT
Definition: linked_list.h:23
@ ULONG
Definition: linked_list.h:23
@ FLOAT
Definition: linked_list.h:23
@ UCHAR
Definition: linked_list.h:23
@ LONG
Definition: linked_list.h:23
@ BOOL
Definition: linked_list.h:23
@ STRING
Definition: linked_list.h:23
@ INT
Definition: linked_list.h:23
uint8_t type
Definition: linked_list.h:28
void * var
Definition: linked_list.h:29
VarNode * next
Definition: linked_list.h:31
const char * key
Definition: linked_list.h:30