all repos — mgba @ e293134a787c4ab434b9f72e6e6d634e6ce92966

mGBA Game Boy Advance Emulator

Unify optional directory file opening
Jeffrey Pfau jeffrey@endrift.com
Thu, 09 Oct 2014 23:55:02 -0700
commit

e293134a787c4ab434b9f72e6e6d634e6ce92966

parent

747e30c5e1b9ab5d2ff6adbbbb119aff4f802bab

4 files changed, 35 insertions(+), 36 deletions(-)

jump to
M src/gba/gba-serialize.csrc/gba/gba-serialize.c

@@ -103,17 +103,9 @@ }

} static struct VFile* _getStateVf(struct GBA* gba, struct VDir* dir, int slot, bool write) { - char path[PATH_MAX]; - path[PATH_MAX - 1] = '\0'; - struct VFile* vf; - if (!dir) { - snprintf(path, PATH_MAX - 1, "%s.ss%d", gba->activeFile, slot); - vf = VFileOpen(path, write ? (O_CREAT | O_TRUNC | O_RDWR) : O_RDONLY); - } else { - snprintf(path, PATH_MAX - 1, "savestate.ss%d", slot); - vf = dir->openFile(dir, path, write ? (O_CREAT | O_TRUNC | O_RDWR) : O_RDONLY); - } - return vf; + char suffix[5] = { '\0' }; + snprintf(suffix, sizeof(suffix), ".ss%d", slot); + return VDirOptionalOpenFile(dir, gba->activeFile, "savestate", suffix, write ? (O_CREAT | O_TRUNC | O_RDWR) : O_RDONLY); } #ifdef USE_PNG
M src/gba/gba-thread.csrc/gba/gba-thread.c

@@ -276,35 +276,12 @@ dirent = threadContext->gameDir->listNext(threadContext->gameDir);

} } - if (threadContext->stateDir) { - threadContext->save = threadContext->stateDir->openFile(threadContext->stateDir, "sram.sav", O_RDWR | O_CREAT); - } if (!threadContext->rom) { return false; } - if (threadContext->fname && !threadContext->save) { - char* savedata = 0; - char* dotPoint = strrchr(threadContext->fname, '.'); - if (dotPoint > strrchr(threadContext->fname, '/') && dotPoint[1] && dotPoint[2] && dotPoint[3]) { - savedata = strdup(threadContext->fname); - dotPoint = strrchr(savedata, '.'); - dotPoint[1] = 's'; - dotPoint[2] = 'a'; - dotPoint[3] = 'v'; - dotPoint[4] = '\0'; - } else if (dotPoint) { - savedata = malloc((dotPoint - threadContext->fname + 5) * sizeof(char)); - strncpy(savedata, threadContext->fname, dotPoint - threadContext->fname + 1); - strcat(savedata, "sav"); - } else { - savedata = malloc(strlen(threadContext->fname + 5) * sizeof(char)); - sprintf(savedata, "%s.sav", threadContext->fname); - } - threadContext->save = VFileOpen(savedata, O_RDWR | O_CREAT); - free(savedata); - } + threadContext->save = VDirOptionalOpenFile(threadContext->stateDir, threadContext->fname, "sram", ".sav", O_CREAT | O_RDWR); MutexInit(&threadContext->stateMutex); ConditionInit(&threadContext->stateCond);

@@ -524,7 +501,7 @@ #ifdef USE_PNG

void GBAThreadTakeScreenshot(struct GBAThread* threadContext) { unsigned stride; void* pixels = 0; - struct VFile* vf = threadContext->stateDir->openFile(threadContext->stateDir, "screenshot.png", O_CREAT | O_WRONLY); + struct VFile* vf = VDirOptionalOpenFile(threadContext->stateDir, threadContext->gba->activeFile, "screenshot", ".png", O_CREAT | O_TRUNC | O_WRONLY); threadContext->gba->video.renderer->getPixels(threadContext->gba->video.renderer, &stride, &pixels); png_structp png = PNGWriteOpen(vf); png_infop info = PNGWriteHeader(png, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
M src/util/vfs.csrc/util/vfs.c

@@ -183,6 +183,34 @@

return &vd->d; } +struct VFile* VDirOptionalOpenFile(struct VDir* dir, const char* realPath, const char* prefix, const char* suffix, int mode) { + char path[PATH_MAX]; + path[PATH_MAX - 1] = '\0'; + struct VFile* vf; + if (!dir) { + if (!realPath) { + return 0; + } + char* dotPoint = strrchr(realPath, '.'); + if (dotPoint - realPath + 1 >= PATH_MAX - 1) { + return 0; + } + if (dotPoint > strrchr(realPath, '/')) { + int len = dotPoint - realPath; + strncpy(path, realPath, len); + path[len] = 0; + strncat(path + len, suffix, PATH_MAX - len - 1); + } else { + snprintf(path, PATH_MAX - 1, "%s%s", realPath, suffix); + } + vf = VFileOpen(path, mode); + } else { + snprintf(path, PATH_MAX - 1, "%s%s", prefix, suffix); + vf = dir->openFile(dir, path, mode); + } + return vf; +} + bool _vdClose(struct VDir* vd) { struct VDirDE* vdde = (struct VDirDE*) vd; if (closedir(vdde->de) < 0) {
M src/util/vfs.hsrc/util/vfs.h

@@ -39,4 +39,6 @@ #ifdef ENABLE_LIBZIP

struct VDir* VDirOpenZip(const char* path, int flags); #endif +struct VFile* VDirOptionalOpenFile(struct VDir* dir, const char* realPath, const char* prefix, const char* suffix, int mode); + #endif