(svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
authorrubidium
Tue, 12 Jun 2007 15:46:34 +0000
changeset 7371 b86b05214afe
parent 7370 a03b5c70eb63
child 7372 c254cca76042
(svn r10116) -Fix [FS#850]: remove invalid characters (for the file system) from savegame names. Based on a patch by TheJosh.
src/fileio.cpp
src/fileio.h
src/misc_gui.cpp
src/screenshot.cpp
--- a/src/fileio.cpp	Tue Jun 12 14:22:28 2007 +0000
+++ b/src/fileio.cpp	Tue Jun 12 15:46:34 2007 +0000
@@ -394,3 +394,20 @@
 	FioCreateDirectory(_paths.scenario_dir);
 	FioCreateDirectory(_paths.heightmap_dir);
 }
+
+/**
+ * Sanitizes a filename, i.e. removes all illegal characters from it.
+ * @param filename the "\0" terminated filename
+ */
+void SanitizeFilename(char *filename)
+{
+	for (; *filename != '\0'; filename++) {
+		switch (*filename) {
+			/* The following characters are not allowed in filenames
+			 * on at least one of the supported operating systems: */
+			case ':': case '\\': case '*': case '?': case '/':
+				*filename = '_';
+				break;
+		}
+	}
+}
--- a/src/fileio.h	Tue Jun 12 14:22:28 2007 +0000
+++ b/src/fileio.h	Tue Jun 12 15:46:34 2007 +0000
@@ -20,6 +20,7 @@
 bool FioCheckFileExists(const char *filename);
 void FioCreateDirectory(const char *filename);
 
+void SanitizeFilename(char *filename);
 void AppendPathSeparator(char *buf, size_t buflen);
 void DeterminePaths(const char *exe);
 
--- a/src/misc_gui.cpp	Tue Jun 12 14:22:28 2007 +0000
+++ b/src/misc_gui.cpp	Tue Jun 12 15:46:34 2007 +0000
@@ -35,6 +35,7 @@
 #include "date.h"
 #include "cargotype.h"
 #include "player_face.h"
+#include "fileio.h"
 
 #include "fios.h"
 /* Variables to display file lists */
@@ -1396,6 +1397,7 @@
 	SetDParam(1, p->name_2);
 	SetDParam(2, _date);
 	GetString(_edit_str_buf, STR_4004, lastof(_edit_str_buf));
+	SanitizeFilename(_edit_str_buf);
 }
 
 extern void StartupEngines();
--- a/src/screenshot.cpp	Tue Jun 12 14:22:28 2007 +0000
+++ b/src/screenshot.cpp	Tue Jun 12 15:46:34 2007 +0000
@@ -14,6 +14,7 @@
 #include "variables.h"
 #include "date.h"
 #include "helpers.hpp"
+#include "fileio.h"
 
 char _screenshot_format_name[8];
 uint _num_screenshot_formats;
@@ -505,6 +506,7 @@
 		GetString(_screenshot_name, STR_4004, lastof(_screenshot_name));
 	}
 
+	SanitizeFilename(_screenshot_name);
 	base = strchr(_screenshot_name, 0);
 	base[0] = '.'; strcpy(base + 1, ext);