-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDict.cpp
654 lines (521 loc) · 14.1 KB
/
Dict.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
#include "Namespaces.h"
#include "Types.h"
#include "MathUtil.h"
#include "Hash.h"
#include "Dict.h"
#include "Char.h"
/*! @brief Fetch value by the key or throw exception if key not found
* @return Item associated with the key
* @throws KeyErrorExeption in case no key found
*/
Variant&
BaseDictManager::operator [ ](const char *akey)
{
DictItem *item;
Variant *tmp;
tmp = lookUp(akey);
if(!tmp) {
item = addItem(akey, Variant());
tmp = &item->value;
}
return *tmp;
}
/*! @brief Fetch value by the key or throw exception if key not found (const version)
* @return Item associated with the key
* @throws KeyErrorException in case no key found
*/
const Variant&
BaseDictManager::operator [ ](const char *akey) const
{
const Variant *tmp;
tmp = lookUp(akey);
if(!tmp)
throw KeyErrorException(akey);
return *tmp;
}
const Variant&
BaseDictManager::get(const char *akey, const Variant& _default) const
{
const Variant *tmp;
tmp = lookUp(akey);
return tmp? *tmp: _default;
}
void
BaseDictManager::copy(const BaseDictManager& other, const char *key)
{
const Variant *tmp;
tmp = other.lookUp(key);
if(tmp)
addItem(key, *tmp);
}
/*! @brief Remove item with a given key from dictionary
* @param akey Key of the item to remove
* @return true if item removed, false otherwise (not found, etc.)
*/
bool
BaseDictManager::removeItem(const char *akey)
{
REF_PARAM(akey);
throw NotImplementedException();
}
/*! @brief Add item with given key to dicitionary
* @param akey Key of the item
* @param val Item value
* @return Pointer to added item or NULL on error
*/
DictItem *
BaseDictManager::addItem(const char *akey, const Variant& val)
{
REF_PARAM(akey);
REF_PARAM(val);
throw NotImplementedException();
}
/*! @brief Looks up item in dictionary by given key
* @param akey Key of item to look up
* @return Pointer to item vaule or NULL if item not found
*/
Variant *
BaseDictManager::lookUp(const char *akey)
{
REF_PARAM(akey);
throw NotImplementedException();
}
const Variant *
BaseDictManager::lookUp(const char *akey) const
{
REF_PARAM(akey);
throw NotImplementedException();
}
/*! @brief Placeholder to enumerate through dictionary key/value pairs
* @param callback User-supplied callback function
* @param cbkData Custom data passed to user-supplied callback function
* @return Number of iterations made or -1 on error
*/
int
BaseDictManager::EnumItems(DictEnumCallback callback, void *cbkData)
{
REF_PARAM(callback);
REF_PARAM(cbkData);
throw NotImplementedException();
}
/*! @brief Linear dictionary manager constructor
*/
LinearDictManager::LinearDictManager()
{
lsdhAlloc = 8;
lsdhSize = 0;
lsdhHead = new DictItem[lsdhAlloc];
}
/*! @brief Linear dictionary manager destructor
*/
LinearDictManager::~LinearDictManager()
{
delete[] lsdhHead;
}
size_t
LinearDictManager::allocItems(size_t nItems)
{
DictItem *temp;
size_t nsize, i;
if(lsdhSize+nItems >= lsdhAlloc) {
nsize = _round(lsdhSize + nItems, 8);
// FIXME Make a copying of existing items more wise
temp = new DictItem[nsize];
for(i=0; i<lsdhSize; i++)
temp[i] = lsdhHead[i];
delete[] lsdhHead;
lsdhHead = temp;
lsdhAlloc = nsize;
}
return lsdhSize++;
}
/*! @brief Remove item from dictionary
* @param akey Key of the item to remove
* @returns true on success, false if item with given key not found in dictionary
*/
bool
LinearDictManager::removeItem(const char *akey)
{
REF_PARAM(akey);
throw NotImplementedException();
}
/*! @brief Add a key/value pair to the dictionary
* @param akey Key string
* @param val Value to add
* @return true if item sucessfully added, or false otherwise (no memory, etc.)
*/
DictItem *
LinearDictManager::addItem(const char *akey, const Variant& val)
{
size_t npos;
try {
npos = allocItems(1);
}
catch(Exception e) {
return false;
}
lsdhHead[npos] = DictItem(akey, val);
return &lsdhHead[npos];
}
/*! @brief Look up item in dictionary by given key
* @param akey Key of the item to look up
* @returns Pointer to item, or NULL if item not found in dictionary
*/
Variant *
LinearDictManager::lookUp(const char *akey)
{
size_t i;
for(i=0; i<lsdhSize; i++)
if(!strcmp(lsdhHead[i].getKey(), akey))
return &lsdhHead[i].value;
return NULL;
}
const Variant *
LinearDictManager::lookUp(const char *akey) const
{
size_t i;
for(i=0; i<lsdhSize; i++)
if(!strcmp(lsdhHead[i].getKey(), akey))
return &lsdhHead[i].value;
return NULL;
}
/**
* Enumerates through dictionary key/value pairs, passing it to callback function
* @param callback User-supplied callback function
* @param data Custom data passed to user callback function
* @return Number of items enumerated or -1 on error
*/
int
LinearDictManager::EnumItems(DictEnumCallback callback, void *data)
{
size_t i;
for(i=0; i<lsdhSize; i++)
if(!callback(lsdhHead[i], data))
break;
return (int)i;
}
HashDictBucket::HashDictBucket(): hdbNext(NULL), hdbCount(0)
{
/* Dummy */
}
HashDictBucket::~HashDictBucket()
{
HashDictBucket *bucket, *last;
/* Free chained buckets */
for(last=NULL, bucket=hdbNext; bucket; bucket=last) {
last = bucket->hdbNext;
delete bucket;
}
}
DictItem&
HashDictBucket::operator [ ](int i)
{
if(i<0 || i>NUM_DICT_BUCKET_ITEMS)
throw IndexErrorException();
return hdbItems[i];
}
const DictItem&
HashDictBucket::operator [ ](int i) const
{
if(i<0 || i>NUM_DICT_BUCKET_ITEMS)
throw IndexErrorException();
return hdbItems[i];
}
DictItem *
HashDictBucket::lookUp(const char *akey)
{
int i;
HashDictBucket *bucket;
for(bucket=this; bucket; bucket=bucket->hdbNext) {
for(i=0; i<bucket->hdbCount; i++) {
if(!strcmp(akey, bucket->hdbItems[i].getKey()))
return &bucket->hdbItems[i];
}
}
return NULL;
}
DictItem *
HashDictBucket::addItem(const DictItem& src)
{
HashDictBucket *ptr, *last;
DictItem *item;
/* Walk the bucket chain in order to find free place for item
*/
for(last=NULL, ptr=this; ptr; last=ptr, ptr=ptr->hdbNext)
if(ptr->hdbCount < NUM_DICT_BUCKET_ITEMS)
break;
/* If no room found, allocate a new bucket and add it to the
* bucket chain
*/
if(!ptr)
ptr = (last->hdbNext = new HashDictBucket());
/* Copy item into bucket */
item = &ptr->hdbItems[ptr->hdbCount++];
*item = src;
return item;
}
int
HashDictBucket::EnumItems(DictEnumCallback callback, void *data)
{
HashDictBucket *bucket;
int i, total;
for(total=0, bucket=this; bucket; bucket=bucket->hdbNext)
for(i=0; i<bucket->hdbCount; i++, total++)
if(!callback(bucket->hdbItems[i], data))
return total;
return total;
}
HashDictManager::HashDictManager()
{
int i;
for(i=0; i<DICT_HASH_SIZE; i++)
hsdhHash[i] = NULL;
}
HashDictManager::~ HashDictManager()
{
int i;
for(i=0; i<DICT_HASH_SIZE; i++)
delete hsdhHash[i];
}
DictItem *
HashDictManager::addItem(const char *akey, const Variant& val)
{
unsigned int hk;
hk = hash_calcstr(akey, DICT_HASH_SIZE);
if(hsdhHash[hk] == NULL)
hsdhHash[hk] = new HashDictBucket();
return hsdhHash[hk]->addItem(DictItem(akey, val));
}
bool
HashDictManager::removeItem(const char *akey)
{
REF_PARAM(akey);
throw NotImplementedException();
}
Variant *
HashDictManager::lookUp(const char *akey)
{
unsigned int hk;
DictItem *item;
hk = hash_calcstr(akey, DICT_HASH_SIZE);
if(hsdhHash[hk] == NULL)
return NULL;
item = hsdhHash[hk]->lookUp(akey);
if(item == NULL)
return NULL;
return &item->value;
}
const Variant *
HashDictManager::lookUp(const char *akey) const
{
unsigned int hk;
DictItem *item;
hk = hash_calcstr(akey, DICT_HASH_SIZE);
if(hsdhHash[hk] == NULL)
return NULL;
item = hsdhHash[hk]->lookUp(akey);
if(item == NULL)
return NULL;
return &item->value;
}
int
HashDictManager::EnumItems(DictEnumCallback callback, void *data)
{
int i, count, total;
for(total=0, i=0; i<DICT_HASH_SIZE; i++) {
if(hsdhHash[i]) {
/* Enumerate items in bucket(s) starting in current hash slot
*/
count = hsdhHash[i]->EnumItems(callback, data);
if(count < 0) {
total += -count;
break;
}
total += count;
}
}
return total;
}
char *
Dict::haveMacro(const char *str, size_t *len, char **name, size_t *namelen)
{
char *pm, *pm2;
char endch;
int i;
pm = const_cast<char *>(strchr(str, '$'));
pm2 = const_cast<char *>(strchr(str, '%'));
if(!pm && !pm2)
return NULL;
if(!pm) {
pm = pm2;
} else {
if(pm2 && (pm2 < pm))
pm = pm2;
}
/* $$ represents single '$', %% - '%', so eat it
*/
if(pm[0]==pm[1]) {
*len = 2;
*name = &pm[1];
*namelen = 1;
return pm;
}
/* After macro character we expect macro name enclosed in
* curly braces {} or simple braces (), or sequence of
* alphanumeric characters, possibly starting with underscore '_'
* and separated by '-', '.' or '_'
*/
if(pm[1]=='_' || _is_alnum(pm[1])) {
for(i=1; _is_ident_char(pm[i]); i++) {
/* Don't allow '--' and '..' sequences
*/
if(pm[i]=='-' || pm[i]=='.')
if(pm[i]==pm[i+1])
break;
}
*len = i;
*name = &pm[1];
*namelen = i-1;
return pm;
}
if(pm[1]!='(' && pm[1]!='{')
return NULL;
/* Scan string until we reach EOS marker or found macro separator
*/
endch = (pm[1]=='(')? ')': '}';
for(i=2; pm[i]!='\0'; i++)
if(pm[i] == endch)
break;
if(pm[i] != endch)
return NULL;
*name = &pm[2];
*namelen = i-2;
*len = i+1;
return pm;
}
void
Dict::makeSpace(size_t cch)
{
char *temp;
size_t cb;
size_t blk, rem;
if(cch == 0)
return;
if((xlatPtr+cch) >= xlatAlloc) {
blk = (xlatPtr + cch) / DICT_XLAT_PAGESIZE;
rem = (xlatPtr + cch) % DICT_XLAT_PAGESIZE;
cb = (blk * DICT_XLAT_PAGESIZE) + (rem? DICT_XLAT_PAGESIZE: 0);
temp = (char *)realloc(xlatBuf, cb*sizeof(xlatBuf[0]));
if(!temp)
/* probably will never happen on modern OS-es */
throw MemoryErrorException();
xlatBuf = temp;
xlatAlloc = cb;
}
}
/**
* Add string to the buffer
* @param str Pointer to string
* @param len number of characters in the string
* @throws MemoryErrorException In case of not enough memory
*/
void
Dict::addChars(const char *str, size_t len)
{
if(len > 0) {
makeSpace(len); // throws an exception in case of memory error
#ifdef _MSC_VER
#if _MSC_VER > 1310
strncpy_s(&xlatBuf[xlatPtr], xlatAlloc-xlatPtr, str, len);
#else
strncpy(&xlatBuf[xlatPtr], str, len);
#endif
#else
strncpy(&xlatBuf[xlatPtr], str, len);
#endif
xlatPtr += len;
}
}
void
Dict::expand(const char *str, size_t len)
{
char temp[MAX_MACRO_LEN+1];
char *pm, *name, *stritem;
size_t mlen, cch, namelen;
Variant *item;
/* find macro name in given string
*/
while((len != 0) && (pm = haveMacro(str, &mlen, &name, &namelen))!=NULL) {
/* Found macro. Split the string into two parts: a string before
* macro and macro definition
*/
addChars(str, pm-str);
/* Avoid buffer overflows: use maximum MAX_MACRO_LEN
* characters of macro name
*/
if(namelen > MAX_MACRO_LEN)
namelen = MAX_MACRO_LEN;
/* Check for special case: %% or $$ macros represents % and $ respectively
*/
if(namelen==1 && (*name=='%' || *name=='$')) {
addChars(name, namelen);
} else {
/* Look up and fetch macro value
*/
#if _MSC_VER >= 1400
strncpy_s(temp, sizeof(temp)-1, name, namelen);
temp[namelen] = '\0';
#else
strncpy(temp, name, namelen);
temp[namelen] = '\0';
#endif
item = lookUp(temp);
if(item) {
stritem = (char *)(*item);
if(stritem)
expand(stritem, strlen(stritem));
} else {
addChars(pm, mlen);
}
}
cch = &pm[mlen] - str;
str = &pm[mlen];
len -= cch;
}
/* Add remaining characters */
addChars(str, len);
}
/*! @brief Translates given string with regards to nested macro definitions
* @param buf Destination buffer or NULL if buffer need to be dynamically allocated
* @param cch Number of characters in destination buffer or 0
* @param str String to be translated
* @return Translated string or NULL on error
* Caller have multiple choices:
* 1) Supply buffer address and size - resulting string will be truncated if his length
* exceeds specified limits. If size are 0 then function will silently fail.
* 2) Support NULL as buffer address and non-zero 'cch' size - fixed-size buffer will be
* allocated and string will be truncated if length exceeds limits.
* 3) Supply NULL as buffer address and 0 as buffer size - buffer will be allocated and
* dynamically grow to accomodate resulting string length.
*/
char *
Dict::xlat(const char *str, u_int flags)
{
Variant *item;
char *itemstr;
REF_PARAM(flags); // for future use. see XLAT_PATH_SEMANTICS
resetXlat();
item = lookUp(str);
if(!item) {
/* If no such key found, try to expand possible macros inside the string */
expand(str, strlen(str));
} else {
/* Convert item to string and perform macro expansion */
itemstr = (char *)(*item);
expand(itemstr, strlen(itemstr));
}
/* Add string termination character */
makeSpace(1);
xlatBuf[xlatPtr] = '\0';
return xlatBuf;
}