tagtree.c move sort_inverse out of compare function

Change-Id: I9c91a96a45fe98d51d27974bc2d6d79eaa5039d3
This commit is contained in:
William Wilgus 2022-11-22 19:14:26 -05:00
parent 2a9482ad7c
commit 263cc13985

View file

@ -117,7 +117,23 @@ static uint32_t uniqbuf[UNIQBUF_SIZE / sizeof(uint32_t)];
#define MAX_MENU_ID_SIZE 32 #define MAX_MENU_ID_SIZE 32
#define RELOAD_TAGTREE (-1024) #define RELOAD_TAGTREE (-1024)
static bool sort_inverse;
static int(*qsort_fn)(const char*, const char*, size_t);
/* dummmy functions to allow compatibility strncasecmp */
static int strnatcasecmp_n(const char *a, const char *b, size_t n)
{
(void)n;
return strnatcasecmp(a, b);
}
static int strnatcasecmp_n_inv(const char *a, const char *b, size_t n)
{
(void)n;
return strnatcasecmp(b, a);
}
static int strncasecmp_inv(const char *a, const char *b, size_t n)
{
return strncasecmp(b, a, n);
}
/* /*
* "%3d. %s" autoscore title %sort = "inverse" %limit = "100" * "%3d. %s" autoscore title %sort = "inverse" %limit = "100"
@ -824,22 +840,7 @@ static int compare(const void *p1, const void *p2)
{ {
struct tagentry *e1 = (struct tagentry *)p1; struct tagentry *e1 = (struct tagentry *)p1;
struct tagentry *e2 = (struct tagentry *)p2; struct tagentry *e2 = (struct tagentry *)p2;
return qsort_fn(e1->name, e2->name, MAX_PATH);
if (sort_inverse)
return strncasecmp(e2->name, e1->name, MAX_PATH);
return strncasecmp(e1->name, e2->name, MAX_PATH);
}
static int nat_compare(const void *p1, const void *p2)
{
struct tagentry *e1 = (struct tagentry *)p1;
struct tagentry *e2 = (struct tagentry *)p2;
if (sort_inverse)
return strnatcasecmp(e2->name, e1->name);
return strnatcasecmp(e1->name, e2->name);
} }
static void tagtree_buffer_event(unsigned short id, void *ev_data) static void tagtree_buffer_event(unsigned short id, void *ev_data)
@ -1428,6 +1429,7 @@ static int retrieve_entries(struct tree_context *c, int offset, bool init)
int level = c->currextra; int level = c->currextra;
int tag; int tag;
bool sort = false; bool sort = false;
bool sort_inverse;
int sort_limit; int sort_limit;
int strip; int strip;
@ -1667,11 +1669,16 @@ entry_skip_formatter:
if (sort) if (sort)
{ {
if (global_settings.interpret_numbers)
qsort_fn = sort_inverse ? strnatcasecmp_n_inv : strnatcasecmp_n;
else
qsort_fn = sort_inverse ? strncasecmp_inv : strncasecmp;
struct tagentry *entries = get_entries(c); struct tagentry *entries = get_entries(c);
qsort(&entries[special_entry_count], qsort(&entries[special_entry_count],
current_entry_count - special_entry_count, current_entry_count - special_entry_count,
sizeof(struct tagentry), sizeof(struct tagentry),
global_settings.interpret_numbers ? nat_compare : compare); compare);
} }
if (!init) if (!init)