summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2011-12-18 18:37:54 +0000
committerrubidium <rubidium@openttd.org>2011-12-18 18:37:54 +0000
commitfefe22b4aa5194af499763d3cc4d4dc19116fa92 (patch)
tree5b2671aa4bc5b68584d962d11a8ceab705ad6321 /src
parent6ae8cac4320e098c1fa628a3afd5c090aacc80a2 (diff)
downloadopenttd-fefe22b4aa5194af499763d3cc4d4dc19116fa92.tar.xz
(svn r23590) -Codechange: make the string validation settings better expandable
Diffstat (limited to 'src')
-rw-r--r--src/misc_gui.cpp2
-rw-r--r--src/network/core/packet.cpp6
-rw-r--r--src/network/core/packet.h3
-rw-r--r--src/network/network_content.cpp2
-rw-r--r--src/newgrf_gui.cpp2
-rw-r--r--src/string.cpp11
-rw-r--r--src/string_func.h2
-rw-r--r--src/string_type.h10
8 files changed, 24 insertions, 14 deletions
diff --git a/src/misc_gui.cpp b/src/misc_gui.cpp
index 1256f5470..354d6227b 100644
--- a/src/misc_gui.cpp
+++ b/src/misc_gui.cpp
@@ -1099,7 +1099,7 @@ struct QueryStringWindow : public QueryStringBaseWindow
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. */
diff --git a/src/network/core/packet.cpp b/src/network/core/packet.cpp
index d30ef9e5f..4d9345e82 100644
--- a/src/network/core/packet.cpp
+++ b/src/network/core/packet.cpp
@@ -283,9 +283,9 @@ uint64 Packet::Recv_uint64()
* 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 @@ void Packet::Recv_string(char *buffer, size_t size, bool allow_newlines)
}
this->pos = pos;
- str_validate(bufp, last, allow_newlines);
+ str_validate(bufp, last, settings);
}
#endif /* ENABLE_NETWORK */
diff --git a/src/network/core/packet.h b/src/network/core/packet.h
index ef9da056a..7f344d017 100644
--- 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 @@ public:
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 */
diff --git a/src/network/network_content.cpp b/src/network/network_content.cpp
index 179603768..6499e5274 100644
--- a/src/network/network_content.cpp
+++ b/src/network/network_content.cpp
@@ -56,7 +56,7 @@ bool ClientNetworkContentSocketHandler::Receive_SERVER_INFO(Packet *p)
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++) {
diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp
index cbd6a734c..24f5f1d60 100644
--- a/src/newgrf_gui.cpp
+++ b/src/newgrf_gui.cpp
@@ -599,7 +599,7 @@ private:
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;
diff --git a/src/string.cpp b/src/string.cpp
index 0c2777371..94377c1b6 100644
--- a/src/string.cpp
+++ b/src/string.cpp
@@ -184,10 +184,9 @@ char *CDECL str_fmt(const char *str, ...)
* 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 @@ void str_validate(char *str, const char *last, bool allow_newlines, bool ignore)
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
diff --git a/src/string_func.h b/src/string_func.h
index b3d8a6219..ac2fd0e76 100644
--- a/src/string_func.h
+++ b/src/string_func.h
@@ -39,7 +39,7 @@ int CDECL seprintf(char *str, const char *last, const char *format, ...) WARN_FO
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);
diff --git a/src/string_type.h b/src/string_type.h
index b2bd6824d..174606635 100644
--- 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_LRO = 0x202D; ///< Force the following characters to
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 */