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