changeset 18742:f2c1edca40f3 draft

(svn r23590) -Codechange: make the string validation settings better expandable
author rubidium <rubidium@openttd.org>
date Sun, 18 Dec 2011 18:37:54 +0000
parents 0f8563e31943
children 21b318379830
files src/misc_gui.cpp src/network/core/packet.cpp src/network/core/packet.h src/network/network_content.cpp src/newgrf_gui.cpp src/string.cpp src/string_func.h src/string_type.h
diffstat 8 files changed, 24 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/misc_gui.cpp
+++ b/src/misc_gui.cpp
@@ -1099,7 +1099,7 @@
 			QueryStringBaseWindow(max_bytes, max_chars)
 	{
 		GetString(this->edit_str_buf, str, &this->edit_str_buf[max_bytes - 1]);
-		str_validate(this->edit_str_buf, &this->edit_str_buf[max_bytes - 1], false, true);
+		str_validate(this->edit_str_buf, &this->edit_str_buf[max_bytes - 1], SVS_NONE);
 
 		/* Make sure the name isn't too long for the text buffer in the number of
 		 * characters (not bytes). max_chars also counts the '\0' characters. */
--- a/src/network/core/packet.cpp
+++ b/src/network/core/packet.cpp
@@ -283,9 +283,9 @@
  * Reads a string till it finds a '\0' in the stream.
  * @param buffer The buffer to put the data into.
  * @param size   The size of the buffer.
- * @param allow_newlines Whether the string validation should remove newlines.
+ * @param settings The string validation settings.
  */
-void Packet::Recv_string(char *buffer, size_t size, bool allow_newlines)
+void Packet::Recv_string(char *buffer, size_t size, StringValidationSettings settings)
 {
 	PacketSize pos;
 	char *bufp = buffer;
@@ -306,7 +306,7 @@
 	}
 	this->pos = pos;
 
-	str_validate(bufp, last, allow_newlines);
+	str_validate(bufp, last, settings);
 }
 
 #endif /* ENABLE_NETWORK */
--- a/src/network/core/packet.h
+++ b/src/network/core/packet.h
@@ -16,6 +16,7 @@
 
 #include "config.h"
 #include "core.h"
+#include "../../string_type.h"
 
 #ifdef ENABLE_NETWORK
 
@@ -83,7 +84,7 @@
 	uint16 Recv_uint16();
 	uint32 Recv_uint32();
 	uint64 Recv_uint64();
-	void   Recv_string(char *buffer, size_t size, bool allow_newlines = false);
+	void   Recv_string(char *buffer, size_t size, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
 };
 
 #endif /* ENABLE_NETWORK */
--- a/src/network/network_content.cpp
+++ b/src/network/network_content.cpp
@@ -56,7 +56,7 @@
 	p->Recv_string(ci->name, lengthof(ci->name));
 	p->Recv_string(ci->version, lengthof(ci->name));
 	p->Recv_string(ci->url, lengthof(ci->url));
-	p->Recv_string(ci->description, lengthof(ci->description),  true);
+	p->Recv_string(ci->description, lengthof(ci->description), SVS_REPLACE_WITH_QUESTION_MARK | SVS_ALLOW_NEWLINE);
 
 	ci->unique_id = p->Recv_uint32();
 	for (uint j = 0; j < sizeof(ci->md5sum); j++) {
--- a/src/newgrf_gui.cpp
+++ b/src/newgrf_gui.cpp
@@ -599,7 +599,7 @@
 		char *p = this->text + (strncmp("\xEF\xBB\xBF", this->text, 3) == 0 ? 3 : 0);
 
 		/* Make sure the string is a valid UTF-8 sequence. */
-		str_validate(p, this->text + filesize, true);
+		str_validate(p, this->text + filesize, SVS_REPLACE_WITH_QUESTION_MARK | SVS_ALLOW_NEWLINE);
 
 		/* Split the string on newlines. */
 		*this->lines.Append() = p;
--- a/src/string.cpp
+++ b/src/string.cpp
@@ -184,10 +184,9 @@
  * replaces them with a question mark '?' (if not ignored)
  * @param str the string to validate
  * @param last the last valid character of str
- * @param allow_newlines whether newlines should be allowed or ignored
- * @param ignore whether to ignore or replace with a question mark
+ * @param settings the settings for the string validation.
  */
-void str_validate(char *str, const char *last, bool allow_newlines, bool ignore)
+void str_validate(char *str, const char *last, StringValidationSettings settings)
 {
 	/* Assume the ABSOLUTE WORST to be in str as it comes from the outside. */
 
@@ -215,16 +214,16 @@
 			do {
 				*dst++ = *str++;
 			} while (--len != 0);
-		} else if (allow_newlines && c == '\n') {
+		} else if ((settings & SVS_ALLOW_NEWLINE) != 0  && c == '\n') {
 			*dst++ = *str++;
 		} else {
-			if (allow_newlines && c == '\r' && str[1] == '\n') {
+			if ((settings & SVS_ALLOW_NEWLINE) != 0 && c == '\r' && str[1] == '\n') {
 				str += len;
 				continue;
 			}
 			/* Replace the undesirable character with a question mark */
 			str += len;
-			if (!ignore) *dst++ = '?';
+			if ((settings & SVS_REPLACE_WITH_QUESTION_MARK) != 0) *dst++ = '?';
 
 			/* In case of these two special cases assume that they really
 			 * mean SETX/SETXY and also "eat" the paramater. If this was
--- a/src/string_func.h
+++ b/src/string_func.h
@@ -39,7 +39,7 @@
 
 char *CDECL str_fmt(const char *str, ...) WARN_FORMAT(1, 2);
 
-void str_validate(char *str, const char *last, bool allow_newlines = false, bool ignore = false);
+void str_validate(char *str, const char *last, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
 void str_strip_colours(char *str);
 bool strtolower(char *str);
 
--- a/src/string_type.h
+++ b/src/string_type.h
@@ -12,6 +12,8 @@
 #ifndef STRING_TYPE_H
 #define STRING_TYPE_H
 
+#include "core/enum_type.hpp"
+
 /** A non-breaking space. */
 #define NBSP "\xC2\xA0"
 
@@ -42,4 +44,12 @@
 static const WChar CHAR_TD_RLO = 0x202E; ///< Force the following characters to be treated as right-to-left characters.
 static const WChar CHAR_TD_PDF = 0x202C; ///< Restore the text-direction state to before the last LRE, RLE, LRO or RLO.
 
+/** Settings for the string validation. */
+enum StringValidationSettings {
+	SVS_NONE                       = 0,      ///< Allow nothing and replace nothing.
+	SVS_REPLACE_WITH_QUESTION_MARK = 1 << 0, ///< Replace the unknown/bad bits with question marks.
+	SVS_ALLOW_NEWLINE              = 1 << 1, ///< Allow newlines.
+};
+DECLARE_ENUM_AS_BIT_SET(StringValidationSettings);
+
 #endif /* STRING_TYPE_H */