tagtree cleanup first letter menu code

no functional difference just some code shared and trimming of
unnecessary format strings

Change-Id: Ia6165508556790ea825e630a79946fbd2dafd9c1
This commit is contained in:
William Wilgus 2024-11-03 13:40:05 -05:00 committed by William Wilgus
parent f3026cd072
commit d60dee6188

View file

@ -191,19 +191,12 @@ struct menu_entry {
}; };
struct menu_root { struct menu_root {
char title[64]; char title[MENUENTRY_MAX_NAME];
char id[MAX_MENU_ID_SIZE]; char id[MAX_MENU_ID_SIZE];
int itemcount; int itemcount;
struct menu_entry *items[TAGMENU_MAX_ITEMS]; struct menu_entry *items[TAGMENU_MAX_ITEMS];
}; };
struct match
{
const char* str;
uint16_t len;
uint16_t symbol;
};
/* Statusbar text of the current view. */ /* Statusbar text of the current view. */
static char current_title[MAX_TAGS][128]; static char current_title[MAX_TAGS][128];
@ -355,12 +348,13 @@ static int get_token_str(char *buf, int size)
static int get_tag(int *tag) static int get_tag(int *tag)
{ {
#define TAG_MATCH(str, tag) {str, sizeof(str) - 1, tag} #define TAG_MATCH(str, tag) {str, sizeof(str) - 1, tag}
struct match {const char* str; uint16_t len; uint16_t symbol;};
static const struct match get_tag_match[] = static const struct match get_tag_match[] =
{ {
TAG_MATCH("Lm", tag_virt_length_min), TAG_MATCH("lm", tag_virt_length_min),
TAG_MATCH("Ls", tag_virt_length_sec), TAG_MATCH("ls", tag_virt_length_sec),
TAG_MATCH("Pm", tag_virt_playtime_min), TAG_MATCH("pm", tag_virt_playtime_min),
TAG_MATCH("Ps", tag_virt_playtime_sec), TAG_MATCH("ps", tag_virt_playtime_sec),
TAG_MATCH("->", menu_next), TAG_MATCH("->", menu_next),
TAG_MATCH("~>", menu_shuffle_songs), TAG_MATCH("~>", menu_shuffle_songs),
@ -383,7 +377,6 @@ static int get_tag(int *tag)
TAG_MATCH("comment", tag_comment), TAG_MATCH("comment", tag_comment),
TAG_MATCH("discnum", tag_discnumber), TAG_MATCH("discnum", tag_discnumber),
TAG_MATCH("%format", var_format), TAG_MATCH("%format", var_format),
TAG_MATCH("%byfirstletter", menu_byfirstletter),
TAG_MATCH("%reload", menu_reload), TAG_MATCH("%reload", menu_reload),
TAG_MATCH("filename", tag_filename), TAG_MATCH("filename", tag_filename),
@ -407,13 +400,15 @@ static int get_tag(int *tag)
TAG_MATCH("lastelapsed", tag_lastelapsed), TAG_MATCH("lastelapsed", tag_lastelapsed),
TAG_MATCH("%menu_start", var_menu_start), TAG_MATCH("%menu_start", var_menu_start),
TAG_MATCH("%byfirstletter", menu_byfirstletter),
TAG_MATCH("canonicalartist", tag_virt_canonicalartist), TAG_MATCH("canonicalartist", tag_virt_canonicalartist),
TAG_MATCH("", 0) /* sentinel */ TAG_MATCH("", 0) /* sentinel */
}; };
#undef TAG_MATCH #undef TAG_MATCH
const size_t max_cmd_sz = 32; /* needs to be >= to len of longest tagstr */ const size_t max_cmd_sz = 32; /* needs to be >= to len of longest tagstr */
const char *tagstr; const char *tagstr;
unsigned int tagstr_len; uint16_t tagstr_len;
const struct match *match; const struct match *match;
/* Find the start. */ /* Find the start. */
@ -1059,10 +1054,11 @@ int tagtree_import(void)
return 0; return 0;
} }
static bool parse_menu(const char *filename); static bool alloc_menu_parse_buf(char *buf, int type)
static bool alloc_menu_item(void)
{ {
/* allocate a new menu item (if needed) initialize it with data parsed
from buf Note: allows setting menu type, type ignored when < 0
*/
/* Allocate */ /* Allocate */
if (menu->items[menu->itemcount] == NULL) if (menu->items[menu->itemcount] == NULL)
menu->items[menu->itemcount] = tagtree_alloc0(sizeof(struct menu_entry)); menu->items[menu->itemcount] = tagtree_alloc0(sizeof(struct menu_entry));
@ -1071,18 +1067,17 @@ static bool alloc_menu_item(void)
logf("tagtree failed to allocate %s", "menu items"); logf("tagtree failed to allocate %s", "menu items");
return false; return false;
} }
return true;
}
static void firstletter_parse_buf(char *buf) /* Initialize */
{
core_pin(tagtree_handle); core_pin(tagtree_handle);
if (parse_search(menu->items[menu->itemcount], buf)) if (parse_search(menu->items[menu->itemcount], buf))
{ {
menu->items[menu->itemcount]->type = menu_byfirstletter; if (type >= 0)
menu->items[menu->itemcount]->type = type;
menu->itemcount++; menu->itemcount++;
} }
core_unpin(tagtree_handle);; core_unpin(tagtree_handle);
return true;
} }
static void build_firstletter_menu(char *buf, size_t bufsz) static void build_firstletter_menu(char *buf, size_t bufsz)
@ -1092,32 +1087,32 @@ static void build_firstletter_menu(char *buf, size_t bufsz)
buf+=l; buf+=l;
bufsz-=l; bufsz-=l;
const char *showalbum = ""; const char * const fmt ="\"%s\"-> %s ? %s %c\"%c\"-> %s =\"fmt_title\"";
const char * const fmt ="\"%s\" -> %s ? %s %c \"%c\" -> %s title = \"fmt_title\""; const char * const showsub = /* album subitem for canonicalartist */
if (!alloc_menu_item()) ((strcasestr(subitem, "artist") == NULL) ? "title" : "album -> title");
return;
if (strcasestr(subitem, "artist") != NULL)
showalbum = "album ->"; /* album subitem for canonicalartist */
/* Numeric ex: "Numeric" -> album ? album < "A" -> title = "fmt_title" */ /* Numeric ex: "Numeric" -> album ? album < "A" -> title = "fmt_title" */
snprintf(buf, bufsz, fmt, snprintf(buf, bufsz, fmt,
str(LANG_DISPLAY_NUMERIC), subitem, subitem,'<', 'A', showalbum); str(LANG_DISPLAY_NUMERIC), subitem, subitem,'<', 'A', showsub);
firstletter_parse_buf(buf); if (!alloc_menu_parse_buf(buf, menu_byfirstletter))
{
return;
}
for (int i = 0; i < 26; i++) for (int i = 0; i < 26; i++)
{ {
if (!alloc_menu_item()) snprintf(buf, bufsz, fmt, "#", subitem, subitem,'^', 'A' + i, showsub);
return;
snprintf(buf, bufsz, fmt, "#", subitem, subitem,'^', 'A' + i, showalbum);
buf[1] = 'A' + i; /* overwrite the placeholder # with the current letter */ buf[1] = 'A' + i; /* overwrite the placeholder # with the current letter */
/* ex: "A" -> title ? title ^ "A" -> title = "fmt_title" */ /* ex: "A" -> title ? title ^ "A" -> title = "fmt_title" */
firstletter_parse_buf(buf); if (!alloc_menu_parse_buf(buf, menu_byfirstletter))
{
return;
}
} }
} }
static bool parse_menu(const char *filename);
static int parse_line(int n, char *buf, void *parameters) static int parse_line(int n, char *buf, void *parameters)
{ {
char data[256]; char data[256];
@ -1275,12 +1270,10 @@ static int parse_line(int n, char *buf, void *parameters)
return 0; return 0;
} }
if (!alloc_menu_item()) if (!alloc_menu_parse_buf(buf, -1))
{
return -2; return -2;
core_pin(tagtree_handle); }
if (parse_search(menu->items[menu->itemcount], buf))
menu->itemcount++;
core_unpin(tagtree_handle);
return 0; return 0;
} }