remove duplicate names from file search results
authordivverent <divverent@d7cf8633-e32d-0410-b094-e92efae38249>
Sun, 12 Jun 2011 19:59:21 +0000 (19:59 +0000)
committerdivverent <divverent@d7cf8633-e32d-0410-b094-e92efae38249>
Sun, 12 Jun 2011 19:59:21 +0000 (19:59 +0000)
git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@11193 d7cf8633-e32d-0410-b094-e92efae38249

common.h
console.c
filematch.c
fs.c
menu.c

index e207c1a..bd715ed 100644 (file)
--- a/common.h
+++ b/common.h
@@ -329,7 +329,7 @@ int matchpattern_with_separator(const char *in, const char *pattern, int caseins
 void stringlistinit(stringlist_t *list);
 void stringlistfreecontents(stringlist_t *list);
 void stringlistappend(stringlist_t *list, const char *text);
-void stringlistsort(stringlist_t *list);
+void stringlistsort(stringlist_t *list, qboolean uniq);
 void listdirectory(stringlist_t *list, const char *basepath, const char *path);
 
 char *SearchInfostring(const char *infostring, const char *key);
index ae73809..bb9c277 100644 (file)
--- a/console.c
+++ b/console.c
@@ -2822,7 +2822,7 @@ void Con_CompleteCommandLine (void)
                                        }
                                        else
                                        {
-                                               stringlistsort(&resultbuf); // dirbuf is already sorted
+                                               stringlistsort(&resultbuf, true); // dirbuf is already sorted
                                                Con_Printf("\n%i possible filenames\n", resultbuf.numstrings + dirbuf.numstrings);
                                                for(i = 0; i < dirbuf.numstrings; ++i)
                                                {
index 4bacbc1..42f9f9d 100644 (file)
@@ -117,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;
        }
 }
 
diff --git a/fs.c b/fs.c
index ec93c09..d810ff5 100644 (file)
--- a/fs.c
+++ b/fs.c
@@ -1206,7 +1206,7 @@ void FS_AddGameDirectory (const char *dir)
 
        stringlistinit(&list);
        listdirectory(&list, "", dir);
-       stringlistsort(&list);
+       stringlistsort(&list, false);
 
        // add any PAK package in the directory
        for (i = 0;i < list.numstrings;i++)
@@ -1647,7 +1647,7 @@ static void FS_ListGameDirs(void)
        stringlistinit(&list);
        listdirectory(&list, va("%s/", fs_basedir), "");
        listdirectory(&list, va("%s/", fs_userdir), "");
-       stringlistsort(&list);
+       stringlistsort(&list, false);
 
        stringlistinit(&list2);
        for(i = 0; i < list.numstrings; ++i)
@@ -3512,7 +3512,7 @@ fssearch_t *FS_Search(const char *pattern, int caseinsensitive, int quiet)
 
        if (resultlist.numstrings)
        {
-               stringlistsort(&resultlist);
+               stringlistsort(&resultlist, true);
                numfiles = resultlist.numstrings;
                numchars = 0;
                for (resultlistindex = 0;resultlistindex < resultlist.numstrings;resultlistindex++)
@@ -3616,7 +3616,7 @@ int FS_ListDirectory(const char *pattern, int oneperline)
 static void FS_ListDirectoryCmd (const char* cmdname, int oneperline)
 {
        const char *pattern;
-       if (Cmd_Argc() > 3)
+       if (Cmd_Argc() >= 3)
        {
                Con_Printf("usage:\n%s [path/pattern]\n", cmdname);
                return;
diff --git a/menu.c b/menu.c
index 5df5370..c09abda 100644 (file)
--- a/menu.c
+++ b/menu.c
@@ -4515,7 +4515,7 @@ void ModList_RebuildList(void)
 
        stringlistinit(&list);
        listdirectory(&list, fs_basedir, "");
-       stringlistsort(&list);
+       stringlistsort(&list, true);
        modlist_count = 0;
        modlist_numenabled = fs_numgamedirs;
        for (i = 0;i < list.numstrings;i++)