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, const 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 (strcasecmp(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
122 static void adddirentry( stringlist_t *list, const char *path, const char *name )
124 if (strcmp(name, ".") && strcmp(name, ".."))
126 char fullpath[MAX_OSPATH];
127 dpsnprintf (fullpath, sizeof (fullpath), "%s%s", path, name);
128 stringlistappend(list, fullpath);
133 void listdirectory(stringlist_t *list, const char *path)
136 char pattern[4096], *c;
137 struct _finddata_t n_file;
139 strlcpy (pattern, *path ? path : "./", sizeof (pattern));
140 strlcat (pattern, "*", sizeof (pattern));
141 // ask for the directory listing handle
142 hFile = _findfirst(pattern, &n_file);
146 adddirentry(list, path, n_file.name );
147 } while (_findnext(hFile, &n_file) == 0);
150 // convert names to lowercase because windows does not care, but pattern matching code often does
151 for (i = 0;i < list->numstrings;i++)
152 for (c = list->strings[i];*c;c++)
153 if (*c >= 'A' && *c <= 'Z')
158 void listdirectory(stringlist_t *list, const char *path)
162 dir = opendir( *path ? path : "./" );
165 while ((ent = readdir(dir)))
166 adddirentry(list, path, ent->d_name);