OSDN Git Service

copy tcstrsplit
[cmcheckwave/cmcheckwave.git] / tclist.c
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  *************************************************************************************************/
15
16 #include "tclist.h"
17
18 /* Get the larger value of two integers. */
19 long tclmax(long a, long b){
20   return (a > b) ? a : b;
21 }
22
23
24 /* Create a list object. */
25 TCLIST *tclistnew(void){
26   TCLIST *list;
27   TCMALLOC(list, sizeof(*list));
28   list->anum = TCLISTUNIT;
29   TCMALLOC(list->array, sizeof(list->array[0]) * list->anum);
30   list->start = 0;
31   list->num = 0;
32   return list;
33 }
34
35 /* Get the number of elements of a list object. */
36 int tclistnum(const TCLIST *list){
37   assert(list);
38   return list->num;
39 }
40
41 /* Add a string element at the end of a list object. */
42 void tclistpush2(TCLIST *list, const char *str){
43   assert(list && 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]));
48   }
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;
54   list->num++;
55 }
56
57
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]));
65   }
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;
71   list->num++;
72 }
73
74 /* Clear a list object. */
75 void tclistclear(TCLIST *list){
76   int i;
77   assert(list);
78   TCLISTDATUM *array = list->array;
79   int end = list->start + list->num;
80   for(i = list->start; i < end; i++){
81     TCFREE(array[i].ptr);
82   }
83   list->start = 0;
84   list->num = 0;
85 }
86
87 const void *tclistval(const TCLIST *list, int index, int *sp){
88   assert(list && index >= 0 && sp);
89   if(index >= list->num) return NULL;
90   index += list->start;
91   *sp = list->array[index].size;
92   return list->array[index].ptr;
93 }
94
95
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;
102 }
103
104
105
106 void *tcmyfatal(const char *message){
107   assert(message);
108   fprintf(stderr, "fatal error: %s\n", message);
109   exit(1);
110   return NULL;
111 }
112
113 TCLIST *tcstrsplit(const char *str, const char *delims){
114   assert(str && delims);
115   TCLIST *list = tclistnew();
116   while(true){
117     const char *sp = str;
118     while(*str != '\0' && !strchr(delims, *str)){
119       str++;
120     }
121     TCLISTPUSH(list, sp, str - sp);
122     if(*str == '\0') break;
123     str++;
124   }
125   return list;
126 }
127
128