tokyomessenger 0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. data/COPYING +504 -0
  2. data/README.rdoc +224 -0
  3. data/Rakefile +72 -0
  4. data/benchmarks/balancer.rb +101 -0
  5. data/benchmarks/bulk_db.rb +92 -0
  6. data/benchmarks/bulk_table.rb +104 -0
  7. data/benchmarks/db.rb +131 -0
  8. data/benchmarks/table.rb +186 -0
  9. data/ext/a.h +496 -0
  10. data/ext/extconf.rb +27 -0
  11. data/ext/md5.c +381 -0
  12. data/ext/md5.h +101 -0
  13. data/ext/tc_myconf.c +493 -0
  14. data/ext/tc_myconf.h +543 -0
  15. data/ext/tcadb.c +4339 -0
  16. data/ext/tcadb.h +533 -0
  17. data/ext/tcbdb.c +4180 -0
  18. data/ext/tcbdb.h +1086 -0
  19. data/ext/tcfdb.c +2746 -0
  20. data/ext/tcfdb.h +842 -0
  21. data/ext/tchdb.c +5153 -0
  22. data/ext/tchdb.h +856 -0
  23. data/ext/tcrdb.c +2637 -0
  24. data/ext/tcrdb.h +785 -0
  25. data/ext/tctdb.c +6199 -0
  26. data/ext/tctdb.h +1070 -0
  27. data/ext/tcutil.c +10528 -0
  28. data/ext/tcutil.h +4166 -0
  29. data/ext/tokyo_messenger.c +147 -0
  30. data/ext/tokyo_messenger.h +49 -0
  31. data/ext/tokyo_messenger_db.c +227 -0
  32. data/ext/tokyo_messenger_db.h +8 -0
  33. data/ext/tokyo_messenger_module.c +453 -0
  34. data/ext/tokyo_messenger_module.h +10 -0
  35. data/ext/tokyo_messenger_query.c +226 -0
  36. data/ext/tokyo_messenger_query.h +9 -0
  37. data/ext/tokyo_messenger_table.c +319 -0
  38. data/ext/tokyo_messenger_table.h +8 -0
  39. data/ext/tt_myconf.c +169 -0
  40. data/ext/tt_myconf.h +408 -0
  41. data/ext/ttutil.c +1509 -0
  42. data/ext/ttutil.h +480 -0
  43. data/lib/tokyo_messenger/balancer.rb +188 -0
  44. data/spec/ext.lua +4 -0
  45. data/spec/plu_db.rb +538 -0
  46. data/spec/spec.rb +1 -0
  47. data/spec/spec_base.rb +17 -0
  48. data/spec/start_tyrants.sh +36 -0
  49. data/spec/stop_tyrants.sh +9 -0
  50. data/spec/tokyo_tyrant_balancer_db_spec.rb +160 -0
  51. data/spec/tokyo_tyrant_balancer_table_spec.rb +177 -0
  52. data/spec/tokyo_tyrant_query_spec.rb +159 -0
  53. data/spec/tokyo_tyrant_spec.rb +254 -0
  54. data/spec/tokyo_tyrant_table_spec.rb +301 -0
  55. metadata +117 -0
