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)
14 return 1; // end of pattern
15 case '?': // match any single character
16 if (*in == 0 || *in == '/' || *in == '\\' || *in == ':')
21 case '*': // match anything until following string
27 if (*in == '/' || *in == '\\' || *in == ':')
29 // see if pattern matches at this offset
30 if (matchpattern(in, pattern, caseinsensitive))
32 // nope, advance to next offset
42 if (c1 >= 'A' && c1 <= 'Z')
45 if (c2 >= 'A' && c2 <= 'Z')
56 return 0; // reached end of pattern but not end of input
60 // a little strings system
61 void stringlistinit(stringlist_t *list)
63 memset(list, 0, sizeof(*list));
66 void stringlistfreecontents(stringlist_t *list)
69 for (i = 0;i < list->numstrings;i++)
72 Z_Free(list->strings[i]);
73 list->strings[i] = NULL;
78 Z_Free(list->strings);
81 void stringlistappend(stringlist_t *list, char *text)
86 if (list->numstrings >= list->maxstrings)
88 oldstrings = list->strings;
89 list->maxstrings += 4096;
90 list->strings = Z_Malloc(list->maxstrings * sizeof(*list->strings));
92 memcpy(list->strings, oldstrings, list->numstrings * sizeof(*list->strings));
96 textlen = strlen(text) + 1;
97 list->strings[list->numstrings] = Z_Malloc(textlen);
98 memcpy(list->strings[list->numstrings], text, textlen);
102 void stringlistsort(stringlist_t *list)
106 // this is a selection sort (finds the best entry for each slot)
107 for (i = 0;i < list->numstrings - 1;i++)
109 for (j = i + 1;j < list->numstrings;j++)
111 if (strcmp(list->strings[i], list->strings[j]) > 0)
113 temp = list->strings[i];
114 list->strings[i] = list->strings[j];
115 list->strings[j] = temp;
121 // operating system specific code
124 void listdirectory(stringlist_t *list, const char *path)
127 char pattern[4096], *c;
128 struct _finddata_t n_file;
130 strlcpy (pattern, path, sizeof (pattern));
131 strlcat (pattern, "*", sizeof (pattern));
132 // ask for the directory listing handle
133 hFile = _findfirst(pattern, &n_file);
136 // start a new chain with the the first name
137 stringlistappend(list, n_file.name);
138 // iterate through the directory
139 while (_findnext(hFile, &n_file) == 0)
140 stringlistappend(list, n_file.name);
143 // convert names to lowercase because windows does not care, but pattern matching code often does
144 for (i = 0;i < list->numstrings;i++)
145 for (c = list->strings[i];*c;c++)
146 if (*c >= 'A' && *c <= 'Z')
151 void listdirectory(stringlist_t *list, const char *path)
158 while ((ent = readdir(dir)))
159 if (strcmp(ent->d_name, ".") && strcmp(ent->d_name, ".."))
160 stringlistappend(list, ent->d_name);