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