Debian Codegraph @

prboom2/src/i_glob.c

raw
1 //
2 // Copyright(C) 2018 Simon Howard
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 //
15 // File globbing API. This allows the contents of the filesystem
16 // to be interrogated.
17 //
18
19 #include <stdio.h>
20 #include <string.h>
21 #include <ctype.h>
22 #include <stdarg.h>
23
24 #include "doomtype.h"
25 #include "i_glob.h"
26 #include "z_zone.h"
27 #include "m_misc.h"
28 #include "m_file.h"
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32 #include "lprintf.h"
33
34 #if defined(_MSC_VER)
35 // For Visual C++, we need to include the win_opendir module.
36 #include "win_opendir.h"
37 #elif defined(HAVE_DIRENT_H)
38 #include <dirent.h>
39 #include <sys/stat.h>
40 #elif defined(__WATCOMC__)
41 // Watcom has the same API in a different header.
42 #include <direct.h>
43 #else
44 #define NO_DIRENT_IMPLEMENTATION
45 #endif
46
47 #ifndef NO_DIRENT_IMPLEMENTATION
48
49 // Only the fields d_name and (as an XSI extension) d_ino are specified
50 // in POSIX.1. Other than Linux, the d_type field is available mainly
51 // only on BSD systems. The remaining fields are available on many, but
52 // not all systems.
53 static dboolean IsDirectory(char *dir, struct dirent *de)
54 {
55 #if defined(_DIRENT_HAVE_D_TYPE)
56 if (de->d_type != DT_UNKNOWN && de->d_type != DT_LNK)
57 {
58 return de->d_type == DT_DIR;
59 }
60 else
61 #endif
62 {
63 char *filename;
64 dboolean result;
65
66 int len = snprintf(NULL, 0, "%s/%s", dir, de->d_name);
67 filename = Z_Malloc(len+1);
68 snprintf(filename, len+1, "%s/%s", dir, de->d_name);
69 result = M_IsDir(filename);
70 Z_Free(filename);
71
72 return result;
73 }
74 }
75
76 struct glob_s
77 {
78 char **globs;
79 int num_globs;
80 int flags;
81 DIR *dir;
82 char *directory;
83 char *last_filename;
84 // These fields are only used when the GLOB_FLAG_SORTED flag is set:
85 char **filenames;
86 int filenames_len;
87 int next_index;
88 };
89
90 static void FreeStringList(char **globs, int num_globs)
91 {
92 int i;
93 for (i = 0; i < num_globs; ++i)
94 {
95 Z_Free(globs[i]);
96 }
97 Z_Free(globs);
98 }
99
100 glob_t *I_StartMultiGlob(const char *directory, int flags,
101 const char *glob, ...)
102 {
103 char **globs;
104 int num_globs;
105 glob_t *result;
106 va_list args;
107 char *directory_native;
108
109 globs = Z_Malloc(sizeof(char *));
110 if (globs == NULL)
111 {
112 return NULL;
113 }
114 globs[0] = Z_Strdup(glob);
115 num_globs = 1;
116
117 va_start(args, glob);
118 for (;;)
119 {
120 const char *arg = va_arg(args, const char *);
121 char **new_globs;
122
123 if (arg == NULL)
124 {
125 break;
126 }
127
128 new_globs = Z_Realloc(globs, sizeof(char *) * (num_globs + 1));
129 if (new_globs == NULL)
130 {
131 FreeStringList(globs, num_globs);
132 }
133 globs = new_globs;
134 globs[num_globs] = Z_Strdup(arg);
135 ++num_globs;
136 }
137 va_end(args);
138
139 result = Z_Malloc(sizeof(glob_t));
140 if (result == NULL)
141 {
142 FreeStringList(globs, num_globs);
143 return NULL;
144 }
145
146 directory_native = ConvertUtf8ToSysNativeMB(directory);
147
148 result->dir = opendir(directory_native);
149 if (result->dir == NULL)
150 {
151 FreeStringList(globs, num_globs);
152 Z_Free(result);
153 Z_Free(directory_native);
154 return NULL;
155 }
156
157 result->directory = directory_native;
158 result->globs = globs;
159 result->num_globs = num_globs;
160 result->flags = flags;
161 result->last_filename = NULL;
162 result->filenames = NULL;
163 result->filenames_len = 0;
164 result->next_index = -1;
165 return result;
166 }
167
168 glob_t *I_StartGlob(const char *directory, const char *glob, int flags)
169 {
170 return I_StartMultiGlob(directory, flags, glob, NULL);
171 }
172
173 void I_EndGlob(glob_t *glob)
174 {
175 if (glob == NULL)
176 {
177 return;
178 }
179
180 FreeStringList(glob->globs, glob->num_globs);
181 FreeStringList(glob->filenames, glob->filenames_len);
182
183 Z_Free(glob->directory);
184 Z_Free(glob->last_filename);
185 (void) closedir(glob->dir);
186 Z_Free(glob);
187 }
188
189 static dboolean MatchesGlob(const char *name, const char *glob, int flags)
190 {
191 int n, g;
192
193 while (*glob != '\0')
194 {
195 n = *name;
196 g = *glob;
197
198 if ((flags & GLOB_FLAG_NOCASE) != 0)
199 {
200 n = tolower(n);
201 g = tolower(g);
202 }
203
204 if (g == '*')
205 {
206 // To handle *-matching we skip past the * and recurse
207 // to check each subsequent character in turn. If none
208 // match then the whole match is a failure.
209 while (*name != '\0')
210 {
211 if (MatchesGlob(name, glob + 1, flags))
212 {
213 return true;
214 }
215 ++name;
216 }
217 return glob[1] == '\0';
218 }
219 else if (g != '?' && n != g)
220 {
221 // For normal characters the name must match the glob,
222 // but for ? we don't care what the character is.
223 return false;
224 }
225
226 ++name;
227 ++glob;
228 }
229
230 // Match successful when glob and name end at the same time.
231 return *name == '\0';
232 }
233
234 static dboolean MatchesAnyGlob(const char *name, glob_t *glob)
235 {
236 int i;
237
238 for (i = 0; i < glob->num_globs; ++i)
239 {
240 if (MatchesGlob(name, glob->globs[i], glob->flags))
241 {
242 return true;
243 }
244 }
245 return false;
246 }
247
248 static char *NextGlob(glob_t *glob)
249 {
250 struct dirent *de;
251 int len;
252 char *temp, *ret;
253
254 do
255 {
256 de = readdir(glob->dir);
257 if (de == NULL)
258 {
259 return NULL;
260 }
261 } while (IsDirectory(glob->directory, de)
262 || !MatchesAnyGlob(de->d_name, glob));
263
264 // Return the fully-qualified path, not just the bare filename.
265 len = snprintf(NULL, 0, "%s/%s", glob->directory, de->d_name);
266 temp = Z_Malloc(len+1);
267 snprintf(temp, len+1, "%s/%s", glob->directory, de->d_name);
268 ret = ConvertSysNativeMBToUtf8(temp);
269 return ret;
270 }
271
272 static void ReadAllFilenames(glob_t *glob)
273 {
274 char *name;
275
276 glob->filenames = NULL;
277 glob->filenames_len = 0;
278 glob->next_index = 0;
279
280 for (;;)
281 {
282 name = NextGlob(glob);
283 if (name == NULL)
284 {
285 break;
286 }
287 glob->filenames = Z_Realloc(glob->filenames,
288 (glob->filenames_len + 1) * sizeof(char *));
289 glob->filenames[glob->filenames_len] = name;
290 ++glob->filenames_len;
291 }
292 }
293
294 static void SortFilenames(char **filenames, int len, int flags)
295 {
296 char *pivot, *tmp;
297 int i, left_len, cmp;
298
299 if (len <= 1)
300 {
301 return;
302 }
303 pivot = filenames[len - 1];
304 left_len = 0;
305 for (i = 0; i < len-1; ++i)
306 {
307 if ((flags & GLOB_FLAG_NOCASE) != 0)
308 {
309 cmp = strcasecmp(filenames[i], pivot);
310 }
311 else
312 {
313 cmp = strcmp(filenames[i], pivot);
314 }
315
316 if (cmp < 0)
317 {
318 tmp = filenames[i];
319 filenames[i] = filenames[left_len];
320 filenames[left_len] = tmp;
321 ++left_len;
322 }
323 }
324 filenames[len - 1] = filenames[left_len];
325 filenames[left_len] = pivot;
326
327 SortFilenames(filenames, left_len, flags);
328 SortFilenames(&filenames[left_len + 1], len - left_len - 1, flags);
329 }
330
331 const char *I_NextGlob(glob_t *glob)
332 {
333 const char *result;
334
335 if (glob == NULL)
336 {
337 return NULL;
338 }
339
340 // In unsorted mode we just return the filenames as we read
341 // them back from the system API.
342 if ((glob->flags & GLOB_FLAG_SORTED) == 0)
343 {
344 Z_Free(glob->last_filename);
345 glob->last_filename = NextGlob(glob);
346 return glob->last_filename;
347 }
348
349 // In sorted mode we read the whole list of filenames into memory,
350 // sort them and return them one at a time.
351 if (glob->next_index < 0)
352 {
353 ReadAllFilenames(glob);
354 SortFilenames(glob->filenames, glob->filenames_len, glob->flags);
355 }
356 if (glob->next_index >= glob->filenames_len)
357 {
358 return NULL;
359 }
360 result = glob->filenames[glob->next_index];
361 ++glob->next_index;
362 return result;
363 }
364
365 #else /* #ifdef NO_DIRENT_IMPLEMENTATION */
366
367 #warning No native implementation of file globbing.
368
369 glob_t *I_StartGlob(const char *directory, const char *glob, int flags)
370 {
371 return NULL;
372 }
373
374 void I_EndGlob(glob_t *glob)
375 {
376 }
377
378 const char *I_NextGlob(glob_t *glob)
379 {
380 return "";
381 }
382
383 #endif /* #ifdef NO_DIRENT_IMPLEMENTATION */