mirror of https://github.com/mgba-emu/mgba.git
Screenshot numbering
This commit is contained in:
parent
e3bfe86579
commit
b24b02d46c
|
@ -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);
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue