+#ifdef WIN32
+#include <windows.h>
+#else
+#include <dirent.h>
+#endif
+
#include "quakedef.h"
// LordHavoc: some portable directory listing code I wrote for lmp2pcx, now used in darkplaces to load id1/*.pak and such...
return matchpattern_with_separator(in, pattern, caseinsensitive, "/\\:", false);
}
+// wildcard_least_one: if true * matches 1 or more characters
+// if false * matches 0 or more characters
int matchpattern_with_separator(const char *in, const char *pattern, int caseinsensitive, const char *separators, qboolean wildcard_least_one)
{
int c1, c2;
break;
case '*': // match anything until following string
if(wildcard_least_one)
+ {
if (*in == 0 || strchr(separators, *in))
return 0; // no match
- if (!*in)
- return 1; // match
+ in++;
+ }
pattern++;
while (*in)
{
list->numstrings++;
}
-void stringlistsort(stringlist_t *list)
+static int stringlistsort_cmp(const void *a, const void *b)
+{
+ return strcasecmp(*(const char **)a, *(const char **)b);
+}
+
+void stringlistsort(stringlist_t *list, qboolean uniq)
{
int i, j;
- char *temp;
- // this is a selection sort (finds the best entry for each slot)
- for (i = 0;i < list->numstrings - 1;i++)
+ if(list->numstrings < 1)
+ return;
+ qsort(&list->strings[0], list->numstrings, sizeof(list->strings[0]), stringlistsort_cmp);
+ if(uniq)
{
- for (j = i + 1;j < list->numstrings;j++)
+ // i: the item to read
+ // j: the item last written
+ for (i = 1, j = 0; i < list->numstrings; ++i)
{
- if (strcasecmp(list->strings[i], list->strings[j]) > 0)
- {
- temp = list->strings[i];
- list->strings[i] = list->strings[j];
- list->strings[j] = temp;
- }
+ char *save;
+ if(!strcasecmp(list->strings[i], list->strings[j]))
+ continue;
+ ++j;
+ save = list->strings[j];
+ list->strings[j] = list->strings[i];
+ list->strings[i] = save;
}
+ for(i = j+1; i < list->numstrings; ++i)
+ {
+ if (list->strings[i])
+ Z_Free(list->strings[i]);
+ }
+ list->numstrings = j+1;
}
}
}
}
#ifdef WIN32
-#include <windows.h>
void listdirectory(stringlist_t *list, const char *basepath, const char *path)
{
int i;
*c += 'a' - 'A';
}
#else
-#include <dirent.h>
void listdirectory(stringlist_t *list, const char *basepath, const char *path)
{
char fullpath[MAX_OSPATH];