From b24b02d46cfd6fb4cc89d34fa38c442d31367017 Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Sat, 18 Oct 2014 16:25:48 -0700 Subject: [PATCH] Screenshot numbering --- src/gba/gba-thread.c | 2 +- src/util/vfs.c | 99 ++++++++++++++++++++++++++++++++++++++++++++ src/util/vfs.h | 1 + 3 files changed, 101 insertions(+), 1 deletion(-) diff --git a/src/gba/gba-thread.c b/src/gba/gba-thread.c index 194711c49..ae502ca43 100644 --- a/src/gba/gba-thread.c +++ b/src/gba/gba-thread.c @@ -504,7 +504,7 @@ struct GBAThread* GBAThreadGetContext(void) { void GBAThreadTakeScreenshot(struct GBAThread* threadContext) { unsigned stride; void* pixels = 0; - struct VFile* vf = VDirOptionalOpenFile(threadContext->stateDir, threadContext->gba->activeFile, "screenshot", ".png", O_CREAT | O_TRUNC | O_WRONLY); + struct VFile* vf = VDirOptionalOpenIncrementFile(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); diff --git a/src/util/vfs.c b/src/util/vfs.c index 7902d55c0..9c8b32332 100644 --- a/src/util/vfs.c +++ b/src/util/vfs.c @@ -211,6 +211,105 @@ struct VFile* VDirOptionalOpenFile(struct VDir* dir, const char* realPath, const return vf; } +struct VFile* VDirOptionalOpenIncrementFile(struct VDir* dir, const char* realPath, const char* prefix, const char* infix, const char* suffix, int mode) { + char path[PATH_MAX]; + path[PATH_MAX - 1] = '\0'; + char realPrefix[PATH_MAX]; + realPrefix[PATH_MAX - 1] = '\0'; + if (!dir) { + if (!realPath) { + return 0; + } + const char* separatorPoint = strrchr(realPath, '/'); + const char* dotPoint; + size_t len; + if (!separatorPoint) { + strcpy(path, "./"); + separatorPoint = realPath; + dotPoint = strrchr(realPath, '.'); + } else { + path[0] = '\0'; + dotPoint = strrchr(separatorPoint, '.'); + + if (separatorPoint - realPath + 1 >= PATH_MAX - 1) { + return 0; + } + + len = separatorPoint - realPath; + strncat(path, realPath, len); + path[len] = '\0'; + ++separatorPoint; + } + + if (dotPoint - realPath + 1 >= PATH_MAX - 1) { + return 0; + } + + if (dotPoint >= separatorPoint) { + len = dotPoint - separatorPoint; + } else { + len = PATH_MAX - 1; + } + + strncpy(realPrefix, separatorPoint, len); + realPrefix[len] = '\0'; + + prefix = realPrefix; + dir = VDirOpen(path); + } + if (!dir) { + // This shouldn't be possible + return 0; + } + dir->rewind(dir); + struct VDirEntry* dirent; + size_t prefixLen = strlen(prefix); + size_t infixLen = strlen(infix); + unsigned next = 0; + while ((dirent = dir->listNext(dir))) { + const char* filename = dirent->name(dirent); + char* dotPoint = strrchr(filename, '.'); + size_t len = strlen(filename); + if (dotPoint) { + len = (dotPoint - filename); + } + const char* separator = 0; + const char* nextSeparator = filename; + size_t strstrlen = len; + while ((nextSeparator = strnstr(nextSeparator, infix, strstrlen))) { + strstrlen -= nextSeparator - separator - 1; + separator = nextSeparator; + ++nextSeparator; + } + if (!separator) { + continue; + } + len = separator - filename; + if (len != prefixLen) { + continue; + } + if (strncmp(filename, prefix, prefixLen) == 0) { + int nlen; + separator += infixLen; + snprintf(path, PATH_MAX - 1, "%%u%s%%n", suffix); + unsigned increment; + if (sscanf(separator, path, &increment, &nlen) < 1) { + continue; + } + len = strlen(separator); + if (nlen < (ssize_t) len) { + continue; + } + if (next <= increment) { + next = increment + 1; + } + } + } + snprintf(path, PATH_MAX - 1, "%s%s%u%s", prefix, infix, next, suffix); + path[PATH_MAX - 1] = '\0'; + return dir->openFile(dir, path, mode); +} + bool _vdClose(struct VDir* vd) { struct VDirDE* vdde = (struct VDirDE*) vd; if (closedir(vdde->de) < 0) { diff --git a/src/util/vfs.h b/src/util/vfs.h index cea38aedf..fd0e75cd5 100644 --- a/src/util/vfs.h +++ b/src/util/vfs.h @@ -40,5 +40,6 @@ 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); +struct VFile* VDirOptionalOpenIncrementFile(struct VDir* dir, const char* realPath, const char* prefix, const char* infix, const char* suffix, int mode); #endif