summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorMichael Pavone <pavone@retrodev.com>2016-04-15 18:29:39 -0700
committerMichael Pavone <pavone@retrodev.com>2016-04-15 18:29:39 -0700
commit470800d02a97ca852a37efe81e97af9e4a0e4a05 (patch)
tree9f0dd3cecca09efb77996ae2ed166e7aa600bb51 /util.c
parentdb0f3ca459cc270e8048c8e554c3051b4014de91 (diff)
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)
Diffstat (limited to 'util.c')
-rw-r--r--util.c70
1 files changed, 70 insertions, 0 deletions
diff --git a/util.c b/util.c
index 57a59a2..9094223 100644
--- a/util.c
+++ b/util.c
@@ -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 @@ char * split_keyval(char * text)
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 @@ void free_dir_list(dir_entry *list, size_t numentries)
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 @@ char const *get_config_dir()
return SDL_AndroidGetInternalStoragePath();
}
+char const *get_save_dir()
+{
+ return SDL_AndroidGetInternalStoragePath();
+}
+
#else
char *read_bundled_file(char *name, long *sizeret)
@@ -430,4 +488,16 @@ char const *get_config_dir()
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