X-Git-Url: http://git.xonotic.org/?a=blobdiff_plain;f=filematch.c;h=997116766de56c1bf599543a16cb69dc0d9dfa21;hb=refs%2Fheads%2FMario%2Fflag_slide_fix;hp=9ff8a767514a39667afb4f3c867a15b014ad7d8e;hpb=b0aee41ce927ef164b1e02b466a293172402e77e;p=xonotic%2Fdarkplaces.git diff --git a/filematch.c b/filematch.c index 9ff8a767..99711676 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... @@ -111,22 +117,37 @@ 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++) + 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; } } @@ -141,7 +162,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; @@ -167,7 +187,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];