diff options
author | Max Horn | 2012-01-28 01:15:49 +0100 |
---|---|---|
committer | Max Horn | 2012-02-15 16:51:37 +0100 |
commit | 658080deeda79d20ea40643569fbcb072573e7cf (patch) | |
tree | d604bf668909a6db44a1ec83e7c51088a5d85592 /common | |
parent | 37e5b209a71af725456a42be2605dea28ffceb84 (diff) | |
download | scummvm-rg350-658080deeda79d20ea40643569fbcb072573e7cf.tar.gz scummvm-rg350-658080deeda79d20ea40643569fbcb072573e7cf.tar.bz2 scummvm-rg350-658080deeda79d20ea40643569fbcb072573e7cf.zip |
ALL: Avoid using is* macros from ctype.h
On some systems, passing signed chars to macros like isspace() etc. lead
to a runtime error. Hence, mark these macros as forbidden by default,
and introduce otherwise equivalent alternatives for them.
Diffstat (limited to 'common')
-rw-r--r-- | common/config-file.cpp | 6 | ||||
-rw-r--r-- | common/config-manager.cpp | 6 | ||||
-rw-r--r-- | common/forbidden.h | 43 | ||||
-rw-r--r-- | common/str.cpp | 8 | ||||
-rw-r--r-- | common/util.cpp | 43 | ||||
-rw-r--r-- | common/util.h | 11 | ||||
-rw-r--r-- | common/xmlparser.cpp | 10 | ||||
-rw-r--r-- | common/xmlparser.h | 2 |
8 files changed, 113 insertions, 16 deletions
diff --git a/common/config-file.cpp b/common/config-file.cpp index 81e0ae6b45..4224d7491d 100644 --- a/common/config-file.cpp +++ b/common/config-file.cpp @@ -30,7 +30,7 @@ namespace Common { bool ConfigFile::isValidName(const String &name) { const char *p = name.c_str(); - while (*p && (isalnum(static_cast<unsigned char>(*p)) || *p == '-' || *p == '_' || *p == '.')) + while (*p && (isAlnum(*p) || *p == '-' || *p == '_' || *p == '.')) p++; return *p == 0; } @@ -108,7 +108,7 @@ bool ConfigFile::loadFromStream(SeekableReadStream &stream) { // is, verify that it only consists of alphanumerics, // periods, dashes and underscores). Mohawk Living Books games // can have periods in their section names. - while (*p && (isalnum(static_cast<unsigned char>(*p)) || *p == '-' || *p == '_' || *p == '.')) + while (*p && (isAlnum(*p) || *p == '-' || *p == '_' || *p == '.')) p++; if (*p == '\0') @@ -131,7 +131,7 @@ bool ConfigFile::loadFromStream(SeekableReadStream &stream) { // Skip leading whitespaces const char *t = line.c_str(); - while (isspace(static_cast<unsigned char>(*t))) + while (isSpace(*t)) t++; // Skip empty lines / lines with only whitespace diff --git a/common/config-manager.cpp b/common/config-manager.cpp index c62dee8bea..d4035e8b92 100644 --- a/common/config-manager.cpp +++ b/common/config-manager.cpp @@ -29,7 +29,7 @@ static bool isValidDomainName(const Common::String &domName) { const char *p = domName.c_str(); - while (*p && (isalnum(static_cast<unsigned char>(*p)) || *p == '-' || *p == '_')) + while (*p && (isAlnum(*p) || *p == '-' || *p == '_')) p++; return *p == 0; } @@ -187,7 +187,7 @@ void ConfigManager::loadFromStream(SeekableReadStream &stream) { // Get the domain name, and check whether it's valid (that // is, verify that it only consists of alphanumerics, // dashes and underscores). - while (*p && (isalnum(static_cast<unsigned char>(*p)) || *p == '-' || *p == '_')) + while (*p && (isAlnum(*p) || *p == '-' || *p == '_')) p++; if (*p == '\0') @@ -205,7 +205,7 @@ void ConfigManager::loadFromStream(SeekableReadStream &stream) { // Skip leading whitespaces const char *t = line.c_str(); - while (isspace(static_cast<unsigned char>(*t))) + while (isSpace(*t)) t++; // Skip empty lines / lines with only whitespace diff --git a/common/forbidden.h b/common/forbidden.h index 8b5a2f738e..eec80bba59 100644 --- a/common/forbidden.h +++ b/common/forbidden.h @@ -317,6 +317,49 @@ #endif // FORBIDDEN_SYMBOL_EXCEPTION_unistd_h + +// +// Disable various symbols from ctype.h +// +#ifndef FORBIDDEN_SYMBOL_EXCEPTION_ctype_h + + #ifndef FORBIDDEN_SYMBOL_EXCEPTION_isalnum + #undef isalnum + #define isalnum(a) FORBIDDEN_SYMBOL_REPLACEMENT + #endif + + #ifndef FORBIDDEN_SYMBOL_EXCEPTION_isalpha + #undef isalpha + #define isalpha(a) FORBIDDEN_SYMBOL_REPLACEMENT + #endif + + #ifndef FORBIDDEN_SYMBOL_EXCEPTION_isdigit + #undef isdigit + #define isdigit(a) FORBIDDEN_SYMBOL_REPLACEMENT + #endif + + #ifndef FORBIDDEN_SYMBOL_EXCEPTION_isnumber + #undef isnumber + #define isnumber(a) FORBIDDEN_SYMBOL_REPLACEMENT + #endif + + #ifndef FORBIDDEN_SYMBOL_EXCEPTION_islower + #undef islower + #define islower(a) FORBIDDEN_SYMBOL_REPLACEMENT + #endif + + #ifndef FORBIDDEN_SYMBOL_EXCEPTION_isspace + #undef isspace + #define isspace(a) FORBIDDEN_SYMBOL_REPLACEMENT + #endif + + #ifndef FORBIDDEN_SYMBOL_EXCEPTION_isupper + #undef isupper + #define isupper(a) FORBIDDEN_SYMBOL_REPLACEMENT + #endif + +#endif // FORBIDDEN_SYMBOL_EXCEPTION_ctype_h + #ifndef FORBIDDEN_SYMBOL_EXCEPTION_mkdir #undef mkdir #define mkdir(a,b) FORBIDDEN_SYMBOL_REPLACEMENT diff --git a/common/str.cpp b/common/str.cpp index 32f4b44e79..a48a290c0b 100644 --- a/common/str.cpp +++ b/common/str.cpp @@ -405,13 +405,13 @@ void String::trim() { makeUnique(); // Trim trailing whitespace - while (_size >= 1 && isspace(static_cast<unsigned char>(_str[_size - 1]))) + while (_size >= 1 && isSpace(_str[_size - 1])) --_size; _str[_size] = 0; // Trim leading whitespace char *t = _str; - while (isspace((unsigned char)*t)) + while (isSpace(*t)) t++; if (t != _str) { @@ -606,14 +606,14 @@ String operator+(const String &x, char y) { } char *ltrim(char *t) { - while (isspace(static_cast<unsigned char>(*t))) + while (isSpace(*t)) t++; return t; } char *rtrim(char *t) { int l = strlen(t) - 1; - while (l >= 0 && isspace(static_cast<unsigned char>(t[l]))) + while (l >= 0 && isSpace(t[l])) t[l--] = 0; return t; } diff --git a/common/util.cpp b/common/util.cpp index 1c4df8b6cd..0a7d0f1e89 100644 --- a/common/util.cpp +++ b/common/util.cpp @@ -19,6 +19,15 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +#define FORBIDDEN_SYMBOL_EXCEPTION_isalnum +#define FORBIDDEN_SYMBOL_EXCEPTION_isalpha +#define FORBIDDEN_SYMBOL_EXCEPTION_isdigit +#define FORBIDDEN_SYMBOL_EXCEPTION_isnumber +#define FORBIDDEN_SYMBOL_EXCEPTION_islower +#define FORBIDDEN_SYMBOL_EXCEPTION_isspace +#define FORBIDDEN_SYMBOL_EXCEPTION_isupper + + #include "common/util.h" #include "common/translation.h" #include "common/config-manager.h" @@ -407,3 +416,37 @@ void updateGameGUIOptions(const String &options, const String &langOption) { } } // End of namespace Common + + +// +// TODO: Instead of a blind cast, we might want to verify +// if c equals EOS; and/or is in the range -255..+255; +// and return false if it isn't. +// +bool isAlnum(int c) { + return isalnum((byte)c); +} + +bool isAlpha(int c) { + return isalpha((byte)c); +} + +bool isDigit(int c) { + return isdigit((byte)c); +} + +bool isNumber(int c) { + return isnumber((byte)c); +} + +bool isLower(int c) { + return islower((byte)c); +} + +bool isSpace(int c) { + return isspace((byte)c); +} + +bool isUpper(int c) { + return isupper((byte)c); +} diff --git a/common/util.h b/common/util.h index dfa57d7259..1df7bbb9c3 100644 --- a/common/util.h +++ b/common/util.h @@ -34,6 +34,17 @@ ((((size_t)value) & ((alignment) - 1)) == 0) +//namespace{ +bool isAlnum(int c); +bool isAlpha(int c); +bool isDigit(int c); +bool isNumber(int c); +bool isLower(int c); +bool isSpace(int c); +bool isUpper(int c); +//} + + #ifdef MIN #undef MIN #endif diff --git a/common/xmlparser.cpp b/common/xmlparser.cpp index f768e44382..d37bfeb99a 100644 --- a/common/xmlparser.cpp +++ b/common/xmlparser.cpp @@ -263,7 +263,7 @@ bool XMLParser::vparseIntegerKey(const char *key, int count, va_list args) { int *num_ptr; while (count--) { - while (isspace(static_cast<unsigned char>(*key))) + while (isSpace(*key)) key++; num_ptr = va_arg(args, int*); @@ -271,7 +271,7 @@ bool XMLParser::vparseIntegerKey(const char *key, int count, va_list args) { key = parseEnd; - while (isspace(static_cast<unsigned char>(*key))) + while (isSpace(*key)) key++; if (count && *key++ != ',') @@ -463,10 +463,10 @@ bool XMLParser::parse() { } bool XMLParser::skipSpaces() { - if (!isspace(static_cast<unsigned char>(_char))) + if (!isSpace(_char)) return false; - while (_char && isspace(static_cast<unsigned char>(_char))) + while (_char && isSpace(_char)) _char = _stream->readByte(); return true; @@ -516,7 +516,7 @@ bool XMLParser::parseToken() { _char = _stream->readByte(); } - return isspace(static_cast<unsigned char>(_char)) != 0 || _char == '>' || _char == '=' || _char == '/'; + return isSpace(_char) != 0 || _char == '>' || _char == '=' || _char == '/'; } } // End of namespace Common diff --git a/common/xmlparser.h b/common/xmlparser.h index 93433b7132..81752543c2 100644 --- a/common/xmlparser.h +++ b/common/xmlparser.h @@ -295,7 +295,7 @@ protected: * in their name. */ virtual inline bool isValidNameChar(char c) { - return isalnum(static_cast<unsigned char>(c)) || c == '_'; + return isAlnum(c) || c == '_'; } /** |