4 // LordHavoc: some portable directory listing code I wrote for lmp2pcx, now used in darkplaces to load id1/*.pak and such...
6 int matchpattern(const char *in, const char *pattern, int caseinsensitive)
8 return matchpattern_with_separator(in, pattern, caseinsensitive, "/\\:", false);
11 int matchpattern_with_separator(const char *in, const char *pattern, int caseinsensitive, const char *separators, qboolean wildcard_least_one)
19 return 1; // end of pattern
20 case '?': // match any single character
21 if (*in == 0 || strchr(separators, *in))
26 case '*': // match anything until following string
27 if(wildcard_least_one)
28 if (*in == 0 || strchr(separators, *in))
35 if (strchr(separators, *in))
37 // see if pattern matches at this offset
38 if (matchpattern_with_separator(in, pattern, caseinsensitive, separators, wildcard_least_one))
40 // nope, advance to next offset
50 if (c1 >= 'A' && c1 <= 'Z')
53 if (c2 >= 'A' && c2 <= 'Z')
64 return 0; // reached end of pattern but not end of input
68 // a little strings system
69 void stringlistinit(stringlist_t *list)
71 memset(list, 0, sizeof(*list));
74 void stringlistfreecontents(stringlist_t *list)
77 for (i = 0;i < list->numstrings;i++)
80 Z_Free(list->strings[i]);
81 list->strings[i] = NULL;
86 Z_Free(list->strings);
90 void stringlistappend(stringlist_t *list, const char *text)
95 if (list->numstrings >= list->maxstrings)
97 oldstrings = list->strings;
98 list->maxstrings += 4096;
99 list->strings = (char **) Z_Malloc(list->maxstrings * sizeof(*list->strings));
100 if (list->numstrings)
101 memcpy(list->strings, oldstrings, list->numstrings * sizeof(*list->strings));
105 textlen = strlen(text) + 1;
106 list->strings[list->numstrings] = (char *) Z_Malloc(textlen);
107 memcpy(list->strings[list->numstrings], text, textlen);
111 void stringlistsort(stringlist_t *list)
115 // this is a selection sort (finds the best entry for each slot)
116 for (i = 0;i < list->numstrings - 1;i++)
118 for (j = i + 1;j < list->numstrings;j++)
120 if (strcasecmp(list->strings[i], list->strings[j]) > 0)
122 temp = list->strings[i];
123 list->strings[i] = list->strings[j];
124 list->strings[j] = temp;
130 // operating system specific code
131 static void adddirentry(stringlist_t *list, const char *path, const char *name)
133 if (strcmp(name, ".") && strcmp(name, ".."))
135 char temp[MAX_OSPATH];
136 dpsnprintf( temp, sizeof( temp ), "%s%s", path, name );
137 stringlistappend(list, temp);
142 void listdirectory(stringlist_t *list, const char *basepath, const char *path)
145 char pattern[4096], *c;
146 WIN32_FIND_DATA n_file;
148 strlcpy (pattern, basepath, sizeof(pattern));
149 strlcat (pattern, path, sizeof (pattern));
150 strlcat (pattern, "*", sizeof (pattern));
151 // ask for the directory listing handle
152 hFile = FindFirstFile(pattern, &n_file);
153 if(hFile == INVALID_HANDLE_VALUE)
156 adddirentry(list, path, n_file.cFileName);
157 } while (FindNextFile(hFile, &n_file) != 0);
160 // convert names to lowercase because windows does not care, but pattern matching code often does
161 for (i = 0;i < list->numstrings;i++)
162 for (c = list->strings[i];*c;c++)
163 if (*c >= 'A' && *c <= 'Z')
168 void listdirectory(stringlist_t *list, const char *basepath, const char *path)
170 char fullpath[MAX_OSPATH];
173 dpsnprintf(fullpath, sizeof(fullpath), "%s%s", basepath, *path ? path : "./");
174 dir = opendir(fullpath);
177 while ((ent = readdir(dir)))
178 adddirentry(list, path, ent->d_name);