Updated Spanish translation
[anjuta-git-plugin.git] / tagmanager / strlist.c
blobe92f603ef0d246b7e01f3b939d74bd0ee2383425
1 /*
2 * $Id$
4 * Copyright (c) 1999-2002, Darren Hiebert
6 * This source code is released for free distribution under the terms of the
7 * GNU General Public License.
9 * This module contains functions managing resizable string lists.
13 * INCLUDE FILES
15 #include "general.h" /* must always come first */
17 #include <string.h>
18 #ifdef HAVE_FNMATCH_H
19 # include <fnmatch.h>
20 #endif
22 #include "debug.h"
23 #include "read.h"
24 #include "routines.h"
25 #include "strlist.h"
28 * FUNCTION DEFINITIONS
31 extern stringList *stringListNew (void)
33 stringList* const result = xMalloc (1, stringList);
34 result->max = 0;
35 result->count = 0;
36 result->list = NULL;
37 return result;
40 extern void stringListAdd (stringList *const current, vString *string)
42 enum { incrementalIncrease = 10 };
43 Assert (current != NULL);
44 if (current->list == NULL)
46 Assert (current->max == 0);
47 current->count = 0;
48 current->max = incrementalIncrease;
49 current->list = xMalloc (current->max, vString*);
51 else if (current->count == current->max)
53 current->max += incrementalIncrease;
54 current->list = xRealloc (current->list, current->max, vString*);
56 current->list [current->count++] = string;
59 extern void stringListRemoveLast (stringList *const current)
61 Assert (current != NULL);
62 Assert (current->count > 0);
63 --current->count;
64 current->list [current->count] = NULL;
67 /* Combine list `from' into `current', deleting `from' */
68 extern void stringListCombine (
69 stringList *const current, stringList *const from)
71 unsigned int i;
72 Assert (current != NULL);
73 Assert (from != NULL);
74 for (i = 0 ; i < from->count ; ++i)
76 stringListAdd (current, from->list [i]);
77 from->list [i] = NULL;
79 stringListDelete (from);
82 extern stringList* stringListNewFromArgv (const char* const* const argv)
84 stringList* const result = stringListNew ();
85 const char *const *p;
86 Assert (argv != NULL);
87 for (p = argv ; *p != NULL ; ++p)
88 stringListAdd (result, vStringNewInit (*p));
89 return result;
92 extern stringList* stringListNewFromFile (const char* const fileName)
94 stringList* result = NULL;
95 FILE* const fp = fopen (fileName, "r");
96 if (fp != NULL)
98 result = stringListNew ();
99 while (! feof (fp))
101 vString* const str = vStringNew ();
102 readLine (str, fp);
103 vStringStripTrailing (str);
104 if (vStringLength (str) > 0)
105 stringListAdd (result, str);
106 else
107 vStringDelete (str);
110 return result;
113 extern unsigned int stringListCount (const stringList *const current)
115 Assert (current != NULL);
116 return current->count;
119 extern vString* stringListItem (
120 const stringList *const current, const unsigned int indx)
122 Assert (current != NULL);
123 return current->list [indx];
126 extern vString* stringListLast (const stringList *const current)
128 Assert (current != NULL);
129 Assert (current->count > 0);
130 return current->list [current->count - 1];
133 extern void stringListClear (stringList *const current)
135 unsigned int i;
136 Assert (current != NULL);
137 for (i = 0 ; i < current->count ; ++i)
139 vStringDelete (current->list [i]);
140 current->list [i] = NULL;
142 current->count = 0;
145 extern void stringListDelete (stringList *const current)
147 if (current != NULL)
149 if (current->list != NULL)
151 stringListClear (current);
152 eFree (current->list);
153 current->list = NULL;
155 current->max = 0;
156 current->count = 0;
157 eFree (current);
161 static boolean compareString (
162 const char *const string, vString *const itm)
164 return (boolean) (strcmp (string, vStringValue (itm)) == 0);
167 static boolean compareStringInsensitive (
168 const char *const string, vString *const itm)
170 return (boolean) (strcasecmp (string, vStringValue (itm)) == 0);
173 static int stringListIndex (
174 const stringList *const current,
175 const char *const string,
176 boolean (*test)(const char *s, vString *const vs))
178 int result = -1;
179 unsigned int i;
180 Assert (current != NULL);
181 Assert (string != NULL);
182 Assert (test != NULL);
183 for (i = 0 ; result == -1 && i < current->count ; ++i)
184 if ((*test)(string, current->list [i]))
185 result = i;
186 return result;
189 extern boolean stringListHas (
190 const stringList *const current, const char *const string)
192 boolean result = FALSE;
193 Assert (current != NULL);
194 result = stringListIndex (current, string, compareString) != -1;
195 return result;
198 extern boolean stringListHasInsensitive (
199 const stringList *const current, const char *const string)
201 boolean result = FALSE;
202 Assert (current != NULL);
203 Assert (string != NULL);
204 result = stringListIndex (current, string, compareStringInsensitive) != -1;
205 return result;
208 extern boolean stringListHasTest (
209 const stringList *const current, boolean (*test)(const char *s))
211 boolean result = FALSE;
212 unsigned int i;
213 Assert (current != NULL);
214 for (i = 0 ; ! result && i < current->count ; ++i)
215 result = (*test)(vStringValue (current->list [i]));
216 return result;
219 extern boolean stringListRemoveExtension (
220 stringList* const current, const char* const extension)
222 boolean result = FALSE;
223 int where;
224 #ifdef CASE_INSENSITIVE_FILENAMES
225 where = stringListIndex (current, extension, compareStringInsensitive);
226 #else
227 where = stringListIndex (current, extension, compareString);
228 #endif
229 if (where != -1)
231 memmove (current->list + where, current->list + where + 1,
232 (current->count - where) * sizeof (*current->list));
233 current->list [current->count - 1] = NULL;
234 --current->count;
235 result = TRUE;
237 return result;
240 extern boolean stringListExtensionMatched (
241 const stringList* const current, const char* const extension)
243 #ifdef CASE_INSENSITIVE_FILENAMES
244 return stringListHasInsensitive (current, extension);
245 #else
246 return stringListHas (current, extension);
247 #endif
250 static boolean fileNameMatched (
251 const vString* const vpattern, const char* const fileName)
253 const char* const pattern = vStringValue (vpattern);
254 #if defined (HAVE_FNMATCH)
255 return (boolean) (fnmatch (pattern, fileName, 0) == 0);
256 #elif defined (CASE_INSENSITIVE_FILENAMES)
257 return (boolean) (strcasecmp (pattern, fileName) == 0);
258 #else
259 return (boolean) (strcmp (pattern, fileName) == 0);
260 #endif
263 extern boolean stringListFileMatched (
264 const stringList* const current, const char* const fileName)
266 boolean result = FALSE;
267 unsigned int i;
268 for (i = 0 ; ! result && i < stringListCount (current) ; ++i)
269 result = fileNameMatched (stringListItem (current, i), fileName);
270 return result;
273 extern void stringListPrint (const stringList *const current)
275 unsigned int i;
276 Assert (current != NULL);
277 for (i = 0 ; i < current->count ; ++i)
278 printf ("%s%s", (i > 0) ? ", " : "", vStringValue (current->list [i]));
281 /* vi:set tabstop=4 shiftwidth=4: */