4 // LordHavoc: some portable directory listing code I wrote for lmp2pcx, now used in darkplaces to load id1/*.pak and such...
6 int matchpattern(char *in, char *pattern)
12 case '?': // match any single character
18 case '*': // match anything until following string
21 while (*pattern == '*')
31 while (*in && *in != *pattern)
36 // *null (* at end of pattern)
49 return 0; // reached end of pattern but not end of input
53 // a little chained strings system
54 stringlist_t *stringlistappend(stringlist_t *current, char *text)
56 stringlist_t *newitem;
57 newitem = Z_Malloc(strlen(text) + 1 + sizeof(stringlist_t));
59 newitem->text = (char *)(newitem + 1);
60 strcpy(newitem->text, text);
62 current->next = newitem;
66 void stringlistfree(stringlist_t *current)
77 stringlist_t *stringlistsort(stringlist_t *start)
80 stringlist_t *current, *previous, *temp2, *temp3, *temp4;
87 while (current && current->next)
89 if (strcmp(current->text, current->next->text) > 0)
91 // current is greater than next
93 temp2 = current->next;
95 temp4 = current->next->next;
97 previous->next = temp2;
105 current = current->next;
111 // operating system specific code
114 stringlist_t *listdirectory(char *path)
116 char pattern[4096], *c;
117 struct _finddata_t n_file;
119 stringlist_t *start, *current;
120 strcpy(pattern, path);
121 strcat(pattern, "\\*");
122 // ask for the directory listing handle
123 hFile = _findfirst(pattern, &n_file);
126 // start a new chain with the the first name
127 start = current = stringlistappend(NULL, n_file.name);
128 // iterate through the directory
129 while (_findnext(hFile, &n_file) == 0)
130 current = stringlistappend(current, n_file.name);
133 // convert names to lowercase because windows does not care, but pattern matching code often does
134 for (current = start;current;current = current->next)
135 for (c = current->text;*c;c++)
136 if (*c >= 'A' && *c <= 'Z')
139 // sort the list alphanumerically
140 start = stringlistsort(start);
148 stringlist_t *listdirectory(char *path)
152 stringlist_t *start, *current;
162 start = current = stringlistappend(NULL, ent->d_name);
163 while((ent = readdir(dir)))
164 current = stringlistappend(current, ent->d_name);
166 // sort the list alphanumerically
167 return stringlistsort(start);
171 void freedirectory(stringlist_t *list)
173 stringlistfree(list);