Skip to content

Commit

Permalink
Handle integer sequences when comparing file names
Browse files Browse the repository at this point in the history
e.g. "d1m9" should come before "d1m10"
  • Loading branch information
andrei-drexler committed Oct 24, 2022
1 parent 7b769ca commit 6c29de2
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions Quake/host_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include "quakedef.h"
#include "q_ctype.h"

extern cvar_t pausable;
extern cvar_t nomonsters;
Expand Down Expand Up @@ -52,6 +53,43 @@ void Host_Quit_f (void)
//johnfitz -- extramaps management
//==============================================================================

/*
==================
FileList_Compare
Case-insensitive string comparison that also handles integer sequences
(e.g. d1m9 comes before d1m10)
==================
*/
static int FileList_Compare (const char *s1, const char *s2)
{
if (s1 == s2)
return 0;

skip_prefix:
while (*s1 && !q_isdigit (*s1) && q_toupper (*s1) == q_toupper (*s2))
{
s1++;
s2++;
continue;
}

if (q_isdigit (*s1) && q_isdigit (*s2))
{
unsigned int num1 = *s1++ - '0';
unsigned int num2 = *s2++ - '0';
while (q_isdigit (*s1))
num1 = num1 * 10 + (*s1++ - '0');
while (q_isdigit (*s2))
num2 = num2 * 10 + (*s2++ - '0');
if (num1 != num2)
return num1 < num2 ? -1 : 1;
goto skip_prefix;
}

return q_toupper (*s1) - q_toupper (*s2);
}

/*
==================
FileList_AddWithData
Expand All @@ -75,7 +113,7 @@ static void FileList_AddWithData (const char *name, const void *data, size_t dat

// insert each entry in alphabetical order
if (*list == NULL ||
q_strcasecmp(item->name, (*list)->name) < 0) //insert at front
FileList_Compare (item->name, (*list)->name) < 0) //insert at front
{
item->next = *list;
*list = item;
Expand All @@ -84,7 +122,7 @@ static void FileList_AddWithData (const char *name, const void *data, size_t dat
{
prev = *list;
cursor = (*list)->next;
while (cursor && (q_strcasecmp(item->name, cursor->name) > 0))
while (cursor && (FileList_Compare (item->name, cursor->name) > 0))
{
prev = cursor;
cursor = cursor->next;
Expand Down

0 comments on commit 6c29de2

Please sign in to comment.