2 * $Id: strlist.c 443 2006-05-30 04:37:13Z darren $
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.
15 #include "general.h" /* must always come first */
28 * FUNCTION DEFINITIONS
31 extern stringList
*stringListNew (void)
33 stringList
* const result
= xMalloc (1, stringList
);
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);
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);
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
)
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 ();
86 Assert (argv
!= NULL
);
87 for (p
= argv
; *p
!= NULL
; ++p
)
88 stringListAdd (result
, vStringNewInit (*p
));
92 extern stringList
* stringListNewFromFile (const char* const fileName
)
94 stringList
* result
= NULL
;
95 FILE* const fp
= fopen (fileName
, "r");
98 result
= stringListNew ();
101 vString
* const str
= vStringNew ();
103 vStringStripTrailing (str
);
104 if (vStringLength (str
) > 0)
105 stringListAdd (result
, str
);
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
)
136 Assert (current
!= NULL
);
137 for (i
= 0 ; i
< current
->count
; ++i
)
139 vStringDelete (current
->list
[i
]);
140 current
->list
[i
] = NULL
;
145 extern void stringListDelete (stringList
*const current
)
149 if (current
->list
!= NULL
)
151 stringListClear (current
);
152 eFree (current
->list
);
153 current
->list
= NULL
;
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
))
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
]))
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;
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;
208 extern boolean
stringListHasTest (
209 const stringList
*const current
, boolean (*test
)(const char *s
))
211 boolean result
= FALSE
;
213 Assert (current
!= NULL
);
214 for (i
= 0 ; ! result
&& i
< current
->count
; ++i
)
215 result
= (*test
)(vStringValue (current
->list
[i
]));
219 extern boolean
stringListRemoveExtension (
220 stringList
* const current
, const char* const extension
)
222 boolean result
= FALSE
;
224 #ifdef CASE_INSENSITIVE_FILENAMES
225 where
= stringListIndex (current
, extension
, compareStringInsensitive
);
227 where
= stringListIndex (current
, extension
, compareString
);
231 memmove (current
->list
+ where
, current
->list
+ where
+ 1,
232 (current
->count
- where
) * sizeof (*current
->list
));
233 current
->list
[current
->count
- 1] = NULL
;
240 extern boolean
stringListExtensionMatched (
241 const stringList
* const current
, const char* const extension
)
243 #ifdef CASE_INSENSITIVE_FILENAMES
244 return stringListHasInsensitive (current
, extension
);
246 return stringListHas (current
, extension
);
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);
259 return (boolean
) (strcmp (pattern
, fileName
) == 0);
263 extern boolean
stringListFileMatched (
264 const stringList
* const current
, const char* const fileName
)
266 boolean result
= FALSE
;
268 for (i
= 0 ; ! result
&& i
< stringListCount (current
) ; ++i
)
269 result
= fileNameMatched (stringListItem (current
, i
), fileName
);
273 extern void stringListPrint (const stringList
*const current
)
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: */