summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Pavone <pavone@retrodev.com>2017-11-25 13:57:38 -0800
committerMichael Pavone <pavone@retrodev.com>2017-11-25 13:57:38 -0800
commit2900092e4e06be77108ed6b8a66c7aa673d0b494 (patch)
tree0dea1d08e10839ff4403ca27123b51cda6878e64
parent01cfcb11d24235ba7b3a25c05ce1bdabe5b3015d (diff)
Fix directory navigation in ROM file chooser in Nuklear UI
--HG-- branch : nuklear_ui
-rw-r--r--menu.c40
-rw-r--r--nuklear_ui/blastem_nuklear.c13
-rw-r--r--paths.c40
-rw-r--r--paths.h1
4 files changed, 52 insertions, 42 deletions
diff --git a/menu.c b/menu.c
index 969b44a..b789d23 100644
--- a/menu.c
+++ b/menu.c
@@ -223,42 +223,10 @@ void * menu_write_w(uint32_t address, void * context, uint16_t value)
case 1: {
char buf[4096];
copy_string_from_guest(m68k, dst, buf, sizeof(buf));
- if (!strcmp(buf, "..")) {
-#ifdef _WIN32
- if (menu->curpath[1] == ':' && !menu->curpath[2]) {
- menu->curpath[0] = PATH_SEP[0];
- menu->curpath[1] = 0;
- break;
- }
-#endif
- size_t len = strlen(menu->curpath);
- while (len > 0) {
- --len;
- if (is_path_sep(menu->curpath[len])) {
- if (!len) {
- //special handling for /
- menu->curpath[len+1] = 0;
- } else {
- menu->curpath[len] = 0;
- }
- break;
- }
- }
- } else {
- char *tmp = menu->curpath;
-#ifdef _WIN32
- if (menu->curpath[0] == PATH_SEP[0] && !menu->curpath[1]) {
- menu->curpath = strdup(buf);
- } else
-#endif
- if (is_path_sep(menu->curpath[strlen(menu->curpath) - 1])) {
- menu->curpath = alloc_concat(menu->curpath, buf);
- } else {
- char const *pieces[] = {menu->curpath, PATH_SEP, buf};
- menu->curpath = alloc_concat_m(3, pieces);
- }
- free(tmp);
- }
+ buf[sizeof(buf)-1] = 0;
+ char *tmp = menu->curpath;
+ menu->curpath = path_append(tmp, buf);
+ free(tmp);
break;
}
case 2:
diff --git a/nuklear_ui/blastem_nuklear.c b/nuklear_ui/blastem_nuklear.c
index a486a64..bcff610 100644
--- a/nuklear_ui/blastem_nuklear.c
+++ b/nuklear_ui/blastem_nuklear.c
@@ -28,7 +28,9 @@ void view_load(struct nk_context *context)
static dir_entry *entries;
static size_t num_entries;
static uint32_t selected_entry;
- get_initial_browse_path(&current_path);
+ if (!current_path) {
+ get_initial_browse_path(&current_path);
+ }
if (!entries) {
entries = get_dir_list(current_path, &num_entries);
}
@@ -53,15 +55,14 @@ void view_load(struct nk_context *context)
current_view = previous_view;
}
if (nk_button_label(context, "Open")) {
- char const *pieces[] = {current_path, PATH_SEP, entries[selected_entry].name};
+ char *full_path = path_append(current_path, entries[selected_entry].name);
if (entries[selected_entry].is_dir) {
- char *old = current_path;
- current_path = alloc_concat_m(3, pieces);
- free(old);
+ free(current_path);
+ current_path = full_path;
free_dir_list(entries, num_entries);
entries = NULL;
} else {
- current_system->next_rom = alloc_concat_m(3, pieces);
+ current_system->next_rom = full_path;
current_system->request_exit(current_system);
current_view = view_play;
}
diff --git a/paths.c b/paths.c
index c3c4ad9..9f7afbd 100644
--- a/paths.c
+++ b/paths.c
@@ -101,3 +101,43 @@ void get_initial_browse_path(char **dst)
*dst = replace_vars(*dst, vars, 1);
tern_free(vars);
}
+
+char *path_append(char *base, char *suffix)
+{
+ if (!strcmp(suffix, "..")) {
+#ifdef _WIN32
+ //handle transition from root of a drive to virtual root
+ if (base[1] == ':' && !base[2]) {
+ return strdup(PATH_SEP)
+ }
+#endif
+ size_t len = strlen(base);
+ while (len > 0) {
+ --len;
+ if (is_path_sep(base[len])) {
+ if (!len) {
+ //special handling for /
+ len++;
+ }
+ char *ret = malloc(len+1);
+ memcpy(ret, base, len);
+ ret[len] = 0;
+ return ret;
+ }
+ }
+ return strdup(PATH_SEP);
+ } else {
+#ifdef _WIN32
+ if (base[0] == PATH_SEP[0] && !base[1]) {
+ //handle transition from virtual root to root of a drive
+ return strdup(suffix);
+ }
+#endif
+ if (is_path_sep(base[strlen(base) - 1])) {
+ return alloc_concat(base, suffix);
+ } else {
+ char const *pieces[] = {base, PATH_SEP, suffix};
+ return alloc_concat_m(3, pieces);
+ }
+ }
+}
diff --git a/paths.h b/paths.h
index 77e878d..da4b424 100644
--- a/paths.h
+++ b/paths.h
@@ -2,5 +2,6 @@
#define PATHS_H_
void get_initial_browse_path(char **dst);
+char *path_append(char *base, char *suffix);
#endif //PATHS_H_ \ No newline at end of file