Mercurial > repos > blastem
changeset 1485:369da70ee2c2 nuklear_ui
Filter file list in Nuklear UI
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Sun, 26 Nov 2017 18:33:36 -0800 |
parents | d82af64c94d2 |
children | a6881d0d76d0 |
files | config.c config.h menu.c nuklear_ui/blastem_nuklear.c util.c util.h |
diffstat | 6 files changed, 57 insertions(+), 26 deletions(-) [+] |
line wrap: on
line diff
--- a/config.c Sun Nov 26 17:33:39 2017 -0800 +++ b/config.c Sun Nov 26 18:33:36 2017 -0800 @@ -155,3 +155,23 @@ //this will never get reached, but the compiler doesn't know that. Let's make it happy return NULL; } + +char **get_extension_list(tern_node *config, uint32_t *num_exts_out) +{ + char *ext_filter = strdup(tern_find_path_default(config, "ui\0extensions\0", (tern_val){.ptrval = "bin gen md smd sms gg"}, TVAL_PTR).ptrval); + uint32_t num_exts = 0, ext_storage = 5; + char **ext_list = malloc(sizeof(char *) * ext_storage); + char *cur_filter = ext_filter; + while (*cur_filter) + { + if (num_exts == ext_storage) { + ext_storage *= 2; + ext_list = realloc(ext_list, sizeof(char *) * ext_storage); + } + ext_list[num_exts++] = cur_filter; + cur_filter = split_keyval(cur_filter); + } + free(ext_filter); + *num_exts_out = num_exts; + return ext_list; +}
--- a/config.h Sun Nov 26 17:33:39 2017 -0800 +++ b/config.h Sun Nov 26 18:33:36 2017 -0800 @@ -10,6 +10,7 @@ tern_node *parse_config_file(char *config_path); tern_node *parse_bundled_config(char *config_name); tern_node *load_config(); +char **get_extension_list(tern_node *config, uint32_t *num_exts_out); #endif //CONFIG_H_
--- a/menu.c Sun Nov 26 17:33:39 2017 -0800 +++ b/menu.c Sun Nov 26 18:33:36 2017 -0800 @@ -10,6 +10,7 @@ #include "gst.h" #include "paths.h" #include "saves.h" +#include "config.h" static menu_context *get_menu(genesis_context *gen) { @@ -166,40 +167,17 @@ dst = copy_dir_entry_to_guest(dst, m68k, "..", 1); } #endif - char *ext_filter = strdup(tern_find_path_default(config, "ui\0extensions\0", (tern_val){.ptrval = "bin gen md smd sms gg"}, TVAL_PTR).ptrval); - uint32_t num_exts = 0, ext_storage = 5; - char **ext_list = malloc(sizeof(char *) * ext_storage); - char *cur_filter = ext_filter; - while (*cur_filter) - { - if (num_exts == ext_storage) { - ext_storage *= 2; - ext_list = realloc(ext_list, sizeof(char *) * ext_storage); - } - ext_list[num_exts++] = cur_filter; - cur_filter = split_keyval(cur_filter); - } + uint32_t num_exts; + char **ext_list = get_extension_list(config, &num_exts); for (size_t i = 0; dst && i < num_entries; i++) { if (num_exts && !entries[i].is_dir) { - char *ext = path_extension(entries[i].name); - if (!ext) { - continue; - } - uint32_t extidx; - for (extidx = 0; extidx < num_exts; extidx++) - { - if (!strcasecmp(ext, ext_list[extidx])) { - break; - } - } - if (extidx == num_exts) { + if (!path_matches_extensions(entries[i].name, ext_list, num_exts)) { continue; } } dst = copy_dir_entry_to_guest(dst, m68k, entries[i].name, entries[i].is_dir); } - free(ext_filter); free(ext_list); //terminate list uint8_t *dest = get_native_pointer(dst, (void **)m68k->mem_pointers, &m68k->options->gen);
--- a/nuklear_ui/blastem_nuklear.c Sun Nov 26 17:33:39 2017 -0800 +++ b/nuklear_ui/blastem_nuklear.c Sun Nov 26 18:33:36 2017 -0800 @@ -10,6 +10,7 @@ #include "../paths.h" #include "../saves.h" #include "../blastem.h" +#include "../config.h" static struct nk_context *context; @@ -28,6 +29,9 @@ static dir_entry *entries; static size_t num_entries; static uint32_t selected_entry; + static char **ext_list; + static uint32_t num_exts; + static uint8_t got_ext_list; if (!current_path) { get_initial_browse_path(¤t_path); } @@ -37,6 +41,10 @@ sort_dir_list(entries, num_entries); } } + if (!got_ext_list) { + ext_list = get_extension_list(config, &num_exts); + got_ext_list = 1; + } uint32_t width = render_width(); uint32_t height = render_height(); if (nk_begin(context, "Load ROM", nk_rect(0, 0, width, height), 0)) { @@ -45,6 +53,12 @@ nk_layout_row_static(context, 28, width-100, 1); for (uint32_t i = 0; i < num_entries; i++) { + if (entries[i].name[0] == '.' && entries[i].name[1] != '.') { + continue; + } + if (num_exts && !entries[i].is_dir && !path_matches_extensions(entries[i].name, ext_list, num_exts)) { + continue; + } int selected = i == selected_entry; nk_selectable_label(context, entries[i].name, NK_TEXT_ALIGN_LEFT, &selected); if (selected) {
--- a/util.c Sun Nov 26 17:33:39 2017 -0800 +++ b/util.c Sun Nov 26 18:33:36 2017 -0800 @@ -270,6 +270,22 @@ return strdup(lastdot+1); } +uint8_t path_matches_extensions(char *path, char **ext_list, uint32_t num_exts) +{ + char *ext = path_extension(path); + if (!ext) { + return 0; + } + uint32_t extidx; + for (extidx = 0; extidx < num_exts; extidx++) + { + if (!strcasecmp(ext, ext_list[extidx])) { + return 1; + } + } + return 0; +} + char * path_dirname(char *path) { char *lastslash = NULL;
--- a/util.h Sun Nov 26 17:33:39 2017 -0800 +++ b/util.h Sun Nov 26 18:33:36 2017 -0800 @@ -42,6 +42,8 @@ char * basename_no_extension(char *path); //Returns the extension from a path or NULL if there is no extension char *path_extension(char *path); +//Returns true if the given path matches one of the extensions in the list +uint8_t path_matches_extensions(char *path, char **ext_list, uint32_t num_exts); //Returns the directory portion of a path or NULL if there is no directory part char *path_dirname(char *path); //Gets the smallest power of two that is >= a certain value, won't work for values > 0x80000000