X-Git-Url: http://de.git.xonotic.org/?a=blobdiff_plain;f=filematch.c;h=42f9f9dbcbeb8f3d0e31d64d6138a51112ddae9e;hb=7c586d061e7308e0e8164827fa0e14b470921d67;hp=c88c704e309057386b673d4221a8bc815f6f6e97;hpb=31c60f86f584447beac6b654e54587229f2c9496;p=xonotic%2Fdarkplaces.git diff --git a/filematch.c b/filematch.c index c88c704e..42f9f9db 100644 --- a/filematch.c +++ b/filematch.c @@ -1,4 +1,10 @@ +#ifdef WIN32 +#include +#else +#include +#endif + #include "quakedef.h" // LordHavoc: some portable directory listing code I wrote for lmp2pcx, now used in darkplaces to load id1/*.pak and such... @@ -8,6 +14,8 @@ int matchpattern(const char *in, const char *pattern, int caseinsensitive) 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; @@ -25,17 +33,18 @@ int matchpattern_with_separator(const char *in, const char *pattern, int caseins 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) { if (strchr(separators, *in)) break; // see if pattern matches at this offset - if (matchpattern(in, pattern, caseinsensitive)) + if (matchpattern_with_separator(in, pattern, caseinsensitive, separators, wildcard_least_one)) return 1; // nope, advance to next offset in++; @@ -108,22 +117,35 @@ void stringlistappend(stringlist_t *list, const char *text) 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++) + 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; } } @@ -138,7 +160,6 @@ static void adddirentry(stringlist_t *list, const char *path, const char *name) } } #ifdef WIN32 -#include void listdirectory(stringlist_t *list, const char *basepath, const char *path) { int i; @@ -164,7 +185,6 @@ void listdirectory(stringlist_t *list, const char *basepath, const char *path) *c += 'a' - 'A'; } #else -#include void listdirectory(stringlist_t *list, const char *basepath, const char *path) { char fullpath[MAX_OSPATH];