]> git.rm.cloudns.org Git - xonotic/darkplaces.git/commitdiff
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)
committerRudolf Polzer <divverent@xonotic.org>
Mon, 13 Jun 2011 17:25:43 +0000 (19:25 +0200)
git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@11193 d7cf8633-e32d-0410-b094-e92efae38249
::stable-branch::merge=e97faa7f06e7821c32fb675cecb474e07ce55df6

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

index e207c1aa1765c6bb605f1c4964af97eec73e5865..bd715ed65eeb2cc9259b33bfabb116c5db959b6a 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 ae73809b9d8decedd2274775a520f37f270a88bb..bb9c277d7a7848429e6fd7f305c79d5166af2a4c 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 4bacbc15a1704029ff223c09b98a8c369bff8bf1..42f9f9dbcbeb8f3d0e31d64d6138a51112ddae9e 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 ec93c091cca705081e8242d86ab9bd12c0cc0b85..d810ff541f1b6e497dc6489150dd1f38415d4c57 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 5df5370fe419708336d411ef9e340adfbd57a78b..c09abda45607d87d3a67eb22f02c091db70456e1 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++)