From 65934e7aa5f3ae7c7644162c7cf0752e2fbc12dc Mon Sep 17 00:00:00 2001 From: Aaron Giles Date: Thu, 29 May 2008 08:00:23 +0000 Subject: [PATCH] Added new option -snapname which lets you provide a template for how snapshot names are generated. Added new astring functions astring_del, astring_replace, and astring_replacec to help perform simple search/replace substitution. --- docs/config.txt | 14 +++++++++ src/emu/emuopts.c | 1 + src/emu/emuopts.h | 1 + src/emu/video.c | 65 ++++++++++++++++++++++++++++++++---------- src/lib/util/astring.c | 59 ++++++++++++++++++++++++++++++++++++++ src/lib/util/astring.h | 9 ++++++ 6 files changed, 134 insertions(+), 15 deletions(-) diff --git a/docs/config.txt b/docs/config.txt index b71b46a3ea9..f3e580dec8f 100644 --- a/docs/config.txt +++ b/docs/config.txt @@ -453,6 +453,20 @@ Core state/playback options (.cfg), NVRAM (.nv), and memory card files deleted. The default is NULL (no recording). +-snapname + + Describes how MAME should name files for snapshots. is a string + that provides a template that is used to generate a filename. Three + simple substitutions are provided: the / character represents the + path separator on any target platform (even Windows); the string %g + represents the driver name of the current game; and the string %i + represents an incrementing index. If %i is omitted, then each + snapshot taken will overwrite the previous one; otherwise, MAME will + find the next empty value for %i and use that for a filename. The + default is %g/%i, which creates a separate folder for each game, + and names the snapshots under it starting with 0000 and increasing + from there. + -mngwrite Writes each video frame to the given in MNG format, diff --git a/src/emu/emuopts.c b/src/emu/emuopts.c index 3f22c2c80b3..d93b6c34728 100644 --- a/src/emu/emuopts.c +++ b/src/emu/emuopts.c @@ -64,6 +64,7 @@ const options_entry mame_core_options[] = { "autosave", "0", OPTION_BOOLEAN, "enable automatic restore at startup, and automatic save at exit time" }, { "playback;pb", NULL, 0, "playback an input file" }, { "record;rec", NULL, 0, "record an input file" }, + { "snapname", "%g/%i", 0, "override of the default snapshot naming; %g == gamename, %i == index" }, { "mngwrite", NULL, 0, "optional filename to write a MNG movie of the current session" }, { "aviwrite", NULL, 0, "optional filename to write an AVI movie of the current session" }, { "wavwrite", NULL, 0, "optional filename to write a WAV file of the current session" }, diff --git a/src/emu/emuopts.h b/src/emu/emuopts.h index 44d49880050..dc9d07cd07c 100644 --- a/src/emu/emuopts.h +++ b/src/emu/emuopts.h @@ -63,6 +63,7 @@ #define OPTION_AUTOSAVE "autosave" #define OPTION_PLAYBACK "playback" #define OPTION_RECORD "record" +#define OPTION_SNAPNAME "snapname" #define OPTION_MNGWRITE "mngwrite" #define OPTION_AVIWRITE "aviwrite" #define OPTION_WAVWRITE "wavwrite" diff --git a/src/emu/video.c b/src/emu/video.c index 6a5c362221e..4d5c89feab9 100644 --- a/src/emu/video.c +++ b/src/emu/video.c @@ -2214,28 +2214,63 @@ static void create_snapshot_bitmap(const device_config *screen) static file_error mame_fopen_next(running_machine *machine, const char *pathoption, const char *extension, mame_file **file) { + const char *snapname = options_get_string(mame_options(), OPTION_SNAPNAME); + astring *snapstr = astring_alloc(); + astring *fname = astring_alloc(); file_error filerr; - char *fname; - int seq; - - /* allocate temp space for the name */ - fname = malloc_or_die(strlen(machine->basename) + 1 + 10 + strlen(extension) + 1); - - /* try until we succeed */ - for (seq = 0; ; seq++) + int index; + + /* handle defaults */ + if (snapname == NULL || snapname[0] == 0) + snapname = "%g/%i"; + astring_cpyc(snapstr, snapname); + + /* strip any extension in the provided name and add our own */ + index = astring_rchr(snapstr, 0, '.'); + if (index != -1) + astring_substr(snapstr, 0, index); + astring_catc(snapstr, "."); + astring_catc(snapstr, extension); + + /* substitute path and gamename up front */ + astring_replacec(snapstr, 0, "/", PATH_SEPARATOR); + astring_replacec(snapstr, 0, "%g", machine->basename); + + /* determine if the template has an index; if not, we always use the same name */ + if (astring_findc(snapstr, 0, "%i") == -1) + astring_cpy(fname, snapstr); + + /* otherwise, we scan for the next available filename */ + else { - sprintf(fname, "%s" PATH_SEPARATOR "%04d.%s", machine->basename, seq, extension); - filerr = mame_fopen(pathoption, fname, OPEN_FLAG_READ, file); - if (filerr != FILERR_NONE) - break; - mame_fclose(*file); + int seq; + + /* try until we succeed */ + for (seq = 0; ; seq++) + { + char seqtext[10]; + + /* make text for the sequence number */ + sprintf(seqtext, "%04d", seq); + + /* build up the filename */ + astring_cpy(fname, snapstr); + astring_replacec(fname, 0, "%i", seqtext); + + /* try to open the file; stop when we fail */ + filerr = mame_fopen(pathoption, astring_c(fname), OPEN_FLAG_READ, file); + if (filerr != FILERR_NONE) + break; + mame_fclose(*file); + } } /* create the final file */ - filerr = mame_fopen(pathoption, fname, OPEN_FLAG_WRITE | OPEN_FLAG_CREATE | OPEN_FLAG_CREATE_PATHS, file); + filerr = mame_fopen(pathoption, astring_c(fname), OPEN_FLAG_WRITE | OPEN_FLAG_CREATE | OPEN_FLAG_CREATE_PATHS, file); /* free the name and get out */ - free(fname); + astring_free(fname); + astring_free(snapstr); return filerr; } diff --git a/src/lib/util/astring.c b/src/lib/util/astring.c index d6956121ef9..9f47108804f 100644 --- a/src/lib/util/astring.c +++ b/src/lib/util/astring.c @@ -290,6 +290,30 @@ astring *astring_substr(astring *str, int start, int count) } +/*------------------------------------------------- + astring_del - delete a substring of + ourself, keeping everything else +-------------------------------------------------*/ + +astring *astring_del(astring *str, int start, int count) +{ + int strlength = strlen(str->text); + + /* ignore attempts to do this on the dummy */ + if (str == &dummy_astring) + return str; + + /* normalize parameters */ + normalize_substr(&start, &count, strlength); + + /* move the data and NULL-terminate */ + if (count > 0) + memmove(str->text + start, str->text + start + count, strlength - (start + count)); + str->text[strlength - count] = 0; + return str; +} + + /*------------------------------------------------- astring_printf - printf text into an astring -------------------------------------------------*/ @@ -498,6 +522,41 @@ int astring_findc(const astring *str, int start, const char *search) } +/*------------------------------------------------- + astring_replace - search in an astring for + another astring, replacing all instances with + a third and returning the number of matches +-------------------------------------------------*/ + +int astring_replace(astring *str, int start, const astring *search, const astring *replace) +{ + return astring_replacec(str, start, search->text, replace->text); +} + + +/*------------------------------------------------- + astring_replacec - search in an astring for a + C string, replacing all instances with another + C string and returning the number of matches +-------------------------------------------------*/ + +int astring_replacec(astring *str, int start, const char *search, const char *replace) +{ + int searchlen = strlen(search); + int replacelen = strlen(replace); + int matches = 0; + int curindex; + + for (curindex = astring_findc(str, start, search); curindex != -1; curindex = astring_findc(str, curindex + replacelen, search)) + { + matches++; + astring_del(str, curindex, searchlen); + astring_insc(str, curindex, replace); + } + return matches; +} + + /*************************************************************************** ASTRING UTILITIES diff --git a/src/lib/util/astring.h b/src/lib/util/astring.h index 7be29ab8f27..854ea876248 100644 --- a/src/lib/util/astring.h +++ b/src/lib/util/astring.h @@ -69,6 +69,9 @@ astring *astring_inssubstr(astring *dst, int insbefore, const astring *src, int /* extract a substring of ourself, removing everything else */ astring *astring_substr(astring *str, int start, int count); +/* delete a substring from ourself, keeping everything else */ +astring *astring_del(astring *str, int start, int count); + /* formatted printf to an astring */ int astring_printf(astring *dst, const char *format, ...) ATTR_PRINTF(2,3); @@ -118,6 +121,12 @@ int astring_find(const astring *str, int start, const astring *search); /* search in an astring for a C string, returning offset or -1 if not found */ int astring_findc(const astring *str, int start, const char *search); +/* search in an astring for another astring, replacing all instances with a third and returning the number of matches */ +int astring_replace(astring *str, int start, const astring *search, const astring *replace); + +/* search in an astring for a C string, replacing all instances with another C string and returning the number of matches */ +int astring_replacec(astring *str, int start, const char *search, const char *replace); + /* ----- astring utilties ----- */