| 1 |
/* |
| 2 |
* $Id: strlist.c 443 2006-05-30 04:37:13Z darren $ |
| 3 |
* |
| 4 |
* Copyright (c) 1999-2002, Darren Hiebert |
| 5 |
* |
| 6 |
* This source code is released for free distribution under the terms of the |
| 7 |
* GNU General Public License. |
| 8 |
* |
| 9 |
* This module contains functions managing resizable string lists. |
| 10 |
*/ |
| 11 |
|
| 12 |
/* |
| 13 |
* INCLUDE FILES |
| 14 |
*/ |
| 15 |
#include "general.h" /* must always come first */ |
| 16 |
|
| 17 |
#include <string.h> |
| 18 |
#ifdef HAVE_FNMATCH_H |
| 19 |
# include <fnmatch.h> |
| 20 |
#endif |
| 21 |
|
| 22 |
#include "debug.h" |
| 23 |
#include "read.h" |
| 24 |
#include "routines.h" |
| 25 |
#include "strlist.h" |
| 26 |
|
| 27 |
/* |
| 28 |
* FUNCTION DEFINITIONS |
| 29 |
*/ |
| 30 |
|
| 31 |
extern stringList *stringListNew (void) |
| 32 |
{ |
| 33 |
stringList* const result = xMalloc (1, stringList); |
| 34 |
result->max = 0; |
| 35 |
result->count = 0; |
| 36 |
result->list = NULL; |
| 37 |
return result; |
| 38 |
} |
| 39 |
|
| 40 |
extern void stringListAdd (stringList *const current, vString *string) |
| 41 |
{ |
| 42 |
enum { incrementalIncrease = 10 }; |
| 43 |
Assert (current != NULL); |
| 44 |
if (current->list == NULL) |
| 45 |
{ |
| 46 |
Assert (current->max == 0); |
| 47 |
current->count = 0; |
| 48 |
current->max = incrementalIncrease; |
| 49 |
current->list = xMalloc (current->max, vString*); |
| 50 |
} |
| 51 |
else if (current->count == current->max) |
| 52 |
{ |
| 53 |
current->max += incrementalIncrease; |
| 54 |
current->list = xRealloc (current->list, current->max, vString*); |
| 55 |
} |
| 56 |
current->list [current->count++] = string; |
| 57 |
} |
| 58 |
|
| 59 |
extern void stringListRemoveLast (stringList *const current) |
| 60 |
{ |
| 61 |
Assert (current != NULL); |
| 62 |
Assert (current->count > 0); |
| 63 |
--current->count; |
| 64 |
current->list [current->count] = NULL; |
| 65 |
} |
| 66 |
|
| 67 |
/* Combine list `from' into `current', deleting `from' */ |
| 68 |
extern void stringListCombine ( |
| 69 |
stringList *const current, stringList *const from) |
| 70 |
{ |
| 71 |
unsigned int i; |
| 72 |
Assert (current != NULL); |
| 73 |
Assert (from != NULL); |
| 74 |
for (i = 0 ; i < from->count ; ++i) |
| 75 |
{ |
| 76 |
stringListAdd (current, from->list [i]); |
| 77 |
from->list [i] = NULL; |
| 78 |
} |
| 79 |
stringListDelete (from); |
| 80 |
} |
| 81 |
|
| 82 |
extern stringList* stringListNewFromArgv (const char* const* const argv) |
| 83 |
{ |
| 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; |
| 90 |
} |
| 91 |
|
| 92 |
extern stringList* stringListNewFromFile (const char* const fileName) |
| 93 |
{ |
| 94 |
stringList* result = NULL; |
| 95 |
FILE* const fp = fopen (fileName, "r"); |
| 96 |
if (fp != NULL) |
| 97 |
{ |
| 98 |
result = stringListNew (); |
| 99 |
while (! feof (fp)) |
| 100 |
{ |
| 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); |
| 108 |
} |
| 109 |
} |
| 110 |
return result; |
| 111 |
} |
| 112 |
|
| 113 |
extern unsigned int stringListCount (const stringList *const current) |
| 114 |
{ |
| 115 |
Assert (current != NULL); |
| 116 |
return current->count; |
| 117 |
} |
| 118 |
|
| 119 |
extern vString* stringListItem ( |
| 120 |
const stringList *const current, const unsigned int indx) |
| 121 |
{ |
| 122 |
Assert (current != NULL); |
| 123 |
return current->list [indx]; |
| 124 |
} |
| 125 |
|
| 126 |
extern vString* stringListLast (const stringList *const current) |
| 127 |
{ |
| 128 |
Assert (current != NULL); |
| 129 |
Assert (current->count > 0); |
| 130 |
return current->list [current->count - 1]; |
| 131 |
} |
| 132 |
|
| 133 |
extern void stringListClear (stringList *const current) |
| 134 |
{ |
| 135 |
unsigned int i; |
| 136 |
Assert (current != NULL); |
| 137 |
for (i = 0 ; i < current->count ; ++i) |
| 138 |
{ |
| 139 |
vStringDelete (current->list [i]); |
| 140 |
current->list [i] = NULL; |
| 141 |
} |
| 142 |
current->count = 0; |
| 143 |
} |
| 144 |
|
| 145 |
extern void stringListDelete (stringList *const current) |
| 146 |
{ |
| 147 |
if (current != NULL) |
| 148 |
{ |
| 149 |
if (current->list != NULL) |
| 150 |
{ |
| 151 |
stringListClear (current); |
| 152 |
eFree (current->list); |
| 153 |
current->list = NULL; |
| 154 |
} |
| 155 |
current->max = 0; |
| 156 |
current->count = 0; |
| 157 |
eFree (current); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
static boolean compareString ( |
| 162 |
const char *const string, vString *const itm) |
| 163 |
{ |
| 164 |
return (boolean) (strcmp (string, vStringValue (itm)) == 0); |
| 165 |
} |
| 166 |
|
| 167 |
static boolean compareStringInsensitive ( |
| 168 |
const char *const string, vString *const itm) |
| 169 |
{ |
| 170 |
return (boolean) (strcasecmp (string, vStringValue (itm)) == 0); |
| 171 |
} |
| 172 |
|
| 173 |
static int stringListIndex ( |
| 174 |
const stringList *const current, |
| 175 |
const char *const string, |
| 176 |
boolean (*test)(const char *s, vString *const vs)) |
| 177 |
{ |
| 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; |
| 187 |
} |
| 188 |
|
| 189 |
extern boolean stringListHas ( |
| 190 |
const stringList *const current, const char *const string) |
| 191 |
{ |
| 192 |
boolean result = FALSE; |
| 193 |
Assert (current != NULL); |
| 194 |
result = stringListIndex (current, string, compareString) != -1; |
| 195 |
return result; |
| 196 |
} |
| 197 |
|
| 198 |
extern boolean stringListHasInsensitive ( |
| 199 |
const stringList *const current, const char *const string) |
| 200 |
{ |
| 201 |
boolean result = FALSE; |
| 202 |
Assert (current != NULL); |
| 203 |
Assert (string != NULL); |
| 204 |
result = stringListIndex (current, string, compareStringInsensitive) != -1; |
| 205 |
return result; |
| 206 |
} |
| 207 |
|
| 208 |
extern boolean stringListHasTest ( |
| 209 |
const stringList *const current, boolean (*test)(const char *s)) |
| 210 |
{ |
| 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; |
| 217 |
} |
| 218 |
|
| 219 |
extern boolean stringListRemoveExtension ( |
| 220 |
stringList* const current, const char* const extension) |
| 221 |
{ |
| 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) |
| 230 |
{ |
| 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; |
| 236 |
} |
| 237 |
return result; |
| 238 |
} |
| 239 |
|
| 240 |
extern boolean stringListExtensionMatched ( |
| 241 |
const stringList* const current, const char* const extension) |
| 242 |
{ |
| 243 |
#ifdef CASE_INSENSITIVE_FILENAMES |
| 244 |
return stringListHasInsensitive (current, extension); |
| 245 |
#else |
| 246 |
return stringListHas (current, extension); |
| 247 |
#endif |
| 248 |
} |
| 249 |
|
| 250 |
static boolean fileNameMatched ( |
| 251 |
const vString* const vpattern, const char* const fileName) |
| 252 |
{ |
| 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 |
| 261 |
} |
| 262 |
|
| 263 |
extern boolean stringListFileMatched ( |
| 264 |
const stringList* const current, const char* const fileName) |
| 265 |
{ |
| 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; |
| 271 |
} |
| 272 |
|
| 273 |
extern void stringListPrint (const stringList *const current) |
| 274 |
{ |
| 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])); |
| 279 |
} |
| 280 |
|
| 281 |
/* vi:set tabstop=4 shiftwidth=4: */ |