mirror of https://github.com/mgba-emu/mgba.git
Unify optional directory file opening
This commit is contained in:
parent
747e30c5e1
commit
e293134a78
|
@ -103,17 +103,9 @@ void GBADeserialize(struct GBA* gba, struct GBASerializedState* state) {
|
|||
}
|
||||
|
||||
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
|
||||
|
|
|
@ -276,35 +276,12 @@ bool GBAThreadStart(struct GBAThread* threadContext) {
|
|||
}
|
||||
|
||||
}
|
||||
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 @@ struct GBAThread* GBAThreadGetContext(void) {
|
|||
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);
|
||||
|
|
|
@ -183,6 +183,34 @@ struct VDir* VDirOpen(const char* path) {
|
|||
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) {
|
||||
|
|
|
@ -39,4 +39,6 @@ struct VDir* VDirOpen(const char* path);
|
|||
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
|
||||
|
|
Loading…
Reference in New Issue