From 2354afb9dcc8db2f33cf1ebcce2d90a012ace8a4 Mon Sep 17 00:00:00 2001 From: divverent Date: Sun, 12 Jun 2011 19:59:21 +0000 Subject: [PATCH] remove duplicate names from file search results git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@11193 d7cf8633-e32d-0410-b094-e92efae38249 ::stable-branch::merge=e97faa7f06e7821c32fb675cecb474e07ce55df6 --- common.h | 2 +- console.c | 2 +- filematch.c | 35 ++++++++++++++++++++++++----------- fs.c | 8 ++++---- menu.c | 2 +- 5 files changed, 31 insertions(+), 18 deletions(-) diff --git a/common.h b/common.h index e207c1aa..bd715ed6 100644 --- 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); diff --git a/console.c b/console.c index ae73809b..bb9c277d 100644 --- 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) { diff --git a/filematch.c b/filematch.c index 4bacbc15..42f9f9db 100644 --- a/filematch.c +++ b/filematch.c @@ -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 ec93c091..d810ff54 100644 --- 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 5df5370f..c09abda4 100644 --- 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++) -- 2.39.2