Merge pull request #482 from philippwiesemann/fix-typos-po-de
[geany-mirror.git] / tagmanager / ctags / strlist.c
blob51df0dbe72962fd7a406d7d6de6618c746b24111
1 /*
2 * Copyright (c) 1999-2002, Darren Hiebert
4 * This source code is released for free distribution under the terms of the
5 * GNU General Public License.
7 * This module contains functions managing resizable string lists.
8 */
11 * INCLUDE FILES
13 #include "general.h" /* must always come first */
15 #include <string.h>
16 #ifdef HAVE_FNMATCH_H
17 # include <fnmatch.h>
18 #endif
19 #include <glib/gstdio.h>
21 #include "main.h"
22 #include "read.h"
23 #include "strlist.h"
26 * FUNCTION DEFINITIONS
29 extern stringList *stringListNew (void)
31 stringList* const result = xMalloc (1, stringList);
32 result->max = 0;
33 result->count = 0;
34 result->list = NULL;
35 return result;
38 extern void stringListAdd (stringList *const current, vString *string)
40 enum { incrementalIncrease = 10 };
41 Assert (current != NULL);
42 if (current->list == NULL)
44 Assert (current->max == 0);
45 current->count = 0;
46 current->max = incrementalIncrease;
47 current->list = xMalloc (current->max, vString*);
49 else if (current->count == current->max)
51 current->max += incrementalIncrease;
52 current->list = xRealloc (current->list, current->max, vString*);
54 current->list [current->count++] = string;
57 extern void stringListRemoveLast (stringList *const current)
59 Assert (current != NULL);
60 Assert (current->count > 0);
61 --current->count;
62 current->list [current->count] = NULL;
65 /* Combine list `from' into `current', deleting `from' */
66 extern void stringListCombine (
67 stringList *const current, stringList *const from)
69 unsigned int i;
70 Assert (current != NULL);
71 Assert (from != NULL);
72 for (i = 0 ; i < from->count ; ++i)
74 stringListAdd (current, from->list [i]);
75 from->list [i] = NULL;
77 stringListDelete (from);
80 extern stringList* stringListNewFromArgv (const char* const* const argv)
82 stringList* const result = stringListNew ();
83 const char *const *p;
84 Assert (argv != NULL);
85 for (p = argv ; *p != NULL ; ++p)
86 stringListAdd (result, vStringNewInit (*p));
87 return result;
90 extern stringList* stringListNewFromFile (const char* const fileName)
92 stringList* result = NULL;
93 MIO* const mio = mio_new_file_full (fileName, "r", g_fopen, fclose);
94 if (mio != NULL)
96 result = stringListNew ();
97 while (! mio_eof (mio))
99 vString* const str = vStringNew ();
100 readLine (str, mio);
101 vStringStripTrailing (str);
102 if (vStringLength (str) > 0)
103 stringListAdd (result, str);
104 else
105 vStringDelete (str);
107 mio_free (mio);
109 return result;
112 extern unsigned int stringListCount (const stringList *const current)
114 Assert (current != NULL);
115 return current->count;
118 extern vString* stringListItem (
119 const stringList *const current, const unsigned int indx)
121 Assert (current != NULL);
122 return current->list [indx];
125 extern vString* stringListLast (const stringList *const current)
127 Assert (current != NULL);
128 Assert (current->count > 0);
129 return current->list [current->count - 1];
132 extern void stringListClear (stringList *const current)
134 unsigned int i;
135 Assert (current != NULL);
136 for (i = 0 ; i < current->count ; ++i)
138 vStringDelete (current->list [i]);
139 current->list [i] = NULL;
141 current->count = 0;
144 extern void stringListDelete (stringList *const current)
146 if (current != NULL)
148 if (current->list != NULL)
150 stringListClear (current);
151 eFree (current->list);
152 current->list = NULL;
154 current->max = 0;
155 current->count = 0;
156 eFree (current);
160 static boolean compareString (
161 const char *const string, vString *const itm)
163 return (boolean) (strcmp (string, vStringValue (itm)) == 0);
166 static boolean compareStringInsensitive (
167 const char *const string, vString *const itm)
169 return (boolean) (strcasecmp (string, vStringValue (itm)) == 0);
172 static int stringListIndex (
173 const stringList *const current,
174 const char *const string,
175 boolean (*test)(const char *s, vString *const vs))
177 int result = -1;
178 unsigned int i;
179 Assert (current != NULL);
180 Assert (string != NULL);
181 Assert (test != NULL);
182 for (i = 0 ; result == -1 && i < current->count ; ++i)
183 if ((*test)(string, current->list [i]))
184 result = i;
185 return result;
188 extern boolean stringListHas (
189 const stringList *const current, const char *const string)
191 boolean result = FALSE;
192 Assert (current != NULL);
193 result = stringListIndex (current, string, compareString) != -1;
194 return result;
197 extern boolean stringListHasInsensitive (
198 const stringList *const current, const char *const string)
200 boolean result = FALSE;
201 Assert (current != NULL);
202 Assert (string != NULL);
203 result = stringListIndex (current, string, compareStringInsensitive) != -1;
204 return result;
207 extern boolean stringListHasTest (
208 const stringList *const current, boolean (*test)(const char *s))
210 boolean result = FALSE;
211 unsigned int i;
212 Assert (current != NULL);
213 for (i = 0 ; ! result && i < current->count ; ++i)
214 result = (*test)(vStringValue (current->list [i]));
215 return result;
218 extern boolean stringListRemoveExtension (
219 stringList* const current, const char* const extension)
221 boolean result = FALSE;
222 int where;
223 #ifdef CASE_INSENSITIVE_FILENAMES
224 where = stringListIndex (current, extension, compareStringInsensitive);
225 #else
226 where = stringListIndex (current, extension, compareString);
227 #endif
228 if (where != -1)
230 memmove (current->list + where, current->list + where + 1,
231 (current->count - where) * sizeof (*current->list));
232 current->list [current->count - 1] = NULL;
233 --current->count;
234 result = TRUE;
236 return result;
239 extern boolean stringListExtensionMatched (
240 const stringList* const current, const char* const extension)
242 #ifdef CASE_INSENSITIVE_FILENAMES
243 return stringListHasInsensitive (current, extension);
244 #else
245 return stringListHas (current, extension);
246 #endif
249 static boolean fileNameMatched (
250 const vString* const vpattern, const char* const fileName)
252 const char* const pattern = vStringValue (vpattern);
253 #if defined (HAVE_FNMATCH)
254 return (boolean) (fnmatch (pattern, fileName, 0) == 0);
255 #elif defined (CASE_INSENSITIVE_FILENAMES)
256 return (boolean) (strcasecmp (pattern, fileName) == 0);
257 #else
258 return (boolean) (strcmp (pattern, fileName) == 0);
259 #endif
262 extern boolean stringListFileMatched (
263 const stringList* const current, const char* const fileName)
265 boolean result = FALSE;
266 unsigned int i;
267 for (i = 0 ; ! result && i < stringListCount (current) ; ++i)
268 result = fileNameMatched (stringListItem (current, i), fileName);
269 return result;
272 extern void stringListPrint (const stringList *const current)
274 unsigned int i;
275 Assert (current != NULL);
276 for (i = 0 ; i < current->count ; ++i)
277 printf ("%s%s", (i > 0) ? ", " : "", vStringValue (current->list [i]));
280 /* vi:set tabstop=4 shiftwidth=4: */