diff util.c @ 955:229c23b3ab73

Switch to storing SRAM/EEPROM and save states in a per-game directory rather than next to the ROM (for SRAM/EEPROM) or in the current working directory (for save states)
author Michael Pavone <pavone@retrodev.com>
date Fri, 15 Apr 2016 18:29:39 -0700
parents 252dfd29831d
children 1618d3676a35
line wrap: on
line diff
--- a/util.c	Tue Apr 12 22:50:31 2016 -0700
+++ b/util.c	Fri Apr 15 18:29:39 2016 -0700
@@ -8,6 +8,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
+#include <errno.h>
 
 #ifdef __ANDROID__
 #include <android/log.h>
@@ -93,6 +94,32 @@
 	return text+1;
 }
 
+char * basename_no_extension(char *path)
+{
+	char *lastdot = NULL;
+	char *lastslash = NULL;
+	char *cur;
+	for (cur = path; *cur; cur++)
+	{
+		if (*cur == '.') {
+			lastdot = cur;
+		} else if (*cur == '/') {
+			lastslash = cur + 1;
+		}
+	}
+	if (!lastdot) {
+		lastdot = cur;
+	}
+	if (!lastslash) {
+		lastslash = path;
+	}
+	char *barename = malloc(lastdot-lastslash+1);
+	memcpy(barename, lastslash, lastdot-lastslash);
+	barename[lastdot-lastslash] = 0;
+	
+	return barename;
+}
+
 uint32_t nearest_pow2(uint32_t val)
 {
 	uint32_t ret = 1;
@@ -339,6 +366,32 @@
 	free(list);
 }
 
+int ensure_dir_exists(char *path)
+{
+	struct stat st;
+	if (stat(path, &st)) {
+		if (errno == ENOENT) {
+			char *parent = strdup(path);
+			char *sep = strrchr(parent, '/');
+			if (sep && sep != parent) {
+				*sep = 0;
+				if (!ensure_dir_exists(parent)) {
+					free(parent);
+					return 0;
+				}
+				free(parent);
+			}
+			return mkdir(path, 0777) == 0;
+		} else {
+			char buf[80];
+			strerror_r(errno, buf, sizeof(buf));
+			warning("stat failed with error: %s", buf);
+			return 0;
+		}
+	}
+	return S_ISDIR(st.st_mode);
+}
+
 #endif
 
 #ifdef __ANDROID__
@@ -377,6 +430,11 @@
 	return SDL_AndroidGetInternalStoragePath();
 }
 
+char const *get_save_dir()
+{
+	return SDL_AndroidGetInternalStoragePath();
+}
+
 #else
 
 char *read_bundled_file(char *name, long *sizeret)
@@ -430,4 +488,16 @@
 	return confdir;
 }
 
+char const *get_save_dir()
+{
+	static char* savedir;
+	if (!savedir) {
+		char *homedir = get_home_dir();
+		if (homedir) {
+			savedir = alloc_concat(homedir, "/.local/share/blastem");
+		}
+	}
+	return savedir;
+}
+
 #endif