]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - filematch.c
win32: add two includes to make sure the constants for file mode and sharing are...
[xonotic/darkplaces.git] / filematch.c
index 9ff8a767514a39667afb4f3c867a15b014ad7d8e..997116766de56c1bf599543a16cb69dc0d9dfa21 100644 (file)
@@ -1,4 +1,10 @@
 
+#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...
@@ -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 <windows.h>
 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 <dirent.h>
 void listdirectory(stringlist_t *list, const char *basepath, const char *path)
 {
        char fullpath[MAX_OSPATH];