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);
}
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)
{
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;
}
}
stringlistinit(&list);
listdirectory(&list, "", dir);
- stringlistsort(&list);
+ stringlistsort(&list, false);
// add any PAK package in the directory
for (i = 0;i < list.numstrings;i++)
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)
if (resultlist.numstrings)
{
- stringlistsort(&resultlist);
+ stringlistsort(&resultlist, true);
numfiles = resultlist.numstrings;
numchars = 0;
for (resultlistindex = 0;resultlistindex < resultlist.numstrings;resultlistindex++)
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;
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++)