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 *************************************************************************************************/
17 #ifndef _TCUTIL_H /* duplication check */
21 #if ! defined(__cplusplus)
32 /*************************************************************************************************
34 *************************************************************************************************/
36 #define TCXSTRUNIT 12 // allocation unit size of an extensible string
38 #define TCLISTUNIT 64 // allocation unit number of a list handle
40 /* Show error message on the standard error output and exit.
41 `message' specifies an error message.
42 This function does not return. */
43 void *tcmyfatal(const char *message);
45 #define TCMALLOC(TC_res, TC_size) \
47 if(!((TC_res) = malloc(TC_size))) tcmyfatal("out of memory"); \
50 #define TCCALLOC(TC_res, TC_nmemb, TC_size) \
52 if(!((TC_res) = calloc((TC_nmemb), (TC_size)))) tcmyfatal("out of memory"); \
55 #define TCREALLOC(TC_res, TC_ptr, TC_size) \
57 if(!((TC_res) = realloc((TC_ptr), (TC_size)))) tcmyfatal("out of memory"); \
60 #define TCFREE(TC_ptr) \
65 /* Alias of `tclistpush'. */
66 #define TCLISTPUSH(TC_list, TC_ptr, TC_size) \
68 int TC_mysize = (TC_size); \
69 int TC_index = (TC_list)->start + (TC_list)->num; \
70 if(TC_index >= (TC_list)->anum){ \
71 (TC_list)->anum += (TC_list)->num + 1; \
72 TCREALLOC((TC_list)->array, (TC_list)->array, \
73 (TC_list)->anum * sizeof((TC_list)->array[0])); \
75 TCLISTDATUM *array = (TC_list)->array; \
76 TCMALLOC(array[TC_index].ptr, TC_mysize + 1); \
77 memcpy(array[TC_index].ptr, (TC_ptr), TC_mysize); \
78 array[TC_index].ptr[TC_mysize] = '\0'; \
79 array[TC_index].size = TC_mysize; \
89 typedef struct { /* type of structure for an element of a list */
90 char *ptr; /* pointer to the region */
91 int size; /* size of the effective region */
94 typedef struct { /* type of structure for an array list */
95 TCLISTDATUM *array; /* array of data */
96 int anum; /* number of the elements of the array */
97 int start; /* start index of used elements */
98 int num; /* number of used elements */
102 /* Create a list object.
103 The return value is the new list object. */
104 TCLIST *tclistnew(void);
106 /* Get the number of elements of a list object.
107 `list' specifies the list object.
108 The return value is the number of elements of the list. */
109 int tclistnum(const TCLIST *list);
111 /* Add an element at the end of a list object.
112 `list' specifies the list object.
113 `ptr' specifies the pointer to the region of the new element.
114 `size' specifies the size of the region. */
115 void tclistpush(TCLIST *list, const void *ptr, int size);
117 /* Add a string element at the end of a list object.
118 `list' specifies the list object.
119 `str' specifies the string of the new element. */
120 void tclistpush2(TCLIST *list, const char *str);
122 /* Get the pointer to the region of an element of a list object.
123 `list' specifies the list object.
124 `index' specifies the index of the element.
125 `sp' specifies the pointer to the variable into which the size of the region of the return
127 The return value is the pointer to the region of the value.
128 Because an additional zero code is appended at the end of the region of the return value,
129 the return value can be treated as a character string. If `index' is equal to or more than
130 the number of elements, the return value is `NULL'. */
131 const void *tclistval(const TCLIST *list, int index, int *sp);
134 /* Get the string of an element of a list object.
135 `list' specifies the list object.
136 `index' specifies the index of the element.
137 The return value is the string of the value.
138 If `index' is equal to or more than the number of elements, the return value is `NULL'. */
139 const char *tclistval2(const TCLIST *list, int index);
143 /* Clear a list object.
144 `list' specifies the list object.
145 All elements are removed. */
146 void tclistclear(TCLIST *list);
148 /* Create a list object by splitting a string.
149 `str' specifies the source string.
150 `delim' specifies a string containing delimiting characters.
151 The return value is a list object of the split elements.
152 If two delimiters are successive, it is assumed that an empty element is between the two.
153 Because the object of the return value is created with the function `tclistnew', it should be
154 deleted with the function `tclistdel' when it is no longer in use. */
155 TCLIST *tcstrsplit(const char *str, const char *delims);
159 #endif /* duplication check */