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, int caseinsensitive)
13 case '?': // match any single character
19 case '*': // match anything until following string
22 while (*pattern == '*')
32 while (*in && *in != *pattern)
37 // *null (* at end of pattern)
47 if (c1 >= 'A' && c1 <= 'Z')
50 if (c2 >= 'A' && c2 <= 'Z')
61 return 0; // reached end of pattern but not end of input
65 // a little chained strings system
66 stringlist_t *stringlistappend(stringlist_t *current, char *text)
68 stringlist_t *newitem;
69 newitem = Z_Malloc(strlen(text) + 1 + sizeof(stringlist_t));
71 newitem->text = (char *)(newitem + 1);
72 strcpy(newitem->text, text);
74 current->next = newitem;
78 void stringlistfree(stringlist_t *current)
89 stringlist_t *stringlistsort(stringlist_t *start)
92 stringlist_t *current, *previous, *temp2, *temp3, *temp4;
99 while (current && current->next)
101 if (strcmp(current->text, current->next->text) > 0)
103 // current is greater than next
105 temp2 = current->next;
107 temp4 = current->next->next;
109 previous->next = temp2;
117 current = current->next;
123 // operating system specific code
126 stringlist_t *listdirectory(char *path)
128 char pattern[4096], *c;
129 struct _finddata_t n_file;
131 stringlist_t *start, *current;
132 strlcpy (pattern, path, sizeof (pattern));
133 strlcat (pattern, "\\*", sizeof (pattern));
134 // ask for the directory listing handle
135 hFile = _findfirst(pattern, &n_file);
138 // start a new chain with the the first name
139 start = current = stringlistappend(NULL, n_file.name);
140 // iterate through the directory
141 while (_findnext(hFile, &n_file) == 0)
142 current = stringlistappend(current, n_file.name);
145 // convert names to lowercase because windows does not care, but pattern matching code often does
146 for (current = start;current;current = current->next)
147 for (c = current->text;*c;c++)
148 if (*c >= 'A' && *c <= 'Z')
151 // sort the list alphanumerically
152 start = stringlistsort(start);
160 stringlist_t *listdirectory(char *path)
164 stringlist_t *start, *current;
174 start = current = stringlistappend(NULL, ent->d_name);
175 while((ent = readdir(dir)))
176 current = stringlistappend(current, ent->d_name);
178 // sort the list alphanumerically
179 return stringlistsort(start);
183 void freedirectory(stringlist_t *list)
185 stringlistfree(list);