changeset 2262:bc68560b4a04

Fix bug when loading cue sheet without leading path
author Michael Pavone <pavone@retrodev.com>
date Fri, 23 Dec 2022 06:23:41 -0800
parents 86dfcf3f418a
children a98b2d0de2f1
files blastem.c paths.c paths.h
diffstat 3 files changed, 45 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/blastem.c	Fri Dec 23 05:49:04 2022 -0800
+++ b/blastem.c	Fri Dec 23 06:23:41 2022 -0800
@@ -23,6 +23,7 @@
 #include "gdb_remote.h"
 #include "gst.h"
 #include "util.h"
+#include "paths.h"
 #include "romdb.h"
 #include "terminal.h"
 #include "arena.h"
@@ -167,6 +168,9 @@
 					}
 					dst->extension = ext;
 					dst->dir = path_dirname(filename);
+					if (!dst->dir) {
+						dst->dir = path_current_dir();
+					}
 					dst->name = basename_no_extension(filename);
 					dst->size = out_size;
 					zip_close(z);
@@ -253,6 +257,9 @@
 		ret = (uint32_t)readsize;
 	}
 	dst->dir = path_dirname(filename);
+	if (!dst->dir) {
+		dst->dir = path_current_dir();
+	}
 	dst->name = basename_no_extension(filename);
 	dst->extension = ext;
 	dst->size = ret;
--- a/paths.c	Fri Dec 23 05:49:04 2022 -0800
+++ b/paths.c	Fri Dec 23 06:23:41 2022 -0800
@@ -2,6 +2,12 @@
 #include <stdlib.h>
 #include "blastem.h"
 #include "util.h"
+#ifdef _WIN32
+#include <windows.h>
+#else
+#include <unistd.h>
+#include <errno.h>
+#endif
 
 static char **current_path;
 
@@ -140,3 +146,34 @@
 		}
 	}
 }
+
+char *path_current_dir(void)
+{
+	size_t size = 128;
+	char *res = malloc(size);
+	for (;;) {
+#ifdef _WIN32
+		DWORD actual = GetCurrentDirectoryA(size, res);
+		if (actual > size) {
+			res = realloc(res, actual);
+			size = actual;
+		} else {
+			return res;
+		}
+#else
+		errno = 0;
+		char *tmp = getcwd(res, size);
+		if (!tmp) {
+			if (errno == ERANGE) {
+				size *= 2;
+				res = realloc(res, size);
+			} else {
+				free(res);
+				return NULL;
+			}
+		} else {
+			return res;
+		}
+#endif
+	}
+}
--- a/paths.h	Fri Dec 23 05:49:04 2022 -0800
+++ b/paths.h	Fri Dec 23 06:23:41 2022 -0800
@@ -3,5 +3,6 @@
 
 void get_initial_browse_path(char **dst);
 char *path_append(const char *base, const char *suffix);
+char *path_current_dir(void);
 
 #endif //PATHS_H_
\ No newline at end of file