strlist.c 5.35 KB
Newer Older
Enrico Tröger's avatar
Enrico Tröger committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
*
*   Copyright (c) 1999-2001, 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>
17
#ifdef HAVE_FNMATCH_H
Enrico Tröger's avatar
Enrico Tröger committed
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
# include <fnmatch.h>
#endif


#include "main.h"
#include "read.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;
}

/* 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)
	error (FATAL | PERROR, "cannot open \"%s\"", fileName);
    else
    {
	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 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);
    }
}

extern boolean stringListHas (const stringList *const current,
			      const char *const str)
{
    boolean result = FALSE;
    unsigned int i;
    Assert (current != NULL);
    for (i = 0  ;  ! result  &&  i < current->count  ;  ++i)
	result = (boolean) (strcmp (str, vStringValue (current->list [i]))==0);
    return result;
}

extern boolean stringListHasInsensitive (const stringList *const current,
					 const char *const str)
{
    boolean result = FALSE;
    unsigned int i;
    Assert (current != NULL);
    for (i = 0  ;  ! result  &&  i < current->count  ;  ++i)
	result = (boolean) (stricmp (str, vStringValue (current->list [i]))==0);
    return result;
}

extern boolean stringListHasFile (const stringList *const current,
				  const char *const file)
{
    boolean result = FALSE;
    unsigned int i;
    Assert (current != NULL);
    for (i = 0  ;  ! result  &&  i < current->count  ;  ++i)
	result = (boolean) (isSameFile (file, vStringValue (current->list [i])));
    return result;
}

extern boolean stringListExtensionMatched (const stringList* const list,
					   const char* const extension)
{
#ifdef CASE_INSENSITIVE_FILENAMES
    return stringListHasInsensitive (list, extension);
#else
    return stringListHas (list, 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) (stricmp (pattern, fileName) == 0);
#else
    return (boolean) (strcmp (pattern, fileName) == 0);
#endif
}

extern boolean stringListFileMatched (const stringList* const list,
				      const char* const fileName)
{
    boolean result = FALSE;
    unsigned int i;
    for (i = 0  ;  ! result  &&  i < stringListCount (list)  ;  ++i)
	result = fileNameMatched (stringListItem (list, 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=8 shiftwidth=4: */