-
Notifications
You must be signed in to change notification settings - Fork 5
/
Properties.h
327 lines (288 loc) · 9.24 KB
/
Properties.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#ifndef PROPERTIES_H
#define PROPERTIES_H
#include "Core.h"
#include "KeyValueTable.h"
#include "TokenParser.h"
#include "Json.h"
#include "StringList.h"
class Properties : public KeyValueTable {
public:
typedef void (*fptr_string)(String);
typedef enum {
NullProperty = 0,
BoolProperty,
NumberProperty,
StringProperty,
JsonProperty
} PropertyType;
typedef enum {
RO = 0,
RW
} PropertyMode;
typedef struct {
PropertyType type;
PropertyMode mode;
fptr_string action;
us8 changed;
} PropertiesTableDetail;
Properties(us8 size = 24)
: KeyValueTable(size)
{
//propertiesTable = (PropertiesTableDetail*)malloc(size * sizeof(PropertiesTableDetail));
for(us8 i = 0; i < tableSize; i++) {
propertiesTable[i].type = NullProperty;
propertiesTable[i].mode = RW;
propertiesTable[i].action = 0;
propertiesTable[i].changed = 0;
}
echoFunction = 0;
batchMode = -1;
executingAction = false;
}
s8 addBool(String key, bool value, fptr_string func = 0, PropertyMode mode = RW)
{
s8 index = setValue(key, value ? "true" : "false", true);
if(index >= 0) {
propertiesTable[index].type = BoolProperty;
propertiesTable[index].mode = mode;
if(func != 0) {
propertiesTable[index].action = func;
}
}
return index;
}
s8 addNumber(String key, String value, fptr_string func = 0, PropertyMode mode = RW)
{
s8 index = setValue(key, value, true);
if(index >= 0) {
propertiesTable[index].type = NumberProperty;
propertiesTable[index].mode = mode;
if(func != 0) {
propertiesTable[index].action = func;
}
}
return index;
}
s8 addString(String key, String value, fptr_string func = 0, PropertyMode mode = RW)
{
s8 index = setValue(key, value, true);
if(index >= 0) {
propertiesTable[index].type = StringProperty;
propertiesTable[index].mode = mode;
if(func != 0) {
propertiesTable[index].action = func;
}
}
return index;
}
s8 addJson(String key, String value, fptr_string func = 0, PropertyMode mode = RW)
{
s8 index = setValue(key, value, true);
if(index >= 0) {
propertiesTable[index].type = JsonProperty;
propertiesTable[index].mode = mode;
if(func != 0) {
propertiesTable[index].action = func;
}
}
return index;
}
void setBatchMode(bool mode) {
if(mode) {
batchMode = 0;
}
else {
for(us8 i = 1; i <= batchMode; i++) {
for(us8 p = 0; p < tableSize; p++) {
if(propertiesTable[p].changed == i) {
propertiesTable[p].changed = 0;
if(echoFunction != 0) {
echoFunction(jsonString(p, i, batchMode));
}
break;
}
}
}
if(echoFunction != 0) {
echoFunction("\r");
}
batchMode = -1;
}
}
bool getBatchMode() {
return batchMode;
}
void disableAction(bool disable) {
executingAction = disable;
}
bool update(String key, String value)
{
s8 index = findIndex(key);
return update(index, value);
}
bool update(s8 index, String value)
{
if(0 <= index && index < tableSize) {
setValue(index, value);
if(batchMode != -1) {
if(propertiesTable[index].changed == 0) {
batchMode++;
propertiesTable[index].changed = batchMode;
}
}
else {
if(echoFunction != 0) {
echoFunction(jsonString(index) + "\r");
}
}
if((propertiesTable[index].action != 0) && (!executingAction)) {
executingAction = true;
propertiesTable[index].action(value);
executingAction = false;
}
return true;
}
return false;
}
String jsonString(s8 index, us8 start = 1, us8 stop = 1)
{
if(0 <= index && index < tableSize) {
StringList list;
list << key(index);
list << value(index);
String temp;
if(start == 1) {
temp += "{";
}
temp += " \"%1\": ";
switch(propertiesTable[index].type) {
case NullProperty:
temp += "null";
break;
case BoolProperty:
case NumberProperty:
case JsonProperty:
temp += "%2";
break;
case StringProperty:
temp += "\"%2\"";
}
if(start != stop) {
temp += ",";
}
else {
temp += " }";
}
return list.augment(temp);
}
return "";
}
void evaluate(TokenParser &parser)
{
if(parser.isJson()) {
parser.advanceHead(parser.remaining());
String string = parser.toString();
Json json(&string);
for(us8 i = 0; i < json.size(); i++) {
s8 index = findIndex(json.key(i));
if(0 <= index && index < tableSize) {
if(propertiesTable[index].mode == RW) {
String value = json.value(i).toString();
setValue(index, value);
if(propertiesTable[index].action != 0) {
propertiesTable[index].action(value);
}
}
}
}
}
else {
command(parser);
}
}
void setEchoFunction(fptr_string func)
{
echoFunction = func;
}
void command(TokenParser &parser)
{
if(parser.compare("get")) {
parser.nextToken();
s8 index = findIndex(parser.toString());
parser.println(jsonString(index));
}
else if(parser.compare("set")) {
parser.nextToken();
s8 index = findIndex(parser.toString());
parser.nextToken();
update(index, parser.toString());
}
else if(parser.compare("keys")) {
us8 count = 0;
parser.print("[");
for(s8 i = 0; i < tableSize; i++) {
if(kvTable[i].key[0] != 0) {
if(count++ > 0) {
parser.print(",");
}
parser.print("\"");
String key(kvTable[i].key);
parser.print(key);
parser.print("\"");
}
}
parser.println("]");
}
else if(parser.compare("properties")) {
us8 length;
for(length = 0; length < tableSize; length++) {
if(kvTable[length].key[0] == 0) {
break;
}
}
parser.print("{\n");
for(us8 i = 0; i < length; i++) {
StringList list;
list << key(i);
String mode = String((propertiesTable[i].mode == RW) ? "rw" : "r");
if(propertiesTable[i].action != 0) {
mode += "x";
}
list << mode;
switch(propertiesTable[i].type) {
case NullProperty:
list << "null";
list << "null";
break;
case BoolProperty:
list << "bool";
list << String((value(i) == "true") ? "true" : "false");
break;
case NumberProperty:
list << "number";
list << value(i);
break;
case StringProperty:
list << "string";
list << String("\"" + value(i) + "\"");
break;
case JsonProperty:
list << "json";
list << value(i);
}
parser.print(list.augment("\t\"%1\": [\"%3\", \"%2\", %4]"));
if(i < (length - 1)) {
parser.print(",");
}
parser.print("\n");
}
parser.println("}");
}
}
private:
PropertiesTableDetail propertiesTable[24];
fptr_string echoFunction;
s8 batchMode;
bool executingAction;
};
#endif // PROPERTIES_H