@@ -0,0 +1,4166 @@
1
+ /*************************************************************************************************
2
+ * The utility API of Tokyo Cabinet
3
+ * Copyright (C) 2006-2009 Mikio Hirabayashi
4
+ * This file is part of Tokyo Cabinet.
5
+ * Tokyo Cabinet is free software; you can redistribute it and/or modify it under the terms of
6
+ * the GNU Lesser General Public License as published by the Free Software Foundation; either
7
+ * version 2.1 of the License or any later version. Tokyo Cabinet is distributed in the hope
8
+ * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
9
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10
+ * License for more details.
11
+ * You should have received a copy of the GNU Lesser General Public License along with Tokyo
12
+ * Cabinet; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
13
+ * Boston, MA 02111-1307 USA.
14
+ *************************************************************************************************/
15
+
16
+
17
+ #ifndef _TCUTIL_H /* duplication check */
18
+ #define _TCUTIL_H
19
+
20
+ #include <stdlib.h>
21
+ #include <stdbool.h>
22
+ #include <stdint.h>
23
+ #include <time.h>
24
+ #include <limits.h>
25
+ #include <math.h>
26
+
27
+ /*************************************************************************************************
28
+ * basic utilities
29
+ *************************************************************************************************/
30
+
31
+
32
+ /* String containing the version information. */
33
+ extern const char *tcversion;
34
+
35
+
36
+ /* Pointer to the call back function for handling a fatal error.
37
+ The argument specifies the error message.
38
+ The initial value of this variable is `NULL'. If the value is `NULL', the default function is
39
+ called when a fatal error occurs. A fatal error occurs when memory allocation is failed. */
40
+ extern void (*tcfatalfunc)(const char *);
41
+
42
+
43
+ /* Allocate a region on memory.
44
+ `size' specifies the size of the region.
45
+ The return value is the pointer to the allocated region.
46
+ This function handles failure of memory allocation implicitly. Because the region of the
47
+ return value is allocated with the `malloc' call, it should be released with the `free' call
48
+ when it is no longer in use. */
49
+ void *tcmalloc(size_t size);
50
+
51
+
52
+ /* Allocate a nullified region on memory.
53
+ `nmemb' specifies the number of elements.
54
+ `size' specifies the size of each element.
55
+ The return value is the pointer to the allocated nullified region.
56
+ This function handles failure of memory allocation implicitly. Because the region of the
57
+ return value is allocated with the `calloc' call, it should be released with the `free' call
58
+ when it is no longer in use. */
59
+ void *tccalloc(size_t nmemb, size_t size);
60
+
61
+
62
+ /* Re-allocate a region on memory.
63
+ `ptr' specifies the pointer to the region.
64
+ `size' specifies the size of the region.
65
+ The return value is the pointer to the re-allocated region.
66
+ This function handles failure of memory allocation implicitly. Because the region of the
67
+ return value is allocated with the `realloc' call, it should be released with the `free' call
68
+ when it is no longer in use. */
69
+ void *tcrealloc(void *ptr, size_t size);
70
+
71
+
72
+ /* Duplicate a region on memory.
73
+ `ptr' specifies the pointer to the region.
74
+ `size' specifies the size of the region.
75
+ The return value is the pointer to the allocated region of the duplicate.
76
+ Because an additional zero code is appended at the end of the region of the return value,
77
+ the return value can be treated as a character string. Because the region of the return
78
+ value is allocated with the `malloc' call, it should be released with the `free' call when
79
+ it is no longer in use. */
80
+ void *tcmemdup(const void *ptr, size_t size);
81
+
82
+
83
+ /* Duplicate a string on memory.
84
+ `str' specifies the string.
85
+ The return value is the allocated string equivalent to the specified string.
86
+ Because the region of the return value is allocated with the `malloc' call, it should be
87
+ released with the `free' call when it is no longer in use. */
88
+ char *tcstrdup(const void *str);
89
+
90
+
91
+ /* Free a region on memory.
92
+ `ptr' specifies the pointer to the region. If it is `NULL', this function has no effect.
93
+ Although this function is just a wrapper of `free' call, this is useful in applications using
94
+ another package of the `malloc' series. */
95
+ void tcfree(void *ptr);
96
+
97
+
98
+
99
+ /*************************************************************************************************
100
+ * basic utilities (for experts)
101
+ *************************************************************************************************/
102
+
103
+
104
+ /* type of the pointer to a comparison function.
105
+ `aptr' specifies the pointer to the region of one key.
106
+ `asiz' specifies the size of the region of one key.
107
+ `bptr' specifies the pointer to the region of the other key.
108
+ `bsiz' specifies the size of the region of the other key.
109
+ `op' specifies the pointer to the optional opaque object.
110
+ The return value is positive if the former is big, negative if the latter is big, 0 if both
111
+ are equivalent. */
112
+ typedef int (*TCCMP)(const char *aptr, int asiz, const char *bptr, int bsiz, void *op);
113
+
114
+ /* type of the pointer to a encoding or decoding function.
115
+ `ptr' specifies the pointer to the region.
116
+ `size' specifies the size of the region.
117
+ `sp' specifies the pointer to the variable into which the size of the region of the return
118
+ value is assigned.
119
+ `op' specifies the pointer to the optional opaque object.
120
+ If successful, the return value is the pointer to the result object allocated with `malloc'
121
+ call, else, it is `NULL'. */
122
+ typedef void *(*TCCODEC)(const void *ptr, int size, int *sp, void *op);
123
+
124
+ /* type of the pointer to a callback function to process record duplication.
125
+ `vbuf' specifies the pointer to the region of the value.
126
+ `vsiz' specifies the size of the region of the value.
127
+ `sp' specifies the pointer to the variable into which the size of the region of the return
128
+ value is assigned.
129
+ `op' specifies the pointer to the optional opaque object.
130
+ The return value is the pointer to the result object allocated with `malloc'. It is
131
+ released by the caller. If it is `NULL', the record is not modified. */
132
+ typedef void *(*TCPDPROC)(const void *vbuf, int vsiz, int *sp, void *op);
133
+
134
+ /* type of the pointer to a iterator function.
135
+ `kbuf' specifies the pointer to the region of the key.
136
+ `ksiz' specifies the size of the region of the key.
137
+ `vbuf' specifies the pointer to the region of the value.
138
+ `vsiz' specifies the size of the region of the value.
139
+ `op' specifies the pointer to the optional opaque object.
140
+ The return value is true to continue iteration or false to stop iteration. */
141
+ typedef bool (*TCITER)(const void *kbuf, int ksiz, const void *vbuf, int vsiz, void *op);
142
+
143
+
144
+
145
+ /*************************************************************************************************
146
+ * extensible string
147
+ *************************************************************************************************/
148
+
149
+
150
+ typedef struct { /* type of structure for an extensible string object */
151
+ char *ptr; /* pointer to the region */
152
+ int size; /* size of the region */
153
+ int asize; /* size of the allocated region */
154
+ } TCXSTR;
155
+
156
+
157
+ /* Create an extensible string object.
158
+ The return value is the new extensible string object. */
159
+ TCXSTR *tcxstrnew(void);
160
+
161
+
162
+ /* Create an extensible string object from a character string.
163
+ `str' specifies the string of the initial content.
164
+ The return value is the new extensible string object containing the specified string. */
165
+ TCXSTR *tcxstrnew2(const char *str);
166
+
167
+
168
+ /* Create an extensible string object with the initial allocation size.
169
+ `asiz' specifies the initial allocation size.
170
+ The return value is the new extensible string object. */
171
+ TCXSTR *tcxstrnew3(int asiz);
172
+
173
+
174
+ /* Copy an extensible string object.
175
+ `xstr' specifies the extensible string object.
176
+ The return value is the new extensible string object equivalent to the specified object. */
177
+ TCXSTR *tcxstrdup(const TCXSTR *xstr);
178
+
179
+
180
+ /* Delete an extensible string object.
181
+ `xstr' specifies the extensible string object.
182
+ Note that the deleted object and its derivatives can not be used anymore. */
183
+ void tcxstrdel(TCXSTR *xstr);
184
+
185
+
186
+ /* Concatenate a region to the end of an extensible string object.
187
+ `xstr' specifies the extensible string object.
188
+ `ptr' specifies the pointer to the region to be appended.
189
+ `size' specifies the size of the region. */
190
+ void tcxstrcat(TCXSTR *xstr, const void *ptr, int size);
191
+
192
+
193
+ /* Concatenate a character string to the end of an extensible string object.
194
+ `xstr' specifies the extensible string object.
195
+ `str' specifies the string to be appended. */
196
+ void tcxstrcat2(TCXSTR *xstr, const char *str);
197
+
198
+
199
+ /* Get the pointer of the region of an extensible string object.
200
+ `xstr' specifies the extensible string object.
201
+ The return value is the pointer of the region of the object.
202
+ Because an additional zero code is appended at the end of the region of the return value,
203
+ the return value can be treated as a character string. */
204
+ const void *tcxstrptr(const TCXSTR *xstr);
205
+
206
+
207
+ /* Get the size of the region of an extensible string object.
208
+ `xstr' specifies the extensible string object.
209
+ The return value is the size of the region of the object. */
210
+ int tcxstrsize(const TCXSTR *xstr);
211
+
212
+
213
+ /* Clear an extensible string object.
214
+ `xstr' specifies the extensible string object.
215
+ The internal buffer of the object is cleared and the size is set zero. */
216
+ void tcxstrclear(TCXSTR *xstr);
217
+
218
+
219
+ /* Perform formatted output into an extensible string object.
220
+ `xstr' specifies the extensible string object.
221
+ `format' specifies the printf-like format string. The conversion character `%' can be used
222
+ with such flag characters as `s', `d', `o', `u', `x', `X', `c', `e', `E', `f', `g', `G', `@',
223
+ `?', `b', and `%'. `@' works as with `s' but escapes meta characters of XML. `?' works as
224
+ with `s' but escapes meta characters of URL. `b' converts an integer to the string as binary
225
+ numbers. The other conversion character work as with each original.
226
+ The other arguments are used according to the format string. */
227
+ void tcxstrprintf(TCXSTR *xstr, const char *format, ...);
228
+
229
+
230
+ /* Allocate a formatted string on memory.
231
+ `format' specifies the printf-like format string. The conversion character `%' can be used
232
+ with such flag characters as `s', `d', `o', `u', `x', `X', `c', `e', `E', `f', `g', `G', `@',
233
+ `?', `b', and `%'. `@' works as with `s' but escapes meta characters of XML. `?' works as
234
+ with `s' but escapes meta characters of URL. `b' converts an integer to the string as binary
235
+ numbers. The other conversion character work as with each original.
236
+ The other arguments are used according to the format string.
237
+ The return value is the pointer to the region of the result string.
238
+ Because the region of the return value is allocated with the `malloc' call, it should be
239
+ released with the `free' call when it is no longer in use. */
240
+ char *tcsprintf(const char *format, ...);
241
+
242
+
243
+
244
+ /*************************************************************************************************
245
+ * extensible string (for experts)
246
+ *************************************************************************************************/
247
+
248
+
249
+ /* Convert an extensible string object into a usual allocated region.
250
+ `xstr' specifies the extensible string object.
251
+ The return value is the pointer to the allocated region of the object.
252
+ Because an additional zero code is appended at the end of the region of the return value,
253
+ the return value can be treated as a character string. Because the region of the return
254
+ value is allocated with the `malloc' call, it should be released with the `free' call when it
255
+ is no longer in use. Because the region of the original object is deleted, it should not be
256
+ deleted again. */
257
+ void *tcxstrtomalloc(TCXSTR *xstr);
258
+
259
+
260
+ /* Create an extensible string object from an allocated region.
261
+ `ptr' specifies the pointer to the region allocated with `malloc' call.
262
+ `size' specifies the size of the region.
263
+ The return value is the new extensible string object wrapping the specified region.
264
+ Note that the specified region is released when the object is deleted. */
265
+ TCXSTR *tcxstrfrommalloc(void *ptr, int size);
266
+
267
+
268
+
269
+ /*************************************************************************************************
270
+ * array list
271
+ *************************************************************************************************/
272
+
273
+
274
+ typedef struct { /* type of structure for an element of a list */
275
+ char *ptr; /* pointer to the region */
276
+ int size; /* size of the effective region */
277
+ } TCLISTDATUM;
278
+
279
+ typedef struct { /* type of structure for an array list */
280
+ TCLISTDATUM *array; /* array of data */
281
+ int anum; /* number of the elements of the array */
282
+ int start; /* start index of used elements */
283
+ int num; /* number of used elements */
284
+ } TCLIST;
285
+
286
+
287
+ /* Create a list object.
288
+ The return value is the new list object. */
289
+ TCLIST *tclistnew(void);
290
+
291
+
292
+ /* Create a list object with expecting the number of elements.
293
+ `anum' specifies the number of elements expected to be stored in the list.
294
+ The return value is the new list object. */
295
+ TCLIST *tclistnew2(int anum);
296
+
297
+
298
+ /* Create a list object with initial string elements.
299
+ `str' specifies the string of the first element.
300
+ The other arguments are other elements. They should be trailed by a `NULL' argument.
301
+ The return value is the new list object. */
302
+ TCLIST *tclistnew3(const char *str, ...);
303
+
304
+
305
+ /* Copy a list object.
306
+ `list' specifies the list object.
307
+ The return value is the new list object equivalent to the specified object. */
308
+ TCLIST *tclistdup(const TCLIST *list);
309
+
310
+
311
+ /* Delete a list object.
312
+ `list' specifies the list object.
313
+ Note that the deleted object and its derivatives can not be used anymore. */
314
+ void tclistdel(TCLIST *list);
315
+
316
+
317
+ /* Get the number of elements of a list object.
318
+ `list' specifies the list object.
319
+ The return value is the number of elements of the list. */
320
+ int tclistnum(const TCLIST *list);
321
+
322
+
323
+ /* Get the pointer to the region of an element of a list object.
324
+ `list' specifies the list object.
325
+ `index' specifies the index of the element.
326
+ `sp' specifies the pointer to the variable into which the size of the region of the return
327
+ value is assigned.
328
+ The return value is the pointer to the region of the value.
329
+ Because an additional zero code is appended at the end of the region of the return value,
330
+ the return value can be treated as a character string. If `index' is equal to or more than
331
+ the number of elements, the return value is `NULL'. */
332
+ const void *tclistval(const TCLIST *list, int index, int *sp);
333
+
334
+
335
+ /* Get the string of an element of a list object.
336
+ `list' specifies the list object.
337
+ `index' specifies the index of the element.
338
+ The return value is the string of the value.
339
+ If `index' is equal to or more than the number of elements, the return value is `NULL'. */
340
+ const char *tclistval2(const TCLIST *list, int index);
341
+
342
+
343
+ /* Add an element at the end of a list object.
344
+ `list' specifies the list object.
345
+ `ptr' specifies the pointer to the region of the new element.
346
+ `size' specifies the size of the region. */
347
+ void tclistpush(TCLIST *list, const void *ptr, int size);
348
+
349
+
350
+ /* Add a string element at the end of a list object.
351
+ `list' specifies the list object.
352
+ `str' specifies the string of the new element. */
353
+ void tclistpush2(TCLIST *list, const char *str);
354
+
355
+
356
+ /* Remove an element of the end of a list object.
357
+ `list' specifies the list object.
358
+ `sp' specifies the pointer to the variable into which the size of the region of the return
359
+ value is assigned.
360
+ The return value is the pointer to the region of the removed element.
361
+ Because an additional zero code is appended at the end of the region of the return value,
362
+ the return value can be treated as a character string. Because the region of the return
363
+ value is allocated with the `malloc' call, it should be released with the `free' call when it
364
+ is no longer in use. If the list is empty, the return value is `NULL'. */
365
+ void *tclistpop(TCLIST *list, int *sp);
366
+
367
+
368
+ /* Remove a string element of the end of a list object.
369
+ `list' specifies the list object.
370
+ The return value is the string of the removed element.
371
+ Because the region of the return value is allocated with the `malloc' call, it should be
372
+ released with the `free' call when it is no longer in use. If the list is empty, the return
373
+ value is `NULL'. */
374
+ char *tclistpop2(TCLIST *list);
375
+
376
+
377
+ /* Add an element at the top of a list object.
378
+ `list' specifies the list object.
379
+ `ptr' specifies the pointer to the region of the new element.
380
+ `size' specifies the size of the region. */
381
+ void tclistunshift(TCLIST *list, const void *ptr, int size);
382
+
383
+
384
+ /* Add a string element at the top of a list object.
385
+ `list' specifies the list object.
386
+ `str' specifies the string of the new element. */
387
+ void tclistunshift2(TCLIST *list, const char *str);
388
+
389
+
390
+ /* Remove an element of the top of a list object.
391
+ `list' specifies the list object.
392
+ `sp' specifies the pointer to the variable into which the size of the region of the return
393
+ value is assigned.
394
+ The return value is the pointer to the region of the removed element.
395
+ Because an additional zero code is appended at the end of the region of the return value,
396
+ the return value can be treated as a character string. Because the region of the return
397
+ value is allocated with the `malloc' call, it should be released with the `free' call when it
398
+ is no longer in use. If the list is empty, the return value is `NULL'. */
399
+ void *tclistshift(TCLIST *list, int *sp);
400
+
401
+
402
+ /* Remove a string element of the top of a list object.
403
+ `list' specifies the list object.
404
+ The return value is the string of the removed element.
405
+ Because the region of the return value is allocated with the `malloc' call, it should be
406
+ released with the `free' call when it is no longer in use. If the list is empty, the return
407
+ value is `NULL'. */
408
+ char *tclistshift2(TCLIST *list);
409
+
410
+
411
+ /* Add an element at the specified location of a list object.
412
+ `list' specifies the list object.
413
+ `index' specifies the index of the new element.
414
+ `ptr' specifies the pointer to the region of the new element.
415
+ `size' specifies the size of the region.
416
+ If `index' is equal to or more than the number of elements, this function has no effect. */
417
+ void tclistinsert(TCLIST *list, int index, const void *ptr, int size);
418
+
419
+
420
+ /* Add a string element at the specified location of a list object.
421
+ `list' specifies the list object.
422
+ `index' specifies the index of the new element.
423
+ `str' specifies the string of the new element.
424
+ If `index' is equal to or more than the number of elements, this function has no effect. */
425
+ void tclistinsert2(TCLIST *list, int index, const char *str);
426
+
427
+
428
+ /* Remove an element at the specified location of a list object.
429
+ `list' specifies the list object.
430
+ `index' specifies the index of the element to be removed.
431
+ `sp' specifies the pointer to the variable into which the size of the region of the return
432
+ value is assigned.
433
+ The return value is the pointer to the region of the removed element.
434
+ Because an additional zero code is appended at the end of the region of the return value,
435
+ the return value can be treated as a character string. Because the region of the return
436
+ value is allocated with the `malloc' call, it should be released with the `free' call when it
437
+ is no longer in use. If `index' is equal to or more than the number of elements, no element
438
+ is removed and the return value is `NULL'. */
439
+ void *tclistremove(TCLIST *list, int index, int *sp);
440
+
441
+
442
+ /* Remove a string element at the specified location of a list object.
443
+ `list' specifies the list object.
444
+ `index' specifies the index of the element to be removed.
445
+ The return value is the string of the removed element.
446
+ Because the region of the return value is allocated with the `malloc' call, it should be
447
+ released with the `free' call when it is no longer in use. If `index' is equal to or more
448
+ than the number of elements, no element is removed and the return value is `NULL'. */
449
+ char *tclistremove2(TCLIST *list, int index);
450
+
451
+
452
+ /* Overwrite an element at the specified location of a list object.
453
+ `list' specifies the list object.
454
+ `index' specifies the index of the element to be overwritten.
455
+ `ptr' specifies the pointer to the region of the new content.
456
+ `size' specifies the size of the new content.
457
+ If `index' is equal to or more than the number of elements, this function has no effect. */
458
+ void tclistover(TCLIST *list, int index, const void *ptr, int size);
459
+
460
+
461
+ /* Overwrite a string element at the specified location of a list object.
462
+ `list' specifies the list object.
463
+ `index' specifies the index of the element to be overwritten.
464
+ `str' specifies the string of the new content.
465
+ If `index' is equal to or more than the number of elements, this function has no effect. */
466
+ void tclistover2(TCLIST *list, int index, const char *str);
467
+
468
+
469
+ /* Sort elements of a list object in lexical order.
470
+ `list' specifies the list object. */
471
+ void tclistsort(TCLIST *list);
472
+
473
+
474
+ /* Search a list object for an element using liner search.
475
+ `list' specifies the list object.
476
+ `ptr' specifies the pointer to the region of the key.
477
+ `size' specifies the size of the region.
478
+ The return value is the index of a corresponding element or -1 if there is no corresponding
479
+ element.
480
+ If two or more elements correspond, the former returns. */
481
+ int tclistlsearch(const TCLIST *list, const void *ptr, int size);
482
+
483
+
484
+ /* Search a list object for an element using binary search.
485
+ `list' specifies the list object. It should be sorted in lexical order.
486
+ `ptr' specifies the pointer to the region of the key.
487
+ `size' specifies the size of the region.
488
+ The return value is the index of a corresponding element or -1 if there is no corresponding
489
+ element.
490
+ If two or more elements correspond, which returns is not defined. */
491
+ int tclistbsearch(const TCLIST *list, const void *ptr, int size);
492
+
493
+
494
+ /* Clear a list object.
495
+ `list' specifies the list object.
496
+ All elements are removed. */
497
+ void tclistclear(TCLIST *list);
498
+
499
+
500
+ /* Serialize a list object into a byte array.
501
+ `list' specifies the list object.
502
+ `sp' specifies the pointer to the variable into which the size of the region of the return
503
+ value is assigned.
504
+ The return value is the pointer to the region of the result serial region.
505
+ Because the region of the return value is allocated with the `malloc' call, it should be
506
+ released with the `free' call when it is no longer in use. */
507
+ void *tclistdump(const TCLIST *list, int *sp);
508
+
509
+
510
+ /* Create a list object from a serialized byte array.
511
+ `ptr' specifies the pointer to the region of serialized byte array.
512
+ `size' specifies the size of the region.
513
+ The return value is a new list object.
514
+ Because the object of the return value is created with the function `tclistnew', it should
515
+ be deleted with the function `tclistdel' when it is no longer in use. */
516
+ TCLIST *tclistload(const void *ptr, int size);
517
+
518
+
519
+
520
+ /*************************************************************************************************
521
+ * array list (for experts)
522
+ *************************************************************************************************/
523
+
524
+
525
+ /* Add an allocated element at the end of a list object.
526
+ `list' specifies the list object.
527
+ `ptr' specifies the pointer to the region allocated with `malloc' call.
528
+ `size' specifies the size of the region.
529
+ Note that the specified region is released when the object is deleted. */
530
+ void tclistpushmalloc(TCLIST *list, void *ptr, int size);
531
+
532
+
533
+ /* Sort elements of a list object in case-insensitive lexical order.
534
+ `list' specifies the list object. */
535
+ void tclistsortci(TCLIST *list);
536
+
537
+
538
+ /* Sort elements of a list object by an arbitrary comparison function.
539
+ `list' specifies the list object.
540
+ `cmp' specifies the pointer to the comparison function. The structure TCLISTDATUM has the
541
+ member "ptr" which is the pointer to the region of the element, and the member "size" which is
542
+ the size of the region. */
543
+ void tclistsortex(TCLIST *list, int (*cmp)(const TCLISTDATUM *, const TCLISTDATUM *));
544
+
545
+
546
+ /* Invert elements of a list object.
547
+ `list' specifies the list object. */
548
+ void tclistinvert(TCLIST *list);
549
+
550
+
551
+ /* Perform formatted output into a list object.
552
+ `list' specifies the list object.
553
+ `format' specifies the printf-like format string. The conversion character `%' can be used
554
+ with such flag characters as `s', `d', `o', `u', `x', `X', `c', `e', `E', `f', `g', `G', `@',
555
+ `?', `b', and `%'. `@' works as with `s' but escapes meta characters of XML. `?' works as
556
+ with `s' but escapes meta characters of URL. `b' converts an integer to the string as binary
557
+ numbers. The other conversion character work as with each original.
558
+ The other arguments are used according to the format string. */
559
+ void tclistprintf(TCLIST *list, const char *format, ...);
560
+
561
+
562
+
563
+ /*************************************************************************************************
564
+ * hash map
565
+ *************************************************************************************************/
566
+
567
+
568
+ typedef struct _TCMAPREC { /* type of structure for an element of a map */
569
+ int32_t ksiz; /* size of the region of the key */
570
+ int32_t vsiz; /* size of the region of the value */
571
+ struct _TCMAPREC *left; /* pointer to the left child */
572
+ struct _TCMAPREC *right; /* pointer to the right child */
573
+ struct _TCMAPREC *prev; /* pointer to the previous element */
574
+ struct _TCMAPREC *next; /* pointer to the next element */
575
+ } TCMAPREC;
576
+
577
+ typedef struct { /* type of structure for a map */
578
+ TCMAPREC **buckets; /* bucket array */
579
+ TCMAPREC *first; /* pointer to the first element */
580
+ TCMAPREC *last; /* pointer to the last element */
581
+ TCMAPREC *cur; /* pointer to the current element */
582
+ uint32_t bnum; /* number of buckets */
583
+ uint64_t rnum; /* number of records */
584
+ uint64_t msiz; /* total size of records */
585
+ } TCMAP;
586
+
587
+
588
+ /* Create a map object.
589
+ The return value is the new map object. */
590
+ TCMAP *tcmapnew(void);
591
+
592
+
593
+ /* Create a map object with specifying the number of the buckets.
594
+ `bnum' specifies the number of the buckets.
595
+ The return value is the new map object. */
596
+ TCMAP *tcmapnew2(uint32_t bnum);
597
+
598
+
599
+ /* Create a map object with initial string elements.
600
+ `str' specifies the string of the first element.
601
+ The other arguments are other elements. They should be trailed by a `NULL' argument.
602
+ The return value is the new map object.
603
+ The key and the value of each record are situated one after the other. */
604
+ TCMAP *tcmapnew3(const char *str, ...);
605
+
606
+
607
+ /* Copy a map object.
608
+ `map' specifies the map object.
609
+ The return value is the new map object equivalent to the specified object. */
610
+ TCMAP *tcmapdup(const TCMAP *map);
611
+
612
+
613
+ /* Delete a map object.
614
+ `map' specifies the map object.
615
+ Note that the deleted object and its derivatives can not be used anymore. */
616
+ void tcmapdel(TCMAP *map);
617
+
618
+
619
+ /* Store a record into a map object.
620
+ `map' specifies the map object.
621
+ `kbuf' specifies the pointer to the region of the key.
622
+ `ksiz' specifies the size of the region of the key.
623
+ `vbuf' specifies the pointer to the region of the value.
624
+ `vsiz' specifies the size of the region of the value.
625
+ If a record with the same key exists in the map, it is overwritten. */
626
+ void tcmapput(TCMAP *map, const void *kbuf, int ksiz, const void *vbuf, int vsiz);
627
+
628
+
629
+ /* Store a string record into a map object.
630
+ `map' specifies the map object.
631
+ `kstr' specifies the string of the key.
632
+ `vstr' specifies the string of the value.
633
+ If a record with the same key exists in the map, it is overwritten. */
634
+ void tcmapput2(TCMAP *map, const char *kstr, const char *vstr);
635
+
636
+
637
+ /* Store a new record into a map object.
638
+ `map' specifies the map object.
639
+ `kbuf' specifies the pointer to the region of the key.
640
+ `ksiz' specifies the size of the region of the key.
641
+ `vbuf' specifies the pointer to the region of the value.
642
+ `vsiz' specifies the size of the region of the value.
643
+ If successful, the return value is true, else, it is false.
644
+ If a record with the same key exists in the map, this function has no effect. */
645
+ bool tcmapputkeep(TCMAP *map, const void *kbuf, int ksiz, const void *vbuf, int vsiz);
646
+
647
+
648
+ /* Store a new string record into a map object.
649
+ `map' specifies the map object.
650
+ `kstr' specifies the string of the key.
651
+ `vstr' specifies the string of the value.
652
+ If successful, the return value is true, else, it is false.
653
+ If a record with the same key exists in the map, this function has no effect. */
654
+ bool tcmapputkeep2(TCMAP *map, const char *kstr, const char *vstr);
655
+
656
+
657
+ /* Concatenate a value at the end of the value of the existing record in a map object.
658
+ `map' specifies the map object.
659
+ `kbuf' specifies the pointer to the region of the key.
660
+ `ksiz' specifies the size of the region of the key.
661
+ `vbuf' specifies the pointer to the region of the value.
662
+ `vsiz' specifies the size of the region of the value.
663
+ If there is no corresponding record, a new record is created. */
664
+ void tcmapputcat(TCMAP *map, const void *kbuf, int ksiz, const void *vbuf, int vsiz);
665
+
666
+
667
+ /* Concatenate a string value at the end of the value of the existing record in a map object.
668
+ `map' specifies the map object.
669
+ `kstr' specifies the string of the key.
670
+ `vstr' specifies the string of the value.
671
+ If there is no corresponding record, a new record is created. */
672
+ void tcmapputcat2(TCMAP *map, const char *kstr, const char *vstr);
673
+
674
+
675
+ /* Remove a record of a map object.
676
+ `map' specifies the map object.
677
+ `kbuf' specifies the pointer to the region of the key.
678
+ `ksiz' specifies the size of the region of the key.
679
+ If successful, the return value is true. False is returned when no record corresponds to
680
+ the specified key. */
681
+ bool tcmapout(TCMAP *map, const void *kbuf, int ksiz);
682
+
683
+
684
+ /* Remove a string record of a map object.
685
+ `map' specifies the map object.
686
+ `kstr' specifies the string of the key.
687
+ If successful, the return value is true. False is returned when no record corresponds to
688
+ the specified key. */
689
+ bool tcmapout2(TCMAP *map, const char *kstr);
690
+
691
+
692
+ /* Retrieve a record in a map object.
693
+ `map' specifies the map object.
694
+ `kbuf' specifies the pointer to the region of the key.
695
+ `ksiz' specifies the size of the region of the key.
696
+ `sp' specifies the pointer to the variable into which the size of the region of the return
697
+ value is assigned.
698
+ If successful, the return value is the pointer to the region of the value of the
699
+ corresponding record. `NULL' is returned when no record corresponds.
700
+ Because an additional zero code is appended at the end of the region of the return value,
701
+ the return value can be treated as a character string. */
702
+ const void *tcmapget(const TCMAP *map, const void *kbuf, int ksiz, int *sp);
703
+
704
+
705
+ /* Retrieve a string record in a map object.
706
+ `map' specifies the map object.
707
+ `kstr' specifies the string of the key.
708
+ If successful, the return value is the string of the value of the corresponding record.
709
+ `NULL' is returned when no record corresponds. */
710
+ const char *tcmapget2(const TCMAP *map, const char *kstr);
711
+
712
+
713
+ /* Move a record to the edge of a map object.
714
+ `map' specifies the map object.
715
+ `kbuf' specifies the pointer to the region of a key.
716
+ `ksiz' specifies the size of the region of the key.
717
+ `head' specifies the destination which is the head if it is true or the tail if else.
718
+ If successful, the return value is true. False is returned when no record corresponds to
719
+ the specified key. */
720
+ bool tcmapmove(TCMAP *map, const void *kbuf, int ksiz, bool head);
721
+
722
+
723
+ /* Move a string record to the edge of a map object.
724
+ `map' specifies the map object.
725
+ `kstr' specifies the string of a key.
726
+ `head' specifies the destination which is the head if it is true or the tail if else.
727
+ If successful, the return value is true. False is returned when no record corresponds to
728
+ the specified key. */
729
+ bool tcmapmove2(TCMAP *map, const char *kstr, bool head);
730
+
731
+
732
+ /* Initialize the iterator of a map object.
733
+ `map' specifies the map object.
734
+ The iterator is used in order to access the key of every record stored in the map object. */
735
+ void tcmapiterinit(TCMAP *map);
736
+
737
+
738
+ /* Get the next key of the iterator of a map object.
739
+ `map' specifies the map object.
740
+ `sp' specifies the pointer to the variable into which the size of the region of the return
741
+ value is assigned.
742
+ If successful, the return value is the pointer to the region of the next key, else, it is
743
+ `NULL'. `NULL' is returned when no record can be fetched from the iterator.
744
+ Because an additional zero code is appended at the end of the region of the return value,
745
+ the return value can be treated as a character string.
746
+ The order of iteration is assured to be the same as the stored order. */
747
+ const void *tcmapiternext(TCMAP *map, int *sp);
748
+
749
+
750
+ /* Get the next key string of the iterator of a map object.
751
+ `map' specifies the map object.
752
+ If successful, the return value is the pointer to the region of the next key, else, it is
753
+ `NULL'. `NULL' is returned when no record can be fetched from the iterator.
754
+ The order of iteration is assured to be the same as the stored order. */
755
+ const char *tcmapiternext2(TCMAP *map);
756
+
757
+
758
+ /* Get the number of records stored in a map object.
759
+ `map' specifies the map object.
760
+ The return value is the number of the records stored in the map object. */
761
+ uint64_t tcmaprnum(const TCMAP *map);
762
+
763
+
764
+ /* Get the total size of memory used in a map object.
765
+ `map' specifies the map object.
766
+ The return value is the total size of memory used in a map object. */
767
+ uint64_t tcmapmsiz(const TCMAP *map);
768
+
769
+
770
+ /* Create a list object containing all keys in a map object.
771
+ `map' specifies the map object.
772
+ The return value is the new list object containing all keys in the map object.
773
+ Because the object of the return value is created with the function `tclistnew', it should
774
+ be deleted with the function `tclistdel' when it is no longer in use. */
775
+ TCLIST *tcmapkeys(const TCMAP *map);
776
+
777
+
778
+ /* Create a list object containing all values in a map object.
779
+ `map' specifies the map object.
780
+ The return value is the new list object containing all values in the map object.
781
+ Because the object of the return value is created with the function `tclistnew', it should
782
+ be deleted with the function `tclistdel' when it is no longer in use. */
783
+ TCLIST *tcmapvals(const TCMAP *map);
784
+
785
+
786
+ /* Add an integer to a record in a map object.
787
+ `map' specifies the map object.
788
+ `kbuf' specifies the pointer to the region of the key.
789
+ `ksiz' specifies the size of the region of the key.
790
+ `num' specifies the additional value.
791
+ The return value is the summation value.
792
+ If the corresponding record exists, the value is treated as an integer and is added to. If no
793
+ record corresponds, a new record of the additional value is stored. */
794
+ int tcmapaddint(TCMAP *map, const void *kbuf, int ksiz, int num);
795
+
796
+
797
+ /* Add a real number to a record in a map object.
798
+ `map' specifies the map object.
799
+ `kbuf' specifies the pointer to the region of the key.
800
+ `ksiz' specifies the size of the region of the key.
801
+ `num' specifies the additional value.
802
+ The return value is the summation value.
803
+ If the corresponding record exists, the value is treated as a real number and is added to. If
804
+ no record corresponds, a new record of the additional value is stored. */
805
+ double tcmapadddouble(TCMAP *map, const void *kbuf, int ksiz, double num);
806
+
807
+
808
+ /* Clear a map object.
809
+ `map' specifies the map object.
810
+ All records are removed. */
811
+ void tcmapclear(TCMAP *map);
812
+
813
+
814
+ /* Remove front records of a map object.
815
+ `map' specifies the map object.
816
+ `num' specifies the number of records to be removed. */
817
+ void tcmapcutfront(TCMAP *map, int num);
818
+
819
+
820
+ /* Serialize a map object into a byte array.
821
+ `map' specifies the map object.
822
+ `sp' specifies the pointer to the variable into which the size of the region of the return
823
+ value is assigned.
824
+ The return value is the pointer to the region of the result serial region.
825
+ Because the region of the return value is allocated with the `malloc' call, it should be
826
+ released with the `free' call when it is no longer in use. */
827
+ void *tcmapdump(const TCMAP *map, int *sp);
828
+
829
+
830
+ /* Create a map object from a serialized byte array.
831
+ `ptr' specifies the pointer to the region of serialized byte array.
832
+ `size' specifies the size of the region.
833
+ The return value is a new map object.
834
+ Because the object of the return value is created with the function `tcmapnew', it should be
835
+ deleted with the function `tcmapdel' when it is no longer in use. */
836
+ TCMAP *tcmapload(const void *ptr, int size);
837
+
838
+
839
+
840
+ /*************************************************************************************************
841
+ * hash map (for experts)
842
+ *************************************************************************************************/
843
+
844
+
845
+ /* Store a record and make it semivolatile in a map object.
846
+ `map' specifies the map object.
847
+ `kbuf' specifies the pointer to the region of the key.
848
+ `ksiz' specifies the size of the region of the key.
849
+ `vbuf' specifies the pointer to the region of the value.
850
+ `vsiz' specifies the size of the region of the value.
851
+ If a record with the same key exists in the map, it is overwritten. The record is moved to
852
+ the tail. */
853
+ void tcmapput3(TCMAP *map, const void *kbuf, int ksiz, const char *vbuf, int vsiz);
854
+
855
+
856
+ /* Store a record of the value of two regions into a map object.
857
+ `map' specifies the map object.
858
+ `kbuf' specifies the pointer to the region of the key.
859
+ `ksiz' specifies the size of the region of the key.
860
+ `fvbuf' specifies the pointer to the former region of the value.
861
+ `fvsiz' specifies the size of the former region of the value.
862
+ `lvbuf' specifies the pointer to the latter region of the value.
863
+ `lvsiz' specifies the size of the latter region of the value.
864
+ If a record with the same key exists in the map, it is overwritten. */
865
+ void tcmapput4(TCMAP *map, const void *kbuf, int ksiz,
866
+ const void *fvbuf, int fvsiz, const void *lvbuf, int lvsiz);
867
+
868
+
869
+ /* Concatenate a value at the existing record and make it semivolatile in a map object.
870
+ `map' specifies the map object.
871
+ `kbuf' specifies the pointer to the region of the key.
872
+ `ksiz' specifies the size of the region of the key.
873
+ `vbuf' specifies the pointer to the region of the value.
874
+ `vsiz' specifies the size of the region of the value.
875
+ If there is no corresponding record, a new record is created. */
876
+ void tcmapputcat3(TCMAP *map, const void *kbuf, int ksiz, const void *vbuf, int vsiz);
877
+
878
+
879
+ /* Store a record into a map object with a duplication handler.
880
+ `map' specifies the map object.
881
+ `kbuf' specifies the pointer to the region of the key.
882
+ `ksiz' specifies the size of the region of the key.
883
+ `vbuf' specifies the pointer to the region of the value. `NULL' means that record addition is
884
+ ommited if there is no corresponding record.
885
+ `vsiz' specifies the size of the region of the value.
886
+ `proc' specifies the pointer to the callback function to process duplication. It receives
887
+ four parameters. The first parameter is the pointer to the region of the value. The second
888
+ parameter is the size of the region of the value. The third parameter is the pointer to the
889
+ variable into which the size of the region of the return value is assigned. The fourth
890
+ parameter is the pointer to the optional opaque object. It returns the pointer to the result
891
+ object allocated with `malloc'. It is released by the caller. If it is `NULL', the record is
892
+ not modified. If it is `(void *)-1', the record is removed.
893
+ `op' specifies an arbitrary pointer to be given as a parameter of the callback function. If
894
+ it is not needed, `NULL' can be specified.
895
+ If successful, the return value is true, else, it is false. */
896
+ bool tcmapputproc(TCMAP *map, const void *kbuf, int ksiz, const void *vbuf, int vsiz,
897
+ TCPDPROC proc, void *op);
898
+
899
+
900
+ /* Retrieve a semivolatile record in a map object.
901
+ `map' specifies the map object.
902
+ `kbuf' specifies the pointer to the region of the key.
903
+ `ksiz' specifies the size of the region of the key.
904
+ `sp' specifies the pointer to the variable into which the size of the region of the return
905
+ value is assigned.
906
+ If successful, the return value is the pointer to the region of the value of the
907
+ corresponding record. `NULL' is returned when no record corresponds.
908
+ Because an additional zero code is appended at the end of the region of the return value,
909
+ the return value can be treated as a character string. The internal region of the returned
910
+ record is moved to the tail so that the record will survive for a time under LRU cache
911
+ algorithm removing records from the head. */
912
+ const void *tcmapget3(TCMAP *map, const void *kbuf, int ksiz, int *sp);
913
+
914
+
915
+ /* Retrieve a string record in a map object with specifying the default value string.
916
+ `map' specifies the map object.
917
+ `kstr' specifies the string of the key.
918
+ `dstr' specifies the string of the default value.
919
+ The return value is the string of the value of the corresponding record or the default value
920
+ string. */
921
+ const char *tcmapget4(TCMAP *map, const char *kstr, const char *dstr);
922
+
923
+
924
+ /* Initialize the iterator of a map object at the record corresponding a key.
925
+ `map' specifies the map object.
926
+ `kbuf' specifies the pointer to the region of the key.
927
+ `ksiz' specifies the size of the region of the key.
928
+ If there is no record corresponding the condition, the iterator is not modified. */
929
+ void tcmapiterinit2(TCMAP *map, const void *kbuf, int ksiz);
930
+
931
+
932
+ /* Initialize the iterator of a map object at the record corresponding a key string.
933
+ `map' specifies the map object.
934
+ `kstr' specifies the string of the key.
935
+ If there is no record corresponding the condition, the iterator is not modified. */
936
+ void tcmapiterinit3(TCMAP *map, const char *kstr);
937
+
938
+
939
+ /* Get the value bound to the key fetched from the iterator of a map object.
940
+ `kbuf' specifies the pointer to the region of the iteration key.
941
+ `sp' specifies the pointer to the variable into which the size of the region of the return
942
+ value is assigned.
943
+ The return value is the pointer to the region of the value of the corresponding record.
944
+ Because an additional zero code is appended at the end of the region of the return value,
945
+ the return value can be treated as a character string. */
946
+ const void *tcmapiterval(const void *kbuf, int *sp);
947
+
948
+
949
+ /* Get the value string bound to the key fetched from the iterator of a map object.
950
+ `kstr' specifies the string of the iteration key.
951
+ The return value is the pointer to the region of the value of the corresponding record. */
952
+ const char *tcmapiterval2(const char *kstr);
953
+
954
+
955
+ /* Create an array of strings of all keys in a map object.
956
+ `map' specifies the map object.
957
+ `np' specifies the pointer to a variable into which the number of elements of the return value
958
+ is assigned.
959
+ The return value is the pointer to the array of all string keys in the map object.
960
+ Because the region of the return value is allocated with the `malloc' call, it should be
961
+ released with the `free' call if when is no longer in use. Note that elements of the array
962
+ point to the inner objects, whose life duration is synchronous with the map object. */
963
+ const char **tcmapkeys2(const TCMAP *map, int *np);
964
+
965
+
966
+ /* Create an array of strings of all values in a map object.
967
+ `map' specifies the map object.
968
+ `np' specifies the pointer to a variable into which the number of elements of the return value
969
+ is assigned.
970
+ The return value is the pointer to the array of all string values in the map object.
971
+ Because the region of the return value is allocated with the `malloc' call, it should be
972
+ released with the `free' call if when is no longer in use. Note that elements of the array
973
+ point to the inner objects, whose life duration is synchronous with the map object. */
974
+ const char **tcmapvals2(const TCMAP *map, int *np);
975
+
976
+
977
+ /* Extract a map record from a serialized byte array.
978
+ `ptr' specifies the pointer to the region of serialized byte array.
979
+ `size' specifies the size of the region.
980
+ `kbuf' specifies the pointer to the region of the key.
981
+ `ksiz' specifies the size of the region of the key.
982
+ `sp' specifies the pointer to the variable into which the size of the region of the return
983
+ value is assigned.
984
+ If successful, the return value is the pointer to the region of the value of the
985
+ corresponding record. `NULL' is returned when no record corresponds.
986
+ Because an additional zero code is appended at the end of the region of the return value,
987
+ the return value can be treated as a character string. */
988
+ void *tcmaploadone(const void *ptr, int size, const void *kbuf, int ksiz, int *sp);
989
+
990
+
991
+ /* Perform formatted output into a map object.
992
+ `map' specifies the map object.
993
+ `kstr' specifies the string of the key.
994
+ `format' specifies the printf-like format string. The conversion character `%' can be used
995
+ with such flag characters as `s', `d', `o', `u', `x', `X', `c', `e', `E', `f', `g', `G', `@',
996
+ `?', `b', and `%'. `@' works as with `s' but escapes meta characters of XML. `?' works as
997
+ with `s' but escapes meta characters of URL. `b' converts an integer to the string as binary
998
+ numbers. The other conversion character work as with each original.
999
+ The other arguments are used according to the format string. */
1000
+ void tcmapprintf(TCMAP *map, const char *kstr, const char *format, ...);
1001
+
1002
+
1003
+
1004
+ /*************************************************************************************************
1005
+ * ordered tree
1006
+ *************************************************************************************************/
1007
+
1008
+
1009
+ typedef struct _TCTREEREC { /* type of structure for an element of a tree */
1010
+ int32_t ksiz; /* size of the region of the key */
1011
+ int32_t vsiz; /* size of the region of the value */
1012
+ struct _TCTREEREC *left; /* pointer to the left child */
1013
+ struct _TCTREEREC *right; /* pointer to the right child */
1014
+ } TCTREEREC;
1015
+
1016
+ typedef struct { /* type of structure for a tree */
1017
+ TCTREEREC *root; /* pointer to the root element */
1018
+ TCTREEREC *cur; /* pointer to the current element */
1019
+ uint64_t rnum; /* number of records */
1020
+ uint64_t msiz; /* total size of records */
1021
+ TCCMP cmp; /* pointer to the comparison function */
1022
+ void *cmpop; /* opaque object for the comparison function */
1023
+ } TCTREE;
1024
+
1025
+
1026
+ /* Create a tree object.
1027
+ The return value is the new tree object. */
1028
+ TCTREE *tctreenew(void);
1029
+
1030
+
1031
+ /* Create a tree object with specifying the custom comparison function.
1032
+ `cmp' specifies the pointer to the custom comparison function. It receives five parameters.
1033
+ The first parameter is the pointer to the region of one key. The second parameter is the size
1034
+ of the region of one key. The third parameter is the pointer to the region of the other key.
1035
+ The fourth parameter is the size of the region of the other key. The fifth parameter is the
1036
+ pointer to the optional opaque object. It returns positive if the former is big, negative if
1037
+ the latter is big, 0 if both are equivalent.
1038
+ `cmpop' specifies an arbitrary pointer to be given as a parameter of the comparison function.
1039
+ If it is not needed, `NULL' can be specified.
1040
+ The return value is the new tree object.
1041
+ The default comparison function compares keys of two records by lexical order. The functions
1042
+ `tccmplexical' (dafault), `tccmpdecimal', `tccmpint32', and `tccmpint64' are built-in. */
1043
+ TCTREE *tctreenew2(TCCMP cmp, void *cmpop);
1044
+
1045
+
1046
+ /* Copy a tree object.
1047
+ `tree' specifies the tree object.
1048
+ The return value is the new tree object equivalent to the specified object. */
1049
+ TCTREE *tctreedup(const TCTREE *tree);
1050
+
1051
+
1052
+ /* Delete a tree object.
1053
+ `tree' specifies the tree object.
1054
+ Note that the deleted object and its derivatives can not be used anymore. */
1055
+ void tctreedel(TCTREE *tree);
1056
+
1057
+
1058
+ /* Store a record into a tree object.
1059
+ `tree' specifies the tree object.
1060
+ `kbuf' specifies the pointer to the region of the key.
1061
+ `ksiz' specifies the size of the region of the key.
1062
+ `vbuf' specifies the pointer to the region of the value.
1063
+ `vsiz' specifies the size of the region of the value.
1064
+ If a record with the same key exists in the tree, it is overwritten. */
1065
+ void tctreeput(TCTREE *tree, const void *kbuf, int ksiz, const void *vbuf, int vsiz);
1066
+
1067
+
1068
+ /* Store a string record into a tree object.
1069
+ `tree' specifies the tree object.
1070
+ `kstr' specifies the string of the key.
1071
+ `vstr' specifies the string of the value.
1072
+ If a record with the same key exists in the tree, it is overwritten. */
1073
+ void tctreeput2(TCTREE *tree, const char *kstr, const char *vstr);
1074
+
1075
+
1076
+ /* Store a new record into a tree object.
1077
+ `tree' specifies the tree object.
1078
+ `kbuf' specifies the pointer to the region of the key.
1079
+ `ksiz' specifies the size of the region of the key.
1080
+ `vbuf' specifies the pointer to the region of the value.
1081
+ `vsiz' specifies the size of the region of the value.
1082
+ If successful, the return value is true, else, it is false.
1083
+ If a record with the same key exists in the tree, this function has no effect. */
1084
+ bool tctreeputkeep(TCTREE *tree, const void *kbuf, int ksiz, const void *vbuf, int vsiz);
1085
+
1086
+
1087
+ /* Store a new string record into a tree object.
1088
+ `tree' specifies the tree object.
1089
+ `kstr' specifies the string of the key.
1090
+ `vstr' specifies the string of the value.
1091
+ If successful, the return value is true, else, it is false.
1092
+ If a record with the same key exists in the tree, this function has no effect. */
1093
+ bool tctreeputkeep2(TCTREE *tree, const char *kstr, const char *vstr);
1094
+
1095
+
1096
+ /* Concatenate a value at the end of the value of the existing record in a tree object.
1097
+ `tree' specifies the tree object.
1098
+ `kbuf' specifies the pointer to the region of the key.
1099
+ `ksiz' specifies the size of the region of the key.
1100
+ `vbuf' specifies the pointer to the region of the value.
1101
+ `vsiz' specifies the size of the region of the value.
1102
+ If there is no corresponding record, a new record is created. */
1103
+ void tctreeputcat(TCTREE *tree, const void *kbuf, int ksiz, const void *vbuf, int vsiz);
1104
+
1105
+
1106
+ /* Concatenate a string value at the end of the value of the existing record in a tree object.
1107
+ `tree' specifies the tree object.
1108
+ `kstr' specifies the string of the key.
1109
+ `vstr' specifies the string of the value.
1110
+ If there is no corresponding record, a new record is created. */
1111
+ void tctreeputcat2(TCTREE *tree, const char *kstr, const char *vstr);
1112
+
1113
+
1114
+ /* Store a record into a tree object with a duplication handler.
1115
+ `tree' specifies the tree object.
1116
+ `kbuf' specifies the pointer to the region of the key.
1117
+ `ksiz' specifies the size of the region of the key.
1118
+ `vbuf' specifies the pointer to the region of the value. `NULL' means that record addition is
1119
+ ommited if there is no corresponding record.
1120
+ `vsiz' specifies the size of the region of the value.
1121
+ `proc' specifies the pointer to the callback function to process duplication. It receives
1122
+ four parameters. The first parameter is the pointer to the region of the value. The second
1123
+ parameter is the size of the region of the value. The third parameter is the pointer to the
1124
+ variable into which the size of the region of the return value is assigned. The fourth
1125
+ parameter is the pointer to the optional opaque object. It returns the pointer to the result
1126
+ object allocated with `malloc'. It is released by the caller. If it is `NULL', the record is
1127
+ not modified. If it is `(void *)-1', the record is removed.
1128
+ `op' specifies an arbitrary pointer to be given as a parameter of the callback function. If
1129
+ it is not needed, `NULL' can be specified.
1130
+ If successful, the return value is true, else, it is false. */
1131
+ bool tctreeputproc(TCTREE *tree, const void *kbuf, int ksiz, const void *vbuf, int vsiz,
1132
+ TCPDPROC proc, void *op);
1133
+
1134
+
1135
+ /* Remove a record of a tree object.
1136
+ `tree' specifies the tree object.
1137
+ `kbuf' specifies the pointer to the region of the key.
1138
+ `ksiz' specifies the size of the region of the key.
1139
+ If successful, the return value is true. False is returned when no record corresponds to
1140
+ the specified key. */
1141
+ bool tctreeout(TCTREE *tree, const void *kbuf, int ksiz);
1142
+
1143
+
1144
+ /* Remove a string record of a tree object.
1145
+ `tree' specifies the tree object.
1146
+ `kstr' specifies the string of the key.
1147
+ If successful, the return value is true. False is returned when no record corresponds to
1148
+ the specified key. */
1149
+ bool tctreeout2(TCTREE *tree, const char *kstr);
1150
+
1151
+
1152
+ /* Retrieve a record in a tree object.
1153
+ `tree' specifies the tree object.
1154
+ `kbuf' specifies the pointer to the region of the key.
1155
+ `ksiz' specifies the size of the region of the key.
1156
+ `sp' specifies the pointer to the variable into which the size of the region of the return
1157
+ value is assigned.
1158
+ If successful, the return value is the pointer to the region of the value of the
1159
+ corresponding record. `NULL' is returned when no record corresponds.
1160
+ Because an additional zero code is appended at the end of the region of the return value,
1161
+ the return value can be treated as a character string. */
1162
+ const void *tctreeget(TCTREE *tree, const void *kbuf, int ksiz, int *sp);
1163
+
1164
+
1165
+ /* Retrieve a string record in a tree object.
1166
+ `tree' specifies the tree object.
1167
+ `kstr' specifies the string of the key.
1168
+ If successful, the return value is the string of the value of the corresponding record.
1169
+ `NULL' is returned when no record corresponds. */
1170
+ const char *tctreeget2(TCTREE *tree, const char *kstr);
1171
+
1172
+
1173
+ /* Initialize the iterator of a tree object.
1174
+ `tree' specifies the tree object.
1175
+ The iterator is used in order to access the key of every record stored in the tree object. */
1176
+ void tctreeiterinit(TCTREE *tree);
1177
+
1178
+
1179
+ /* Get the next key of the iterator of a tree object.
1180
+ `tree' specifies the tree object.
1181
+ `sp' specifies the pointer to the variable into which the size of the region of the return
1182
+ value is assigned.
1183
+ If successful, the return value is the pointer to the region of the next key, else, it is
1184
+ `NULL'. `NULL' is returned when no record can be fetched from the iterator.
1185
+ Because an additional zero code is appended at the end of the region of the return value,
1186
+ the return value can be treated as a character string.
1187
+ The order of iteration is assured to be ascending of the keys. */
1188
+ const void *tctreeiternext(TCTREE *tree, int *sp);
1189
+
1190
+
1191
+ /* Get the next key string of the iterator of a tree object.
1192
+ `tree' specifies the tree object.
1193
+ If successful, the return value is the pointer to the region of the next key, else, it is
1194
+ `NULL'. `NULL' is returned when no record can be fetched from the iterator.
1195
+ The order of iteration is assured to be ascending of the keys. */
1196
+ const char *tctreeiternext2(TCTREE *tree);
1197
+
1198
+
1199
+ /* Get the number of records stored in a tree object.
1200
+ `tree' specifies the tree object.
1201
+ The return value is the number of the records stored in the tree object. */
1202
+ uint64_t tctreernum(const TCTREE *tree);
1203
+
1204
+
1205
+ /* Get the total size of memory used in a tree object.
1206
+ `tree' specifies the tree object.
1207
+ The return value is the total size of memory used in a tree object. */
1208
+ uint64_t tctreemsiz(const TCTREE *tree);
1209
+
1210
+
1211
+ /* Create a list object containing all keys in a tree object.
1212
+ `tree' specifies the tree object.
1213
+ The return value is the new list object containing all keys in the tree object.
1214
+ Because the object of the return value is created with the function `tclistnew', it should
1215
+ be deleted with the function `tclistdel' when it is no longer in use. */
1216
+ TCLIST *tctreekeys(const TCTREE *tree);
1217
+
1218
+
1219
+ /* Create a list object containing all values in a tree object.
1220
+ `tree' specifies the tree object.
1221
+ The return value is the new list object containing all values in the tree object.
1222
+ Because the object of the return value is created with the function `tclistnew', it should
1223
+ be deleted with the function `tclistdel' when it is no longer in use. */
1224
+ TCLIST *tctreevals(const TCTREE *tree);
1225
+
1226
+
1227
+ /* Add an integer to a record in a tree object.
1228
+ `tree' specifies the tree object.
1229
+ `kbuf' specifies the pointer to the region of the key.
1230
+ `ksiz' specifies the size of the region of the key.
1231
+ `num' specifies the additional value.
1232
+ The return value is the summation value.
1233
+ If the corresponding record exists, the value is treated as an integer and is added to. If no
1234
+ record corresponds, a new record of the additional value is stored. */
1235
+ int tctreeaddint(TCTREE *tree, const void *kbuf, int ksiz, int num);
1236
+
1237
+
1238
+ /* Add a real number to a record in a tree object.
1239
+ `tree' specifies the tree object.
1240
+ `kbuf' specifies the pointer to the region of the key.
1241
+ `ksiz' specifies the size of the region of the key.
1242
+ `num' specifies the additional value.
1243
+ The return value is the summation value.
1244
+ If the corresponding record exists, the value is treated as a real number and is added to. If
1245
+ no record corresponds, a new record of the additional value is stored. */
1246
+ double tctreeadddouble(TCTREE *tree, const void *kbuf, int ksiz, double num);
1247
+
1248
+
1249
+ /* Clear a tree object.
1250
+ `tree' specifies the tree object.
1251
+ All records are removed. */
1252
+ void tctreeclear(TCTREE *tree);
1253
+
1254
+
1255
+ /* Remove fringe records of a tree object.
1256
+ `tree' specifies the tree object.
1257
+ `num' specifies the number of records to be removed. */
1258
+ void tctreecutfringe(TCTREE *tree, int num);
1259
+
1260
+
1261
+ /* Serialize a tree object into a byte array.
1262
+ `tree' specifies the tree object.
1263
+ `sp' specifies the pointer to the variable into which the size of the region of the return
1264
+ value is assigned.
1265
+ The return value is the pointer to the region of the result serial region.
1266
+ Because the region of the return value is allocated with the `malloc' call, it should be
1267
+ released with the `free' call when it is no longer in use. */
1268
+ void *tctreedump(const TCTREE *tree, int *sp);
1269
+
1270
+
1271
+ /* Create a tree object from a serialized byte array.
1272
+ `ptr' specifies the pointer to the region of serialized byte array.
1273
+ `size' specifies the size of the region.
1274
+ `cmp' specifies the pointer to the custom comparison function.
1275
+ `cmpop' specifies an arbitrary pointer to be given as a parameter of the comparison function.
1276
+ If it is not needed, `NULL' can be specified.
1277
+ The return value is a new tree object.
1278
+ Because the object of the return value is created with the function `tctreenew', it should be
1279
+ deleted with the function `tctreedel' when it is no longer in use. */
1280
+ TCTREE *tctreeload(const void *ptr, int size, TCCMP cmp, void *cmpop);
1281
+
1282
+
1283
+
1284
+ /*************************************************************************************************
1285
+ * ordered tree (for experts)
1286
+ *************************************************************************************************/
1287
+
1288
+
1289
+ /* Store a record into a tree object without balancing nodes.
1290
+ `tree' specifies the tree object.
1291
+ `kbuf' specifies the pointer to the region of the key.
1292
+ `ksiz' specifies the size of the region of the key.
1293
+ `vbuf' specifies the pointer to the region of the value.
1294
+ `vsiz' specifies the size of the region of the value.
1295
+ If a record with the same key exists in the tree, it is overwritten. The structure of the
1296
+ tree is not modifed by this function. */
1297
+ void tctreeput3(TCTREE *tree, const void *kbuf, int ksiz, const void *vbuf, int vsiz);
1298
+
1299
+
1300
+ /* Store a new record into a tree object without balancing nodes.
1301
+ `tree' specifies the tree object.
1302
+ `kbuf' specifies the pointer to the region of the key.
1303
+ `ksiz' specifies the size of the region of the key.
1304
+ `vbuf' specifies the pointer to the region of the value.
1305
+ `vsiz' specifies the size of the region of the value.
1306
+ If successful, the return value is true, else, it is false.
1307
+ If a record with the same key exists in the tree, this function has no effect. The structure
1308
+ of the tree is not modifed by this function. */
1309
+ bool tctreeputkeep3(TCTREE *tree, const void *kbuf, int ksiz, const void *vbuf, int vsiz);
1310
+
1311
+
1312
+ /* Concatenate a value at the existing record in a tree object without balancing nodes.
1313
+ `tree' specifies the tree object.
1314
+ `kbuf' specifies the pointer to the region of the key.
1315
+ `ksiz' specifies the size of the region of the key.
1316
+ `vbuf' specifies the pointer to the region of the value.
1317
+ `vsiz' specifies the size of the region of the value.
1318
+ If there is no corresponding record, a new record is created. The structure of the tree is
1319
+ not modifed by this function. */
1320
+ void tctreeputcat3(TCTREE *tree, const void *kbuf, int ksiz, const void *vbuf, int vsiz);
1321
+
1322
+
1323
+ /* Retrieve a record in a tree object without balancing nodes.
1324
+ `tree' specifies the tree object.
1325
+ `kbuf' specifies the pointer to the region of the key.
1326
+ `ksiz' specifies the size of the region of the key.
1327
+ `sp' specifies the pointer to the variable into which the size of the region of the return
1328
+ value is assigned.
1329
+ If successful, the return value is the pointer to the region of the value of the
1330
+ corresponding record. `NULL' is returned when no record corresponds.
1331
+ Because an additional zero code is appended at the end of the region of the return value,
1332
+ the return value can be treated as a character string. The structure of the tree is not
1333
+ modifed by this function. */
1334
+ const void *tctreeget3(const TCTREE *tree, const void *kbuf, int ksiz, int *sp);
1335
+
1336
+
1337
+ /* Retrieve a string record in a tree object with specifying the default value string.
1338
+ `tree' specifies the tree object.
1339
+ `kstr' specifies the string of the key.
1340
+ `dstr' specifies the string of the default value.
1341
+ The return value is the string of the value of the corresponding record or the default value
1342
+ string. */
1343
+ const char *tctreeget4(TCTREE *tree, const char *kstr, const char *dstr);
1344
+
1345
+
1346
+ /* Initialize the iterator of a tree object in front of records corresponding a key.
1347
+ `tree' specifies the tree object.
1348
+ `kbuf' specifies the pointer to the region of the key.
1349
+ `ksiz' specifies the size of the region of the key.
1350
+ The iterator is set to the first record corresponding the key or the next substitute if
1351
+ completely matching record does not exist. */
1352
+ void tctreeiterinit2(TCTREE *tree, const void *kbuf, int ksiz);
1353
+
1354
+
1355
+ /* Initialize the iterator of a tree object in front of records corresponding a key string.
1356
+ `tree' specifies the tree object.
1357
+ `kstr' specifies the string of the key.
1358
+ The iterator is set to the first record corresponding the key or the next substitute if
1359
+ completely matching record does not exist. */
1360
+ void tctreeiterinit3(TCTREE *tree, const char *kstr);
1361
+
1362
+
1363
+ /* Get the value bound to the key fetched from the iterator of a tree object.
1364
+ `kbuf' specifies the pointer to the region of the iteration key.
1365
+ `sp' specifies the pointer to the variable into which the size of the region of the return
1366
+ value is assigned.
1367
+ The return value is the pointer to the region of the value of the corresponding record.
1368
+ Because an additional zero code is appended at the end of the region of the return value,
1369
+ the return value can be treated as a character string. */
1370
+ const void *tctreeiterval(const void *kbuf, int *sp);
1371
+
1372
+
1373
+ /* Get the value string bound to the key fetched from the iterator of a tree object.
1374
+ `kstr' specifies the string of the iteration key.
1375
+ The return value is the pointer to the region of the value of the corresponding record. */
1376
+ const char *tctreeiterval2(const char *kstr);
1377
+
1378
+
1379
+ /* Create an array of strings of all keys in a tree object.
1380
+ `tree' specifies the tree object.
1381
+ `np' specifies the pointer to a variable into which the number of elements of the return value
1382
+ is assigned.
1383
+ The return value is the pointer to the array of all string keys in the tree object.
1384
+ Because the region of the return value is allocated with the `malloc' call, it should be
1385
+ released with the `free' call if when is no longer in use. Note that elements of the array
1386
+ point to the inner objects, whose life duration is synchronous with the tree object. */
1387
+ const char **tctreekeys2(const TCTREE *tree, int *np);
1388
+
1389
+
1390
+ /* Create an array of strings of all values in a tree object.
1391
+ `tree' specifies the tree object.
1392
+ `np' specifies the pointer to a variable into which the number of elements of the return value
1393
+ is assigned.
1394
+ The return value is the pointer to the array of all string values in the tree object.
1395
+ Because the region of the return value is allocated with the `malloc' call, it should be
1396
+ released with the `free' call if when is no longer in use. Note that elements of the array
1397
+ point to the inner objects, whose life duration is synchronous with the tree object. */
1398
+ const char **tctreevals2(const TCTREE *tree, int *np);
1399
+
1400
+
1401
+ /* Extract a tree record from a serialized byte array.
1402
+ `ptr' specifies the pointer to the region of serialized byte array.
1403
+ `size' specifies the size of the region.
1404
+ `kbuf' specifies the pointer to the region of the key.
1405
+ `ksiz' specifies the size of the region of the key.
1406
+ `sp' specifies the pointer to the variable into which the size of the region of the return
1407
+ value is assigned.
1408
+ If successful, the return value is the pointer to the region of the value of the
1409
+ corresponding record. `NULL' is returned when no record corresponds.
1410
+ Because an additional zero code is appended at the end of the region of the return value,
1411
+ the return value can be treated as a character string. */
1412
+ void *tctreeloadone(const void *ptr, int size, const void *kbuf, int ksiz, int *sp);
1413
+
1414
+
1415
+ /* Perform formatted output into a tree object.
1416
+ `map' specifies the tree object.
1417
+ `kstr' specifies the string of the key.
1418
+ `format' specifies the printf-like format string. The conversion character `%' can be used
1419
+ with such flag characters as `s', `d', `o', `u', `x', `X', `c', `e', `E', `f', `g', `G', `@',
1420
+ `?', `b', and `%'. `@' works as with `s' but escapes meta characters of XML. `?' works as
1421
+ with `s' but escapes meta characters of URL. `b' converts an integer to the string as binary
1422
+ numbers. The other conversion character work as with each original.
1423
+ The other arguments are used according to the format string. */
1424
+ void tctreeprintf(TCTREE *tree, const char *kstr, const char *format, ...);
1425
+
1426
+
1427
+
1428
+ /*************************************************************************************************
1429
+ * on-memory hash database
1430
+ *************************************************************************************************/
1431
+
1432
+
1433
+ typedef struct { /* type of structure for a on-memory hash database */
1434
+ void **mmtxs; /* mutexes for method */
1435
+ void *imtx; /* mutex for iterator */
1436
+ TCMAP **maps; /* internal map objects */
1437
+ int iter; /* index of maps for the iterator */
1438
+ } TCMDB;
1439
+
1440
+
1441
+ /* Create an on-memory hash database object.
1442
+ The return value is the new on-memory hash database object.
1443
+ The object can be shared by plural threads because of the internal mutex. */
1444
+ TCMDB *tcmdbnew(void);
1445
+
1446
+
1447
+ /* Create an on-memory hash database object with specifying the number of the buckets.
1448
+ `bnum' specifies the number of the buckets.
1449
+ The return value is the new on-memory hash database object.
1450
+ The object can be shared by plural threads because of the internal mutex. */
1451
+ TCMDB *tcmdbnew2(uint32_t bnum);
1452
+
1453
+
1454
+ /* Delete an on-memory hash database object.
1455
+ `mdb' specifies the on-memory hash database object. */
1456
+ void tcmdbdel(TCMDB *mdb);
1457
+
1458
+
1459
+ /* Store a record into an on-memory hash database object.
1460
+ `mdb' specifies the on-memory hash database object.
1461
+ `kbuf' specifies the pointer to the region of the key.
1462
+ `ksiz' specifies the size of the region of the key.
1463
+ `vbuf' specifies the pointer to the region of the value.
1464
+ `vsiz' specifies the size of the region of the value.
1465
+ If a record with the same key exists in the database, it is overwritten. */
1466
+ void tcmdbput(TCMDB *mdb, const void *kbuf, int ksiz, const void *vbuf, int vsiz);
1467
+
1468
+
1469
+ /* Store a string record into an on-memory hash database object.
1470
+ `mdb' specifies the on-memory hash database object.
1471
+ `kstr' specifies the string of the key.
1472
+ `vstr' specifies the string of the value.
1473
+ If a record with the same key exists in the database, it is overwritten. */
1474
+ void tcmdbput2(TCMDB *mdb, const char *kstr, const char *vstr);
1475
+
1476
+
1477
+ /* Store a new record into an on-memory hash database object.
1478
+ `mdb' specifies the on-memory hash database object.
1479
+ `kbuf' specifies the pointer to the region of the key.
1480
+ `ksiz' specifies the size of the region of the key.
1481
+ `vbuf' specifies the pointer to the region of the value.
1482
+ `vsiz' specifies the size of the region of the value.
1483
+ If successful, the return value is true, else, it is false.
1484
+ If a record with the same key exists in the database, this function has no effect. */
1485
+ bool tcmdbputkeep(TCMDB *mdb, const void *kbuf, int ksiz, const void *vbuf, int vsiz);
1486
+
1487
+
1488
+ /* Store a new string record into an on-memory hash database object.
1489
+ `mdb' specifies the on-memory hash database object.
1490
+ `kstr' specifies the string of the key.
1491
+ `vstr' specifies the string of the value.
1492
+ If successful, the return value is true, else, it is false.
1493
+ If a record with the same key exists in the database, this function has no effect. */
1494
+ bool tcmdbputkeep2(TCMDB *mdb, const char *kstr, const char *vstr);
1495
+
1496
+
1497
+ /* Concatenate a value at the end of the existing record in an on-memory hash database.
1498
+ `mdb' specifies the on-memory hash database object.
1499
+ `kbuf' specifies the pointer to the region of the key.
1500
+ `ksiz' specifies the size of the region of the key.
1501
+ `vbuf' specifies the pointer to the region of the value.
1502
+ `vsiz' specifies the size of the region of the value.
1503
+ If there is no corresponding record, a new record is created. */
1504
+ void tcmdbputcat(TCMDB *mdb, const void *kbuf, int ksiz, const void *vbuf, int vsiz);
1505
+
1506
+
1507
+ /* Concatenate a string at the end of the existing record in an on-memory hash database.
1508
+ `mdb' specifies the on-memory hash database object.
1509
+ `kstr' specifies the string of the key.
1510
+ `vstr' specifies the string of the value.
1511
+ If there is no corresponding record, a new record is created. */
1512
+ void tcmdbputcat2(TCMDB *mdb, const char *kstr, const char *vstr);
1513
+
1514
+
1515
+ /* Remove a record of an on-memory hash database object.
1516
+ `mdb' specifies the on-memory hash database object.
1517
+ `kbuf' specifies the pointer to the region of the key.
1518
+ `ksiz' specifies the size of the region of the key.
1519
+ If successful, the return value is true. False is returned when no record corresponds to
1520
+ the specified key. */
1521
+ bool tcmdbout(TCMDB *mdb, const void *kbuf, int ksiz);
1522
+
1523
+
1524
+ /* Remove a string record of an on-memory hash database object.
1525
+ `mdb' specifies the on-memory hash database object.
1526
+ `kstr' specifies the string of the key.
1527
+ If successful, the return value is true. False is returned when no record corresponds to
1528
+ the specified key. */
1529
+ bool tcmdbout2(TCMDB *mdb, const char *kstr);
1530
+
1531
+
1532
+ /* Retrieve a record in an on-memory hash database object.
1533
+ `mdb' specifies the on-memory hash database object.
1534
+ `kbuf' specifies the pointer to the region of the key.
1535
+ `ksiz' specifies the size of the region of the key.
1536
+ `sp' specifies the pointer to the variable into which the size of the region of the return
1537
+ value is assigned.
1538
+ If successful, the return value is the pointer to the region of the value of the
1539
+ corresponding record. `NULL' is returned when no record corresponds.
1540
+ Because an additional zero code is appended at the end of the region of the return value,
1541
+ the return value can be treated as a character string. Because the region of the return
1542
+ value is allocated with the `malloc' call, it should be released with the `free' call when
1543
+ it is no longer in use. */
1544
+ void *tcmdbget(TCMDB *mdb, const void *kbuf, int ksiz, int *sp);
1545
+
1546
+
1547
+ /* Retrieve a string record in an on-memory hash database object.
1548
+ `mdb' specifies the on-memory hash database object.
1549
+ `kstr' specifies the string of the key.
1550
+ If successful, the return value is the string of the value of the corresponding record.
1551
+ `NULL' is returned when no record corresponds.
1552
+ Because the region of the return value is allocated with the `malloc' call, it should be
1553
+ released with the `free' call when it is no longer in use. */
1554
+ char *tcmdbget2(TCMDB *mdb, const char *kstr);
1555
+
1556
+
1557
+ /* Get the size of the value of a record in an on-memory hash database object.
1558
+ `mdb' specifies the on-memory hash database object.
1559
+ `kbuf' specifies the pointer to the region of the key.
1560
+ `ksiz' specifies the size of the region of the key.
1561
+ If successful, the return value is the size of the value of the corresponding record, else,
1562
+ it is -1. */
1563
+ int tcmdbvsiz(TCMDB *mdb, const void *kbuf, int ksiz);
1564
+
1565
+
1566
+ /* Get the size of the value of a string record in an on-memory hash database object.
1567
+ `mdb' specifies the on-memory hash database object.
1568
+ `kstr' specifies the string of the key.
1569
+ If successful, the return value is the size of the value of the corresponding record, else,
1570
+ it is -1. */
1571
+ int tcmdbvsiz2(TCMDB *mdb, const char *kstr);
1572
+
1573
+
1574
+ /* Initialize the iterator of an on-memory hash database object.
1575
+ `mdb' specifies the on-memory hash database object.
1576
+ The iterator is used in order to access the key of every record stored in the on-memory
1577
+ database. */
1578
+ void tcmdbiterinit(TCMDB *mdb);
1579
+
1580
+
1581
+ /* Get the next key of the iterator of an on-memory hash database object.
1582
+ `mdb' specifies the on-memory hash database object.
1583
+ `sp' specifies the pointer to the variable into which the size of the region of the return
1584
+ value is assigned.
1585
+ If successful, the return value is the pointer to the region of the next key, else, it is
1586
+ `NULL'. `NULL' is returned when no record can be fetched from the iterator.
1587
+ Because an additional zero code is appended at the end of the region of the return value,
1588
+ the return value can be treated as a character string. Because the region of the return
1589
+ value is allocated with the `malloc' call, it should be released with the `free' call when
1590
+ it is no longer in use. The order of iteration is assured to be the same as the stored
1591
+ order. */
1592
+ void *tcmdbiternext(TCMDB *mdb, int *sp);
1593
+
1594
+
1595
+ /* Get the next key string of the iterator of an on-memory hash database object.
1596
+ `mdb' specifies the on-memory hash database object.
1597
+ If successful, the return value is the pointer to the region of the next key, else, it is
1598
+ `NULL'. `NULL' is returned when no record can be fetched from the iterator.
1599
+ Because the region of the return value is allocated with the `malloc' call, it should be
1600
+ released with the `free' call when it is no longer in use. The order of iteration is assured
1601
+ to be the same as the stored order. */
1602
+ char *tcmdbiternext2(TCMDB *mdb);
1603
+
1604
+
1605
+ /* Get forward matching keys in an on-memory hash database object.
1606
+ `mdb' specifies the on-memory hash database object.
1607
+ `pbuf' specifies the pointer to the region of the prefix.
1608
+ `psiz' specifies the size of the region of the prefix.
1609
+ `max' specifies the maximum number of keys to be fetched. If it is negative, no limit is
1610
+ specified.
1611
+ The return value is a list object of the corresponding keys. This function does never fail.
1612
+ It returns an empty list even if no key corresponds.
1613
+ Because the object of the return value is created with the function `tclistnew', it should be
1614
+ deleted with the function `tclistdel' when it is no longer in use. Note that this function
1615
+ may be very slow because every key in the database is scanned. */
1616
+ TCLIST *tcmdbfwmkeys(TCMDB *mdb, const void *pbuf, int psiz, int max);
1617
+
1618
+
1619
+ /* Get forward matching string keys in an on-memory hash database object.
1620
+ `mdb' specifies the on-memory hash database object.
1621
+ `pstr' specifies the string of the prefix.
1622
+ `max' specifies the maximum number of keys to be fetched. If it is negative, no limit is
1623
+ specified.
1624
+ The return value is a list object of the corresponding keys. This function does never fail.
1625
+ It returns an empty list even if no key corresponds.
1626
+ Because the object of the return value is created with the function `tclistnew', it should be
1627
+ deleted with the function `tclistdel' when it is no longer in use. Note that this function
1628
+ may be very slow because every key in the database is scanned. */
1629
+ TCLIST *tcmdbfwmkeys2(TCMDB *mdb, const char *pstr, int max);
1630
+
1631
+
1632
+ /* Get the number of records stored in an on-memory hash database object.
1633
+ `mdb' specifies the on-memory hash database object.
1634
+ The return value is the number of the records stored in the database. */
1635
+ uint64_t tcmdbrnum(TCMDB *mdb);
1636
+
1637
+
1638
+ /* Get the total size of memory used in an on-memory hash database object.
1639
+ `mdb' specifies the on-memory hash database object.
1640
+ The return value is the total size of memory used in the database. */
1641
+ uint64_t tcmdbmsiz(TCMDB *mdb);
1642
+
1643
+
1644
+ /* Add an integer to a record in an on-memory hash database object.
1645
+ `mdb' specifies the on-memory hash database object.
1646
+ `kbuf' specifies the pointer to the region of the key.
1647
+ `ksiz' specifies the size of the region of the key.
1648
+ `num' specifies the additional value.
1649
+ The return value is the summation value.
1650
+ If the corresponding record exists, the value is treated as an integer and is added to. If no
1651
+ record corresponds, a new record of the additional value is stored. */
1652
+ int tcmdbaddint(TCMDB *mdb, const void *kbuf, int ksiz, int num);
1653
+
1654
+
1655
+ /* Add a real number to a record in an on-memory hash database object.
1656
+ `mdb' specifies the on-memory hash database object.
1657
+ `kbuf' specifies the pointer to the region of the key.
1658
+ `ksiz' specifies the size of the region of the key.
1659
+ `num' specifies the additional value.
1660
+ The return value is the summation value.
1661
+ If the corresponding record exists, the value is treated as a real number and is added to. If
1662
+ no record corresponds, a new record of the additional value is stored. */
1663
+ double tcmdbadddouble(TCMDB *mdb, const void *kbuf, int ksiz, double num);
1664
+
1665
+
1666
+ /* Clear an on-memory hash database object.
1667
+ `mdb' specifies the on-memory hash database object.
1668
+ All records are removed. */
1669
+ void tcmdbvanish(TCMDB *mdb);
1670
+
1671
+
1672
+ /* Remove front records of an on-memory hash database object.
1673
+ `mdb' specifies the on-memory hash database object.
1674
+ `num' specifies the number of records to be removed. */
1675
+ void tcmdbcutfront(TCMDB *mdb, int num);
1676
+
1677
+
1678
+
1679
+ /*************************************************************************************************
1680
+ * on-memory hash database (for experts)
1681
+ *************************************************************************************************/
1682
+
1683
+
1684
+ /* Store a record and make it semivolatile in an on-memory hash database object.
1685
+ `mdb' specifies the on-memory hash database object.
1686
+ `kbuf' specifies the pointer to the region of the key.
1687
+ `ksiz' specifies the size of the region of the key.
1688
+ `vbuf' specifies the pointer to the region of the value.
1689
+ `vsiz' specifies the size of the region of the value.
1690
+ If a record with the same key exists in the map, it is overwritten. The record is moved to
1691
+ the tail. */
1692
+ void tcmdbput3(TCMDB *mdb, const void *kbuf, int ksiz, const char *vbuf, int vsiz);
1693
+
1694
+
1695
+ /* Store a record of the value of two regions into an on-memory hash database object.
1696
+ `mdb' specifies the on-memory hash database object.
1697
+ `kbuf' specifies the pointer to the region of the key.
1698
+ `ksiz' specifies the size of the region of the key.
1699
+ `fvbuf' specifies the pointer to the former region of the value.
1700
+ `fvsiz' specifies the size of the former region of the value.
1701
+ `lvbuf' specifies the pointer to the latter region of the value.
1702
+ `lvsiz' specifies the size of the latter region of the value.
1703
+ If a record with the same key exists in the database, it is overwritten. */
1704
+ void tcmdbput4(TCMDB *mdb, const void *kbuf, int ksiz,
1705
+ const void *fvbuf, int fvsiz, const void *lvbuf, int lvsiz);
1706
+
1707
+
1708
+ /* Concatenate a value and make it semivolatile in on-memory hash database object.
1709
+ `mdb' specifies the on-memory hash database object.
1710
+ `kbuf' specifies the pointer to the region of the key.
1711
+ `ksiz' specifies the size of the region of the key.
1712
+ `vbuf' specifies the pointer to the region of the value.
1713
+ `vsiz' specifies the size of the region of the value.
1714
+ If there is no corresponding record, a new record is created. */
1715
+ void tcmdbputcat3(TCMDB *mdb, const void *kbuf, int ksiz, const void *vbuf, int vsiz);
1716
+
1717
+
1718
+ /* Store a record into a on-memory hash database object with a duplication handler.
1719
+ `mdb' specifies the on-memory hash database object.
1720
+ `kbuf' specifies the pointer to the region of the key.
1721
+ `ksiz' specifies the size of the region of the key.
1722
+ `vbuf' specifies the pointer to the region of the value. `NULL' means that record addition is
1723
+ ommited if there is no corresponding record.
1724
+ `vsiz' specifies the size of the region of the value.
1725
+ `proc' specifies the pointer to the callback function to process duplication. It receives
1726
+ four parameters. The first parameter is the pointer to the region of the value. The second
1727
+ parameter is the size of the region of the value. The third parameter is the pointer to the
1728
+ variable into which the size of the region of the return value is assigned. The fourth
1729
+ parameter is the pointer to the optional opaque object. It returns the pointer to the result
1730
+ object allocated with `malloc'. It is released by the caller. If it is `NULL', the record is
1731
+ not modified. If it is `(void *)-1', the record is removed.
1732
+ `op' specifies an arbitrary pointer to be given as a parameter of the callback function. If
1733
+ it is not needed, `NULL' can be specified.
1734
+ If successful, the return value is true, else, it is false. */
1735
+ bool tcmdbputproc(TCMDB *mdb, const void *kbuf, int ksiz, const void *vbuf, int vsiz,
1736
+ TCPDPROC proc, void *op);
1737
+
1738
+
1739
+ /* Retrieve a record and move it astern in an on-memory hash database object.
1740
+ `mdb' specifies the on-memory hash database object.
1741
+ `kbuf' specifies the pointer to the region of the key.
1742
+ `ksiz' specifies the size of the region of the key.
1743
+ `sp' specifies the pointer to the variable into which the size of the region of the return
1744
+ value is assigned.
1745
+ If successful, the return value is the pointer to the region of the value of the
1746
+ corresponding record. `NULL' is returned when no record corresponds.
1747
+ Because an additional zero code is appended at the end of the region of the return value,
1748
+ the return value can be treated as a character string. Because the region of the return value
1749
+ is allocated with the `malloc' call, it should be released with the `free' call when it is no
1750
+ longer in use. The internal region of the returned record is moved to the tail so that the
1751
+ record will survive for a time under LRU cache algorithm removing records from the head. */
1752
+ void *tcmdbget3(TCMDB *mdb, const void *kbuf, int ksiz, int *sp);
1753
+
1754
+
1755
+ /* Initialize the iterator of an on-memory map database object in front of a key.
1756
+ `mdb' specifies the on-memory map database object.
1757
+ `kbuf' specifies the pointer to the region of the key.
1758
+ `ksiz' specifies the size of the region of the key.
1759
+ If there is no record corresponding the condition, the iterator is not modified. */
1760
+ void tcmdbiterinit2(TCMDB *mdb, const void *kbuf, int ksiz);
1761
+
1762
+
1763
+ /* Initialize the iterator of an on-memory map database object in front of a key string.
1764
+ `mdb' specifies the on-memory map database object.
1765
+ `kstr' specifies the string of the key.
1766
+ If there is no record corresponding the condition, the iterator is not modified. */
1767
+ void tcmdbiterinit3(TCMDB *mdb, const char *kstr);
1768
+
1769
+
1770
+ /* Process each record atomically of an on-memory hash database object.
1771
+ `iter' specifies the pointer to the iterator function called for each record. It receives
1772
+ five parameters. The first parameter is the pointer to the region of the key. The second
1773
+ parameter is the size of the region of the key. The third parameter is the pointer to the
1774
+ region of the value. The fourth parameter is the size of the region of the value. The fifth
1775
+ parameter is the pointer to the optional opaque object. It returns true to continue iteration
1776
+ or false to stop iteration.
1777
+ `op' specifies an arbitrary pointer to be given as a parameter of the iterator function. If
1778
+ it is not needed, `NULL' can be specified. */
1779
+ void tcmdbforeach(TCMDB *mdb, TCITER iter, void *op);
1780
+
1781
+
1782
+
1783
+ /*************************************************************************************************
1784
+ * on-memory tree database
1785
+ *************************************************************************************************/
1786
+
1787
+
1788
+ typedef struct { /* type of structure for a on-memory tree database */
1789
+ void *mmtx; /* mutex for method */
1790
+ TCTREE *tree; /* internal tree object */
1791
+ } TCNDB;
1792
+
1793
+
1794
+ /* Create an on-memory tree database object.
1795
+ The return value is the new on-memory tree database object.
1796
+ The object can be shared by plural threads because of the internal mutex. */
1797
+ TCNDB *tcndbnew(void);
1798
+
1799
+
1800
+ /* Create an on-memory tree database object with specifying the custom comparison function.
1801
+ `cmp' specifies the pointer to the custom comparison function.
1802
+ `cmpop' specifies an arbitrary pointer to be given as a parameter of the comparison function.
1803
+ If it is not needed, `NULL' can be specified.
1804
+ The return value is the new on-memory tree database object.
1805
+ The default comparison function compares keys of two records by lexical order. The functions
1806
+ `tccmplexical' (dafault), `tccmpdecimal', `tccmpint32', and `tccmpint64' are built-in. The
1807
+ object can be shared by plural threads because of the internal mutex. */
1808
+ TCNDB *tcndbnew2(TCCMP cmp, void *cmpop);
1809
+
1810
+
1811
+ /* Delete an on-memory tree database object.
1812
+ `ndb' specifies the on-memory tree database object. */
1813
+ void tcndbdel(TCNDB *ndb);
1814
+
1815
+
1816
+ /* Store a record into an on-memory tree database object.
1817
+ `ndb' specifies the on-memory tree database object.
1818
+ `kbuf' specifies the pointer to the region of the key.
1819
+ `ksiz' specifies the size of the region of the key.
1820
+ `vbuf' specifies the pointer to the region of the value.
1821
+ `vsiz' specifies the size of the region of the value.
1822
+ If a record with the same key exists in the database, it is overwritten. */
1823
+ void tcndbput(TCNDB *ndb, const void *kbuf, int ksiz, const void *vbuf, int vsiz);
1824
+
1825
+
1826
+ /* Store a string record into an on-memory tree database object.
1827
+ `ndb' specifies the on-memory tree database object.
1828
+ `kstr' specifies the string of the key.
1829
+ `vstr' specifies the string of the value.
1830
+ If a record with the same key exists in the database, it is overwritten. */
1831
+ void tcndbput2(TCNDB *ndb, const char *kstr, const char *vstr);
1832
+
1833
+
1834
+ /* Store a new record into an on-memory tree database object.
1835
+ `ndb' specifies the on-memory tree database object.
1836
+ `kbuf' specifies the pointer to the region of the key.
1837
+ `ksiz' specifies the size of the region of the key.
1838
+ `vbuf' specifies the pointer to the region of the value.
1839
+ `vsiz' specifies the size of the region of the value.
1840
+ If successful, the return value is true, else, it is false.
1841
+ If a record with the same key exists in the database, this function has no effect. */
1842
+ bool tcndbputkeep(TCNDB *ndb, const void *kbuf, int ksiz, const void *vbuf, int vsiz);
1843
+
1844
+
1845
+ /* Store a new string record into an on-memory tree database object.
1846
+ `ndb' specifies the on-memory tree database object.
1847
+ `kstr' specifies the string of the key.
1848
+ `vstr' specifies the string of the value.
1849
+ If successful, the return value is true, else, it is false.
1850
+ If a record with the same key exists in the database, this function has no effect. */
1851
+ bool tcndbputkeep2(TCNDB *ndb, const char *kstr, const char *vstr);
1852
+
1853
+
1854
+ /* Concatenate a value at the end of the existing record in an on-memory tree database.
1855
+ `ndb' specifies the on-memory tree database object.
1856
+ `kbuf' specifies the pointer to the region of the key.
1857
+ `ksiz' specifies the size of the region of the key.
1858
+ `vbuf' specifies the pointer to the region of the value.
1859
+ `vsiz' specifies the size of the region of the value.
1860
+ If there is no corresponding record, a new record is created. */
1861
+ void tcndbputcat(TCNDB *ndb, const void *kbuf, int ksiz, const void *vbuf, int vsiz);
1862
+
1863
+
1864
+ /* Concatenate a string at the end of the existing record in an on-memory tree database.
1865
+ `ndb' specifies the on-memory tree database object.
1866
+ `kstr' specifies the string of the key.
1867
+ `vstr' specifies the string of the value.
1868
+ If there is no corresponding record, a new record is created. */
1869
+ void tcndbputcat2(TCNDB *ndb, const char *kstr, const char *vstr);
1870
+
1871
+
1872
+ /* Remove a record of an on-memory tree database object.
1873
+ `ndb' specifies the on-memory tree database object.
1874
+ `kbuf' specifies the pointer to the region of the key.
1875
+ `ksiz' specifies the size of the region of the key.
1876
+ If successful, the return value is true. False is returned when no record corresponds to
1877
+ the specified key. */
1878
+ bool tcndbout(TCNDB *ndb, const void *kbuf, int ksiz);
1879
+
1880
+
1881
+ /* Remove a string record of an on-memory tree database object.
1882
+ `ndb' specifies the on-memory tree database object.
1883
+ `kstr' specifies the string of the key.
1884
+ If successful, the return value is true. False is returned when no record corresponds to
1885
+ the specified key. */
1886
+ bool tcndbout2(TCNDB *ndb, const char *kstr);
1887
+
1888
+
1889
+ /* Retrieve a record in an on-memory tree database object.
1890
+ `ndb' specifies the on-memory tree database object.
1891
+ `kbuf' specifies the pointer to the region of the key.
1892
+ `ksiz' specifies the size of the region of the key.
1893
+ `sp' specifies the pointer to the variable into which the size of the region of the return
1894
+ value is assigned.
1895
+ If successful, the return value is the pointer to the region of the value of the
1896
+ corresponding record. `NULL' is returned when no record corresponds.
1897
+ Because an additional zero code is appended at the end of the region of the return value,
1898
+ the return value can be treated as a character string. Because the region of the return
1899
+ value is allocated with the `malloc' call, it should be released with the `free' call when
1900
+ it is no longer in use. */
1901
+ void *tcndbget(TCNDB *ndb, const void *kbuf, int ksiz, int *sp);
1902
+
1903
+
1904
+ /* Retrieve a string record in an on-memory tree database object.
1905
+ `ndb' specifies the on-memory tree database object.
1906
+ `kstr' specifies the string of the key.
1907
+ If successful, the return value is the string of the value of the corresponding record.
1908
+ `NULL' is returned when no record corresponds.
1909
+ Because the region of the return value is allocated with the `malloc' call, it should be
1910
+ released with the `free' call when it is no longer in use. */
1911
+ char *tcndbget2(TCNDB *ndb, const char *kstr);
1912
+
1913
+
1914
+ /* Get the size of the value of a record in an on-memory tree database object.
1915
+ `ndb' specifies the on-memory tree database object.
1916
+ `kbuf' specifies the pointer to the region of the key.
1917
+ `ksiz' specifies the size of the region of the key.
1918
+ If successful, the return value is the size of the value of the corresponding record, else,
1919
+ it is -1. */
1920
+ int tcndbvsiz(TCNDB *ndb, const void *kbuf, int ksiz);
1921
+
1922
+
1923
+ /* Get the size of the value of a string record in an on-memory tree database object.
1924
+ `ndb' specifies the on-memory tree database object.
1925
+ `kstr' specifies the string of the key.
1926
+ If successful, the return value is the size of the value of the corresponding record, else,
1927
+ it is -1. */
1928
+ int tcndbvsiz2(TCNDB *ndb, const char *kstr);
1929
+
1930
+
1931
+ /* Initialize the iterator of an on-memory tree database object.
1932
+ `ndb' specifies the on-memory tree database object.
1933
+ The iterator is used in order to access the key of every record stored in the on-memory
1934
+ database. */
1935
+ void tcndbiterinit(TCNDB *ndb);
1936
+
1937
+
1938
+ /* Get the next key of the iterator of an on-memory tree database object.
1939
+ `ndb' specifies the on-memory tree database object.
1940
+ `sp' specifies the pointer to the variable into which the size of the region of the return
1941
+ value is assigned.
1942
+ If successful, the return value is the pointer to the region of the next key, else, it is
1943
+ `NULL'. `NULL' is returned when no record can be fetched from the iterator.
1944
+ Because an additional zero code is appended at the end of the region of the return value,
1945
+ the return value can be treated as a character string. Because the region of the return
1946
+ value is allocated with the `malloc' call, it should be released with the `free' call when
1947
+ it is no longer in use. The order of iteration is assured to be the same as the stored
1948
+ order. */
1949
+ void *tcndbiternext(TCNDB *ndb, int *sp);
1950
+
1951
+
1952
+ /* Get the next key string of the iterator of an on-memory tree database object.
1953
+ `ndb' specifies the on-memory tree database object.
1954
+ If successful, the return value is the pointer to the region of the next key, else, it is
1955
+ `NULL'. `NULL' is returned when no record can be fetched from the iterator.
1956
+ Because the region of the return value is allocated with the `malloc' call, it should be
1957
+ released with the `free' call when it is no longer in use. The order of iteration is assured
1958
+ to be the same as the stored order. */
1959
+ char *tcndbiternext2(TCNDB *ndb);
1960
+
1961
+
1962
+ /* Get forward matching keys in an on-memory tree database object.
1963
+ `ndb' specifies the on-memory tree database object.
1964
+ `pbuf' specifies the pointer to the region of the prefix.
1965
+ `psiz' specifies the size of the region of the prefix.
1966
+ `max' specifies the maximum number of keys to be fetched. If it is negative, no limit is
1967
+ specified.
1968
+ The return value is a list object of the corresponding keys. This function does never fail.
1969
+ It returns an empty list even if no key corresponds.
1970
+ Because the object of the return value is created with the function `tclistnew', it should be
1971
+ deleted with the function `tclistdel' when it is no longer in use. */
1972
+ TCLIST *tcndbfwmkeys(TCNDB *ndb, const void *pbuf, int psiz, int max);
1973
+
1974
+
1975
+ /* Get forward matching string keys in an on-memory tree database object.
1976
+ `ndb' specifies the on-memory tree database object.
1977
+ `pstr' specifies the string of the prefix.
1978
+ `max' specifies the maximum number of keys to be fetched. If it is negative, no limit is
1979
+ specified.
1980
+ The return value is a list object of the corresponding keys. This function does never fail.
1981
+ It returns an empty list even if no key corresponds.
1982
+ Because the object of the return value is created with the function `tclistnew', it should be
1983
+ deleted with the function `tclistdel' when it is no longer in use. */
1984
+ TCLIST *tcndbfwmkeys2(TCNDB *ndb, const char *pstr, int max);
1985
+
1986
+
1987
+ /* Get the number of records stored in an on-memory tree database object.
1988
+ `ndb' specifies the on-memory tree database object.
1989
+ The return value is the number of the records stored in the database. */
1990
+ uint64_t tcndbrnum(TCNDB *ndb);
1991
+
1992
+
1993
+ /* Get the total size of memory used in an on-memory tree database object.
1994
+ `ndb' specifies the on-memory tree database object.
1995
+ The return value is the total size of memory used in the database. */
1996
+ uint64_t tcndbmsiz(TCNDB *ndb);
1997
+
1998
+
1999
+ /* Add an integer to a record in an on-memory tree database object.
2000
+ `ndb' specifies the on-memory tree database object.
2001
+ `kbuf' specifies the pointer to the region of the key.
2002
+ `ksiz' specifies the size of the region of the key.
2003
+ `num' specifies the additional value.
2004
+ The return value is the summation value.
2005
+ If the corresponding record exists, the value is treated as an integer and is added to. If no
2006
+ record corresponds, a new record of the additional value is stored. */
2007
+ int tcndbaddint(TCNDB *ndb, const void *kbuf, int ksiz, int num);
2008
+
2009
+
2010
+ /* Add a real number to a record in an on-memory tree database object.
2011
+ `ndb' specifies the on-memory tree database object.
2012
+ `kbuf' specifies the pointer to the region of the key.
2013
+ `ksiz' specifies the size of the region of the key.
2014
+ `num' specifies the additional value.
2015
+ The return value is the summation value.
2016
+ If the corresponding record exists, the value is treated as a real number and is added to. If
2017
+ no record corresponds, a new record of the additional value is stored. */
2018
+ double tcndbadddouble(TCNDB *ndb, const void *kbuf, int ksiz, double num);
2019
+
2020
+
2021
+ /* Clear an on-memory tree database object.
2022
+ `ndb' specifies the on-memory tree database object.
2023
+ All records are removed. */
2024
+ void tcndbvanish(TCNDB *ndb);
2025
+
2026
+
2027
+ /* Remove fringe records of an on-memory tree database object.
2028
+ `ndb' specifies the on-memory tree database object.
2029
+ `num' specifies the number of records to be removed. */
2030
+ void tcndbcutfringe(TCNDB *ndb, int num);
2031
+
2032
+
2033
+
2034
+ /*************************************************************************************************
2035
+ * ordered tree (for experts)
2036
+ *************************************************************************************************/
2037
+
2038
+
2039
+ /* Store a record into a on-memory tree database without balancing nodes.
2040
+ `ndb' specifies the on-memory tree database.
2041
+ `kbuf' specifies the pointer to the region of the key.
2042
+ `ksiz' specifies the size of the region of the key.
2043
+ `vbuf' specifies the pointer to the region of the value.
2044
+ `vsiz' specifies the size of the region of the value.
2045
+ If a record with the same key exists in the database, it is overwritten. The structure of the
2046
+ tree is not modifed by this function. */
2047
+ void tcndbput3(TCNDB *ndb, const void *kbuf, int ksiz, const void *vbuf, int vsiz);
2048
+
2049
+
2050
+ /* Store a new record into a on-memory tree database object without balancing nodes.
2051
+ `ndb' specifies the on-memory tree database.
2052
+ `kbuf' specifies the pointer to the region of the key.
2053
+ `ksiz' specifies the size of the region of the key.
2054
+ `vbuf' specifies the pointer to the region of the value.
2055
+ `vsiz' specifies the size of the region of the value.
2056
+ If successful, the return value is true, else, it is false.
2057
+ If a record with the same key exists in the database, this function has no effect. The
2058
+ structure of the tree is not modifed by this function. */
2059
+ bool tcndbputkeep3(TCNDB *ndb, const void *kbuf, int ksiz, const void *vbuf, int vsiz);
2060
+
2061
+
2062
+ /* Concatenate a value in a on-memory tree database without balancing nodes.
2063
+ `ndb' specifies the on-memory tree database.
2064
+ `kbuf' specifies the pointer to the region of the key.
2065
+ `ksiz' specifies the size of the region of the key.
2066
+ `vbuf' specifies the pointer to the region of the value.
2067
+ `vsiz' specifies the size of the region of the value.
2068
+ If there is no corresponding record, a new record is created. The structure of the tree is
2069
+ not modifed by this function. */
2070
+ void tcndbputcat3(TCNDB *ndb, const void *kbuf, int ksiz, const void *vbuf, int vsiz);
2071
+
2072
+
2073
+ /* Store a record into a on-memory tree database object with a duplication handler.
2074
+ `ndb' specifies the on-memory tree database.
2075
+ `kbuf' specifies the pointer to the region of the key.
2076
+ `ksiz' specifies the size of the region of the key.
2077
+ `vbuf' specifies the pointer to the region of the value. `NULL' means that record addition is
2078
+ ommited if there is no corresponding record.
2079
+ `vsiz' specifies the size of the region of the value.
2080
+ `proc' specifies the pointer to the callback function to process duplication. It receives
2081
+ four parameters. The first parameter is the pointer to the region of the value. The second
2082
+ parameter is the size of the region of the value. The third parameter is the pointer to the
2083
+ variable into which the size of the region of the return value is assigned. The fourth
2084
+ parameter is the pointer to the optional opaque object. It returns the pointer to the result
2085
+ object allocated with `malloc'. It is released by the caller. If it is `NULL', the record is
2086
+ not modified. If it is `(void *)-1', the record is removed.
2087
+ `op' specifies an arbitrary pointer to be given as a parameter of the callback function. If
2088
+ it is not needed, `NULL' can be specified.
2089
+ If successful, the return value is true, else, it is false. */
2090
+ bool tcndbputproc(TCNDB *ndb, const void *kbuf, int ksiz, const void *vbuf, int vsiz,
2091
+ TCPDPROC proc, void *op);
2092
+
2093
+
2094
+ /* Retrieve a record in an on-memory tree database object without balancing nodes.
2095
+ `ndb' specifies the on-memory tree database object.
2096
+ `kbuf' specifies the pointer to the region of the key.
2097
+ `ksiz' specifies the size of the region of the key.
2098
+ `sp' specifies the pointer to the variable into which the size of the region of the return
2099
+ value is assigned.
2100
+ If successful, the return value is the pointer to the region of the value of the
2101
+ corresponding record. `NULL' is returned when no record corresponds.
2102
+ Because an additional zero code is appended at the end of the region of the return value,
2103
+ the return value can be treated as a character string. Because the region of the return value
2104
+ is allocated with the `malloc' call, it should be released with the `free' call when it is no
2105
+ longer in use. The structure of the tree is not modifed by this function. */
2106
+ void *tcndbget3(TCNDB *ndb, const void *kbuf, int ksiz, int *sp);
2107
+
2108
+
2109
+ /* Initialize the iterator of an on-memory tree database object in front of a key.
2110
+ `ndb' specifies the on-memory tree database object.
2111
+ `kbuf' specifies the pointer to the region of the key.
2112
+ `ksiz' specifies the size of the region of the key.
2113
+ The iterator is set to the first record corresponding the key or the next substitute if
2114
+ completely matching record does not exist. */
2115
+ void tcndbiterinit2(TCNDB *ndb, const void *kbuf, int ksiz);
2116
+
2117
+
2118
+ /* Initialize the iterator of an on-memory tree database object in front of a key string.
2119
+ `ndb' specifies the on-memory tree database object.
2120
+ `kstr' specifies the string of the key.
2121
+ The iterator is set to the first record corresponding the key or the next substitute if
2122
+ completely matching record does not exist. */
2123
+ void tcndbiterinit3(TCNDB *ndb, const char *kstr);
2124
+
2125
+
2126
+ /* Process each record atomically of an on-memory tree database object.
2127
+ `iter' specifies the pointer to the iterator function called for each record. It receives
2128
+ five parameters. The first parameter is the pointer to the region of the key. The second
2129
+ parameter is the size of the region of the key. The third parameter is the pointer to the
2130
+ region of the value. The fourth parameter is the size of the region of the value. The fifth
2131
+ parameter is the pointer to the optional opaque object. It returns true to continue iteration
2132
+ or false to stop iteration.
2133
+ `op' specifies an arbitrary pointer to be given as a parameter of the iterator function. If
2134
+ it is not needed, `NULL' can be specified. */
2135
+ void tcndbforeach(TCNDB *ndb, TCITER iter, void *op);
2136
+
2137
+
2138
+
2139
+ /*************************************************************************************************
2140
+ * memory pool
2141
+ *************************************************************************************************/
2142
+
2143
+
2144
+ typedef struct { /* type of an element of memory pool */
2145
+ void *ptr; /* pointer */
2146
+ void (*del)(void *); /* deleting function */
2147
+ } TCMPELEM;
2148
+
2149
+ typedef struct { /* type of structure for a memory pool object */
2150
+ void *mutex; /* mutex for operations */
2151
+ TCMPELEM *elems; /* array of elements */
2152
+ int anum; /* number of the elements of the array */
2153
+ int num; /* number of used elements */
2154
+ } TCMPOOL;
2155
+
2156
+
2157
+ /* Create a memory pool object.
2158
+ The return value is the new memory pool object. */
2159
+ TCMPOOL *tcmpoolnew(void);
2160
+
2161
+
2162
+ /* Delete a memory pool object.
2163
+ `mpool' specifies the memory pool object.
2164
+ Note that the deleted object and its derivatives can not be used anymore. */
2165
+ void tcmpooldel(TCMPOOL *mpool);
2166
+
2167
+
2168
+ /* Relegate an arbitrary object to a memory pool object.
2169
+ `mpool' specifies the memory pool object.
2170
+ `ptr' specifies the pointer to the object to be relegated. If it is `NULL', this function has
2171
+ no effect.
2172
+ `del' specifies the pointer to the function to delete the object.
2173
+ The return value is the pointer to the given object.
2174
+ This function assures that the specified object is deleted when the memory pool object is
2175
+ deleted. */
2176
+ void *tcmpoolpush(TCMPOOL *mpool, void *ptr, void (*del)(void *));
2177
+
2178
+
2179
+ /* Relegate an allocated region to a memory pool object.
2180
+ `mpool' specifies the memory pool object.
2181
+ `ptr' specifies the pointer to the region to be relegated. If it is `NULL', this function has
2182
+ no effect.
2183
+ The return value is the pointer to the given object.
2184
+ This function assures that the specified region is released when the memory pool object is
2185
+ deleted. */
2186
+ void *tcmpoolpushptr(TCMPOOL *mpool, void *ptr);
2187
+
2188
+
2189
+ /* Relegate an extensible string object to a memory pool object.
2190
+ `mpool' specifies the memory pool object.
2191
+ `xstr' specifies the extensible string object. If it is `NULL', this function has no effect.
2192
+ The return value is the pointer to the given object.
2193
+ This function assures that the specified object is deleted when the memory pool object is
2194
+ deleted. */
2195
+ TCXSTR *tcmpoolpushxstr(TCMPOOL *mpool, TCXSTR *xstr);
2196
+
2197
+
2198
+ /* Relegate a list object to a memory pool object.
2199
+ `mpool' specifies the memory pool object.
2200
+ `list' specifies the list object. If it is `NULL', this function has no effect.
2201
+ The return value is the pointer to the given object.
2202
+ This function assures that the specified object is deleted when the memory pool object is
2203
+ deleted. */
2204
+ TCLIST *tcmpoolpushlist(TCMPOOL *mpool, TCLIST *list);
2205
+
2206
+
2207
+ /* Relegate a map object to a memory pool object.
2208
+ `mpool' specifies the memory pool object.
2209
+ `map' specifies the map object. If it is `NULL', this function has no effect.
2210
+ The return value is the pointer to the given object.
2211
+ This function assures that the specified object is deleted when the memory pool object is
2212
+ deleted. */
2213
+ TCMAP *tcmpoolpushmap(TCMPOOL *mpool, TCMAP *map);
2214
+
2215
+
2216
+ /* Relegate a tree object to a memory pool object.
2217
+ `mpool' specifies the memory pool object.
2218
+ `tree' specifies the tree object. If it is `NULL', this function has no effect.
2219
+ The return value is the pointer to the given object.
2220
+ This function assures that the specified object is deleted when the memory pool object is
2221
+ deleted. */
2222
+ TCTREE *tcmpoolpushtree(TCMPOOL *mpool, TCTREE *tree);
2223
+
2224
+
2225
+ /* Allocate a region relegated to a memory pool object.
2226
+ `mpool' specifies the memory pool object.
2227
+ The return value is the pointer to the allocated region under the memory pool. */
2228
+ void *tcmpoolmalloc(TCMPOOL *mpool, size_t size);
2229
+
2230
+
2231
+ /* Create an extensible string object relegated to a memory pool object.
2232
+ The return value is the new extensible string object under the memory pool. */
2233
+ TCXSTR *tcmpoolxstrnew(TCMPOOL *mpool);
2234
+
2235
+
2236
+ /* Create a list object relegated to a memory pool object.
2237
+ The return value is the new list object under the memory pool. */
2238
+ TCLIST *tcmpoollistnew(TCMPOOL *mpool);
2239
+
2240
+
2241
+ /* Create a map object relegated to a memory pool object.
2242
+ The return value is the new map object under the memory pool. */
2243
+ TCMAP *tcmpoolmapnew(TCMPOOL *mpool);
2244
+
2245
+
2246
+ /* Create a tree object relegated to a memory pool object.
2247
+ The return value is the new tree object under the memory pool. */
2248
+ TCTREE *tcmpooltreenew(TCMPOOL *mpool);
2249
+
2250
+
2251
+ /* Remove the most recently installed cleanup handler of a memory pool object.
2252
+ `mpool' specifies the memory pool object.
2253
+ `exe' specifies whether to execute the destructor of the removed handler. */
2254
+ void tcmpoolpop(TCMPOOL *mpool, bool exe);
2255
+
2256
+
2257
+ /* Remove all cleanup handler of a memory pool object.
2258
+ `mpool' specifies the memory pool object.
2259
+ `exe' specifies whether to execute the destructors of the removed handlers. */
2260
+ void tcmpoolclear(TCMPOOL *mpool, bool exe);
2261
+
2262
+
2263
+ /* Get the global memory pool object.
2264
+ The return value is the global memory pool object.
2265
+ The global memory pool object is a singleton and assured to be deleted when the porcess is
2266
+ terminating normally. */
2267
+ TCMPOOL *tcmpoolglobal(void);
2268
+
2269
+
2270
+
2271
+ /*************************************************************************************************
2272
+ * miscellaneous utilities
2273
+ *************************************************************************************************/
2274
+
2275
+
2276
+ /* Get the larger value of two integers.
2277
+ `a' specifies an integer.
2278
+ `b' specifies the other integer.
2279
+ The return value is the larger value of the two. */
2280
+ long tclmax(long a, long b);
2281
+
2282
+
2283
+ /* Get the lesser value of two integers.
2284
+ `a' specifies an integer.
2285
+ `b' specifies the other integer.
2286
+ The return value is the lesser value of the two. */
2287
+ long tclmin(long a, long b);
2288
+
2289
+
2290
+ /* Get a random number as long integer based on uniform distribution.
2291
+ The return value is the random number between 0 and `ULONG_MAX'.
2292
+ This function uses the random number source device and generates a real random number if
2293
+ possible. */
2294
+ unsigned long tclrand(void);
2295
+
2296
+
2297
+ /* Get a random number as double decimal based on uniform distribution.
2298
+ The return value is the random number equal to or greater than 0, and less than 1.0.
2299
+ This function uses the random number source device and generates a real random number if
2300
+ possible. */
2301
+ double tcdrand(void);
2302
+
2303
+
2304
+ /* Get a random number as double decimal based on normal distribution.
2305
+ `avg' specifies the average.
2306
+ `sd' specifies the standard deviation.
2307
+ The return value is the random number.
2308
+ This function uses the random number source device and generates a real random number if
2309
+ possible. */
2310
+ double tcdrandnd(double avg, double sd);
2311
+
2312
+
2313
+ /* Compare two strings with case insensitive evaluation.
2314
+ `astr' specifies a string.
2315
+ `bstr' specifies of the other string.
2316
+ The return value is positive if the former is big, negative if the latter is big, 0 if both
2317
+ are equivalent. */
2318
+ int tcstricmp(const char *astr, const char *bstr);
2319
+
2320
+
2321
+ /* Check whether a string begins with a key.
2322
+ `str' specifies the target string.
2323
+ `key' specifies the forward matching key string.
2324
+ The return value is true if the target string begins with the key, else, it is false. */
2325
+ bool tcstrfwm(const char *str, const char *key);
2326
+
2327
+
2328
+ /* Check whether a string begins with a key with case insensitive evaluation.
2329
+ `str' specifies the target string.
2330
+ `key' specifies the forward matching key string.
2331
+ The return value is true if the target string begins with the key, else, it is false. */
2332
+ bool tcstrifwm(const char *str, const char *key);
2333
+
2334
+
2335
+ /* Check whether a string ends with a key.
2336
+ `str' specifies the target string.
2337
+ `key' specifies the backward matching key string.
2338
+ The return value is true if the target string ends with the key, else, it is false. */
2339
+ bool tcstrbwm(const char *str, const char *key);
2340
+
2341
+
2342
+ /* Check whether a string ends with a key with case insensitive evaluation.
2343
+ `str' specifies the target string.
2344
+ `key' specifies the backward matching key string.
2345
+ The return value is true if the target string ends with the key, else, it is false. */
2346
+ bool tcstribwm(const char *str, const char *key);
2347
+
2348
+
2349
+ /* Calculate the edit distance of two strings.
2350
+ `astr' specifies a string.
2351
+ `bstr' specifies of the other string.
2352
+ The return value is the edit distance which is known as the Levenshtein distance. The cost is
2353
+ calculated by byte. */
2354
+ int tcstrdist(const char *astr, const char *bstr);
2355
+
2356
+
2357
+ /* Calculate the edit distance of two UTF-8 strings.
2358
+ `astr' specifies a string.
2359
+ `bstr' specifies of the other string.
2360
+ The return value is the edit distance which is known as the Levenshtein distance. The cost is
2361
+ calculated by Unicode character. */
2362
+ int tcstrdistutf(const char *astr, const char *bstr);
2363
+
2364
+
2365
+ /* Convert the letters of a string into upper case.
2366
+ `str' specifies the string to be converted.
2367
+ The return value is the string itself. */
2368
+ char *tcstrtoupper(char *str);
2369
+
2370
+
2371
+ /* Convert the letters of a string into lower case.
2372
+ `str' specifies the string to be converted.
2373
+ The return value is the string itself. */
2374
+ char *tcstrtolower(char *str);
2375
+
2376
+
2377
+ /* Cut space characters at head or tail of a string.
2378
+ `str' specifies the string to be converted.
2379
+ The return value is the string itself. */
2380
+ char *tcstrtrim(char *str);
2381
+
2382
+
2383
+ /* Squeeze space characters in a string and trim it.
2384
+ `str' specifies the string to be converted.
2385
+ The return value is the string itself. */
2386
+ char *tcstrsqzspc(char *str);
2387
+
2388
+
2389
+ /* Substitute characters in a string.
2390
+ `str' specifies the string to be converted.
2391
+ `rstr' specifies the string containing characters to be replaced.
2392
+ `sstr' specifies the string containing characters to be substituted.
2393
+ If the substitute string is shorter then the replacement string, corresponding characters are
2394
+ removed. */
2395
+ char *tcstrsubchr(char *str, const char *rstr, const char *sstr);
2396
+
2397
+
2398
+ /* Count the number of characters in a string of UTF-8.
2399
+ `str' specifies the string of UTF-8.
2400
+ The return value is the number of characters in the string. */
2401
+ int tcstrcntutf(const char *str);
2402
+
2403
+
2404
+ /* Cut a string of UTF-8 at the specified number of characters.
2405
+ `str' specifies the string of UTF-8.
2406
+ `num' specifies the number of characters to be kept.
2407
+ The return value is the string itself. */
2408
+ char *tcstrcututf(char *str, int num);
2409
+
2410
+
2411
+ /* Convert a UTF-8 string into a UCS-2 array.
2412
+ `str' specifies the UTF-8 string.
2413
+ `ary' specifies the pointer to the region into which the result UCS-2 codes are written. The
2414
+ size of the buffer should be sufficient.
2415
+ `np' specifies the pointer to a variable into which the number of elements of the result array
2416
+ is assigned. */
2417
+ void tcstrutftoucs(const char *str, uint16_t *ary, int *np);
2418
+
2419
+
2420
+ /* Convert a UCS-2 array into a UTF-8 string.
2421
+ `ary' specifies the array of UCS-2 codes.
2422
+ `num' specifies the number of the array.
2423
+ `str' specifies the pointer to the region into which the result UTF-8 string is written. The
2424
+ size of the buffer should be sufficient.
2425
+ The return value is the length of the result string. */
2426
+ int tcstrucstoutf(const uint16_t *ary, int num, char *str);
2427
+
2428
+
2429
+ /* Create a list object by splitting a string.
2430
+ `str' specifies the source string.
2431
+ `delim' specifies a string containing delimiting characters.
2432
+ The return value is a list object of the split elements.
2433
+ If two delimiters are successive, it is assumed that an empty element is between the two.
2434
+ Because the object of the return value is created with the function `tclistnew', it should be
2435
+ deleted with the function `tclistdel' when it is no longer in use. */
2436
+ TCLIST *tcstrsplit(const char *str, const char *delims);
2437
+
2438
+
2439
+ /* Create a string by joining all elements of a list object.
2440
+ `list' specifies a list object.
2441
+ `delim' specifies a delimiting character.
2442
+ The return value is the result string.
2443
+ Because the region of the return value is allocated with the `malloc' call, it should be
2444
+ released with the `free' call when it is no longer in use. */
2445
+ char *tcstrjoin(const TCLIST *list, char delim);
2446
+
2447
+
2448
+ /* Convert a string to an integer.
2449
+ `str' specifies the string.
2450
+ The return value is the integer. If the string does not contain numeric expression, 0 is
2451
+ returned.
2452
+ This function is equivalent to `atoll' except that it does not depend on the locale. */
2453
+ int64_t tcatoi(const char *str);
2454
+
2455
+
2456
+ /* Convert a string with a metric prefix to an integer.
2457
+ `str' specifies the string, which can be trailed by a binary metric prefix. "K", "M", "G",
2458
+ "T", "P", and "E" are supported. They are case-insensitive.
2459
+ The return value is the integer. If the string does not contain numeric expression, 0 is
2460
+ returned. If the integer overflows the domain, `INT64_MAX' or `INT64_MIN' is returned
2461
+ according to the sign. */
2462
+ int64_t tcatoix(const char *str);
2463
+
2464
+
2465
+ /* Convert a string to a real number.
2466
+ `str' specifies the string.
2467
+ The return value is the real number. If the string does not contain numeric expression, 0.0
2468
+ is returned.
2469
+ This function is equivalent to `atof' except that it does not depend on the locale. */
2470
+ double tcatof(const char *str);
2471
+
2472
+
2473
+ /* Check whether a string matches a regular expression.
2474
+ `str' specifies the target string.
2475
+ `regex' specifies the regular expression string. If it begins with `*', the trailing
2476
+ substring is used as a case-insensitive regular expression.
2477
+ The return value is true if matching is success, else, it is false. */
2478
+ bool tcregexmatch(const char *str, const char *regex);
2479
+
2480
+
2481
+ /* Replace each substring matching a regular expression string.
2482
+ `str' specifies the target string.
2483
+ `regex' specifies the regular expression string for substrings. If it begins with `*', the
2484
+ trailing substring is used as a case-insensitive regular expression.
2485
+ `alt' specifies the alternative string with which each substrings is replaced. Each `&' in
2486
+ the string is replaced with the matched substring. Each `\' in the string escapes the
2487
+ following character. Special escapes "\1" through "\9" referring to the corresponding
2488
+ matching sub-expressions in the regular expression string are supported.
2489
+ The return value is a new converted string. Even if the regular expression is invalid, a copy
2490
+ of the original string is returned.
2491
+ Because the region of the return value is allocated with the `malloc' call, it should be
2492
+ released with the `free' call when it is no longer in use. */
2493
+ char *tcregexreplace(const char *str, const char *regex, const char *alt);
2494
+
2495
+
2496
+ /* Get the MD5 hash value of a serial object.
2497
+ `ptr' specifies the pointer to the region.
2498
+ `size' specifies the size of the region.
2499
+ `buf' specifies the pointer to the region into which the result string is written. The size
2500
+ of the buffer should be equal to or more than 48 bytes. */
2501
+ void tcmd5hash(const void *ptr, int size, char *buf);
2502
+
2503
+
2504
+ /* Cipher or decipher a serial object with the Arcfour stream cipher.
2505
+ `ptr' specifies the pointer to the region.
2506
+ `size' specifies the size of the region.
2507
+ `kbuf' specifies the pointer to the region of the cipher key.
2508
+ `ksiz' specifies the size of the region of the cipher key.
2509
+ `obuf' specifies the pointer to the region into which the result data is written. The size
2510
+ of the buffer should be equal to or more than the input region. */
2511
+ void tcarccipher(const void *ptr, int size, const void *kbuf, int ksiz, void *obuf);
2512
+
2513
+
2514
+ /* Get the time of day in seconds.
2515
+ The return value is the time of day in seconds. The accuracy is in microseconds. */
2516
+ double tctime(void);
2517
+
2518
+
2519
+ /* Get the Gregorian calendar of a time.
2520
+ `t' specifies the source time in seconds from the epoch. If it is `INT64_MAX', the current
2521
+ time is specified.
2522
+ `jl' specifies the jet lag of a location in seconds. If it is `INT_MAX', the local jet lag is
2523
+ specified.
2524
+ `yearp' specifies the pointer to a variable to which the year is assigned. If it is `NULL',
2525
+ it is not used.
2526
+ `monp' specifies the pointer to a variable to which the month is assigned. If it is `NULL',
2527
+ it is not used. 1 means January and 12 means December.
2528
+ `dayp' specifies the pointer to a variable to which the day of the month is assigned. If it
2529
+ is `NULL', it is not used.
2530
+ `hourp' specifies the pointer to a variable to which the hours is assigned. If it is `NULL',
2531
+ it is not used.
2532
+ `minp' specifies the pointer to a variable to which the minutes is assigned. If it is `NULL',
2533
+ it is not used.
2534
+ `secp' specifies the pointer to a variable to which the seconds is assigned. If it is `NULL',
2535
+ it is not used. */
2536
+ void tccalendar(int64_t t, int jl, int *yearp, int *monp, int *dayp,
2537
+ int *hourp, int *minp, int *secp);
2538
+
2539
+
2540
+ /* Format a date as a string in W3CDTF.
2541
+ `t' specifies the source time in seconds from the epoch. If it is `INT64_MAX', the current
2542
+ time is specified.
2543
+ `jl' specifies the jet lag of a location in seconds. If it is `INT_MAX', the local jet lag is
2544
+ specified.
2545
+ `buf' specifies the pointer to the region into which the result string is written. The size
2546
+ of the buffer should be equal to or more than 48 bytes.
2547
+ W3CDTF represents a date as "YYYY-MM-DDThh:mm:ddTZD". */
2548
+ void tcdatestrwww(int64_t t, int jl, char *buf);
2549
+
2550
+
2551
+ /* Format a date as a string in RFC 1123 format.
2552
+ `t' specifies the source time in seconds from the epoch. If it is `INT64_MAX', the current
2553
+ time is specified.
2554
+ `jl' specifies the jet lag of a location in seconds. If it is `INT_MAX', the local jet lag is
2555
+ specified.
2556
+ `buf' specifies the pointer to the region into which the result string is written. The size
2557
+ of the buffer should be equal to or more than 48 bytes.
2558
+ RFC 1123 format represents a date as "Wdy, DD-Mon-YYYY hh:mm:dd TZD". */
2559
+ void tcdatestrhttp(int64_t t, int jl, char *buf);
2560
+
2561
+
2562
+ /* Get the time value of a date string.
2563
+ `str' specifies the date string in decimal, hexadecimal, W3CDTF, or RFC 822 (1123). Decimal
2564
+ can be trailed by "s" for in seconds, "m" for in minutes, "h" for in hours, and "d" for in
2565
+ days.
2566
+ The return value is the time value of the date or `INT64_MIN' if the format is invalid. */
2567
+ int64_t tcstrmktime(const char *str);
2568
+
2569
+
2570
+ /* Get the jet lag of the local time.
2571
+ The return value is the jet lag of the local time in seconds. */
2572
+ int tcjetlag(void);
2573
+
2574
+
2575
+ /* Get the day of week of a date.
2576
+ `year' specifies the year of a date.
2577
+ `mon' specifies the month of the date.
2578
+ `day' specifies the day of the date.
2579
+ The return value is the day of week of the date. 0 means Sunday and 6 means Saturday. */
2580
+ int tcdayofweek(int year, int mon, int day);
2581
+
2582
+
2583
+
2584
+ /*************************************************************************************************
2585
+ * miscellaneous utilities (for experts)
2586
+ *************************************************************************************************/
2587
+
2588
+
2589
+ enum { /* enumeration for UCS normalization */
2590
+ TCUNSPACE = 1 << 0, /* white space normalization */
2591
+ TCUNLOWER = 1 << 1, /* lower case normalization */
2592
+ TCUNNOACC = 1 << 2, /* strip accent marks */
2593
+ TCUNWIDTH = 1 << 3 /* half-width normalization */
2594
+ };
2595
+
2596
+ enum { /* enumeration for KWIC generator */
2597
+ TCKWMUTAB = 1 << 0, /* mark up by tabs */
2598
+ TCKWMUCTRL = 1 << 1, /* mark up by control characters */
2599
+ TCKWMUBRCT = 1 << 2, /* mark up by brackets */
2600
+ TCKWNOOVER = 1 << 24, /* no overlap */
2601
+ TCKWPULEAD = 1 << 25 /* pick up the lead string */
2602
+ };
2603
+
2604
+ typedef struct { /* type of structure for a consistent hashing node */
2605
+ uint32_t seq; /* sequential number */
2606
+ uint32_t hash; /* hash value */
2607
+ } TCCHIDXNODE;
2608
+
2609
+ typedef struct { /* type of structure for a consistent hashing object */
2610
+ TCCHIDXNODE *nodes; /* node array */
2611
+ int nnum; /* number of the node array */
2612
+ } TCCHIDX;
2613
+
2614
+
2615
+ /* Check whether a string is numeric completely or not.
2616
+ `str' specifies the string to be checked.
2617
+ The return value is true if the string is numeric, else, it is false. */
2618
+ bool tcstrisnum(const char *str);
2619
+
2620
+
2621
+ /* Convert a hexadecimal string to an integer.
2622
+ `str' specifies the string.
2623
+ The return value is the integer. If the string does not contain numeric expression, 0 is
2624
+ returned. */
2625
+ int64_t tcatoih(const char *str);
2626
+
2627
+
2628
+ /* Skip space characters at head of a string.
2629
+ `str' specifies the string.
2630
+ The return value is the pointer to the first non-space character. */
2631
+ const char *tcstrskipspc(const char *str);
2632
+
2633
+
2634
+ /* Normalize a UTF-8 string.
2635
+ `str' specifies the string of UTF-8.
2636
+ `opts' specifies options by bitwise-or: `TCUNSPACE' specifies that white space characters are
2637
+ normalized into the ASCII space and they are squeezed into one, `TCUNLOWER' specifies that
2638
+ alphabetical characters are normalized into lower cases, `TCUNNOACC' specifies that
2639
+ alphabetical characters with accent marks are normalized without accent marks, `TCUNWIDTH'
2640
+ specifies that full-width characters are normalized into half-width characters.
2641
+ The return value is the string itself. */
2642
+ char *tcstrutfnorm(char *str, int opts);
2643
+
2644
+
2645
+ /* Normalize a UCS-2 array.
2646
+ `ary' specifies the array of UCS-2 codes.
2647
+ `num' specifies the number of elements of the array.
2648
+ `opts' specifies options by bitwise-or: `TCUNSPACE' specifies that white space characters are
2649
+ normalized into the ASCII space and they are squeezed into one, `TCUNLOWER' specifies that
2650
+ alphabetical characters are normalized into lower cases, `TCUNNOACC' specifies that
2651
+ alphabetical characters with accent marks are normalized without accent marks, `TCUNWIDTH'
2652
+ specifies that full-width characters are normalized into half-width characters.
2653
+ The return value is the number of elements of the result array. */
2654
+ int tcstrucsnorm(uint16_t *ary, int num, int opts);
2655
+
2656
+
2657
+ /* Generate a keyword-in-context string from a text and keywords.
2658
+ `str' specifies the text string of UTF-8.
2659
+ `words' specifies a list object of the keyword strings.
2660
+ `width' specifies the width of strings picked up around each keyword.
2661
+ `opts' specifies options by bitwise-or: `TCKWMUTAB' specifies that each keyword is marked up
2662
+ between two tab characters, `TCKWMUCTRL' specifies that each keyword is marked up by the STX
2663
+ (0x02) code and the ETX (0x03) code, `TCKWMUBRCT' specifies that each keyword is marked up by
2664
+ the two square brackets, `TCKWNOOVER' specifies that each context does not overlap,
2665
+ `TCKWPULEAD' specifies that the lead string is picked up forcibly.
2666
+ The return value is the list object whose elements are strings around keywords.
2667
+ Because the object of the return value is created with the function `tclistnew', it should
2668
+ be deleted with the function `tclistdel' when it is no longer in use. */
2669
+ TCLIST *tcstrkwic(const char *str, const TCLIST *words, int width, int opts);
2670
+
2671
+
2672
+ /* Tokenize a text separating by white space characters.
2673
+ `str' specifies the string.
2674
+ The return value is the list object whose elements are extracted tokens.
2675
+ Because the object of the return value is created with the function `tclistnew', it should
2676
+ be deleted with the function `tclistdel' when it is no longer in use. */
2677
+ TCLIST *tcstrtokenize(const char *str);
2678
+
2679
+
2680
+ /* Create a list object by splitting a region by zero code.
2681
+ `ptr' specifies the pointer to the region.
2682
+ `size' specifies the size of the region.
2683
+ The return value is a list object of the split elements.
2684
+ If two delimiters are successive, it is assumed that an empty element is between the two.
2685
+ Because the object of the return value is created with the function `tclistnew', it should be
2686
+ deleted with the function `tclistdel' when it is no longer in use. */
2687
+ TCLIST *tcstrsplit2(const void *ptr, int size);
2688
+
2689
+
2690
+ /* Create a map object by splitting a string.
2691
+ `str' specifies the source string where the key and the value of each record are situated one
2692
+ after the other.
2693
+ `delim' specifies a string containing delimiting characters.
2694
+ The return value is a map object of the split records.
2695
+ Because the object of the return value is created with the function `tcmapnew', it should be
2696
+ deleted with the function `tcmapdel' when it is no longer in use. */
2697
+ TCMAP *tcstrsplit3(const char *str, const char *delims);
2698
+
2699
+
2700
+ /* Create a map object by splitting a region by zero code.
2701
+ `ptr' specifies the pointer to the region where the key and the value of each record are
2702
+ situated one after the other.
2703
+ `size' specifies the size of the region.
2704
+ The return value is a map object of the split records.
2705
+ Because the object of the return value is created with the function `tcmapnew', it should be
2706
+ deleted with the function `tcmapdel' when it is no longer in use. */
2707
+ TCMAP *tcstrsplit4(const void *ptr, int size);
2708
+
2709
+
2710
+ /* Create a region separated by zero code by joining all elements of a list object.
2711
+ `list' specifies a list object.
2712
+ The return value is the result region.
2713
+ `sp' specifies the pointer to the variable into which the size of the region of the return
2714
+ value is assigned.
2715
+ Because the region of the return value is allocated with the `malloc' call, it should be
2716
+ released with the `free' call when it is no longer in use. */
2717
+ void *tcstrjoin2(const TCLIST *list, int *sp);
2718
+
2719
+
2720
+ /* Create a string by joining all records of a map object.
2721
+ `map' specifies a map object.
2722
+ `delim' specifies a delimiting character.
2723
+ The return value is the result string where the key and the value of each record are situated
2724
+ one after the other.
2725
+ Because the region of the return value is allocated with the `malloc' call, it should be
2726
+ released with the `free' call when it is no longer in use. */
2727
+ char *tcstrjoin3(const TCMAP *map, char delim);
2728
+
2729
+
2730
+ /* Create a region separated by zero code by joining all records of a map object.
2731
+ `list' specifies a list object.
2732
+ The return value is the result region, where the key and the value of each record are
2733
+ situated one after the other.
2734
+ `sp' specifies the pointer to the variable into which the size of the region of the return
2735
+ value is assigned.
2736
+ Because the region of the return value is allocated with the `malloc' call, it should be
2737
+ released with the `free' call when it is no longer in use. */
2738
+ void *tcstrjoin4(const TCMAP *map, int *sp);
2739
+
2740
+
2741
+ /* Sort top records of an array.
2742
+ `base' spacifies the pointer to an array.
2743
+ `nmemb' specifies the number of elements of the array.
2744
+ `size' specifies the size of each element.
2745
+ `top' specifies the number of top records.
2746
+ `compar' specifies the pointer to comparing function. The two arguments specify the pointers
2747
+ of elements. The comparing function should returns positive if the former is big, negative
2748
+ if the latter is big, 0 if both are equal. */
2749
+ void tctopsort(void *base, size_t nmemb, size_t size, size_t top,
2750
+ int(*compar)(const void *, const void *));
2751
+
2752
+
2753
+ /* Suspend execution of the current thread.
2754
+ `sec' specifies the interval of the suspension in seconds.
2755
+ If successful, the return value is true, else, it is false. */
2756
+ bool tcsleep(double sec);
2757
+
2758
+
2759
+ /* Get the current system information.
2760
+ The return value is a map object of the current system information or `NULL' on failure.
2761
+ The key "utime" indicates the user time of the CPU. The key "stime" indicates the system time
2762
+ of the CPU. The key "size" indicates the process size in bytes. The "rss" indicates the
2763
+ resident set size in bytes. "total" indicates the total size of the real memory. "free"
2764
+ indicates the free size of the real memory. "cached" indicates the cached size of the real
2765
+ memory.
2766
+ Because the object of the return value is created with the function `tcmapnew', it should be
2767
+ deleted with the function `tcmapdel' when it is no longer in use. */
2768
+ TCMAP *tcsysinfo(void);
2769
+
2770
+
2771
+ /* Create a consistent hashing object.
2772
+ `range' specifies the number of nodes. It should be more than 0. The range of hash values is
2773
+ from 0 to less than the specified number.
2774
+ The return value is the new consistent hashing object.
2775
+ Consistent hashing is useful because the addition or removal of one node does not
2776
+ significantly change the mapping of keys to nodes. */
2777
+ TCCHIDX *tcchidxnew(int range);
2778
+
2779
+
2780
+ /* Delete a consistent hashing object.
2781
+ `chidx' specifies the consistent hashing object. */
2782
+ void tcchidxdel(TCCHIDX *chidx);
2783
+
2784
+
2785
+ /* Get the consistent hashing value of a record.
2786
+ `chidx' specifies the consistent hashing object.
2787
+ `ptr' specifies the pointer to the region of the record.
2788
+ `size' specifies the size of the region.
2789
+ The return value is the hash value of the record. */
2790
+ int tcchidxhash(TCCHIDX *chidx, const void *ptr, int size);
2791
+
2792
+
2793
+
2794
+ /*************************************************************************************************
2795
+ * filesystem utilities
2796
+ *************************************************************************************************/
2797
+
2798
+
2799
+ /* Get the canonicalized absolute path of a file.
2800
+ `path' specifies the path of the file.
2801
+ The return value is the canonicalized absolute path of a file, or `NULL' if the path is
2802
+ invalid.
2803
+ Because the region of the return value is allocated with the `malloc' call, it should be
2804
+ released with the `free' call when it is no longer in use. */
2805
+ char *tcrealpath(const char *path);
2806
+
2807
+
2808
+ /* Get the status information of a file.
2809
+ `path' specifies the path of the file.
2810
+ `isdirp' specifies the pointer to a variable into which whether the file is a directory is
2811
+ assigned. If it is `NULL', it is ignored.
2812
+ `sizep' specifies the pointer to a variable into which the size of the file is assigned. If
2813
+ it is `NULL', it is ignored.
2814
+ `ntimep' specifies the pointer to a variable into which the size of the file is assigned. If
2815
+ it is `NULL', it is ignored.
2816
+ If successful, the return value is true, else, it is false. */
2817
+ bool tcstatfile(const char *path, bool *isdirp, int64_t *sizep, int64_t *mtimep);
2818
+
2819
+
2820
+ /* Read whole data of a file.
2821
+ `path' specifies the path of the file. If it is `NULL', the standard input is specified.
2822
+ `limit' specifies the limiting size of reading data. If it is not more than 0, the limitation
2823
+ is not specified.
2824
+ `sp' specifies the pointer to the variable into which the size of the region of the return
2825
+ value is assigned. If it is `NULL', it is not used.
2826
+ The return value is the pointer to the allocated region of the read data, or `NULL' if the
2827
+ file could not be opened.
2828
+ Because an additional zero code is appended at the end of the region of the return value, the
2829
+ return value can be treated as a character string. Because the region of the return value is
2830
+ allocated with the `malloc' call, it should be released with the `free' call when when is no
2831
+ longer in use. */
2832
+ void *tcreadfile(const char *path, int limit, int *sp);
2833
+
2834
+
2835
+ /* Read every line of a file.
2836
+ `path' specifies the path of the file. If it is `NULL', the standard input is specified.
2837
+ The return value is a list object of every lines if successful, else it is `NULL'.
2838
+ Line separators are cut out. Because the object of the return value is created with the
2839
+ function `tclistnew', it should be deleted with the function `tclistdel' when it is no longer
2840
+ in use. */
2841
+ TCLIST *tcreadfilelines(const char *path);
2842
+
2843
+
2844
+ /* Write data into a file.
2845
+ `path' specifies the path of the file. If it is `NULL', the standard output is specified.
2846
+ `ptr' specifies the pointer to the data region.
2847
+ `size' specifies the size of the region.
2848
+ If successful, the return value is true, else, it is false. */
2849
+ bool tcwritefile(const char *path, const void *ptr, int size);
2850
+
2851
+
2852
+ /* Copy a file.
2853
+ `src' specifies the path of the source file.
2854
+ `dest' specifies the path of the destination file.
2855
+ The return value is true if successful, else, it is false.
2856
+ If the destination file exists, it is overwritten. */
2857
+ bool tccopyfile(const char *src, const char *dest);
2858
+
2859
+
2860
+ /* Read names of files in a directory.
2861
+ `path' specifies the path of the directory.
2862
+ The return value is a list object of names if successful, else it is `NULL'.
2863
+ Links to the directory itself and to the parent directory are ignored.
2864
+ Because the object of the return value is created with the function `tclistnew', it should
2865
+ be deleted with the function `tclistdel' when it is no longer in use. */
2866
+ TCLIST *tcreaddir(const char *path);
2867
+
2868
+
2869
+ /* Expand a pattern into a list of matched paths.
2870
+ `pattern' specifies the matching pattern.
2871
+ The return value is a list object of matched paths. If no path is matched, an empty list is
2872
+ returned.
2873
+ Because the object of the return value is created with the function `tclistnew', it should
2874
+ be deleted with the function `tclistdel' when it is no longer in use. */
2875
+ TCLIST *tcglobpat(const char *pattern);
2876
+
2877
+
2878
+ /* Remove a file or a directory and its sub ones recursively.
2879
+ `path' specifies the path of the link.
2880
+ If successful, the return value is true, else, it is false. False is returned when the link
2881
+ does not exist or the permission is denied. */
2882
+ bool tcremovelink(const char *path);
2883
+
2884
+
2885
+ /* Write data into a file.
2886
+ `fd' specifies the file descriptor.
2887
+ `buf' specifies the buffer to be written.
2888
+ `size' specifies the size of the buffer.
2889
+ The return value is true if successful, else, it is false. */
2890
+ bool tcwrite(int fd, const void *buf, size_t size);
2891
+
2892
+
2893
+ /* Read data from a file.
2894
+ `fd' specifies the file descriptor.
2895
+ `buf' specifies the buffer to store into.
2896
+ `size' specifies the size of the buffer.
2897
+ The return value is true if successful, else, it is false. */
2898
+ bool tcread(int fd, void *buf, size_t size);
2899
+
2900
+
2901
+ /* Lock a file.
2902
+ `fd' specifies the file descriptor.
2903
+ `ex' specifies whether an exclusive lock or a shared lock is performed.
2904
+ `nb' specifies whether to request with non-blocking.
2905
+ The return value is true if successful, else, it is false. */
2906
+ bool tclock(int fd, bool ex, bool nb);
2907
+
2908
+
2909
+ /* Unlock a file.
2910
+ `fd' specifies the file descriptor.
2911
+ The return value is true if successful, else, it is false. */
2912
+ bool tcunlock(int fd);
2913
+
2914
+
2915
+ /* Execute a shell command.
2916
+ `args' specifies an array of the command name and its arguments.
2917
+ `anum' specifies the number of elements of the array.
2918
+ The return value is the exit code of the command or `INT_MAX' on failure.
2919
+ The command name and the arguments are quoted and meta characters are escaped. */
2920
+ int tcsystem(const char **args, int anum);
2921
+
2922
+
2923
+
2924
+ /*************************************************************************************************
2925
+ * encoding utilities
2926
+ *************************************************************************************************/
2927
+
2928
+
2929
+ /* Encode a serial object with URL encoding.
2930
+ `ptr' specifies the pointer to the region.
2931
+ `size' specifies the size of the region.
2932
+ The return value is the result string.
2933
+ Because the region of the return value is allocated with the `malloc' call, it should be
2934
+ released with the `free' call if when is no longer in use. */
2935
+ char *tcurlencode(const char *ptr, int size);
2936
+
2937
+
2938
+ /* Decode a string encoded with URL encoding.
2939
+ `str' specifies the encoded string.
2940
+ `sp' specifies the pointer to a variable into which the size of the region of the return
2941
+ value is assigned.
2942
+ The return value is the pointer to the region of the result.
2943
+ Because an additional zero code is appended at the end of the region of the return value,
2944
+ the return value can be treated as a character string. Because the region of the return
2945
+ value is allocated with the `malloc' call, it should be released with the `free' call when
2946
+ it is no longer in use. */
2947
+ char *tcurldecode(const char *str, int *sp);
2948
+
2949
+
2950
+ /* Break up a URL into elements.
2951
+ `str' specifies the URL string.
2952
+ The return value is the map object whose keys are the name of elements. The key "self"
2953
+ specifies the URL itself. The key "scheme" indicates the scheme. The key "host" indicates
2954
+ the host of the server. The key "port" indicates the port number of the server. The key
2955
+ "authority" indicates the authority information. The key "path" indicates the path of the
2956
+ resource. The key "file" indicates the file name without the directory section. The key
2957
+ "query" indicates the query string. The key "fragment" indicates the fragment string.
2958
+ Supported schema are HTTP, HTTPS, FTP, and FILE. Absolute URL and relative URL are supported.
2959
+ Because the object of the return value is created with the function `tcmapnew', it should be
2960
+ deleted with the function `tcmapdel' when it is no longer in use. */
2961
+ TCMAP *tcurlbreak(const char *str);
2962
+
2963
+
2964
+ /* Resolve a relative URL with an absolute URL.
2965
+ `base' specifies the absolute URL of the base location.
2966
+ `target' specifies the URL to be resolved.
2967
+ The return value is the resolved URL. If the target URL is relative, a new URL of relative
2968
+ location from the base location is returned. Else, a copy of the target URL is returned.
2969
+ Because the region of the return value is allocated with the `malloc' call, it should be
2970
+ released with the `free' call when it is no longer in use. */
2971
+ char *tcurlresolve(const char *base, const char *target);
2972
+
2973
+
2974
+ /* Encode a serial object with Base64 encoding.
2975
+ `ptr' specifies the pointer to the region.
2976
+ `size' specifies the size of the region.
2977
+ The return value is the result string.
2978
+ Because the region of the return value is allocated with the `malloc' call, it should be
2979
+ released with the `free' call if when is no longer in use. */
2980
+ char *tcbaseencode(const char *ptr, int size);
2981
+
2982
+
2983
+ /* Decode a string encoded with Base64 encoding.
2984
+ `str' specifies the encoded string.
2985
+ `sp' specifies the pointer to a variable into which the size of the region of the return
2986
+ value is assigned.
2987
+ The return value is the pointer to the region of the result.
2988
+ Because an additional zero code is appended at the end of the region of the return value,
2989
+ the return value can be treated as a character string. Because the region of the return
2990
+ value is allocated with the `malloc' call, it should be released with the `free' call when
2991
+ it is no longer in use. */
2992
+ char *tcbasedecode(const char *str, int *sp);
2993
+
2994
+
2995
+ /* Encode a serial object with Quoted-printable encoding.
2996
+ `ptr' specifies the pointer to the region.
2997
+ `size' specifies the size of the region.
2998
+ The return value is the result string.
2999
+ Because the region of the return value is allocated with the `malloc' call, it should be
3000
+ released with the `free' call if when is no longer in use. */
3001
+ char *tcquoteencode(const char *ptr, int size);
3002
+
3003
+
3004
+ /* Decode a string encoded with Quoted-printable encoding.
3005
+ `str' specifies the encoded string.
3006
+ `sp' specifies the pointer to a variable into which the size of the region of the return
3007
+ value is assigned.
3008
+ The return value is the pointer to the region of the result.
3009
+ Because an additional zero code is appended at the end of the region of the return value,
3010
+ the return value can be treated as a character string. Because the region of the return
3011
+ value is allocated with the `malloc' call, it should be released with the `free' call when
3012
+ it is no longer in use. */
3013
+ char *tcquotedecode(const char *str, int *sp);
3014
+
3015
+
3016
+ /* Encode a string with MIME encoding.
3017
+ `str' specifies the string.
3018
+ `encname' specifies the string of the name of the character encoding.
3019
+ `base' specifies whether to use Base64 encoding. If it is false, Quoted-printable is used.
3020
+ The return value is the result string.
3021
+ Because the region of the return value is allocated with the `malloc' call, it should be
3022
+ released with the `free' call when it is no longer in use. */
3023
+ char *tcmimeencode(const char *str, const char *encname, bool base);
3024
+
3025
+
3026
+ /* Decode a string encoded with MIME encoding.
3027
+ `str' specifies the encoded string.
3028
+ `enp' specifies the pointer to the region into which the name of encoding is written. If it
3029
+ is `NULL', it is not used. The size of the buffer should be equal to or more than 32 bytes.
3030
+ The return value is the result string.
3031
+ Because the region of the return value is allocated with the `malloc' call, it should be
3032
+ released with the `free' call when it is no longer in use. */
3033
+ char *tcmimedecode(const char *str, char *enp);
3034
+
3035
+
3036
+ /* Split a string of MIME into headers and the body.
3037
+ `ptr' specifies the pointer to the region of MIME data.
3038
+ `size' specifies the size of the region.
3039
+ `headers' specifies a map object to store headers. If it is `NULL', it is not used. Each key
3040
+ of the map is an uncapitalized header name.
3041
+ `sp' specifies the pointer to the variable into which the size of the region of the return
3042
+ value is assigned.
3043
+ The return value is the pointer to the region of the body data.
3044
+ If the content type is defined, the header map has the key "TYPE" specifying the type. If the
3045
+ character encoding is defined, the key "CHARSET" indicates the encoding name. If the boundary
3046
+ string of multipart is defined, the key "BOUNDARY" indicates the string. If the content
3047
+ disposition is defined, the key "DISPOSITION" indicates the direction. If the file name is
3048
+ defined, the key "FILENAME" indicates the name. If the attribute name is defined, the key
3049
+ "NAME" indicates the name. Because the region of the return value is allocated with the
3050
+ `malloc' call, it should be released with the `free' call when it is no longer in use. */
3051
+ char *tcmimebreak(const char *ptr, int size, TCMAP *headers, int *sp);
3052
+
3053
+
3054
+ /* Split multipart data of MIME into its parts.
3055
+ `ptr' specifies the pointer to the region of multipart data of MIME.
3056
+ `size' specifies the size of the region.
3057
+ `boundary' specifies the boundary string.
3058
+ The return value is a list object. Each element of the list is the data of a part.
3059
+ Because the object of the return value is created with the function `tclistnew', it should be
3060
+ deleted with the function `tclistdel' when it is no longer in use. */
3061
+ TCLIST *tcmimeparts(const char *ptr, int size, const char *boundary);
3062
+
3063
+
3064
+ /* Encode a serial object with hexadecimal encoding.
3065
+ `ptr' specifies the pointer to the region.
3066
+ `size' specifies the size of the region.
3067
+ The return value is the result string.
3068
+ Because the region of the return value is allocated with the `malloc' call, it should be
3069
+ released with the `free' call if when is no longer in use. */
3070
+ char *tchexencode(const char *ptr, int size);
3071
+
3072
+
3073
+ /* Decode a string encoded with hexadecimal encoding.
3074
+ `str' specifies the encoded string.
3075
+ `sp' specifies the pointer to a variable into which the size of the region of the return
3076
+ value is assigned.
3077
+ The return value is the pointer to the region of the result.
3078
+ Because an additional zero code is appended at the end of the region of the return value,
3079
+ the return value can be treated as a character string. Because the region of the return
3080
+ value is allocated with the `malloc' call, it should be released with the `free' call when
3081
+ it is no longer in use. */
3082
+ char *tchexdecode(const char *str, int *sp);
3083
+
3084
+
3085
+ /* Compress a serial object with Packbits encoding.
3086
+ `ptr' specifies the pointer to the region.
3087
+ `size' specifies the size of the region.
3088
+ `sp' specifies the pointer to the variable into which the size of the region of the return
3089
+ value is assigned.
3090
+ If successful, the return value is the pointer to the result object, else, it is `NULL'.
3091
+ Because the region of the return value is allocated with the `malloc' call, it should be
3092
+ released with the `free' call when it is no longer in use. */
3093
+ char *tcpackencode(const char *ptr, int size, int *sp);
3094
+
3095
+
3096
+ /* Decompress a serial object compressed with Packbits encoding.
3097
+ `ptr' specifies the pointer to the region.
3098
+ `size' specifies the size of the region.
3099
+ `sp' specifies the pointer to a variable into which the size of the region of the return
3100
+ value is assigned.
3101
+ If successful, the return value is the pointer to the result object, else, it is `NULL'.
3102
+ Because an additional zero code is appended at the end of the region of the return value,
3103
+ the return value can be treated as a character string. Because the region of the return
3104
+ value is allocated with the `malloc' call, it should be released with the `free' call when it
3105
+ is no longer in use. */
3106
+ char *tcpackdecode(const char *ptr, int size, int *sp);
3107
+
3108
+
3109
+ /* Compress a serial object with TCBS encoding.
3110
+ `ptr' specifies the pointer to the region.
3111
+ `size' specifies the size of the region.
3112
+ `sp' specifies the pointer to the variable into which the size of the region of the return
3113
+ value is assigned.
3114
+ If successful, the return value is the pointer to the result object, else, it is `NULL'.
3115
+ Because the region of the return value is allocated with the `malloc' call, it should be
3116
+ released with the `free' call when it is no longer in use. */
3117
+ char *tcbsencode(const char *ptr, int size, int *sp);
3118
+
3119
+
3120
+ /* Decompress a serial object compressed with TCBS encoding.
3121
+ `ptr' specifies the pointer to the region.
3122
+ `size' specifies the size of the region.
3123
+ `sp' specifies the pointer to a variable into which the size of the region of the return
3124
+ value is assigned.
3125
+ If successful, the return value is the pointer to the result object, else, it is `NULL'.
3126
+ Because an additional zero code is appended at the end of the region of the return value,
3127
+ the return value can be treated as a character string. Because the region of the return
3128
+ value is allocated with the `malloc' call, it should be released with the `free' call when it
3129
+ is no longer in use. */
3130
+ char *tcbsdecode(const char *ptr, int size, int *sp);
3131
+
3132
+
3133
+ /* Compress a serial object with Deflate encoding.
3134
+ `ptr' specifies the pointer to the region.
3135
+ `size' specifies the size of the region.
3136
+ `sp' specifies the pointer to the variable into which the size of the region of the return
3137
+ value is assigned.
3138
+ If successful, the return value is the pointer to the result object, else, it is `NULL'.
3139
+ Because the region of the return value is allocated with the `malloc' call, it should be
3140
+ released with the `free' call when it is no longer in use. */
3141
+ char *tcdeflate(const char *ptr, int size, int *sp);
3142
+
3143
+
3144
+ /* Decompress a serial object compressed with Deflate encoding.
3145
+ `ptr' specifies the pointer to the region.
3146
+ `size' specifies the size of the region.
3147
+ `sp' specifies the pointer to a variable into which the size of the region of the return
3148
+ value is assigned.
3149
+ If successful, the return value is the pointer to the result object, else, it is `NULL'.
3150
+ Because an additional zero code is appended at the end of the region of the return value,
3151
+ the return value can be treated as a character string. Because the region of the return
3152
+ value is allocated with the `malloc' call, it should be released with the `free' call when it
3153
+ is no longer in use. */
3154
+ char *tcinflate(const char *ptr, int size, int *sp);
3155
+
3156
+
3157
+ /* Compress a serial object with GZIP encoding.
3158
+ `ptr' specifies the pointer to the region.
3159
+ `size' specifies the size of the region.
3160
+ `sp' specifies the pointer to the variable into which the size of the region of the return
3161
+ value is assigned.
3162
+ If successful, the return value is the pointer to the result object, else, it is `NULL'.
3163
+ Because the region of the return value is allocated with the `malloc' call, it should be
3164
+ released with the `free' call when it is no longer in use. */
3165
+ char *tcgzipencode(const char *ptr, int size, int *sp);
3166
+
3167
+
3168
+ /* Decompress a serial object compressed with GZIP encoding.
3169
+ `ptr' specifies the pointer to the region.
3170
+ `size' specifies the size of the region.
3171
+ `sp' specifies the pointer to a variable into which the size of the region of the return
3172
+ value is assigned.
3173
+ If successful, the return value is the pointer to the result object, else, it is `NULL'.
3174
+ Because an additional zero code is appended at the end of the region of the return value,
3175
+ the return value can be treated as a character string. Because the region of the return
3176
+ value is allocated with the `malloc' call, it should be released with the `free' call when it
3177
+ is no longer in use. */
3178
+ char *tcgzipdecode(const char *ptr, int size, int *sp);
3179
+
3180
+
3181
+ /* Get the CRC32 checksum of a serial object.
3182
+ `ptr' specifies the pointer to the region.
3183
+ `size' specifies the size of the region.
3184
+ The return value is the CRC32 checksum of the object. */
3185
+ unsigned int tcgetcrc(const char *ptr, int size);
3186
+
3187
+
3188
+ /* Compress a serial object with BZIP2 encoding.
3189
+ `ptr' specifies the pointer to the region.
3190
+ `size' specifies the size of the region.
3191
+ `sp' specifies the pointer to the variable into which the size of the region of the return
3192
+ value is assigned.
3193
+ If successful, the return value is the pointer to the result object, else, it is `NULL'.
3194
+ Because the region of the return value is allocated with the `malloc' call, it should be
3195
+ released with the `free' call when it is no longer in use. */
3196
+ char *tcbzipencode(const char *ptr, int size, int *sp);
3197
+
3198
+
3199
+ /* Decompress a serial object compressed with BZIP2 encoding.
3200
+ `ptr' specifies the pointer to the region.
3201
+ `size' specifies the size of the region.
3202
+ `sp' specifies the pointer to a variable into which the size of the region of the return
3203
+ value is assigned.
3204
+ If successful, the return value is the pointer to the result object, else, it is `NULL'.
3205
+ Because an additional zero code is appended at the end of the region of the return value,
3206
+ the return value can be treated as a character string. Because the region of the return
3207
+ value is allocated with the `malloc' call, it should be released with the `free' call when it
3208
+ is no longer in use. */
3209
+ char *tcbzipdecode(const char *ptr, int size, int *sp);
3210
+
3211
+
3212
+ /* Encode an array of nonnegative integers with BER encoding.
3213
+ `ary' specifies the pointer to the array of nonnegative integers.
3214
+ `anum' specifies the size of the array.
3215
+ `sp' specifies the pointer to a variable into which the size of the region of the return
3216
+ value is assigned.
3217
+ The return value is the pointer to the region of the result.
3218
+ Because the region of the return value is allocated with the `malloc' call, it should be
3219
+ released with the `free' call if when is no longer in use. */
3220
+ char *tcberencode(const unsigned int *ary, int anum, int *sp);
3221
+
3222
+
3223
+ /* Decode a serial object encoded with BER encoding.
3224
+ `ptr' specifies the pointer to the region.
3225
+ `size' specifies the size of the region.
3226
+ `np' specifies the pointer to a variable into which the number of elements of the return value
3227
+ is assigned.
3228
+ The return value is the pointer to the array of the result.
3229
+ Because the region of the return value is allocated with the `malloc' call, it should be
3230
+ released with the `free' call if when is no longer in use. */
3231
+ unsigned int *tcberdecode(const char *ptr, int size, int *np);
3232
+
3233
+
3234
+ /* Escape meta characters in a string with the entity references of XML.
3235
+ `str' specifies the string.
3236
+ The return value is the pointer to the escaped string.
3237
+ This function escapes only `&', `<', `>', and `"'. Because the region of the return value
3238
+ is allocated with the `malloc' call, it should be released with the `free' call when it is no
3239
+ longer in use. */
3240
+ char *tcxmlescape(const char *str);
3241
+
3242
+
3243
+ /* Unescape entity references in a string of XML.
3244
+ `str' specifies the string.
3245
+ The return value is the unescaped string.
3246
+ This function restores only `&amp;', `&lt;', `&gt;', and `&quot;'. Because the region of the
3247
+ return value is allocated with the `malloc' call, it should be released with the `free' call
3248
+ when it is no longer in use. */
3249
+ char *tcxmlunescape(const char *str);
3250
+
3251
+
3252
+
3253
+ /*************************************************************************************************
3254
+ * encoding utilities (for experts)
3255
+ *************************************************************************************************/
3256
+
3257
+
3258
+ /* Encode a map object into a string in the x-www-form-urlencoded format.
3259
+ `params' specifies a map object of parameters.
3260
+ The return value is the result string.
3261
+ Because the region of the return value is allocated with the `malloc' call, it should be
3262
+ released with the `free' call when it is no longer in use. */
3263
+ char *tcwwwformencode(const TCMAP *params);
3264
+
3265
+
3266
+ /* Decode a query string in the x-www-form-urlencoded format.
3267
+ `str' specifies the query string.
3268
+ `params' specifies a map object into which the result parameters are stored. */
3269
+ void tcwwwformdecode(const char *str, TCMAP *params);
3270
+
3271
+
3272
+ /* Decode a data region in the x-www-form-urlencoded or multipart-form-data format.
3273
+ `ptr' specifies the pointer to the data region.
3274
+ `size' specifies the size of the data region.
3275
+ `type' specifies the value of the content-type header. If it is `NULL', the type is specified
3276
+ as x-www-form-urlencoded.
3277
+ `params' specifies a map object into which the result parameters are stored. */
3278
+ void tcwwwformdecode2(const void *ptr, int size, const char *type, TCMAP *params);
3279
+
3280
+
3281
+ /* Split an XML string into tags and text sections.
3282
+ `str' specifies the string.
3283
+ The return value is the list object whose elements are strings of tags or text sections.
3284
+ Because the object of the return value is created with the function `tclistnew', it should
3285
+ be deleted with the function `tclistdel' when it is no longer in use. Because this function
3286
+ does not check validation, it can handle also HTML and SGML. */
3287
+ TCLIST *tcxmlbreak(const char *str);
3288
+
3289
+
3290
+ /* Get the map of attributes of an XML tag.
3291
+ `str' specifies the pointer to the region of a tag string.
3292
+ The return value is the map object containing attribute names and their values which are
3293
+ unescaped. You can get the name of the tag with the key of an empty string.
3294
+ Because the object of the return value is created with the function `tcmapnew', it should
3295
+ be deleted with the function `tcmapdel' when it is no longer in use. */
3296
+ TCMAP *tcxmlattrs(const char *str);
3297
+
3298
+
3299
+ /* Escape meta characters in a string with backslash escaping of the C language.
3300
+ `str' specifies the string.
3301
+ The return value is the pointer to the escaped string.
3302
+ Because the region of the return value is allocated with the `malloc' call, it should be
3303
+ released with the `free' call if when is no longer in use. */
3304
+ char *tccstrescape(const char *str);
3305
+
3306
+
3307
+ /* Unescape a string escaped by backslash escaping of the C language.
3308
+ `str' specifies the string.
3309
+ The return value is the unescaped string.
3310
+ Because the region of the return value is allocated with the `malloc' call, it should be
3311
+ released with the `free' call if when is no longer in use. */
3312
+ char *tccstrunescape(const char *str);
3313
+
3314
+
3315
+ /* Escape meta characters in a string with backslash escaping of JSON.
3316
+ `str' specifies the string.
3317
+ The return value is the pointer to the escaped string.
3318
+ Because the region of the return value is allocated with the `malloc' call, it should be
3319
+ released with the `free' call if when is no longer in use. */
3320
+ char *tcjsonescape(const char *str);
3321
+
3322
+
3323
+ /* Unescape a string escaped by backslash escaping of JSON.
3324
+ `str' specifies the string.
3325
+ The return value is the unescaped string.
3326
+ Because the region of the return value is allocated with the `malloc' call, it should be
3327
+ released with the `free' call if when is no longer in use. */
3328
+ char *tcjsonunescape(const char *str);
3329
+
3330
+
3331
+
3332
+ /*************************************************************************************************
3333
+ * template serializer
3334
+ *************************************************************************************************/
3335
+
3336
+
3337
+ typedef struct { /* type of structure for a template */
3338
+ TCLIST *elems; /* elements separated by the separators */
3339
+ char *begsep; /* beginning separator */
3340
+ char *endsep; /* ending separator */
3341
+ TCMAP *conf; /* configuration variables */
3342
+ } TCTMPL;
3343
+
3344
+
3345
+ /* Create a template object.
3346
+ The return value is the new template object. */
3347
+ TCTMPL *tctmplnew(void);
3348
+
3349
+
3350
+ /* Delete a template object.
3351
+ `tmpl' specifies the template object. */
3352
+ void tctmpldel(TCTMPL *tmpl);
3353
+
3354
+
3355
+ /* Set the separator strings of a template object.
3356
+ `tmpl' specifies the template object.
3357
+ `begsep' specifies the beginning separator string. By default, it is "[%".
3358
+ `endsep' specifies the ending separator string. By default, it is "%]". */
3359
+ void tctmplsetsep(TCTMPL *tmpl, const char *begsep, const char *endsep);
3360
+
3361
+
3362
+ /* Load a template string into a template object.
3363
+ `tmpl' specifies the template object.
3364
+ `str' specifies the template string. Directives between "[%" and "%]" can be included in the
3365
+ template string. If the variable name is specified in the directive, it is expanded as the
3366
+ value of the variable. "." is used in order to access a record of a hash variable. For
3367
+ example, "[% foo.bar.baz %]" is expanded as the value of the record whose key is "baz" in the
3368
+ hash variable of the record whose key is "bar" in the hash variable whose name is "foo".
3369
+ Moreover, control flow directives are also supported. "[% IF ... %]", "[% ELSE %]", and
3370
+ "[% END %]" are conditional directives. "[% FOREACH ... %]" and "[% END %]" are iterator
3371
+ directives for a list object. "[% SET ... %]" is a session variable setting directive.
3372
+ "[% CONF ... %]" is a configuration directive. If the ending separator of a directive is
3373
+ leaded by "\", the next linefeed character is ignored. Variable expansion directive needs the
3374
+ parameter for the variable name. The optional parameter "DEF" trailed by a string specifies
3375
+ the default value. The optional parameter "ENC" trailed by a string specifies the encoding
3376
+ format. "URL" for the URL escape encoding, "XML" for the XML escape encoding, "CSTR" for
3377
+ C-string escape encoding, and "JSON" for JSON escape encoding are supported. The conditional
3378
+ directive needs the parameter for the variable name. If the variable exists, the block to the
3379
+ correspondent ending directive is evaluated, else, the block is ignored. The optional
3380
+ parameter "EQ" trailed by a string specifies the string full matching test. The optional
3381
+ parameter "INC" trailed by a string specifies the string including matching test. The
3382
+ optional parameter "PRT" indicates the printable test. The optional parameter "RX" trailed by
3383
+ a string specifies the regular expression matching test. The optional parameter "NOT" inverts
3384
+ the logical determination. The iterator directive needs the parameter for the variable name
3385
+ of a list object. The block to the correspondent ending directive is evaluated for each
3386
+ element of the list. The optional parameter specifies the local variable name of each
3387
+ element. The session variable setting directive needs the parameters for the variable name
3388
+ and its value. The configuration directive needs the parameters for the variable name and
3389
+ its value. */
3390
+ void tctmplload(TCTMPL *tmpl, const char *str);
3391
+
3392
+
3393
+ /* Load a template string from a file into a template object.
3394
+ `tmpl' specifies the template object.
3395
+ `path' specifies the input file.
3396
+ If successful, the return value is true, else, it is false. */
3397
+ bool tctmplload2(TCTMPL *tmpl, const char *path);
3398
+
3399
+
3400
+ /* Serialize the template string of a template object.
3401
+ `tmpl' specifies the template object.
3402
+ `vars' specifies the variables to be applied into the template.
3403
+ The return value is the dumped template string.
3404
+ Because the region of the return value is allocated with the `malloc' call, it should be
3405
+ released with the `free' call when it is no longer in use. */
3406
+ char *tctmpldump(TCTMPL *tmpl, const TCMAP *vars);
3407
+
3408
+
3409
+ /* Get the value of a configuration variable of a template object.
3410
+ `tmpl' specifies the template object.
3411
+ `name' specifies the name of the configuration variable.
3412
+ The return value is the string value of the configuration variable or `NULL' if it is not
3413
+ defined. */
3414
+ const char *tctmplconf(TCTMPL *tmpl, const char *name);
3415
+
3416
+
3417
+ /* Store a list object into a list object with the type information.
3418
+ `list' specifies the container list object.
3419
+ `obj' specifies the list object to be stored. */
3420
+ void tclistpushlist(TCLIST *list, const TCLIST *obj);
3421
+
3422
+
3423
+ /* Store a map object into a list object with the type information.
3424
+ `list' specifies the container list object.
3425
+ `obj' specifies the map object to be stored. */
3426
+ void tclistpushmap(TCLIST *list, const TCMAP *obj);
3427
+
3428
+
3429
+ /* Store a list object into a map object with the type information.
3430
+ `map' specifies the container map object.
3431
+ `kstr' specifies the string of the key.
3432
+ `obj' specifies the list object to be stored. */
3433
+ void tcmapputlist(TCMAP *map, const char *kstr, const TCLIST *obj);
3434
+
3435
+
3436
+ /* Store a map object into a map object with the type information.
3437
+ `map' specifies the container map object.
3438
+ `kstr' specifies the string of the key.
3439
+ `obj' specifies the map object to be stored. */
3440
+ void tcmapputmap(TCMAP *map, const char *kstr, const TCMAP *obj);
3441
+
3442
+
3443
+
3444
+ /*************************************************************************************************
3445
+ * pointer list
3446
+ *************************************************************************************************/
3447
+
3448
+
3449
+ typedef struct { /* type of structure for a pointer list */
3450
+ void **array; /* array of pointers */
3451
+ int anum; /* number of the elements of the array */
3452
+ int start; /* start index of used elements */
3453
+ int num; /* number of used elements */
3454
+ } TCPTRLIST;
3455
+
3456
+
3457
+ /* Create a pointer list object.
3458
+ The return value is the new pointer list object. */
3459
+ TCPTRLIST *tcptrlistnew(void);
3460
+
3461
+
3462
+ /* Create a pointer list object with expecting the number of elements.
3463
+ `anum' specifies the number of elements expected to be stored in the list.
3464
+ The return value is the new pointer list object. */
3465
+ TCPTRLIST *tcptrlistnew2(int anum);
3466
+
3467
+
3468
+ /* Copy a pointer list object.
3469
+ `ptrlist' specifies the pointer list object.
3470
+ The return value is the new pointer list object equivalent to the specified object. */
3471
+ TCPTRLIST *tcptrlistdup(const TCPTRLIST *ptrlist);
3472
+
3473
+
3474
+ /* Delete a pointer list object.
3475
+ `ptrlist' specifies the pointer list object.
3476
+ Note that the deleted object and its derivatives can not be used anymore. */
3477
+ void tcptrlistdel(TCPTRLIST *ptrlist);
3478
+
3479
+
3480
+ /* Get the number of elements of a pointer list object.
3481
+ `ptrlist' specifies the pointer list object.
3482
+ The return value is the number of elements of the list. */
3483
+ int tcptrlistnum(const TCPTRLIST *ptrlist);
3484
+
3485
+
3486
+ /* Get the pointer to the region of an element of a pointer list object.
3487
+ `ptrlist' specifies the pointer list object.
3488
+ `index' specifies the index of the element.
3489
+ The return value is the pointer to the region of the value.
3490
+ If `index' is equal to or more than the number of elements, the return value is `NULL'. */
3491
+ void *tcptrlistval(const TCPTRLIST *ptrlist, int index);
3492
+
3493
+
3494
+ /* Add an element at the end of a pointer list object.
3495
+ `ptrlist' specifies the pointer list object.
3496
+ `ptr' specifies the pointer to the region of the new element. */
3497
+ void tcptrlistpush(TCPTRLIST *ptrlist, void *ptr);
3498
+
3499
+
3500
+ /* Remove an element of the end of a pointer list object.
3501
+ `ptrlist' specifies the pointer list object.
3502
+ The return value is the pointer to the region of the removed element.
3503
+ If the list is empty, the return value is `NULL'. */
3504
+ void *tcptrlistpop(TCPTRLIST *ptrlist);
3505
+
3506
+
3507
+ /* Add an element at the top of a pointer list object.
3508
+ `ptrlist' specifies the pointer list object.
3509
+ `ptr' specifies the pointer to the region of the new element. */
3510
+ void tcptrlistunshift(TCPTRLIST *ptrlist, void *ptr);
3511
+
3512
+
3513
+ /* Remove an element of the top of a pointer list object.
3514
+ `ptrlist' specifies the pointer list object.
3515
+ The return value is the pointer to the region of the removed element.
3516
+ If the list is empty, the return value is `NULL'. */
3517
+ void *tcptrlistshift(TCPTRLIST *ptrlist);
3518
+
3519
+
3520
+ /* Add an element at the specified location of a pointer list object.
3521
+ `ptrlist' specifies the pointer list object.
3522
+ `index' specifies the index of the new element.
3523
+ `ptr' specifies the pointer to the region of the new element.
3524
+ If `index' is equal to or more than the number of elements, this function has no effect. */
3525
+ void tcptrlistinsert(TCPTRLIST *ptrlist, int index, void *ptr);
3526
+
3527
+
3528
+ /* Remove an element at the specified location of a pointer list object.
3529
+ `ptrlist' specifies the pointer list object.
3530
+ `index' specifies the index of the element to be removed.
3531
+ The return value is the pointer to the region of the removed element.
3532
+ If `index' is equal to or more than the number of elements, no element is removed and the
3533
+ return value is `NULL'. */
3534
+ void *tcptrlistremove(TCPTRLIST *ptrlist, int index);
3535
+
3536
+
3537
+ /* Overwrite an element at the specified location of a pointer list object.
3538
+ `ptrlist' specifies the pointer list object.
3539
+ `index' specifies the index of the element to be overwritten.
3540
+ `ptr' specifies the pointer to the region of the new content.
3541
+ If `index' is equal to or more than the number of elements, this function has no effect. */
3542
+ void tcptrlistover(TCPTRLIST *ptrlist, int index, void *ptr);
3543
+
3544
+
3545
+ /* Clear a pointer list object.
3546
+ `ptrlist' specifies the pointer list object.
3547
+ All elements are removed. */
3548
+ void tcptrlistclear(TCPTRLIST *ptrlist);
3549
+
3550
+
3551
+
3552
+ /*************************************************************************************************
3553
+ * bit operation utilities
3554
+ *************************************************************************************************/
3555
+
3556
+
3557
+ typedef struct { /* type of structure for a bit stream object */
3558
+ uint8_t *sp; /* start pointer */
3559
+ uint8_t *cp; /* current pointer */
3560
+ int idx; /* bit index */
3561
+ int size; /* size of used region */
3562
+ } TCBITSTRM;
3563
+
3564
+ typedef unsigned char TCBITMAP; /* type of a bit map object */
3565
+
3566
+
3567
+ /* Create a bitmap object. */
3568
+ #define TCBITMAPNEW(TC_num) \
3569
+ tccalloc(((TC_num) >> 3) + 1, 1);
3570
+
3571
+
3572
+ /* Delete a bitmap object */
3573
+ #define TCBITMAPDEL(TC_bitmap) \
3574
+ do { \
3575
+ tcfree((TC_bitmap)); \
3576
+ } while(false);
3577
+
3578
+
3579
+ /* Turn on a field of a bitmap object. */
3580
+ #define TCBITMAPON(TC_bitmap, TC_idx) \
3581
+ do { \
3582
+ (TC_bitmap)[(TC_idx)>>3] |= 0x1 << ((TC_idx) & 0x7); \
3583
+ } while(false);
3584
+
3585
+
3586
+ /* Turn off a field of a bitmap object. */
3587
+ #define TCBITMAPOFF(TC_bitmap, TC_idx) \
3588
+ do { \
3589
+ (TC_bitmap)[(TC_idx)>>3] &= ~(0x1 << ((TC_idx) & 0x7)); \
3590
+ } while(false);
3591
+
3592
+
3593
+ /* Check a field of a bitmap object. */
3594
+ #define TCBITMAPCHECK(TC_bitmap, TC_idx) \
3595
+ ((TC_bitmap)[(TC_idx)>>3] & 0x1 << ((TC_idx) & 0x7))
3596
+
3597
+
3598
+ /* Initialize a bit stream object as writer. */
3599
+ #define TCBITSTRMINITW(TC_bitstrm, TC_ptr) \
3600
+ do { \
3601
+ (TC_bitstrm).sp = (uint8_t *)(TC_ptr); \
3602
+ (TC_bitstrm).cp = (TC_bitstrm).sp; \
3603
+ *(TC_bitstrm).cp = 0; \
3604
+ (TC_bitstrm).idx = 3; \
3605
+ (TC_bitstrm).size = 1; \
3606
+ } while(false);
3607
+
3608
+
3609
+ /* Concatenate a bit to a bit stream object. */
3610
+ #define TCBITSTRMCAT(TC_bitstrm, sign) \
3611
+ do { \
3612
+ if((TC_bitstrm).idx >= 8){ \
3613
+ *(++(TC_bitstrm).cp) = 0; \
3614
+ (TC_bitstrm).idx = 0; \
3615
+ (TC_bitstrm).size++; \
3616
+ } \
3617
+ *(TC_bitstrm).cp |= (sign << (TC_bitstrm).idx); \
3618
+ (TC_bitstrm).idx++; \
3619
+ } while(false);
3620
+
3621
+
3622
+ /* Set the end mark to a bit stream object. */
3623
+ #define TCBITSTRMSETEND(TC_bitstrm) \
3624
+ do { \
3625
+ if((TC_bitstrm).idx >= 8){ \
3626
+ *(++(TC_bitstrm).cp) = 0; \
3627
+ (TC_bitstrm).idx = 0; \
3628
+ (TC_bitstrm).size++; \
3629
+ } \
3630
+ *(TC_bitstrm).sp |= (TC_bitstrm).idx & 7; \
3631
+ } while(false);
3632
+
3633
+
3634
+ /* Get the size of the used region of a bit stream object. */
3635
+ #define TCBITSTRMSIZE(TC_bitstrm) \
3636
+ ((TC_bitstrm).size)
3637
+
3638
+
3639
+ /* Initialize a bit stream object as reader. */
3640
+ #define TCBITSTRMINITR(TC_bitstrm, TC_ptr, TC_size) \
3641
+ do { \
3642
+ (TC_bitstrm).sp = (uint8_t *)(TC_ptr); \
3643
+ (TC_bitstrm).cp = (TC_bitstrm).sp; \
3644
+ (TC_bitstrm).idx = 3; \
3645
+ (TC_bitstrm).size = (TC_size); \
3646
+ } while(false);
3647
+
3648
+
3649
+ /* Read a bit from a bit stream object. */
3650
+ #define TCBITSTRMREAD(TC_bitstrm, TC_sign) \
3651
+ do { \
3652
+ if((TC_bitstrm).idx >= 8){ \
3653
+ (TC_bitstrm).cp++; \
3654
+ (TC_bitstrm).idx = 0; \
3655
+ } \
3656
+ (TC_sign) = (*((TC_bitstrm).cp) & (1 << (TC_bitstrm).idx)) > 0; \
3657
+ (TC_bitstrm).idx++; \
3658
+ } while(false);
3659
+
3660
+
3661
+ /* Get the number of bits of a bit stream object. */
3662
+ #define TCBITSTRMNUM(TC_bitstrm) \
3663
+ ((((TC_bitstrm).size - 1) << 3) + (*(TC_bitstrm).sp & 7) - 3)
3664
+
3665
+
3666
+
3667
+ /*************************************************************************************************
3668
+ * features for experts
3669
+ *************************************************************************************************/
3670
+
3671
+
3672
+ #include <stdio.h>
3673
+
3674
+ #define _TC_VERSION "1.4.41"
3675
+ #define _TC_LIBVER 904
3676
+ #define _TC_FORMATVER "1.0"
3677
+
3678
+ enum { /* enumeration for error codes */
3679
+ TCESUCCESS, /* success */
3680
+ TCETHREAD, /* threading error */
3681
+ TCEINVALID, /* invalid operation */
3682
+ TCENOFILE, /* file not found */
3683
+ TCENOPERM, /* no permission */
3684
+ TCEMETA, /* invalid meta data */
3685
+ TCERHEAD, /* invalid record header */
3686
+ TCEOPEN, /* open error */
3687
+ TCECLOSE, /* close error */
3688
+ TCETRUNC, /* trunc error */
3689
+ TCESYNC, /* sync error */
3690
+ TCESTAT, /* stat error */
3691
+ TCESEEK, /* seek error */
3692
+ TCEREAD, /* read error */
3693
+ TCEWRITE, /* write error */
3694
+ TCEMMAP, /* mmap error */
3695
+ TCELOCK, /* lock error */
3696
+ TCEUNLINK, /* unlink error */
3697
+ TCERENAME, /* rename error */
3698
+ TCEMKDIR, /* mkdir error */
3699
+ TCERMDIR, /* rmdir error */
3700
+ TCEKEEP, /* existing record */
3701
+ TCENOREC, /* no record found */
3702
+ TCEMISC = 9999 /* miscellaneous error */
3703
+ };
3704
+
3705
+ enum { /* enumeration for database type */
3706
+ TCDBTHASH, /* hash table */
3707
+ TCDBTBTREE, /* B+ tree */
3708
+ TCDBTFIXED, /* fixed-length */
3709
+ TCDBTTABLE /* table */
3710
+ };
3711
+
3712
+
3713
+ /* Get the message string corresponding to an error code.
3714
+ `ecode' specifies the error code.
3715
+ The return value is the message string of the error code. */
3716
+ const char *tcerrmsg(int ecode);
3717
+
3718
+
3719
+ /* Show error message on the standard error output and exit.
3720
+ `message' specifies an error message.
3721
+ This function does not return. */
3722
+ void *tcmyfatal(const char *message);
3723
+
3724
+
3725
+ /* Allocate a large nullified region.
3726
+ `size' specifies the size of the region.
3727
+ The return value is the pointer to the allocated nullified region.
3728
+ This function handles failure of memory allocation implicitly. The region of the return value
3729
+ should be released with the function `tczerounmap' when it is no longer in use. */
3730
+ void *tczeromap(uint64_t size);
3731
+
3732
+
3733
+ /* Free a large nullfied region.
3734
+ `ptr' specifies the pointer to the region. */
3735
+ void tczerounmap(void *ptr);
3736
+
3737
+
3738
+ /* Lock the global mutex object.
3739
+ If successful, the return value is true, else, it is false. */
3740
+ bool tcglobalmutexlock(void);
3741
+
3742
+
3743
+ /* Lock the global mutex object by shared locking.
3744
+ If successful, the return value is true, else, it is false. */
3745
+ bool tcglobalmutexlockshared(void);
3746
+
3747
+
3748
+ /* Unlock the global mutex object.
3749
+ If successful, the return value is true, else, it is false. */
3750
+ bool tcglobalmutexunlock(void);
3751
+
3752
+
3753
+ /* Lock the absolute path of a file.
3754
+ `path' specifies the path of the file.
3755
+ If successful, the return value is true, else, it is false. */
3756
+ bool tcpathlock(const char *path);
3757
+
3758
+
3759
+ /* Unock the absolute path of a file.
3760
+ `path' specifies the path of the file.
3761
+ If successful, the return value is true, else, it is false. */
3762
+ bool tcpathunlock(const char *path);
3763
+
3764
+
3765
+ /* Convert an integer to the string as binary numbers.
3766
+ `num' specifies the integer.
3767
+ `buf' specifies the pointer to the region into which the result string is written. The size
3768
+ of the buffer should be equal to or more than 65 bytes.
3769
+ `col' specifies the number of columns. If it is not more than 0, it depends on the integer.
3770
+ `fc' specifies the filling character.
3771
+ The return value is the length of the result string. */
3772
+ int tcnumtostrbin(uint64_t num, char *buf, int col, int fc);
3773
+
3774
+
3775
+ /* Compare two keys by lexical order.
3776
+ `aptr' specifies the pointer to the region of one key.
3777
+ `asiz' specifies the size of the region of one key.
3778
+ `bptr' specifies the pointer to the region of the other key.
3779
+ `bsiz' specifies the size of the region of the other key.
3780
+ `op' is ignored.
3781
+ The return value is positive if the former is big, negative if the latter is big, 0 if both
3782
+ are equivalent. */
3783
+ int tccmplexical(const char *aptr, int asiz, const char *bptr, int bsiz, void *op);
3784
+
3785
+
3786
+ /* Compare two keys as decimal strings of real numbers.
3787
+ `aptr' specifies the pointer to the region of one key.
3788
+ `asiz' specifies the size of the region of one key.
3789
+ `bptr' specifies the pointer to the region of the other key.
3790
+ `bsiz' specifies the size of the region of the other key.
3791
+ `op' is ignored.
3792
+ The return value is positive if the former is big, negative if the latter is big, 0 if both
3793
+ are equivalent. */
3794
+ int tccmpdecimal(const char *aptr, int asiz, const char *bptr, int bsiz, void *op);
3795
+
3796
+
3797
+ /* Compare two keys as 32-bit integers in the native byte order.
3798
+ `aptr' specifies the pointer to the region of one key.
3799
+ `asiz' specifies the size of the region of one key.
3800
+ `bptr' specifies the pointer to the region of the other key.
3801
+ `bsiz' specifies the size of the region of the other key.
3802
+ `op' is ignored.
3803
+ The return value is positive if the former is big, negative if the latter is big, 0 if both
3804
+ are equivalent. */
3805
+ int tccmpint32(const char *aptr, int asiz, const char *bptr, int bsiz, void *op);
3806
+
3807
+
3808
+ /* Compare two keys as 64-bit integers in the native byte order.
3809
+ `aptr' specifies the pointer to the region of one key.
3810
+ `asiz' specifies the size of the region of one key.
3811
+ `bptr' specifies the pointer to the region of the other key.
3812
+ `bsiz' specifies the size of the region of the other key.
3813
+ `op' is ignored.
3814
+ The return value is positive if the former is big, negative if the latter is big, 0 if both
3815
+ are equivalent. */
3816
+ int tccmpint64(const char *aptr, int asiz, const char *bptr, int bsiz, void *op);
3817
+
3818
+
3819
+ /* Encode a serial object with BWT encoding.
3820
+ `ptr' specifies the pointer to the region.
3821
+ `size' specifies the size of the region.
3822
+ `idxp' specifies the pointer to the variable into which the index of the original string in
3823
+ the rotation array is assigned.
3824
+ The return value is the pointer to the result object.
3825
+ Because an additional zero code is appended at the end of the region of the return value,
3826
+ the return value can be treated as a character string. Because the region of the return
3827
+ value is allocated with the `malloc' call, it should be released with the `free' call when it
3828
+ is no longer in use. */
3829
+ char *tcbwtencode(const char *ptr, int size, int *idxp);
3830
+
3831
+
3832
+ /* Decode a serial object encoded with BWT encoding.
3833
+ `ptr' specifies the pointer to the region.
3834
+ `size' specifies the size of the region.
3835
+ `idx' specifies the index of the original string in the rotation array is assigned.
3836
+ The return value is the pointer to the result object.
3837
+ Because an additional zero code is appended at the end of the region of the return value,
3838
+ the return value can be treated as a character string. Because the region of the return
3839
+ value is allocated with the `malloc' call, it should be released with the `free' call when it
3840
+ is no longer in use. */
3841
+ char *tcbwtdecode(const char *ptr, int size, int idx);
3842
+
3843
+
3844
+ /* Get the binary logarithm of an integer.
3845
+ `num' specifies an integer.
3846
+ The return value is the binary logarithm. */
3847
+ long tclog2l(long num);
3848
+
3849
+
3850
+ /* Get the binary logarithm of a real number.
3851
+ `num' specifies a real number.
3852
+ The return value is the binary logarithm. */
3853
+ double tclog2d(double num);
3854
+
3855
+
3856
+ /* Get the aligned offset of a file offset.
3857
+ `off' specifies the file offset.
3858
+ The return value is the aligned offset. */
3859
+ uint64_t tcpagealign(uint64_t off);
3860
+
3861
+
3862
+ /* Print debug information with a formatted string as with `printf'. */
3863
+ #if __STDC_VERSION__ >= 199901L
3864
+ #define TCDPRINTF(...) \
3865
+ do { \
3866
+ fprintf(stderr, "%s:%d:%s: ", __FILE__, __LINE__, __func__); \
3867
+ fprintf(stderr, __VA_ARGS__); \
3868
+ fprintf(stderr, "\n"); \
3869
+ } while(false);
3870
+ #else
3871
+ #define TCDPRINTF(TC_str) \
3872
+ do { \
3873
+ fprintf(stderr, "%s:%d:%s: %s\n", __FILE__, __LINE__, __func__, TC_str); \
3874
+ } while(false);
3875
+ #endif
3876
+
3877
+
3878
+ /* Print hexadecimal pattern of a binary region. */
3879
+ #define TCPRINTHEX(TC_ptr, TC_size) \
3880
+ do { \
3881
+ for(int TC_i = 0; TC_i < (TC_size); TC_i++){ \
3882
+ if(TC_i > 0) putchar(' '); \
3883
+ printf("%02X", ((unsigned char *)(TC_ptr))[TC_i]); \
3884
+ } \
3885
+ putchar('\n'); \
3886
+ } while(false);
3887
+
3888
+
3889
+ /* Print an extensible string object. */
3890
+ #define TCPRINTXSTR(TC_xstr) \
3891
+ do { \
3892
+ fwrite(tcxstrptr((TC_xstr)), tcxstrsize((TC_xstr)), 1, stdout); \
3893
+ putchar('\n'); \
3894
+ } while(false);
3895
+
3896
+
3897
+ /* Print all elements of a list object. */
3898
+ #define TCPRINTLIST(TC_list) \
3899
+ do { \
3900
+ for(int TC_i = 0; TC_i < tclistnum((TC_list)); TC_i++){ \
3901
+ int TC_size; \
3902
+ const char *TC_ptr = tclistval((TC_list), TC_i, &TC_size); \
3903
+ printf("%p\t", (void *)(TC_list)); \
3904
+ fwrite(TC_ptr, TC_size, 1, stdout); \
3905
+ putchar('\n'); \
3906
+ } \
3907
+ putchar('\n'); \
3908
+ } while(false);
3909
+
3910
+
3911
+ /* Print all records of a list object. */
3912
+ #define TCPRINTMAP(TC_map) \
3913
+ do { \
3914
+ TCLIST *TC_keys = tcmapkeys((TC_map)); \
3915
+ for(int TC_i = 0; TC_i < tclistnum(TC_keys); TC_i++){ \
3916
+ int TC_ksiz; \
3917
+ const char *TC_kbuf = tclistval(TC_keys, TC_i, &TC_ksiz); \
3918
+ int TC_vsiz; \
3919
+ const char *TC_vbuf = tcmapget((TC_map), TC_kbuf, TC_ksiz, &TC_vsiz); \
3920
+ printf("%p\t", (void *)(TC_map)); \
3921
+ fwrite(TC_kbuf, TC_ksiz, 1, stdout); \
3922
+ putchar('\t'); \
3923
+ fwrite(TC_vbuf, TC_vsiz, 1, stdout); \
3924
+ putchar('\n'); \
3925
+ } \
3926
+ putchar('\n'); \
3927
+ tclistdel(TC_keys); \
3928
+ } while(false);
3929
+
3930
+
3931
+ /* Alias of `tcmalloc'. */
3932
+ #if defined(_MYFASTEST)
3933
+ #define TCMALLOC(TC_res, TC_size) \
3934
+ do { \
3935
+ (TC_res) = MYMALLOC(TC_size); \
3936
+ } while(false)
3937
+ #else
3938
+ #define TCMALLOC(TC_res, TC_size) \
3939
+ do { \
3940
+ if(!((TC_res) = MYMALLOC(TC_size))) tcmyfatal("out of memory"); \
3941
+ } while(false)
3942
+ #endif
3943
+
3944
+
3945
+ /* Alias of `tccalloc'. */
3946
+ #if defined(_MYFASTEST)
3947
+ #define TCCALLOC(TC_res, TC_nmemb, TC_size) \
3948
+ do { \
3949
+ (TC_res) = MYCALLOC((TC_nmemb), (TC_size)); \
3950
+ } while(false)
3951
+ #else
3952
+ #define TCCALLOC(TC_res, TC_nmemb, TC_size) \
3953
+ do { \
3954
+ if(!((TC_res) = MYCALLOC((TC_nmemb), (TC_size)))) tcmyfatal("out of memory"); \
3955
+ } while(false)
3956
+ #endif
3957
+
3958
+
3959
+ /* Alias of `tcrealloc'. */
3960
+ #if defined(_MYFASTEST)
3961
+ #define TCREALLOC(TC_res, TC_ptr, TC_size) \
3962
+ do { \
3963
+ (TC_res) = MYREALLOC((TC_ptr), (TC_size)); \
3964
+ } while(false)
3965
+ #else
3966
+ #define TCREALLOC(TC_res, TC_ptr, TC_size) \
3967
+ do { \
3968
+ if(!((TC_res) = MYREALLOC((TC_ptr), (TC_size)))) tcmyfatal("out of memory"); \
3969
+ } while(false)
3970
+ #endif
3971
+
3972
+
3973
+ /* Alias of `tcmemdup'. */
3974
+ #define TCMEMDUP(TC_res, TC_ptr, TC_size) \
3975
+ do { \
3976
+ TCMALLOC((TC_res), (TC_size) + 1); \
3977
+ memcpy((TC_res), (TC_ptr), (TC_size)); \
3978
+ (TC_res)[TC_size] = '\0'; \
3979
+ } while(false)
3980
+
3981
+
3982
+ /* Alias of `tcfree'. */
3983
+ #define TCFREE(TC_ptr) \
3984
+ do { \
3985
+ MYFREE(TC_ptr); \
3986
+ } while(false)
3987
+
3988
+
3989
+ /* Get the alignment of a variable type. */
3990
+ #define TCALIGNOF(TC_a) \
3991
+ ((int)offsetof(struct { int8_t TC_top; TC_a TC_bot; }, TC_bot))
3992
+
3993
+
3994
+ /* Get the size of padding bytes for pointer alignment. */
3995
+ typedef union { int32_t i; int64_t l; double d; void *p; TCCMP f; } tcgeneric_t;
3996
+ #define TCALIGNPAD(TC_hsiz) \
3997
+ (((TC_hsiz | ~-TCALIGNOF(tcgeneric_t)) + 1) - TC_hsiz)
3998
+
3999
+
4000
+ /* Alias of `tcxstrcat'. */
4001
+ #define TCXSTRCAT(TC_xstr, TC_ptr, TC_size) \
4002
+ do { \
4003
+ int TC_mysize = (TC_size); \
4004
+ int TC_nsize = (TC_xstr)->size + TC_mysize + 1; \
4005
+ if((TC_xstr)->asize < TC_nsize){ \
4006
+ while((TC_xstr)->asize < TC_nsize){ \
4007
+ (TC_xstr)->asize *= 2; \
4008
+ if((TC_xstr)->asize < TC_nsize) (TC_xstr)->asize = TC_nsize; \
4009
+ } \
4010
+ TCREALLOC((TC_xstr)->ptr, (TC_xstr)->ptr, (TC_xstr)->asize); \
4011
+ } \
4012
+ memcpy((TC_xstr)->ptr + (TC_xstr)->size, (TC_ptr), TC_mysize); \
4013
+ (TC_xstr)->size += TC_mysize; \
4014
+ (TC_xstr)->ptr[(TC_xstr)->size] = '\0'; \
4015
+ } while(false)
4016
+
4017
+
4018
+ /* Alias of `tcxstrptr'. */
4019
+ #define TCXSTRPTR(TC_xstr) \
4020
+ ((TC_xstr)->ptr)
4021
+
4022
+
4023
+ /* Alias of `tcxstrsize'. */
4024
+ #define TCXSTRSIZE(TC_xstr) \
4025
+ ((TC_xstr)->size)
4026
+
4027
+
4028
+ /* Alias of `tclistnum'. */
4029
+ #define TCLISTNUM(TC_list) \
4030
+ ((TC_list)->num)
4031
+
4032
+
4033
+ /* Alias of `tclistval' but not checking size. */
4034
+ #define TCLISTVAL(TC_ptr, TC_list, TC_index, TC_size) \
4035
+ do { \
4036
+ (TC_ptr) = (TC_list)->array[(TC_index)+(TC_list)->start].ptr; \
4037
+ (TC_size) = (TC_list)->array[(TC_index)+(TC_list)->start].size; \
4038
+ } while(false)
4039
+
4040
+
4041
+ /* Alias of `tclistval' but not checking size and not using the third parameter. */
4042
+ #define TCLISTVALPTR(TC_list, TC_index) \
4043
+ ((void *)((TC_list)->array[(TC_index)+(TC_list)->start].ptr))
4044
+
4045
+
4046
+ /* Alias of `tclistval' but not checking size and returning the size of the value. */
4047
+ #define TCLISTVALSIZ(TC_list, TC_index) \
4048
+ ((TC_list)->array[(TC_index)+(TC_list)->start].size)
4049
+
4050
+
4051
+ /* Alias of `tclistpush'. */
4052
+ #define TCLISTPUSH(TC_list, TC_ptr, TC_size) \
4053
+ do { \
4054
+ int TC_mysize = (TC_size); \
4055
+ int TC_index = (TC_list)->start + (TC_list)->num; \
4056
+ if(TC_index >= (TC_list)->anum){ \
4057
+ (TC_list)->anum += (TC_list)->num + 1; \
4058
+ TCREALLOC((TC_list)->array, (TC_list)->array, \
4059
+ (TC_list)->anum * sizeof((TC_list)->array[0])); \
4060
+ } \
4061
+ TCLISTDATUM *array = (TC_list)->array; \
4062
+ TCMALLOC(array[TC_index].ptr, TC_mysize + 1); \
4063
+ memcpy(array[TC_index].ptr, (TC_ptr), TC_mysize); \
4064
+ array[TC_index].ptr[TC_mysize] = '\0'; \
4065
+ array[TC_index].size = TC_mysize; \
4066
+ (TC_list)->num++; \
4067
+ } while(false)
4068
+
4069
+
4070
+ /* Alias of `tclistinsert'. */
4071
+ #define TCLISTINSERT(TC_list, TC_index, TC_ptr, TC_size) \
4072
+ do { \
4073
+ int TC_myindex = (TC_index); \
4074
+ TC_myindex += (TC_list)->start; \
4075
+ if((TC_list)->start + (TC_list)->num >= (TC_list)->anum){ \
4076
+ (TC_list)->anum += (TC_list)->num + 1; \
4077
+ TCREALLOC((TC_list)->array, (TC_list)->array, \
4078
+ (TC_list)->anum * sizeof((TC_list)->array[0])); \
4079
+ } \
4080
+ memmove((TC_list)->array + TC_myindex + 1, (TC_list)->array + TC_myindex, \
4081
+ sizeof((TC_list)->array[0]) * ((TC_list)->start + (TC_list)->num - TC_myindex)); \
4082
+ TCMALLOC((TC_list)->array[TC_myindex].ptr, (TC_size) + 1); \
4083
+ memcpy((TC_list)->array[TC_myindex].ptr, (TC_ptr), (TC_size)); \
4084
+ (TC_list)->array[TC_myindex].ptr[(TC_size)] = '\0'; \
4085
+ (TC_list)->array[TC_myindex].size = (TC_size); \
4086
+ (TC_list)->num++; \
4087
+ } while(false)
4088
+
4089
+
4090
+ /* Truncate a list object. */
4091
+ #define TCLISTTRUNC(TC_list, TC_num) \
4092
+ do { \
4093
+ while((TC_list)->num > (TC_num)){ \
4094
+ TCFREE((TC_list)->array[--(TC_list)->num].ptr); \
4095
+ } \
4096
+ } while(false)
4097
+
4098
+
4099
+ /* Alias of `tcmaprnum'. */
4100
+ #define TCMAPRNUM(TC_map) \
4101
+ ((TC_map)->rnum)
4102
+
4103
+
4104
+ /* Alias of `tcptrlistnum'. */
4105
+ #define TCPTRLISTNUM(TC_ptrlist) \
4106
+ ((TC_ptrlist)->num)
4107
+
4108
+
4109
+ /* Alias of `tcptrlistval'. */
4110
+ #define TCPTRLISTVAL(TC_ptrlist, TC_index) \
4111
+ ((void *)((TC_ptrlist)->array[(TC_index)+(TC_ptrlist)->start]))
4112
+
4113
+
4114
+ /* Alias of `tcptrlistpush'. */
4115
+ #define TCPTRLISTPUSH(TC_ptrlist, TC_ptr) \
4116
+ do { \
4117
+ int TC_index = (TC_ptrlist)->start + (TC_ptrlist)->num; \
4118
+ if(TC_index >= (TC_ptrlist)->anum){ \
4119
+ (TC_ptrlist)->anum += (TC_ptrlist)->num + 1; \
4120
+ TCREALLOC((TC_ptrlist)->array, (TC_ptrlist)->array, \
4121
+ (TC_ptrlist)->anum * sizeof((TC_ptrlist)->array[0])); \
4122
+ } \
4123
+ (TC_ptrlist)->array[TC_index] = (TC_ptr); \
4124
+ (TC_ptrlist)->num++; \
4125
+ } while(false)
4126
+
4127
+
4128
+ /* Alias of `tcptrlistinsert'. */
4129
+ #define TCPTRLISTINSERT(TC_ptrlist, TC_index, TC_ptr) \
4130
+ do { \
4131
+ int TC_myindex = (TC_index); \
4132
+ TC_myindex += (TC_ptrlist)->start; \
4133
+ if((TC_ptrlist)->start + (TC_ptrlist)->num >= (TC_ptrlist)->anum){ \
4134
+ (TC_ptrlist)->anum += (TC_ptrlist)->num + 1; \
4135
+ TCREALLOC((TC_ptrlist)->array, (TC_ptrlist)->array, \
4136
+ (TC_ptrlist)->anum * sizeof((TC_ptrlist)->array[0])); \
4137
+ } \
4138
+ memmove((TC_ptrlist)->array + TC_myindex + 1, (TC_ptrlist)->array + TC_myindex, \
4139
+ sizeof((TC_ptrlist)->array[0]) * ((TC_ptrlist)->start + \
4140
+ (TC_ptrlist)->num - TC_myindex)); \
4141
+ (TC_ptrlist)->array[TC_myindex] = (TC_ptr); \
4142
+ (TC_ptrlist)->num++; \
4143
+ } while(false)
4144
+
4145
+
4146
+ /* Truncate a pointer list object. */
4147
+ #define TCPTRLISTTRUNC(TC_ptrlist, TC_num) \
4148
+ do { \
4149
+ (TC_ptrlist)->num = (TC_num); \
4150
+ } while(false)
4151
+
4152
+
4153
+ /* tricks for backward compatibility */
4154
+ #define BDBCMP TCCMP
4155
+ #define tcbdbrange3 tcbdbfwmkeys2
4156
+ #define tcbdbcmplexical tccmplexical
4157
+ #define tcbdbcmpdecimal tccmpdecimal
4158
+ #define tcbdbcmpint32 tccmpint32
4159
+ #define tcbdbcmpint64 tccmpint64
4160
+ #define tctdbqryprocout tctdbqrysearchout
4161
+ #define tctdbqrysetmax(TC_tdb, TC_max) \
4162
+ tctdbqrysetlimit((TC_tdb), (TC_max), 0)
4163
+
4164
+ #endif /* duplication check */
4165
+
4166
+ /* END OF FILE */