diff --git a/source/src/command.cpp b/source/src/command.cpp index f238ad08..de2bae56 100644 --- a/source/src/command.cpp +++ b/source/src/command.cpp @@ -1430,6 +1430,7 @@ COMMANDF(rnd, "i", (int *a) { intret(*a>0 ? rnd(*a) : 0); }); #ifndef STANDALONE void writecfg() { + filerotate("config/saved", "cfg", CONFIGROTATEMAX); // keep five old config sets stream *f = openfile(path("config/saved.cfg", true), "w"); if(!f) return; f->printf("// automatically written on exit, DO NOT MODIFY\n// delete this file to have defaults.cfg overwrite these settings\n// modify settings in game, or put settings in autoexec.cfg to override anything\n\n"); diff --git a/source/src/cube.h b/source/src/cube.h index dcecccce..cd20b747 100644 --- a/source/src/cube.h +++ b/source/src/cube.h @@ -66,6 +66,7 @@ extern int verbose; #define AC_MASTER_HTTP 1 // default #define AC_MASTER_RAW 0 #define MAXCL 16 +#define CONFIGROTATEMAX 5 // keep 5 old versions of saved.cfg and init.cfg around #include "protos.h" // external function decls diff --git a/source/src/main.cpp b/source/src/main.cpp index d016f151..06ee2e35 100644 --- a/source/src/main.cpp +++ b/source/src/main.cpp @@ -123,6 +123,7 @@ VARF(fullscreen, 0, 1, 1, setfullscreen(fullscreen!=0)); void writeinitcfg() { if(!restoredinits) return; + filerotate("config/init", "cfg", CONFIGROTATEMAX); // keep five old init config sets stream *f = openfile(path("config/init.cfg", true), "w"); if(!f) return; f->printf("// automatically written on exit, DO NOT MODIFY\n// modify settings in game\n"); @@ -992,22 +993,13 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw) void initclientlog() // rotate old logfiles and create new one { - const int MAXROT = 5; // keep five old logfiles - string fname1, fname2 = ""; - const char *basename = findfile("clientlog", "w"); - for(int i = MAXROT; i >= 0; i--) - { - formatstring(fname1)(i > 0 ? "%s-old-%d.txt" : "%s.txt", basename, i); - if(fname2[0]) rename(fname1, fname2); - else remove(fname1); - copystring(fname2, fname1); - } + filerotate("clientlog", "txt", 5); // keep five old logfiles clientlogfile = openfile("clientlog.txt", "w"); if(clientlogfile) { if(bootclientlog && bootclientlog->length()) clientlogfile->write(bootclientlog->getbuf(), bootclientlog->length()); } - else conoutf("could not create logfile \"%s\"", fname2); + else conoutf("could not create logfile \"%s\"", findfile("clientlog.txt", "w")); DELETEP(bootclientlog); } diff --git a/source/src/stream.cpp b/source/src/stream.cpp index 4fba3c35..e28ac085 100644 --- a/source/src/stream.cpp +++ b/source/src/stream.cpp @@ -787,3 +787,19 @@ char *loadfile(const char *fn, int *size, const char *mode) return buf; } +void filerotate(const char *basename, const char *ext, int keepold, const char *oldformat) // rotate old logfiles +{ + char fname1[MAXSTRLEN * 2], fname2[MAXSTRLEN] = ""; + copystring(fname1, findfile(basename, "w")); + int mid = strlen(fname1); + if(!oldformat) oldformat = "-old-%d"; + for(; keepold >= 0; keepold--) + { + formatstring(fname1 + mid)(keepold ? oldformat : "", keepold); + concatformatstring(fname1 + mid, ".%s", ext); + if(fname2[0]) rename(fname1, fname2); + else remove(fname1); + copystring(fname2, fname1); + } +} + diff --git a/source/src/tools.h b/source/src/tools.h index 75c086fb..0aeacff9 100644 --- a/source/src/tools.h +++ b/source/src/tools.h @@ -945,6 +945,7 @@ extern stream *openfile(const char *filename, const char *mode); extern stream *opentempfile(const char *filename, const char *mode); extern stream *opengzfile(const char *filename, const char *mode, stream *file = NULL, int level = Z_BEST_COMPRESSION); extern char *loadfile(const char *fn, int *size, const char *mode = NULL); +extern void filerotate(const char *basename, const char *ext, int keepold, const char *oldformat = NULL); extern bool listdir(const char *dir, const char *ext, vector &files); extern int listfiles(const char *dir, const char *ext, vector &files); extern int listzipfiles(const char *dir, const char *ext, vector &files);