-
Notifications
You must be signed in to change notification settings - Fork 5
/
KeyValueTable.h
210 lines (179 loc) · 5.05 KB
/
KeyValueTable.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
#ifndef KEYVALUETABLE_H
#define KEYVALUETABLE_H
#include "Core.h"
#include "TokenParser.h"
#include "StringList.h"
#define keySize 20
#define valueSize 16
class KeyValueTable {
friend class Properties;
typedef struct {
char key[keySize];
char value[valueSize];
} KeyValueTableDetail;
public:
KeyValueTable(us8 size = 24)
{
//kvTable = (KeyValueTableDetail*)malloc(size * sizeof(KeyValueTableDetail));
tableSize = size;
}
void clear()
{
for(us8 i = 0; i < tableSize; i++) {
remove(i);
}
}
s8 index(String key)
{
return findIndex(key);
}
bool remove(s8 index)
{
if(0 <= index && index < tableSize) {
for(us8 y = 0; y < keySize; y++) {
kvTable[index].key[y] = 0;
}
for(us8 z = 0; z < valueSize; z++) {
kvTable[index].value[z] = 0;
}
return true;
}
return false;
}
bool remove(String key)
{
return remove(findIndex(key));
}
String key(s8 index)
{
if(index < 0 || tableSize <= index) {
return String("null");
}
return kvTable[index].key;
}
String value(s8 index)
{
if(index < 0 || tableSize <= index) {
return String("null");
}
return kvTable[index].value;
}
String value(String key)
{
return value(findIndex(key));
}
s8 setValue(String key, String value, bool create = false)
{
s8 index = findIndex(key, create);
if(kvTable[index].key[0] == 0) {
if(key.length() < keySize) {
for(us8 i = 0; i < key.length(); i++) {
kvTable[index].key[i] = key.charAt(i);
}
}
else {
return -1;
}
}
setValue(index, value);
return index;
}
void setValue(s8 index, String value)
{
if(0 <= index && index < tableSize) {
if(value.length() < valueSize) {
us8 i;
for(i = 0; i < value.length(); i++) {
char *a = kvTable[index].value + i;
char b = value.charAt(i);
if(*a != b) {
*a = b;
}
}
if(i < valueSize) {
kvTable[index].value[i] = 0;
}
}
}
}
void command(TokenParser &parser)
{
if(parser.compare("get")) {
parser.nextToken();
String key = parser.toString();
parser.println(value(key));
}
else if(parser.compare("set")) {
parser.nextToken();
String key = parser.toString();
parser.nextToken();
String value = parser.toString();
setValue(key, value);
}
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("dump")) {
for(us8 i = 0; i < tableSize; i++) {
if(kvTable[i].key[0] == 0) {
break;
}
StringList list;
list << String(i, DEC);
list << key(i);
list << value(i);
parser.println(list.augment("%1: %2 = %3"));
}
}
}
private:
us8 tableSize;
KeyValueTableDetail kvTable[24];
bool compare(String string, char* buffer, us8 maxLength)
{
us8 length = 0;
while(buffer[length] != 0 && length < maxLength) {
length++;
}
if(string.length() != length) {
return false;
}
for(us8 i = 0; i < length; i++) {
if(string.charAt(i) != buffer[i]) {
return false;
}
}
return true;
}
// finds empty slot or matching key
s8 findIndex(String key, bool includeEmpty = false)
{
if(key.length() > 0) {
for(s8 i = 0; i < tableSize; i++) {
if(compare(key, kvTable[i].key, keySize)) {
return i;
}
if(includeEmpty) {
if(kvTable[i].key[0] == 0) {
return i;
}
}
}
}
return -1;
}
};
#endif // KEYVALUETABLE_H