-
Notifications
You must be signed in to change notification settings - Fork 5
/
KeyValue.cpp
779 lines (598 loc) · 16.5 KB
/
KeyValue.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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
//
// SpeedyKeyV
// https://github.com/ozxybox/SpeedyKeyV
//
#include "KeyValue.h"
#include <cstring>
#include <cstdlib>
// For min and max
#include <algorithm>
#define ALLOW_QUOTELESS_STRINGS 1
#define BLOCK_BEGIN '{'
#define BLOCK_END '}'
#define STRING_CONTAINER '"'
#define SINGLE_LINE_COMMENT "//"
#define TAB_STYLE " "
#define POOL_STARTING_LENGTH 0
#define POOL_INCREMENT_LENGTH 4
#ifdef _WIN32
#define strcasecmp _stricmp
#endif
//////////////////////
// Helper Functions //
//////////////////////
// Notes:
// These pass const char* references so that they can increment them. Otherwise, the parse loop would get stuck!
// Will return true on end of string!
inline bool IsWhitespace(char c)
{
// Anything that isn't ASCII is treated as not whitespace
return (c < '!' || c > '~') && ( c >= 0 && c <= 127 );
}
// Is c a character used by something else?
inline bool IsSpecialCharacter(char c, char cc)
{
return c == STRING_CONTAINER || c == BLOCK_BEGIN || c == BLOCK_END ||
// If it's a line comment then it should always have another / right after the first one
(c == SINGLE_LINE_COMMENT[0] && cc == SINGLE_LINE_COMMENT[1]);
}
// Ends on the first character after whitespace
void SkipWhitespace(const char*& str)
{
startOfSkip:
for (; IsWhitespace(*str) && *str; str++);
// Not really whitespace, but we might as well check if there's any comments here...
if (strncmp(str, SINGLE_LINE_COMMENT, sizeof(SINGLE_LINE_COMMENT) - 1) == 0)
{
// Read until end line
for (char c = *str; c && c != '\r' && c != '\n'; c = *++str);
// There might be more whitespace after the comment, so we should go back and check for more
goto startOfSkip;
}
}
KeyValueErrorCode ReadQuotedString(const char*& str, kvString_t& inset)
{
/*
We probably should check if this is a quote, but, we already know that it's a quote due to earlier logic.
If this is a problem ever, just uncomment this
if(*str != STRING_CONTAINER)
return KeyValueErrorCode::QUOTED_STRING_MUST_BEGIN_WITH_QUOTE;
*/
str++;
inset.string = const_cast<char*>(str);
for (char c = *str; c && c != STRING_CONTAINER; c = *++str);
if (!*str)
{
// We hit the end of the file, but the string was never closed
return KeyValueErrorCode::INCOMPLETE_STRING;
}
inset.length = str - inset.string;
// Skip over the quote
str++;
return KeyValueErrorCode::NONE;
}
#if ALLOW_QUOTELESS_STRINGS
kvString_t ReadQuotelessString(const char*& str)
{
kvString_t inset;
inset.string = const_cast<char*>(str);
// Read until whitespace, special character, or end of string
for (; !IsWhitespace(*str) && !IsSpecialCharacter(str[0], str[1]); str++ );
inset.length = str - inset.string;
return inset;
}
#endif // ALLOW_QUOTELESS_STRINGS
////////////////////
// Key Value Pool //
////////////////////
template<typename T>
KeyValuePool<T>::KeyValuePool()
{
position = 0;
firstPool = new PoolChunk(POOL_STARTING_LENGTH);
currentPool = firstPool;
}
template<typename T>
KeyValuePool<T>::~KeyValuePool()
{
if (firstPool)
Drain();
}
template<typename T>
void KeyValuePool<T>::Drain()
{
PoolChunk* nextPool = firstPool;
PoolChunk* old;
while (nextPool)
{
free(nextPool->pool);
old = nextPool;
nextPool = nextPool->next;
delete old;
}
firstPool = nullptr;
currentPool = nullptr;
}
template<typename T>
T* KeyValuePool<T>::Create()
{
if (!IsFull())
{
returnKV:
T* kv = ¤tPool->pool[position];
position++;
return kv;
}
// If the pool is full, we have to allocated a new one, and try again
position = 0;
size_t newLength = currentPool->length + POOL_INCREMENT_LENGTH;
currentPool = new PoolChunk(newLength, currentPool);
goto returnKV;
}
template<typename T>
KeyValuePool<T>::PoolChunk::PoolChunk(size_t length)
{
next = nullptr;
pool = (T*)malloc(sizeof(T) * length);
this->length = length;
}
template<typename T>
KeyValuePool<T>::PoolChunk::PoolChunk(size_t length, PoolChunk* last) : PoolChunk(length)
{
last->next = this;
}
////////////////////
// Key Value Root //
////////////////////
KeyValueRoot::KeyValueRoot(const char* str) : KeyValueRoot()
{
Parse(str);
}
KeyValueRoot::KeyValueRoot()
{
bufferSize = 0;
solidified = false;
rootNode = this;
key = { nullptr, 0 };
isNode = true;
data.node = { nullptr, nullptr, 0 };
stringBuffer = nullptr;
}
KeyValueRoot::~KeyValueRoot()
{
if (bufferSize > 0)
free(stringBuffer);
// writePoolStrings are allocated on creation of a node.. We have to clean all of these up manually :(
for (KeyValuePool<char*>::PoolChunk* current = writePoolStrings.firstPool; current != writePoolStrings.currentPool; current = current->next)
{
for (size_t i = 0; i < current->length; i++)
{
delete[] current->pool[i];
}
}
// The last pool might not be totally filled out...
for (size_t i = 0; i < writePoolStrings.position; i++)
{
delete[] writePoolStrings.currentPool->pool[i];
}
}
void KeyValueRoot::Solidify()
{
if (solidified)
return;
solidified = true;
// We need to take the pool, move the stuff into their correct positions, and delete it
if (data.node.childCount > 0)
{
KeyValue::Solidify();
}
// Copied of all of these values will be made. No need to retain the pools...
readPool.Drain();
writePool.Drain();
// Sadly, we can't drain the string pool... It contains keys and values for newly added nodes and pairs
}
KeyValueErrorCode KeyValueRoot::Parse(const char* str)
{
if ( !str )
return KeyValueErrorCode::NO_INPUT;
KeyValueErrorCode err = KeyValue::Parse(str, true);
if (err != KeyValueErrorCode::NONE)
return err;
if (bufferSize > 0)
{
stringBuffer = (char*)malloc(sizeof(char) * bufferSize);
// Can't straight pass it, otherwise it'd mess with it
char* temp = stringBuffer;
BuildData(temp);
}
// All good. Return no error
return KeyValueErrorCode::NONE;
}
///////////////
// Key Value //
///////////////
// An invalid KV for use in returns with references
KeyValue& KeyValue::GetInvalid()
{
static const KeyValue invalid(true);
return const_cast<KeyValue&>(invalid);
}
void KeyValue::Solidify()
{
KeyValue* newArray = new KeyValue[data.node.childCount];
KeyValue* current = data.node.children;
data.node.children = newArray;
// Is it worth block copying out of the pool?
size_t cc = data.node.childCount;
for (size_t i = 0; i < cc; i++)
{
memcpy(&newArray[i], current, sizeof(KeyValue));
if (newArray[i].isNode && newArray[i].data.node.childCount > 0)
newArray[i].Solidify();
// Maintains compatibility with linked list code
newArray[i].next = &newArray[i + 1];
current = current->next;
}
newArray[cc - 1].next = nullptr;
}
KeyValue& KeyValue::InternalGet(const char* keyName) const
{
if (!isNode || data.node.childCount <= 0 || !IsValid())
return GetInvalid();
// If we're solid, we can use a quicker route
if (rootNode->solidified)
{
size_t cc = data.node.childCount;
for (size_t i = 0; i < cc; i++)
{
if (strcasecmp(data.node.children[i].key.string, keyName) == 0)
{
return data.node.children[i];
}
}
}
else
{
for (KeyValue* current = data.node.children; current; current = current->next)
{
if (strcasecmp(current->key.string, keyName) == 0)
{
return *current;
}
}
}
return GetInvalid();
}
KeyValue& KeyValue::InternalAt(size_t index) const
{
// If we cant get something, return invalid
if(!isNode || data.node.childCount <= 0 || index < 0 || index >= data.node.childCount || !IsValid())
return GetInvalid();
if (rootNode->solidified)
{
return data.node.children[index];
}
else
{
KeyValue* current = data.node.children;
for (int i = 0; i != index; i++)
{
current = current->next;
}
return *current;
}
}
KeyValue* KeyValue::Add(const char* keyName, const char* value)
{
// Can't add to a solid kv without kids!
if (rootNode->solidified && !isNode)
return nullptr;
size_t keyLength = strlen(keyName);
char*& copiedKey = *rootNode->writePoolStrings.Create();
copiedKey = new char[keyLength + 1];
memcpy(copiedKey, keyName, keyLength);
copiedKey[keyLength] = '\0';
size_t valueLength = strlen(value);
char*& copiedValue = *rootNode->writePoolStrings.Create();
copiedValue = new char[valueLength + 1];
memcpy(copiedValue, value, valueLength);
copiedValue[valueLength] = '\0';
KeyValue* newKV = CreateKVPair({ copiedKey, keyLength }, { copiedValue, valueLength }, rootNode->writePool);
newKV->next = nullptr;
if (data.node.children)
{
data.node.lastChild->next = newKV;
}
else
{
data.node.children = newKV;
}
data.node.lastChild = newKV;
data.node.childCount++;
return newKV;
}
KeyValue* KeyValue::AddNode(const char* keyName)
{
// Can't add to a solid kv without kids!
if (rootNode->solidified && !isNode)
return nullptr;
KeyValue* node = rootNode->writePool.Create();
size_t keyLength = strlen(keyName);
char*& copiedKey = *rootNode->writePoolStrings.Create();
copiedKey = new char[keyLength + 1];
memcpy(copiedKey, keyName, keyLength);
copiedKey[keyLength] = '\0';
node->key = { copiedKey, keyLength };
node->isNode = true;
node->data.node = { nullptr, nullptr, 0 };
node->rootNode = rootNode;
node->next = nullptr;
if (data.node.childCount == 0)
{
data.node.children = node;
}
else
{
data.node.lastChild->next = node;
}
data.node.lastChild = node;
data.node.childCount++;
return node;
}
bool KeyValue::IsValid() const
{
// The invalid KV is always invalid, and infinite loops are invalid
return this != &GetInvalid()
&& next != this && data.node.children != this && data.node.lastChild != this;
}
KeyValue::KeyValue(bool invalid) : data({})
{
if (invalid)
{
// Zero the key and root
key = { nullptr, 0 };
rootNode = nullptr;
// Explicitly invalid data
next = this;
isNode = true;
data.node.children = this;
data.node.lastChild = this;
data.node.childCount = 0;
}
}
KeyValue::~KeyValue()
{
if (isNode && data.node.childCount > 0 && rootNode->solidified)
{
delete[] data.node.children;
}
}
KeyValue* KeyValue::CreateKVPair(kvString_t keyName, kvString_t string, KeyValuePool<KeyValue>& pool)
{
KeyValue* kv = pool.Create();
kv->key = keyName;
kv->data.leaf.value = string;
kv->isNode = false;
kv->rootNode = rootNode;
return kv;
}
KeyValueErrorCode KeyValue::Parse(const char*& str, const bool isRoot)
{
KeyValue* lastKV = nullptr;
char c;
for (;;)
{
SkipWhitespace(str);
c = *str;
kvString_t pairkey;
// Parse the key out
switch (c)
{
case STRING_CONTAINER:
{
KeyValueErrorCode error = ReadQuotedString(str, pairkey);
if (error != KeyValueErrorCode::NONE)
return error;
break;
}
case BLOCK_END:
// Make sure we skip a char here so that the above layer doesn't read it!
str++;
// If we hit a block end as root, we've got a syntax error on our hands... Let's skedaddle!
if (isRoot)
return KeyValueErrorCode::UNEXPECTED_END_OF_BLOCK;
// Otherwise, we should be totally fine to just return at this point
goto end;
// Did we hit the end of the file?
case 0:
// If we're root and at the end of the file after this whitespace skip, that means there's no next kv pair. End now
if (isRoot)
goto end;
// If we're not root and at the end of the file after this whitespace skip, we've failed to find the block end
return KeyValueErrorCode::INCOMPLETE_BLOCK;
case BLOCK_BEGIN:
return KeyValueErrorCode::UNEXPECTED_START_OF_BLOCK;
#if ALLOW_QUOTELESS_STRINGS
// It's gotta be a quoteless string
default:
pairkey = ReadQuotelessString(str);
break;
#endif
}
rootNode->bufferSize += pairkey.length + 1; // + 1 for \0
// We've got our key, so let's find its value
SkipWhitespace(str);
c = *str;
KeyValue* pair;
// Same kinda stuff as earlier but a bit different for the value
switch (c)
{
case STRING_CONTAINER:
{
kvString_t stringValue;
KeyValueErrorCode error = ReadQuotedString(str, stringValue);
if (error != KeyValueErrorCode::NONE)
return error;
pair = CreateKVPair(pairkey, stringValue, rootNode->readPool);
rootNode->bufferSize += stringValue.length + 1; // + 1 for \0
break;
}
case BLOCK_BEGIN:
{
pair = rootNode->readPool.Create();
pair->rootNode = rootNode;
//skip over the BLOCK_BEGIN
str++;
pair->isNode = true;
pair->data.node = { nullptr, nullptr, 0 };
KeyValueErrorCode error = pair->Parse(str, false);
if (error != KeyValueErrorCode::NONE)
return error;
pair->key = pairkey;
break;
}
case 0:
// Hit EOF before the value
return KeyValueErrorCode::INCOMPLETE_PAIR;
case BLOCK_END:
return KeyValueErrorCode::UNEXPECTED_END_OF_BLOCK;
#if ALLOW_QUOTELESS_STRINGS
// It's gotta be a quoteless string
default:
{
kvString_t stringValue = ReadQuotelessString(str);
pair = CreateKVPair(pairkey, stringValue, rootNode->readPool);
rootNode->bufferSize += stringValue.length + 1; // + 1 for \0
break;
}
#endif
}
data.node.childCount++;
if (lastKV)
{
lastKV->next = pair;
}
else
{
data.node.children = pair;
}
lastKV = pair;
}
end:
if (lastKV)
{
lastKV->next = nullptr;
data.node.lastChild = lastKV;
}
return KeyValueErrorCode::NONE;
}
// Copies all of the keys and values out of the input string and copies them all into a massive buffer.
void KeyValue::BuildData(char*& destBuffer)
{
KeyValue* current = data.node.children;
for (size_t i = 0; i < data.node.childCount; i++)
{
// Copy the string in, null terminate it, and increment the destBuffer
memcpy(destBuffer, current->key.string, current->key.length);
destBuffer[current->key.length] = '\0';
current->key.string = destBuffer;
destBuffer += current->key.length + 1;
if (current->isNode)
{
if (current->data.node.childCount > 0)
{
current->BuildData(destBuffer);
}
}
else
{
memcpy(destBuffer, current->data.leaf.value.string, current->data.leaf.value.length);
destBuffer[current->data.leaf.value.length] = '\0';
current->data.leaf.value.string = destBuffer;
destBuffer += current->data.leaf.value.length + 1;
}
current = current->next;
}
}
// Copies memory and shifts the inputs
void CopyAndShift(char*& dest, char* src, size_t& destLength, size_t srcLength)
{
size_t minLength = std::min(destLength, srcLength);
memcpy(dest, src, minLength);
destLength -= minLength;
dest += minLength;
}
void CopyAndShift(char*& dest, const char* src, size_t& destLength, size_t srcLength)
{
return CopyAndShift(dest, const_cast<char*>(src), destLength, srcLength);
}
void TabFill(char*& str, size_t& maxLength, int tabCount)
{
for (int i = 0; i < tabCount; i++)
{
CopyAndShift(str, TAB_STYLE, maxLength, sizeof(TAB_STYLE) - 1); // - 1 due to null terminator
}
}
void KeyValue::ToString(char*& str, size_t& maxLength, int tabCount) const
{
// Make a solidified version?
for (KeyValue* current = data.node.children; current; current = current->next)
{
TabFill(str, maxLength, tabCount);
// Copy in the key
CopyAndShift(str, "\"", maxLength, 1);
CopyAndShift(str, current->key.string, maxLength, current->key.length);
CopyAndShift(str, "\" ", maxLength, 2);
if (current->isNode)
{
CopyAndShift(str, "\n", maxLength, 1);
TabFill(str, maxLength, tabCount);
CopyAndShift(str, "{\n", maxLength, 2);
current->ToString(str, maxLength, tabCount + 1);
TabFill(str, maxLength, tabCount);
CopyAndShift(str, "}\n", maxLength, 2);
}
else
{
// Copy in the value
CopyAndShift(str, "\"", maxLength, 1);
CopyAndShift(str, current->data.leaf.value.string, maxLength, current->data.leaf.value.length);
CopyAndShift(str, "\"\n", maxLength, 2);
}
}
}
char* KeyValue::ToString() const
{
// Length of all kvs + 1 for the null terminator
size_t length = ToStringLength(0) + 1;
char* str = new char[length];
ToString(str, length);
// Null terminate it
str[length - 1] = 0;
return str;
}
size_t KeyValue::ToStringLength(int tabCount) const
{
size_t len = 0;
for (KeyValue* current = data.node.children; current; current = current->next)
{
len += tabCount * sizeof(TAB_STYLE);
// String container + Key + String container
len += sizeof(STRING_CONTAINER) + current->key.length + sizeof(STRING_CONTAINER);
if (current->isNode)
{
// If we have kids, new line + tabs + start block + new line
len += 1 + tabCount * sizeof(TAB_STYLE) + sizeof(BLOCK_BEGIN) + 1;
len += current->ToStringLength(tabCount + 1);
// End line + tabs + end block + end line
len += 1 + tabCount * sizeof(TAB_STYLE) + sizeof(BLOCK_END) + 1;
}
else
{
// If we don't have any children, we just have a value
// Space + string container + value + string container + new line
len += 1 + sizeof(STRING_CONTAINER) + current->data.leaf.value.length + sizeof(STRING_CONTAINER) + 1;
}
}
return len;
}