changeset 1481:77a401044935 nuklear_ui

Fix directory navigation in ROM file chooser in Nuklear UI
author Michael Pavone <pavone@retrodev.com>
date Sat, 25 Nov 2017 13:57:38 -0800
parents 8464a3f09b94
children 2d203bf73dbd
files menu.c nuklear_ui/blastem_nuklear.c paths.c paths.h
diffstat 4 files changed, 52 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/menu.c	Sat Nov 25 11:31:08 2017 -0800
+++ b/menu.c	Sat Nov 25 13:57:38 2017 -0800
@@ -223,42 +223,10 @@
 		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:
--- a/nuklear_ui/blastem_nuklear.c	Sat Nov 25 11:31:08 2017 -0800
+++ b/nuklear_ui/blastem_nuklear.c	Sat Nov 25 13:57:38 2017 -0800
@@ -28,7 +28,9 @@
 	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 @@
 			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;
 			}
--- a/paths.c	Sat Nov 25 11:31:08 2017 -0800
+++ b/paths.c	Sat Nov 25 13:57:38 2017 -0800
@@ -101,3 +101,43 @@
 	*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);
+		}
+	}
+}
--- a/paths.h	Sat Nov 25 11:31:08 2017 -0800
+++ b/paths.h	Sat Nov 25 13:57:38 2017 -0800
@@ -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