1 /*************************************************************************************************
2 * The utility API of Tokyo Cabinet
3 * Copyright (C) 2006-2011 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 *************************************************************************************************/
18 /* Get the larger value of two integers. */
19 long tclmax(long a, long b){
20 return (a > b) ? a : b;
24 /* Create a list object. */
25 TCLIST *tclistnew(void){
27 TCMALLOC(list, sizeof(*list));
28 list->anum = TCLISTUNIT;
29 TCMALLOC(list->array, sizeof(list->array[0]) * list->anum);
35 /* Get the number of elements of a list object. */
36 int tclistnum(const TCLIST *list){
41 /* Add a string element at the end of a list object. */
42 void tclistpush2(TCLIST *list, const char *str){
44 int index = list->start + list->num;
45 if(index >= list->anum){
46 list->anum += list->num + 1;
47 TCREALLOC(list->array, list->array, list->anum * sizeof(list->array[0]));
49 int size = strlen(str);
50 TCLISTDATUM *array = list->array;
51 TCMALLOC(array[index].ptr, tclmax(size + 1, TCXSTRUNIT));
52 memcpy(array[index].ptr, str, size + 1);
53 array[index].size = size;
58 /* Add an element at the end of a list object. */
59 void tclistpush(TCLIST *list, const void *ptr, int size){
60 assert(list && ptr && size >= 0);
61 int index = list->start + list->num;
62 if(index >= list->anum){
63 list->anum += list->num + 1;
64 TCREALLOC(list->array, list->array, list->anum * sizeof(list->array[0]));
66 TCLISTDATUM *array = list->array;
67 TCMALLOC(array[index].ptr, tclmax(size + 1, TCXSTRUNIT));
68 memcpy(array[index].ptr, ptr, size);
69 array[index].ptr[size] = '\0';
70 array[index].size = size;
74 /* Clear a list object. */
75 void tclistclear(TCLIST *list){
78 TCLISTDATUM *array = list->array;
79 int end = list->start + list->num;
80 for(i = list->start; i < end; i++){
87 const void *tclistval(const TCLIST *list, int index, int *sp){
88 assert(list && index >= 0 && sp);
89 if(index >= list->num) return NULL;
91 *sp = list->array[index].size;
92 return list->array[index].ptr;
96 /* Get the string of an element of a list object. */
97 const char *tclistval2(const TCLIST *list, int index){
98 assert(list && index >= 0);
99 if(index >= list->num) return NULL;
100 index += list->start;
101 return list->array[index].ptr;
106 void *tcmyfatal(const char *message){
108 fprintf(stderr, "fatal error: %s\n", message);
113 TCLIST *tcstrsplit(const char *str, const char *delims){
114 assert(str && delims);
115 TCLIST *list = tclistnew();
117 const char *sp = str;
118 while(*str != '\0' && !strchr(delims, *str)){
121 TCLISTPUSH(list, sp, str - sp);
122 if(*str == '\0') break;