From 6b76a9a0882ecc5f4568c6b6ae1c91ded31dd34e Mon Sep 17 00:00:00 2001 From: sorgelig Date: Fri, 11 Jan 2019 11:23:49 +0800 Subject: [PATCH] Use separate save folder for all saves. --- file_io.cpp | 48 +++++++++++++++++++++++------------------------- file_io.h | 4 +++- user_io.cpp | 2 +- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/file_io.cpp b/file_io.cpp index 9eba0b8..18d21c7 100644 --- a/file_io.cpp +++ b/file_io.cpp @@ -62,7 +62,7 @@ static bool FileIsZipped(char* path, char** zip_path, char** file_path) return false; } -static char* make_fullpath(char *path, int mode = 0) +static char* make_fullpath(const char *path, int mode = 0) { const char *root = getRootDir(); if (strncasecmp(getRootDir(), path, strlen(root))) @@ -644,12 +644,6 @@ int FileWriteSec(fileTYPE *file, void *pBuffer) int FileSave(const char *name, void *pBuffer, int size) { sprintf(full_path, "%s/%s", getRootDir(), name); - if (!FileCanWrite(name)) - { - printf("FileSave(FileCanWrite) File:%s, not writable.\n", full_path); - return 0; - } - int fd = open(full_path, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRWXU | S_IRWXG | S_IRWXO); if (fd < 0) { @@ -741,34 +735,38 @@ int FileCanWrite(const char *name) return ((st.st_mode & S_IWUSR) != 0); } -void FileGenerateSavePath(const char *name, const char* extension, char* out_name, int length) +void FileGenerateSavePath(const char *name, char* out_name) { - const char *d = strrchr(name, '.'); - if (d) + make_fullpath(SAVE_DIR); + mkdir(full_path, S_IRWXU | S_IRWXG | S_IRWXO); + strcat(full_path, "/"); + strcat(full_path, HomeDir); + mkdir(full_path, S_IRWXU | S_IRWXG | S_IRWXO); + + sprintf(out_name, "%s/%s/", SAVE_DIR, HomeDir); + char *fname = out_name + strlen(out_name); + + const char *p = strrchr(name, '/'); + if (p) { - const int l = MIN(d - name, length); - strncpy(out_name, name, l); - out_name[l] = '\0'; + strcat(fname, p+1); } else { - strncpy(out_name, name, length); + strcat(fname, name); } - char *z = strcasestr(out_name, ".zip"); - if (z) + char *e = strrchr(fname, '.'); + if (e) { - // Remove '.' from '.zip' so file logic won't think - // the file has been compressed. - *z = '-'; - for (char *p = z; (p = strchr(p, '/')); ) - { - *p = '-'; - } + strcpy(e,".sav"); + } + else + { + strcat(fname, ".sav"); } - strncat(out_name, ".", length); - strncat(out_name, extension, length); + printf("SavePath=%s\n", out_name); } uint32_t getFileType(const char *name) diff --git a/file_io.h b/file_io.h index 580684f..79f098b 100644 --- a/file_io.h +++ b/file_io.h @@ -68,7 +68,9 @@ int FileWriteAdv(fileTYPE *file, void *pBuffer, int length); int FileWriteSec(fileTYPE *file, void *pBuffer); int FileCanWrite(const char *name); -void FileGenerateSavePath(const char *name, const char* extension, char* out_name, int length); + +#define SAVE_DIR "saves" +void FileGenerateSavePath(const char *name, char* out_name); int FileSave(const char *name, void *pBuffer, int size); int FileLoad(const char *name, void *pBuffer, int size); // supply pBuffer = 0 to get the file size without loading diff --git a/user_io.cpp b/user_io.cpp index 630a80a..a41e324 100644 --- a/user_io.cpp +++ b/user_io.cpp @@ -1299,7 +1299,7 @@ int user_io_file_tx(const char* name, unsigned char index, char opensave, char m if (opensave) { - FileGenerateSavePath(name, "sav", (char*)buf, sizeof(buf)); + FileGenerateSavePath(name, (char*)buf); user_io_file_mount((char*)buf, 0, 1); }