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.
13 #include "general.h" /* must always come first */
19 #include <glib/gstdio.h>
26 * FUNCTION DEFINITIONS
29 extern stringList
*stringListNew (void)
31 stringList
* const result
= xMalloc (1, stringList
);
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);
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);
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
)
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 ();
84 Assert (argv
!= NULL
);
85 for (p
= argv
; *p
!= NULL
; ++p
)
86 stringListAdd (result
, vStringNewInit (*p
));
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
);
96 result
= stringListNew ();
97 while (! mio_eof (mio
))
99 vString
* const str
= vStringNew ();
101 vStringStripTrailing (str
);
102 if (vStringLength (str
) > 0)
103 stringListAdd (result
, str
);
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
)
135 Assert (current
!= NULL
);
136 for (i
= 0 ; i
< current
->count
; ++i
)
138 vStringDelete (current
->list
[i
]);
139 current
->list
[i
] = NULL
;
144 extern void stringListDelete (stringList
*const current
)
148 if (current
->list
!= NULL
)
150 stringListClear (current
);
151 eFree (current
->list
);
152 current
->list
= NULL
;
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
))
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
]))
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;
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;
207 extern boolean
stringListHasTest (
208 const stringList
*const current
, boolean (*test
)(const char *s
))
210 boolean result
= FALSE
;
212 Assert (current
!= NULL
);
213 for (i
= 0 ; ! result
&& i
< current
->count
; ++i
)
214 result
= (*test
)(vStringValue (current
->list
[i
]));
218 extern boolean
stringListRemoveExtension (
219 stringList
* const current
, const char* const extension
)
221 boolean result
= FALSE
;
223 #ifdef CASE_INSENSITIVE_FILENAMES
224 where
= stringListIndex (current
, extension
, compareStringInsensitive
);
226 where
= stringListIndex (current
, extension
, compareString
);
230 memmove (current
->list
+ where
, current
->list
+ where
+ 1,
231 (current
->count
- where
) * sizeof (*current
->list
));
232 current
->list
[current
->count
- 1] = NULL
;
239 extern boolean
stringListExtensionMatched (
240 const stringList
* const current
, const char* const extension
)
242 #ifdef CASE_INSENSITIVE_FILENAMES
243 return stringListHasInsensitive (current
, extension
);
245 return stringListHas (current
, extension
);
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);
258 return (boolean
) (strcmp (pattern
, fileName
) == 0);
262 extern boolean
stringListFileMatched (
263 const stringList
* const current
, const char* const fileName
)
265 boolean result
= FALSE
;
267 for (i
= 0 ; ! result
&& i
< stringListCount (current
) ; ++i
)
268 result
= fileNameMatched (stringListItem (current
, i
), fileName
);
272 extern void stringListPrint (const stringList
*const current
)
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: */