-
Notifications
You must be signed in to change notification settings - Fork 0
/
km_kmkv.cpp
570 lines (518 loc) · 18.8 KB
/
km_kmkv.cpp
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#include "km_kmkv.h"
#ifdef KM_KMKV_JSON
#include <cJSON.h>
#endif
#include "km_string.h"
template <typename Allocator>
KmkvItem<Allocator>::KmkvItem()
: keywordTag(), type(KmkvItemType::NONE)
{
}
template <typename Allocator>
KmkvItem<Allocator>& KmkvItem<Allocator>::operator=(const KmkvItem<Allocator>& other)
{
keywordTag = other.keywordTag;
type = other.type;
// TODO fix this allocator-passing madness
switch (other.type) {
case KmkvItemType::NONE: {
} break;
case KmkvItemType::STRING: {
dynamicStringPtr = defaultAllocator_.template New<DynamicArray<char>>();
DEBUG_ASSERT(dynamicStringPtr);
new (dynamicStringPtr) DynamicArray<char>(other.dynamicStringPtr->ToArray());
} break;
case KmkvItemType::KMKV: {
hashTablePtr = defaultAllocator_.template New<HashTable<KmkvItem<Allocator>, Allocator>>();
DEBUG_ASSERT(hashTablePtr);
new (hashTablePtr) HashTable<KmkvItem<Allocator>, Allocator>();
*hashTablePtr = *other.hashTablePtr;
} break;
}
return *this;
}
template <typename Allocator>
KmkvItem<Allocator>::~KmkvItem()
{
// TODO fix this allocator-passing madness
switch (type) {
case KmkvItemType::NONE: {
} break;
case KmkvItemType::STRING: {
dynamicStringPtr->~DynamicArray();
defaultAllocator_.Free(dynamicStringPtr);
} break;
case KmkvItemType::KMKV: {
hashTablePtr->~HashTable();
defaultAllocator_.Free(hashTablePtr);
} break;
}
}
template <typename Allocator>
DynamicArray<char, Allocator>* GetKmkvItemStrValue(
HashTable<KmkvItem<Allocator>, Allocator>& kmkv, const HashKey& itemKey)
{
const KmkvItem<Allocator>* itemValuePtr = kmkv.GetValue(itemKey);
if (itemValuePtr == nullptr) {
return nullptr;
}
if (itemValuePtr->type != KmkvItemType::STRING) {
return nullptr;
}
return itemValuePtr->dynamicStringPtr;
}
template <typename Allocator>
const DynamicArray<char, Allocator>* GetKmkvItemStrValue(
const HashTable<KmkvItem<Allocator>, Allocator>& kmkv, const HashKey& itemKey)
{
return GetKmkvItemStrValue(const_cast<HashTable<KmkvItem<Allocator>>&>(kmkv), itemKey);
}
template <typename Allocator>
HashTable<KmkvItem<Allocator>>* GetKmkvItemObjValue(
HashTable<KmkvItem<Allocator>, Allocator>& kmkv, const HashKey& itemKey)
{
const KmkvItem<Allocator>* itemValuePtr = kmkv.GetValue(itemKey);
if (itemValuePtr == nullptr) {
return nullptr;
}
if (itemValuePtr->type != KmkvItemType::KMKV) {
return nullptr;
}
return itemValuePtr->hashTablePtr;
}
template <typename Allocator>
const DynamicArray<char, Allocator>* GetKmkvItemObjValue(const HashTable<KmkvItem<Allocator>, Allocator>& kmkv,
const HashKey& itemKey)
{
return GetKmkvItemObjValue(const_cast<HashTable<KmkvItem<Allocator>>&>(kmkv), itemKey);
}
int ReadNextKeywordValue(const_string str, string* outKeyword, string* outValue)
{
if (str.size == 0 || str[0] == '\0') {
return 0;
}
int i = 0;
outKeyword->size = 0;
outKeyword->data = (char*)str.data;
while (i < (int)str.size && !IsWhitespace(str[i])) {
outKeyword->size++;
i++;
}
if (outKeyword->size == 0) {
return -1;
}
while (i < (int)str.size && str[i] == ' ') {
i++;
}
outValue->size = 0;
outValue->data = (char*)str.data + i;
bool bracketValue = false;
while (i < (int)str.size) {
if (IsNewline(str[i])) {
// End of inline value
i++;
break;
}
if (str[i] == '{' && outValue->size == 0) {
// Start of bracket value, read in separately
i++;
bracketValue = true;
break;
}
outValue->size++;
i++;
}
if (bracketValue) {
int bracketDepth = 1;
bool prevNewline = false;
bool bracketMatched = false;
while (i < (int)str.size) {
if (str[i] == '{' && (i + 1 < (int)str.size) && IsNewline(str[i + 1])) {
bracketDepth++;
}
else if (str[i] == '}' && prevNewline) {
bracketDepth--;
if (bracketDepth == 0) {
i++;
bracketMatched = true;
break;
}
}
if (IsNewline(str[i])) {
prevNewline = true;
}
else if (prevNewline && !IsWhitespace(str[i])) {
prevNewline = false;
}
if (outValue->size == 0 && IsWhitespace(str[i])) {
// Gobble starting whitespace
i++;
outValue->data = (char*)str.data + i;
continue;
}
outValue->size++;
i++;
}
if (!bracketMatched) {
LOG_ERROR("Value bracket unmatched pair for keyword %.*s\n",
(int)outKeyword->size, outKeyword->data);
return -1;
}
}
// Trim trailing whitespace
while (outValue->size > 0 && IsWhitespace((*outValue)[outValue->size - 1])) {
outValue->size--;
}
while (i < (int)str.size && IsWhitespace(str[i])) {
i++;
}
return i;
}
template <uint64 KEYWORD_SIZE, typename Allocator>
int ReadNextKeywordValue(const Array<char>& str,
FixedArray<char, KEYWORD_SIZE>* outKeyword, DynamicArray<char, Allocator>* outValue)
{
string keyword, value;
int result = ReadNextKeywordValue(str, &keyword, &value);
if (result > 0) {
outKeyword->FromArray(keyword);
outValue->FromArray(value);
}
return result;
}
template <typename Allocator>
internal bool LoadKmkvRecursive(string str, HashTable<KmkvItem<Allocator>, Allocator>* outKmkv)
{
const uint64 KEYWORD_MAX_LENGTH = 32;
FixedArray<char, KEYWORD_MAX_LENGTH> keyword;
DynamicArray<char, Allocator> valueBuffer(outKmkv->allocator);
while (true) {
int read = ReadNextKeywordValue(str, &keyword, &valueBuffer);
if (read < 0) {
LOG_ERROR("kmkv file keyword/value error\n");
return false;
}
else if (read == 0) {
break;
}
str.size -= read;
str.data += read;
DynamicArray<char> keywordTag;
bool keywordHasTag = false;
uint64 keywordTagInd = 0;
while (keywordTagInd < keyword.size) {
if (keyword[keywordTagInd] == '{') {
keywordHasTag = true;
break;
}
keywordTagInd++;
}
if (keywordHasTag) {
keywordTagInd++;
bool bracketMatched = false;
while (keywordTagInd < keyword.size) {
if (keyword[keywordTagInd] == '}') {
bracketMatched = true;
break;
}
keywordTag.Append(keyword[keywordTagInd++]);
}
if (!bracketMatched) {
LOG_ERROR("kmkv keyword tag unmatched bracket\n");
return false;
}
if (bracketMatched && keywordTagInd != keyword.size - 1) {
LOG_ERROR("found characters after kmkv keyword tag bracket, keyword %.*s\n",
(int)keyword.size, keyword.data);
return false;
}
}
string keywordArray = keyword.ToArray();
if (keywordHasTag) {
keywordArray = keywordArray.SliceTo(keywordArray.size - keywordTag.size - 2);
}
if (outKmkv->GetValue(keywordArray)) {
LOG_ERROR("kmkv duplicate keyword: %.*s\n", (int)keywordArray.size, keywordArray.data);
return false;
}
KmkvItem<Allocator>* newItem = outKmkv->Add(keywordArray);
DEBUG_ASSERT(newItem);
newItem->keywordTag = keywordTag;
if (StringEquals(newItem->keywordTag.ToArray(), ToString("kmkv"))) {
newItem->type = KmkvItemType::KMKV;
// "placement new" - allocate with custom allocator, but still call constructor
// Also, oh my god... that "template" keyword... C++ SUCKS
newItem->hashTablePtr = outKmkv->allocator->template New<HashTable<KmkvItem<Allocator>, Allocator>>();
if (newItem->hashTablePtr == nullptr) {
return false;
}
new (newItem->hashTablePtr) HashTable<KmkvItem<Allocator>>();
if (!LoadKmkvRecursive(valueBuffer.ToArray(), newItem->hashTablePtr)) {
return false;
}
}
else {
newItem->type = KmkvItemType::STRING;
// "placement new" - allocate with custom allocator, but still call constructor
// Also, oh my god... that "template" keyword... C++ SUCKS
newItem->dynamicStringPtr = outKmkv->allocator->template New<DynamicArray<char>>();
if (newItem->dynamicStringPtr == nullptr) {
return false;
}
new (newItem->dynamicStringPtr) DynamicArray<char>(valueBuffer.ToArray());
}
}
return true;
}
template <typename Allocator>
bool LoadKmkv(const Array<char>& filePath, Allocator* allocator,
HashTable<KmkvItem<Allocator>>* outKmkv)
{
Array<uint8> kmkvFile = LoadEntireFile(filePath, allocator);
if (kmkvFile.data == nullptr) {
LOG_ERROR("Failed to load file %.*s\n", (int)filePath.size, filePath.data);
return false;
}
defer(FreeFile(kmkvFile, allocator));
Array<char> fileString;
fileString.size = kmkvFile.size;
fileString.data = (char*)kmkvFile.data;
return LoadKmkv(fileString, outKmkv);
}
template <typename Allocator>
bool LoadKmkv(const Array<char>& kmkvString, HashTable<KmkvItem<Allocator>, Allocator>* outKmkv)
{
outKmkv->Clear();
return LoadKmkvRecursive(kmkvString, outKmkv);
}
template <typename Allocator>
internal bool KmkvToStringRecursive(const HashTable<KmkvItem<Allocator>>& kmkv, int indentSpaces,
DynamicArray<char, Allocator>* outString)
{
for (uint32 i = 0; i < kmkv.capacity; i++) {
const HashKey& key = kmkv.pairs[i].key;
if (key.s.size == 0) {
continue;
}
for (int j = 0; j < indentSpaces; j++) outString->Append(' ');
outString->Append(key.s.ToArray());
const KmkvItem<Allocator>& item = kmkv.pairs[i].value;
switch (item.type) {
case KmkvItemType::NONE: {
} break;
case KmkvItemType::STRING: {
if (item.keywordTag.size > 0) {
outString->Append('{');
outString->Append(item.keywordTag.ToArray());
outString->Append('}');
}
outString->Append(' ');
bool inlineValue = item.dynamicStringPtr->IndexOf('\n') == item.dynamicStringPtr->size;
if (!inlineValue) {
outString->Append('{');
outString->Append('\n');
}
outString->Append(item.dynamicStringPtr->ToArray());
if (!inlineValue) {
outString->Append('\n');
for (int j = 0; j < indentSpaces; j++) outString->Append(' ');
outString->Append('}');
}
} break;
case KmkvItemType::KMKV: {
outString->Append(ToString("{kmkv} {\n"));
if (!KmkvToStringRecursive(*item.hashTablePtr, indentSpaces + 4, outString)) {
LOG_ERROR("Failed to convert nested kmkv to string, key %.*s\n",
(int)key.s.size, key.s.data);
}
for (int j = 0; j < indentSpaces; j++) outString->Append(' ');
outString->Append('}');
} break;
}
outString->Append('\n');
}
return true;
}
template <typename Allocator>
bool KmkvToString(const HashTable<KmkvItem<Allocator>>& kmkv,
DynamicArray<char, Allocator>* outString)
{
return KmkvToStringRecursive(kmkv, 0, outString);
}
#ifdef KM_KMKV_JSON
template <typename Allocator>
void AddAndMaybeEscapeJson(const Array<char>& string, DynamicArray<char, Allocator>* outJson)
{
for (uint32 i = 0; i < string.size; i++) {
switch (string[i]) {
case '\b': {
outJson->Append('\\');
outJson->Append('b');
} break;
case '\f': {
outJson->Append('\\');
outJson->Append('f');
} break;
case '\n': {
outJson->Append('\\');
outJson->Append('n');
} break;
case '\r': {
outJson->Append('\\');
outJson->Append('r');
} break;
case '\t': {
outJson->Append('\\');
outJson->Append('t');
} break;
case '\"': {
outJson->Append('\\');
outJson->Append('"');
} break;
case '\\': {
outJson->Append('\\');
outJson->Append('\\');
} break;
default: {
outJson->Append(string[i]);
} break;
}
}
}
template <typename Allocator>
internal bool KmkvToJsonRecursive(const HashTable<KmkvItem<Allocator>>& kmkv,
DynamicArray<char, Allocator>* outJson)
{
for (uint32 i = 0; i < kmkv.capacity; i++) {
const HashKey& key = kmkv.pairs[i].key;
if (key.s.size == 0) {
continue;
}
outJson->Append('"');
outJson->Append(key.s.ToArray());
outJson->Append('"');
outJson->Append(':');
const KmkvItem<Allocator>& item = kmkv.pairs[i].value;
switch (item.type) {
case KmkvItemType::NONE: {
} break;
case KmkvItemType::STRING: {
if (StringEquals(item.keywordTag.ToArray(), ToString("array"))) {
outJson->Append('[');
Array<char> arrayString = item.dynamicStringPtr->ToArray();
bool atLeastOne = arrayString.size > 0;
while (arrayString.size > 0) {
Array<char> split = NextSplitElement(&arrayString, ',');
split = TrimWhitespace(split);
outJson->Append('"');
AddAndMaybeEscapeJson(split, outJson);
outJson->Append('"');
outJson->Append(',');
}
if (atLeastOne) {
outJson->RemoveLast();
}
outJson->Append(']');
}
else {
outJson->Append('"');
AddAndMaybeEscapeJson(item.dynamicStringPtr->ToArray(), outJson);
outJson->Append('"');
}
} break;
case KmkvItemType::KMKV: {
outJson->Append('{');
if (!KmkvToJsonRecursive(*item.hashTablePtr, outJson)) {
return false;
}
outJson->Append('}');
} break;
}
outJson->Append(',');
}
if ((*outJson)[outJson->size - 1] == ',') {
outJson->RemoveLast();
}
return true;
}
template <typename Allocator>
bool KmkvToJson(const HashTable<KmkvItem<Allocator>>& kmkv, DynamicArray<char, Allocator>* outJson)
{
outJson->Append('{');
if (!KmkvToJsonRecursive(kmkv, outJson)) {
return false;
}
outJson->Append('}');
return true;
}
template <typename Allocator>
internal bool JsonToKmkvRecursive(const cJSON* json, Allocator* allocator,
HashTable<KmkvItem<Allocator>>* outKmkv)
{
const cJSON* child = json->child;
while (child != NULL && child->string != NULL) {
KmkvItem<Allocator>* item = outKmkv->Add(child->string);
if (cJSON_IsObject(child)) {
item->type = KmkvItemType::KMKV;
item->hashTablePtr = allocator->template New<HashTable<KmkvItem<Allocator>, Allocator>>();
DEBUG_ASSERT(item->hashTablePtr != nullptr);
new (item->hashTablePtr) HashTable<KmkvItem<Allocator>, Allocator>();
if (!JsonToKmkvRecursive(child, allocator, item->hashTablePtr)) {
LOG_ERROR("Failed to parse child JSON object, key %s\n", child->string);
return false;
}
}
else if (cJSON_IsString(child)) {
item->type = KmkvItemType::STRING;
item->dynamicStringPtr = allocator->template New<DynamicArray<char, Allocator>>();
DEBUG_ASSERT(item->dynamicStringPtr != nullptr);
new (item->dynamicStringPtr) DynamicArray<char, Allocator>(ToNonConstString(ToString(child->valuestring)));
}
else if (cJSON_IsArray(child)) {
item->type = KmkvItemType::STRING;
item->dynamicStringPtr = allocator->template New<DynamicArray<char, Allocator>>();
DEBUG_ASSERT(item->dynamicStringPtr != nullptr);
new (item->dynamicStringPtr) DynamicArray<char, Allocator>();
const cJSON* arrayItem;
cJSON_ArrayForEach(arrayItem, child) {
if (!cJSON_IsString(arrayItem)) {
LOG_ERROR("JSON array item not a string, key %s\n", child->string);
return false;
}
item->dynamicStringPtr->Append(ToString(arrayItem->valuestring));
item->dynamicStringPtr->Append(',');
}
if (cJSON_GetArraySize(child) > 0) {
item->dynamicStringPtr->RemoveLast();
}
item->keywordTag.Append(ToString("array"));
}
else {
LOG_ERROR("Unhandled JSON type: %d\n", child->type);
return false;
}
child = child->next;
}
return true;
}
template <typename Allocator>
bool JsonToKmkv(const Array<char>& jsonString, Allocator* allocator,
HashTable<KmkvItem<Allocator>>* outKmkv)
{
const char* jsonCString = ToCString(jsonString, allocator);
cJSON* json = cJSON_Parse(jsonCString);
if (json == NULL) {
const char *errorPtr = cJSON_GetErrorPtr();
if (errorPtr != NULL) {
LOG_ERROR("Error parsing JSON before: %s\n", errorPtr);
}
return false;
}
defer(cJSON_Delete(json));
if (!cJSON_IsObject(json)) {
LOG_ERROR("Top-level json not object type: %s\n", jsonCString);
return false;
}
return JsonToKmkvRecursive(json, allocator, outKmkv);
}
#endif