keep the last five versions of saved.cfg and init.cfg

Renames the existing saved.cfg to saved-old-1.cfg (and an existing -1 to
-2, and so on) before the new saved.cfg is written.

This should help in cases where players mess up their config, because the
config is deleted with a five-game-starts delay.
This commit is contained in:
ac-stef
2014-11-02 23:17:00 +01:00
parent bc3be8136e
commit 6ede08d011
5 changed files with 22 additions and 11 deletions
+1
View File
@@ -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");
+1
View File
@@ -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
+3 -11
View File
@@ -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);
}
+16
View File
@@ -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);
}
}
+1
View File
@@ -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<char *> &files);
extern int listfiles(const char *dir, const char *ext, vector<char *> &files);
extern int listzipfiles(const char *dir, const char *ext, vector<char *> &files);