Skip to content

Latest commit

 

History

History
239 lines (202 loc) · 6.41 KB

globbing.c

File metadata and controls

239 lines (202 loc) · 6.41 KB
 
Jun 11, 2003
Jun 11, 2003
1
2
3
4
5
6
/** \file globbing.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
Mar 28, 2009
Mar 28, 2009
7
#include <assert.h>
Jun 11, 2003
Jun 11, 2003
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "globbing.h"
/**
* Please see globbing.h for details.
*
* License: this code is public domain. I make no warranty that it is useful,
* correct, harmless, or environmentally safe.
*
* This particular file may be used however you like, including copying it
* verbatim into a closed-source project, exploiting it commercially, and
* removing any trace of my name from the source (although I hope you won't
* do that). I welcome enhancements and corrections to this file, but I do
Jul 20, 2003
Jul 20, 2003
21
22
* not require you to send me patches if you make changes. This code has
* NO WARRANTY.
Jun 11, 2003
Jun 11, 2003
23
*
Jul 20, 2003
Jul 20, 2003
24
* Unless otherwise stated, the rest of PhysicsFS falls under the zlib license.
Mar 28, 2009
Mar 28, 2009
25
* Please see the file LICENSE.txt in the source's root directory.
Jun 11, 2003
Jun 11, 2003
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
*
* \author Ryan C. Gordon.
*/
static int matchesPattern(const char *fname, const char *wildcard,
int caseSensitive)
{
char x, y;
const char *fnameptr = fname;
const char *wildptr = wildcard;
while ((*wildptr) && (*fnameptr))
{
y = *wildptr;
if (y == '*')
{
Jun 11, 2003
Jun 11, 2003
43
44
45
46
47
do
{
wildptr++; /* skip multiple '*' in a row... */
} while (*wildptr == '*');
Jun 11, 2003
Jun 11, 2003
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
y = (caseSensitive) ? *wildptr : (char) tolower(*wildptr);
while (1)
{
x = (caseSensitive) ? *fnameptr : (char) tolower(*fnameptr);
if ((!x) || (x == y))
break;
else
fnameptr++;
} /* while */
} /* if */
else if (y == '?')
{
wildptr++;
fnameptr++;
} /* else if */
else
{
if (caseSensitive)
x = *fnameptr;
else
{
x = tolower(*fnameptr);
y = tolower(y);
} /* if */
wildptr++;
fnameptr++;
if (x != y)
Jan 28, 2010
Jan 28, 2010
80
return 0;
Jun 11, 2003
Jun 11, 2003
81
82
83
} /* else */
} /* while */
Jun 11, 2003
Jun 11, 2003
84
85
86
while (*wildptr == '*')
wildptr++;
Jan 28, 2010
Jan 28, 2010
87
return (*fnameptr == *wildptr);
Jun 11, 2003
Jun 11, 2003
88
89
} /* matchesPattern */
Mar 28, 2009
Mar 28, 2009
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
typedef struct
{
const PHYSFS_Allocator *allocator;
const char *wildcard;
int caseSensitive;
PHYSFS_EnumFilesCallback callback;
void *origData;
} WildcardCallbackData;
/*
* This callback sits between the enumerator and the enduser callback,
* filtering out files that don't match the wildcard pattern.
*/
static void wildcardCallback(void *_d, const char *origdir, const char *fname)
{
const WildcardCallbackData *data = (const WildcardCallbackData *) _d;
if (matchesPattern(fname, data->wildcard, data->caseSensitive))
data->callback(data->origData, origdir, fname);
} /* wildcardCallback */
void PHYSFSEXT_enumerateFilesCallbackWildcard(const char *dir,
const char *wildcard,
int caseSensitive,
PHYSFS_EnumFilesCallback c,
void *d)
{
WildcardCallbackData data;
data.allocator = PHYSFS_getAllocator();
data.wildcard = wildcard;
data.caseSensitive = caseSensitive;
data.callback = c;
data.origData = d;
PHYSFS_enumerateFilesCallback(dir, wildcardCallback, &data);
} /* PHYSFSEXT_enumerateFilesCallbackWildcard */
void PHYSFSEXT_freeEnumeration(char **list)
{
const PHYSFS_Allocator *allocator = PHYSFS_getAllocator();
int i;
if (list != NULL)
{
for (i = 0; list[i] != NULL; i++)
allocator->Free(list[i]);
allocator->Free(list);
} /* if */
} /* PHYSFSEXT_freeEnumeration */
Jun 11, 2003
Jun 11, 2003
140
141
142
143
char **PHYSFSEXT_enumerateFilesWildcard(const char *dir, const char *wildcard,
int caseSensitive)
{
Mar 28, 2009
Mar 28, 2009
144
145
146
147
148
149
const PHYSFS_Allocator *allocator = PHYSFS_getAllocator();
char **list = PHYSFS_enumerateFiles(dir);
char **retval = NULL;
int totalmatches = 0;
int matches = 0;
char **i;
Jun 11, 2003
Jun 11, 2003
150
Mar 28, 2009
Mar 28, 2009
151
for (i = list; *i != NULL; i++)
Jun 11, 2003
Jun 11, 2003
152
{
Mar 28, 2009
Mar 28, 2009
153
154
155
156
157
#if 0
printf("matchesPattern: '%s' vs '%s' (%s) ... %s\n", *i, wildcard,
caseSensitive ? "case" : "nocase",
matchesPattern(*i, wildcard, caseSensitive) ? "true" : "false");
#endif
Jun 11, 2003
Jun 11, 2003
158
if (matchesPattern(*i, wildcard, caseSensitive))
Mar 28, 2009
Mar 28, 2009
159
totalmatches++;
Jun 11, 2003
Jun 11, 2003
160
161
} /* for */
Mar 28, 2009
Mar 28, 2009
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
retval = (char **) allocator->Malloc(sizeof (char *) * (totalmatches+1));
if (retval != NULL)
{
for (i = list; ((matches < totalmatches) && (*i != NULL)); i++)
{
if (matchesPattern(*i, wildcard, caseSensitive))
{
retval[matches] = (char *) allocator->Malloc(strlen(*i) + 1);
if (retval[matches] == NULL)
{
while (matches--)
allocator->Free(retval[matches]);
allocator->Free(retval);
retval = NULL;
break;
} /* if */
strcpy(retval[matches], *i);
matches++;
} /* if */
} /* for */
if (retval != NULL)
{
assert(totalmatches == matches);
retval[matches] = NULL;
} /* if */
} /* if */
PHYSFS_freeList(list);
Jan 28, 2010
Jan 28, 2010
191
return retval;
Jun 11, 2003
Jun 11, 2003
192
193
194
195
196
197
198
199
200
201
202
203
204
205
} /* PHYSFSEXT_enumerateFilesWildcard */
#ifdef TEST_PHYSFSEXT_ENUMERATEFILESWILDCARD
int main(int argc, char **argv)
{
int rc;
char **flist;
char **i;
if (argc != 3)
{
printf("USAGE: %s <pattern> <caseSen>\n"
" where <caseSen> is 1 or 0.\n", argv[0]);
Jan 28, 2010
Jan 28, 2010
206
return 1;
Jun 11, 2003
Jun 11, 2003
207
208
209
210
} /* if */
if (!PHYSFS_init(argv[0]))
{
Feb 20, 2018
Feb 20, 2018
211
fprintf(stderr, "PHYSFS_init(): %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
Jan 28, 2010
Jan 28, 2010
212
return 1;
Jun 11, 2003
Jun 11, 2003
213
214
215
216
} /* if */
if (!PHYSFS_addToSearchPath(".", 1))
{
Feb 20, 2018
Feb 20, 2018
217
fprintf(stderr, "PHYSFS_addToSearchPath(): %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
Jun 11, 2003
Jun 11, 2003
218
PHYSFS_deinit();
Jan 28, 2010
Jan 28, 2010
219
return 1;
Jun 11, 2003
Jun 11, 2003
220
221
222
223
224
225
226
227
228
229
230
} /* if */
flist = PHYSFSEXT_enumerateFilesWildcard("/", argv[1], atoi(argv[2]));
rc = 0;
for (i = flist; *i; i++)
{
printf("%s\n", *i);
rc++;
} /* for */
printf("\n total %d files.\n\n", rc);
Mar 28, 2009
Mar 28, 2009
231
PHYSFSEXT_freeEnumeration(flist);
Jun 11, 2003
Jun 11, 2003
232
233
PHYSFS_deinit();
Jan 28, 2010
Jan 28, 2010
234
return 0;
Jun 11, 2003
Jun 11, 2003
235
236
237
238
} /* main */
#endif
/* end of globbing.c ... */