-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcutil.c
10482 lines (9556 loc) · 288 KB
/
tcutil.c
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
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*************************************************************************************************
* The utility API of Tokyo Cabinet
* Copyright (C) 2006-2009 Mikio Hirabayashi
* This file is part of Tokyo Cabinet.
* Tokyo Cabinet is free software; you can redistribute it and/or modify it under the terms of
* the GNU Lesser General Public License as published by the Free Software Foundation; either
* version 2.1 of the License or any later version. Tokyo Cabinet is distributed in the hope
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
* You should have received a copy of the GNU Lesser General Public License along with Tokyo
* Cabinet; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA.
*************************************************************************************************/
#include "tcutil.h"
#include "myconf.h"
#include "md5.h"
/*************************************************************************************************
* basic utilities
*************************************************************************************************/
/* String containing the version information. */
const char *tcversion = _TC_VERSION;
/* Call back function for handling a fatal error. */
void (*tcfatalfunc)(const char *message) = NULL;
/* Allocate a region on memory. */
void *tcmalloc(size_t size){
assert(size > 0 && size < INT_MAX);
char *p;
TCMALLOC(p, size);
return p;
}
/* Allocate a nullified region on memory. */
void *tccalloc(size_t nmemb, size_t size){
assert(nmemb > 0 && size < INT_MAX && size > 0 && size < INT_MAX);
char *p;
TCCALLOC(p, nmemb, size);
return p;
}
/* Re-allocate a region on memory. */
void *tcrealloc(void *ptr, size_t size){
assert(size >= 0 && size < INT_MAX);
char *p;
TCREALLOC(p, ptr, size);
return p;
}
/* Duplicate a region on memory. */
void *tcmemdup(const void *ptr, size_t size){
assert(ptr && size >= 0);
char *p;
TCMALLOC(p, size + 1);
memcpy(p, ptr, size);
p[size] = '\0';
return p;
}
/* Duplicate a string on memory. */
char *tcstrdup(const void *str){
assert(str);
int size = strlen(str);
char *p;
TCMALLOC(p, size + 1);
memcpy(p, str, size);
p[size] = '\0';
return p;
}
/* Free a region on memory. */
void tcfree(void *ptr){
TCFREE(ptr);
}
/*************************************************************************************************
* extensible string
*************************************************************************************************/
#define TCXSTRUNIT 12 // allocation unit size of an extensible string
/* private function prototypes */
static void tcvxstrprintf(TCXSTR *xstr, const char *format, va_list ap);
/* Create an extensible string object. */
TCXSTR *tcxstrnew(void){
TCXSTR *xstr;
TCMALLOC(xstr, sizeof(*xstr));
TCMALLOC(xstr->ptr, TCXSTRUNIT);
xstr->size = 0;
xstr->asize = TCXSTRUNIT;
xstr->ptr[0] = '\0';
return xstr;
}
/* Create an extensible string object from a character string. */
TCXSTR *tcxstrnew2(const char *str){
assert(str);
TCXSTR *xstr;
TCMALLOC(xstr, sizeof(*xstr));
int size = strlen(str);
int asize = tclmax(size + 1, TCXSTRUNIT);
TCMALLOC(xstr->ptr, asize);
xstr->size = size;
xstr->asize = asize;
memcpy(xstr->ptr, str, size + 1);
return xstr;
}
/* Create an extensible string object with the initial allocation size. */
TCXSTR *tcxstrnew3(int asiz){
assert(asiz >= 0);
asiz = tclmax(asiz, TCXSTRUNIT);
TCXSTR *xstr;
TCMALLOC(xstr, sizeof(*xstr));
TCMALLOC(xstr->ptr, asiz);
xstr->size = 0;
xstr->asize = asiz;
xstr->ptr[0] = '\0';
return xstr;
}
/* Copy an extensible string object. */
TCXSTR *tcxstrdup(const TCXSTR *xstr){
assert(xstr);
TCXSTR *nxstr;
TCMALLOC(nxstr, sizeof(*nxstr));
int asize = tclmax(xstr->size + 1, TCXSTRUNIT);
TCMALLOC(nxstr->ptr, asize);
nxstr->size = xstr->size;
nxstr->asize = asize;
memcpy(nxstr->ptr, xstr->ptr, xstr->size + 1);
return nxstr;
}
/* Delete an extensible string object. */
void tcxstrdel(TCXSTR *xstr){
assert(xstr);
TCFREE(xstr->ptr);
TCFREE(xstr);
}
/* Concatenate a region to the end of an extensible string object. */
void tcxstrcat(TCXSTR *xstr, const void *ptr, int size){
assert(xstr && ptr && size >= 0);
int nsize = xstr->size + size + 1;
if(xstr->asize < nsize){
while(xstr->asize < nsize){
xstr->asize *= 2;
if(xstr->asize < nsize) xstr->asize = nsize;
}
TCREALLOC(xstr->ptr, xstr->ptr, xstr->asize);
}
memcpy(xstr->ptr + xstr->size, ptr, size);
xstr->size += size;
xstr->ptr[xstr->size] = '\0';
}
/* Concatenate a character string to the end of an extensible string object. */
void tcxstrcat2(TCXSTR *xstr, const char *str){
assert(xstr && str);
int size = strlen(str);
int nsize = xstr->size + size + 1;
if(xstr->asize < nsize){
while(xstr->asize < nsize){
xstr->asize *= 2;
if(xstr->asize < nsize) xstr->asize = nsize;
}
TCREALLOC(xstr->ptr, xstr->ptr, xstr->asize);
}
memcpy(xstr->ptr + xstr->size, str, size + 1);
xstr->size += size;
}
/* Get the pointer of the region of an extensible string object. */
const void *tcxstrptr(const TCXSTR *xstr){
assert(xstr);
return xstr->ptr;
}
/* Get the size of the region of an extensible string object. */
int tcxstrsize(const TCXSTR *xstr){
assert(xstr);
return xstr->size;
}
/* Clear an extensible string object. */
void tcxstrclear(TCXSTR *xstr){
assert(xstr);
xstr->size = 0;
xstr->ptr[0] = '\0';
}
/* Perform formatted output into an extensible string object. */
void tcxstrprintf(TCXSTR *xstr, const char *format, ...){
assert(xstr && format);
va_list ap;
va_start(ap, format);
tcvxstrprintf(xstr, format, ap);
va_end(ap);
}
/* Allocate a formatted string on memory. */
char *tcsprintf(const char *format, ...){
assert(format);
TCXSTR *xstr = tcxstrnew();
va_list ap;
va_start(ap, format);
tcvxstrprintf(xstr, format, ap);
va_end(ap);
return tcxstrtomalloc(xstr);
}
/* Perform formatted output into an extensible string object. */
static void tcvxstrprintf(TCXSTR *xstr, const char *format, va_list ap){
assert(xstr && format);
while(*format != '\0'){
if(*format == '%'){
char cbuf[TCNUMBUFSIZ];
cbuf[0] = '%';
int cblen = 1;
int lnum = 0;
format++;
while(strchr("0123456789 .+-hlLz", *format) && *format != '\0' &&
cblen < TCNUMBUFSIZ - 1){
if(*format == 'l' || *format == 'L') lnum++;
cbuf[cblen++] = *(format++);
}
cbuf[cblen++] = *format;
cbuf[cblen] = '\0';
int tlen;
char *tmp, tbuf[TCNUMBUFSIZ*4];
switch(*format){
case 's':
tmp = va_arg(ap, char *);
if(!tmp) tmp = "(null)";
tcxstrcat2(xstr, tmp);
break;
case 'd':
if(lnum >= 2){
tlen = sprintf(tbuf, cbuf, va_arg(ap, long long));
} else if(lnum >= 1){
tlen = sprintf(tbuf, cbuf, va_arg(ap, long));
} else {
tlen = sprintf(tbuf, cbuf, va_arg(ap, int));
}
TCXSTRCAT(xstr, tbuf, tlen);
break;
case 'o': case 'u': case 'x': case 'X': case 'c':
if(lnum >= 2){
tlen = sprintf(tbuf, cbuf, va_arg(ap, unsigned long long));
} else if(lnum >= 1){
tlen = sprintf(tbuf, cbuf, va_arg(ap, unsigned long));
} else {
tlen = sprintf(tbuf, cbuf, va_arg(ap, unsigned int));
}
TCXSTRCAT(xstr, tbuf, tlen);
break;
case 'e': case 'E': case 'f': case 'g': case 'G':
if(lnum >= 1){
tlen = snprintf(tbuf, sizeof(tbuf), cbuf, va_arg(ap, long double));
} else {
tlen = snprintf(tbuf, sizeof(tbuf), cbuf, va_arg(ap, double));
}
if(tlen < 0 || tlen > sizeof(tbuf)){
tbuf[sizeof(tbuf)-1] = '*';
tlen = sizeof(tbuf);
}
TCXSTRCAT(xstr, tbuf, tlen);
break;
case '@':
tmp = va_arg(ap, char *);
if(!tmp) tmp = "(null)";
while(*tmp){
switch(*tmp){
case '&': TCXSTRCAT(xstr, "&", 5); break;
case '<': TCXSTRCAT(xstr, "<", 4); break;
case '>': TCXSTRCAT(xstr, ">", 4); break;
case '"': TCXSTRCAT(xstr, """, 6); break;
default:
if(!((*tmp >= 0 && *tmp <= 0x8) || (*tmp >= 0x0e && *tmp <= 0x1f)))
TCXSTRCAT(xstr, tmp, 1);
break;
}
tmp++;
}
break;
case '?':
tmp = va_arg(ap, char *);
if(!tmp) tmp = "(null)";
while(*tmp){
unsigned char c = *(unsigned char *)tmp;
if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
(c >= '0' && c <= '9') || (c != '\0' && strchr("_-.", c))){
TCXSTRCAT(xstr, tmp, 1);
} else {
tlen = sprintf(tbuf, "%%%02X", c);
TCXSTRCAT(xstr, tbuf, tlen);
}
tmp++;
}
break;
case 'b':
if(lnum >= 2){
tlen = tcnumtostrbin(va_arg(ap, unsigned long long), tbuf,
tcatoi(cbuf + 1), (cbuf[1] == '0') ? '0' : ' ');
} else if(lnum >= 1){
tlen = tcnumtostrbin(va_arg(ap, unsigned long), tbuf,
tcatoi(cbuf + 1), (cbuf[1] == '0') ? '0' : ' ');
} else {
tlen = tcnumtostrbin(va_arg(ap, unsigned int), tbuf,
tcatoi(cbuf + 1), (cbuf[1] == '0') ? '0' : ' ');
}
TCXSTRCAT(xstr, tbuf, tlen);
break;
case '%':
TCXSTRCAT(xstr, "%", 1);
break;
}
} else {
TCXSTRCAT(xstr, format, 1);
}
format++;
}
}
/*************************************************************************************************
* extensible string (for experts)
*************************************************************************************************/
/* Convert an extensible string object into a usual allocated region. */
void *tcxstrtomalloc(TCXSTR *xstr){
assert(xstr);
char *ptr;
ptr = xstr->ptr;
TCFREE(xstr);
return ptr;
}
/* Create an extensible string object from an allocated region. */
TCXSTR *tcxstrfrommalloc(void *ptr, int size){
TCXSTR *xstr;
TCMALLOC(xstr, sizeof(*xstr));
TCREALLOC(xstr->ptr, ptr, size + 1);
xstr->ptr[size] = '\0';
xstr->size = size;
xstr->asize = size;
return xstr;
}
/*************************************************************************************************
* array list
*************************************************************************************************/
#define TCLISTUNIT 64 // allocation unit number of a list handle
/* private function prototypes */
static int tclistelemcmp(const void *a, const void *b);
static int tclistelemcmpci(const void *a, const void *b);
/* Create a list object. */
TCLIST *tclistnew(void){
TCLIST *list;
TCMALLOC(list, sizeof(*list));
list->anum = TCLISTUNIT;
TCMALLOC(list->array, sizeof(list->array[0]) * list->anum);
list->start = 0;
list->num = 0;
return list;
}
/* Create a list object. */
TCLIST *tclistnew2(int anum){
TCLIST *list;
TCMALLOC(list, sizeof(*list));
if(anum < 1) anum = 1;
list->anum = anum;
TCMALLOC(list->array, sizeof(list->array[0]) * list->anum);
list->start = 0;
list->num = 0;
return list;
}
/* Create a list object with initial string elements. */
TCLIST *tclistnew3(const char *str, ...){
TCLIST *list = tclistnew();
if(str){
tclistpush2(list, str);
va_list ap;
va_start(ap, str);
const char *elem;
while((elem = va_arg(ap, char *)) != NULL){
tclistpush2(list, elem);
}
va_end(ap);
}
return list;
}
/* Copy a list object. */
TCLIST *tclistdup(const TCLIST *list){
assert(list);
int num = list->num;
if(num < 1) return tclistnew();
const TCLISTDATUM *array = list->array + list->start;
TCLIST *nlist;
TCMALLOC(nlist, sizeof(*nlist));
TCLISTDATUM *narray;
TCMALLOC(narray, sizeof(list->array[0]) * num);
for(int i = 0; i < num; i++){
int size = array[i].size;
TCMALLOC(narray[i].ptr, tclmax(size + 1, TCXSTRUNIT));
memcpy(narray[i].ptr, array[i].ptr, size + 1);
narray[i].size = array[i].size;
}
nlist->anum = num;
nlist->array = narray;
nlist->start = 0;
nlist->num = num;
return nlist;
}
/* Delete a list object. */
void tclistdel(TCLIST *list){
assert(list);
TCLISTDATUM *array = list->array;
int end = list->start + list->num;
for(int i = list->start; i < end; i++){
TCFREE(array[i].ptr);
}
TCFREE(list->array);
TCFREE(list);
}
/* Get the number of elements of a list object. */
int tclistnum(const TCLIST *list){
assert(list);
return list->num;
}
/* Get the pointer to the region of an element of a list object. */
const void *tclistval(const TCLIST *list, int index, int *sp){
assert(list && index >= 0 && sp);
if(index >= list->num) return NULL;
index += list->start;
*sp = list->array[index].size;
return list->array[index].ptr;
}
/* Get the string of an element of a list object. */
const char *tclistval2(const TCLIST *list, int index){
assert(list && index >= 0);
if(index >= list->num) return NULL;
index += list->start;
return list->array[index].ptr;
}
/* Add an element at the end of a list object. */
void tclistpush(TCLIST *list, const void *ptr, int size){
assert(list && ptr && size >= 0);
int index = list->start + list->num;
if(index >= list->anum){
list->anum += list->num + 1;
TCREALLOC(list->array, list->array, list->anum * sizeof(list->array[0]));
}
TCLISTDATUM *array = list->array;
TCMALLOC(array[index].ptr, tclmax(size + 1, TCXSTRUNIT));
memcpy(array[index].ptr, ptr, size);
array[index].ptr[size] = '\0';
array[index].size = size;
list->num++;
}
/* Add a string element at the end of a list object. */
void tclistpush2(TCLIST *list, const char *str){
assert(list && str);
int index = list->start + list->num;
if(index >= list->anum){
list->anum += list->num + 1;
TCREALLOC(list->array, list->array, list->anum * sizeof(list->array[0]));
}
int size = strlen(str);
TCLISTDATUM *array = list->array;
TCMALLOC(array[index].ptr, tclmax(size + 1, TCXSTRUNIT));
memcpy(array[index].ptr, str, size + 1);
array[index].size = size;
list->num++;
}
/* Remove an element of the end of a list object. */
void *tclistpop(TCLIST *list, int *sp){
assert(list && sp);
if(list->num < 1) return NULL;
int index = list->start + list->num - 1;
list->num--;
*sp = list->array[index].size;
return list->array[index].ptr;
}
/* Remove a string element of the end of a list object. */
char *tclistpop2(TCLIST *list){
assert(list);
if(list->num < 1) return NULL;
int index = list->start + list->num - 1;
list->num--;
return list->array[index].ptr;
}
/* Add an element at the top of a list object. */
void tclistunshift(TCLIST *list, const void *ptr, int size){
assert(list && ptr && size >= 0);
if(list->start < 1){
if(list->start + list->num >= list->anum){
list->anum += list->num + 1;
TCREALLOC(list->array, list->array, list->anum * sizeof(list->array[0]));
}
list->start = list->anum - list->num;
memmove(list->array + list->start, list->array, list->num * sizeof(list->array[0]));
}
int index = list->start - 1;
TCMALLOC(list->array[index].ptr, tclmax(size + 1, TCXSTRUNIT));
memcpy(list->array[index].ptr, ptr, size);
list->array[index].ptr[size] = '\0';
list->array[index].size = size;
list->start--;
list->num++;
}
/* Add a string element at the top of a list object. */
void tclistunshift2(TCLIST *list, const char *str){
assert(list && str);
if(list->start < 1){
if(list->start + list->num >= list->anum){
list->anum += list->num + 1;
TCREALLOC(list->array, list->array, list->anum * sizeof(list->array[0]));
}
list->start = list->anum - list->num;
memmove(list->array + list->start, list->array, list->num * sizeof(list->array[0]));
}
int index = list->start - 1;
int size = strlen(str);
TCMALLOC(list->array[index].ptr, tclmax(size + 1, TCXSTRUNIT));
memcpy(list->array[index].ptr, str, size + 1);
list->array[index].size = size;
list->start--;
list->num++;
}
/* Remove an element of the top of a list object. */
void *tclistshift(TCLIST *list, int *sp){
assert(list && sp);
if(list->num < 1) return NULL;
int index = list->start;
list->start++;
list->num--;
*sp = list->array[index].size;
void *rv = list->array[index].ptr;
if((list->start & 0xff) == 0 && list->start > (list->num >> 1)){
memmove(list->array, list->array + list->start, list->num * sizeof(list->array[0]));
list->start = 0;
}
return rv;
}
/* Remove a string element of the top of a list object. */
char *tclistshift2(TCLIST *list){
assert(list);
if(list->num < 1) return NULL;
int index = list->start;
list->start++;
list->num--;
void *rv = list->array[index].ptr;
if((list->start & 0xff) == 0 && list->start > (list->num >> 1)){
memmove(list->array, list->array + list->start, list->num * sizeof(list->array[0]));
list->start = 0;
}
return rv;
}
/* Add an element at the specified location of a list object. */
void tclistinsert(TCLIST *list, int index, const void *ptr, int size){
assert(list && index >= 0 && ptr && size >= 0);
if(index > list->num) return;
index += list->start;
if(list->start + list->num >= list->anum){
list->anum += list->num + 1;
TCREALLOC(list->array, list->array, list->anum * sizeof(list->array[0]));
}
memmove(list->array + index + 1, list->array + index,
sizeof(list->array[0]) * (list->start + list->num - index));
TCMALLOC(list->array[index].ptr, tclmax(size + 1, TCXSTRUNIT));
memcpy(list->array[index].ptr, ptr, size);
list->array[index].ptr[size] = '\0';
list->array[index].size = size;
list->num++;
}
/* Add a string element at the specified location of a list object. */
void tclistinsert2(TCLIST *list, int index, const char *str){
assert(list && index >= 0 && str);
if(index > list->num) return;
index += list->start;
if(list->start + list->num >= list->anum){
list->anum += list->num + 1;
TCREALLOC(list->array, list->array, list->anum * sizeof(list->array[0]));
}
memmove(list->array + index + 1, list->array + index,
sizeof(list->array[0]) * (list->start + list->num - index));
int size = strlen(str);
TCMALLOC(list->array[index].ptr, tclmax(size + 1, TCXSTRUNIT));
memcpy(list->array[index].ptr, str, size);
list->array[index].ptr[size] = '\0';
list->array[index].size = size;
list->num++;
}
/* Remove an element at the specified location of a list object. */
void *tclistremove(TCLIST *list, int index, int *sp){
assert(list && index >= 0 && sp);
if(index >= list->num) return NULL;
index += list->start;
void *rv = list->array[index].ptr;
*sp = list->array[index].size;
list->num--;
memmove(list->array + index, list->array + index + 1,
sizeof(list->array[0]) * (list->start + list->num - index));
return rv;
}
/* Remove a string element at the specified location of a list object. */
char *tclistremove2(TCLIST *list, int index){
assert(list && index >= 0);
if(index >= list->num) return NULL;
index += list->start;
void *rv = list->array[index].ptr;
list->num--;
memmove(list->array + index, list->array + index + 1,
sizeof(list->array[0]) * (list->start + list->num - index));
return rv;
}
/* Overwrite an element at the specified location of a list object. */
void tclistover(TCLIST *list, int index, const void *ptr, int size){
assert(list && index >= 0 && ptr && size >= 0);
if(index >= list->num) return;
index += list->start;
if(size > list->array[index].size)
TCREALLOC(list->array[index].ptr, list->array[index].ptr, size + 1);
memcpy(list->array[index].ptr, ptr, size);
list->array[index].size = size;
list->array[index].ptr[size] = '\0';
}
/* Overwrite a string element at the specified location of a list object. */
void tclistover2(TCLIST *list, int index, const char *str){
assert(list && index >= 0 && str);
if(index >= list->num) return;
index += list->start;
int size = strlen(str);
if(size > list->array[index].size)
TCREALLOC(list->array[index].ptr, list->array[index].ptr, size + 1);
memcpy(list->array[index].ptr, str, size + 1);
list->array[index].size = size;
}
/* Sort elements of a list object in lexical order. */
void tclistsort(TCLIST *list){
assert(list);
qsort(list->array + list->start, list->num, sizeof(list->array[0]), tclistelemcmp);
}
/* Search a list object for an element using liner search. */
int tclistlsearch(const TCLIST *list, const void *ptr, int size){
assert(list && ptr && size >= 0);
int end = list->start + list->num;
for(int i = list->start; i < end; i++){
if(list->array[i].size == size && !memcmp(list->array[i].ptr, ptr, size))
return i - list->start;
}
return -1;
}
/* Search a list object for an element using binary search. */
int tclistbsearch(const TCLIST *list, const void *ptr, int size){
assert(list && ptr && size >= 0);
TCLISTDATUM key;
key.ptr = (char *)ptr;
key.size = size;
TCLISTDATUM *res = bsearch(&key, list->array + list->start,
list->num, sizeof(list->array[0]), tclistelemcmp);
return res ? res - list->array - list->start : -1;
}
/* Clear a list object. */
void tclistclear(TCLIST *list){
assert(list);
TCLISTDATUM *array = list->array;
int end = list->start + list->num;
for(int i = list->start; i < end; i++){
TCFREE(array[i].ptr);
}
list->start = 0;
list->num = 0;
}
/* Serialize a list object into a byte array. */
void *tclistdump(const TCLIST *list, int *sp){
assert(list && sp);
const TCLISTDATUM *array = list->array;
int end = list->start + list->num;
int tsiz = 0;
for(int i = list->start; i < end; i++){
tsiz += array[i].size + sizeof(int);
}
char *buf;
TCMALLOC(buf, tsiz + 1);
char *wp = buf;
for(int i = list->start; i < end; i++){
int step;
TCSETVNUMBUF(step, wp, array[i].size);
wp += step;
memcpy(wp, array[i].ptr, array[i].size);
wp += array[i].size;
}
*sp = wp - buf;
return buf;
}
/* Create a list object from a serialized byte array. */
TCLIST *tclistload(const void *ptr, int size){
assert(ptr && size >= 0);
TCLIST *list;
TCMALLOC(list, sizeof(*list));
int anum = size / sizeof(int) + 1;
TCLISTDATUM *array;
TCMALLOC(array, sizeof(array[0]) * anum);
int num = 0;
const char *rp = ptr;
const char *ep = (char *)ptr + size;
while(rp < ep){
int step, vsiz;
TCREADVNUMBUF(rp, vsiz, step);
rp += step;
if(num >= anum){
anum *= 2;
TCREALLOC(array, array, anum * sizeof(array[0]));
}
TCMALLOC(array[num].ptr, tclmax(vsiz + 1, TCXSTRUNIT));
memcpy(array[num].ptr, rp, vsiz);
array[num].ptr[vsiz] = '\0';
array[num].size = vsiz;
num++;
rp += vsiz;
}
list->anum = anum;
list->array = array;
list->start = 0;
list->num = num;
return list;
}
/* Compare two list elements in lexical order.
`a' specifies the pointer to one element.
`b' specifies the pointer to the other element.
The return value is positive if the former is big, negative if the latter is big, 0 if both
are equivalent. */
static int tclistelemcmp(const void *a, const void *b){
assert(a && b);
unsigned char *ao = (unsigned char *)((TCLISTDATUM *)a)->ptr;
unsigned char *bo = (unsigned char *)((TCLISTDATUM *)b)->ptr;
int size = (((TCLISTDATUM *)a)->size < ((TCLISTDATUM *)b)->size) ?
((TCLISTDATUM *)a)->size : ((TCLISTDATUM *)b)->size;
for(int i = 0; i < size; i++){
if(ao[i] > bo[i]) return 1;
if(ao[i] < bo[i]) return -1;
}
return ((TCLISTDATUM *)a)->size - ((TCLISTDATUM *)b)->size;
}
/* Compare two list elements in case-insensitive lexical order..
`a' specifies the pointer to one element.
`b' specifies the pointer to the other element.
The return value is positive if the former is big, negative if the latter is big, 0 if both
are equivalent. */
static int tclistelemcmpci(const void *a, const void *b){
assert(a && b);
TCLISTDATUM *ap = (TCLISTDATUM *)a;
TCLISTDATUM *bp = (TCLISTDATUM *)b;
unsigned char *ao = (unsigned char *)ap->ptr;
unsigned char *bo = (unsigned char *)bp->ptr;
int size = (ap->size < bp->size) ? ap->size : bp->size;
for(int i = 0; i < size; i++){
int ac = ao[i];
bool ab = false;
if(ac >= 'A' && ac <= 'Z'){
ac += 'a' - 'A';
ab = true;
}
int bc = bo[i];
bool bb = false;
if(bc >= 'A' && bc <= 'Z'){
bc += 'a' - 'A';
bb = true;
}
if(ac > bc) return 1;
if(ac < bc) return -1;
if(!ab && bb) return 1;
if(ab && !bb) return -1;
}
return ap->size - bp->size;
}
/*************************************************************************************************
* array list (for experts)
*************************************************************************************************/
/* Add an allocated element at the end of a list object. */
void tclistpushmalloc(TCLIST *list, void *ptr, int size){
assert(list && ptr && size >= 0);
int index = list->start + list->num;
if(index >= list->anum){
list->anum += list->num + 1;
TCREALLOC(list->array, list->array, list->anum * sizeof(list->array[0]));
}
TCLISTDATUM *array = list->array;
TCREALLOC(array[index].ptr, ptr, size + 1);
array[index].ptr[size] = '\0';
array[index].size = size;
list->num++;
}
/* Sort elements of a list object in case-insensitive lexical order. */
void tclistsortci(TCLIST *list){
assert(list);
qsort(list->array + list->start, list->num, sizeof(list->array[0]), tclistelemcmpci);
}
/* Sort elements of a list object by an arbitrary comparison function. */
void tclistsortex(TCLIST *list, int (*cmp)(const TCLISTDATUM *, const TCLISTDATUM *)){
assert(list && cmp);
qsort(list->array + list->start, list->num, sizeof(list->array[0]),
(int (*)(const void *, const void *))cmp);
}
/* Invert elements of a list object. */
void tclistinvert(TCLIST *list){
assert(list);
TCLISTDATUM *top = list->array + list->start;
TCLISTDATUM *bot = top + list->num - 1;
while(top < bot){
TCLISTDATUM swap = *top;
*top = *bot;
*bot = swap;
top++;
bot--;
}
}
/* Perform formatted output into a list object. */
void tclistprintf(TCLIST *list, const char *format, ...){
assert(list && format);
TCXSTR *xstr = tcxstrnew();
va_list ap;
va_start(ap, format);
tcvxstrprintf(xstr, format, ap);
va_end(ap);
int size = TCXSTRSIZE(xstr);
char *ptr = tcxstrtomalloc(xstr);
tclistpushmalloc(list, ptr, size);
}
/*************************************************************************************************
* hash map
*************************************************************************************************/
#define TCMAPKMAXSIZ 0xfffff // maximum size of each key
#define TCMAPDEFBNUM 4093 // default bucket number
#define TCMAPZMMINSIZ 131072 // minimum memory size to use nullified region
#define TCMAPCSUNIT 52 // small allocation unit size of map concatenation
#define TCMAPCBUNIT 252 // big allocation unit size of map concatenation
#define TCMAPTINYBNUM 31 // bucket number of a tiny map
/* get the first hash value */
#define TCMAPHASH1(TC_res, TC_kbuf, TC_ksiz) \
do { \
const unsigned char *_TC_p = (const unsigned char *)(TC_kbuf); \
int _TC_ksiz = TC_ksiz; \
for((TC_res) = 19780211; _TC_ksiz--;){ \
(TC_res) = (TC_res) * 37 + *(_TC_p)++; \
} \
} while(false)
/* get the second hash value */
#define TCMAPHASH2(TC_res, TC_kbuf, TC_ksiz) \
do { \
const unsigned char *_TC_p = (const unsigned char *)(TC_kbuf) + TC_ksiz - 1; \
int _TC_ksiz = TC_ksiz; \
for((TC_res) = 0x13579bdf; _TC_ksiz--;){ \
(TC_res) = (TC_res) * 31 + *(_TC_p)--; \
} \
} while(false)
/* compare two keys */
#define TCKEYCMP(TC_abuf, TC_asiz, TC_bbuf, TC_bsiz) \
((TC_asiz > TC_bsiz) ? 1 : (TC_asiz < TC_bsiz) ? -1 : memcmp(TC_abuf, TC_bbuf, TC_asiz))
/* Create a map object. */
TCMAP *tcmapnew(void){
return tcmapnew2(TCMAPDEFBNUM);
}
/* Create a map object with specifying the number of the buckets. */
TCMAP *tcmapnew2(uint32_t bnum){
if(bnum < 1) bnum = 1;
TCMAP *map;
TCMALLOC(map, sizeof(*map));
TCMAPREC **buckets;
if(bnum >= TCMAPZMMINSIZ / sizeof(*buckets)){