From 8e3aafd30d14bcd586cc06a525e2dc2a8298c7b2 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Mon, 23 May 2011 18:32:42 +0200 Subject: COMMON: Provide our own implementations for scumm_str(n)icmp This takes up a tiny little bit of extra binary size, but gets rid of some awful #ifdef hackery. --- backends/platform/wince/missing/io.h | 1 - backends/platform/wince/portdefs.h | 4 ---- common/scummsys.h | 24 +++++++++--------------- common/str.cpp | 33 +++++++++++++++++++++++++++++++++ test/common/str.h | 17 +++++++++++++++++ 5 files changed, 59 insertions(+), 20 deletions(-) diff --git a/backends/platform/wince/missing/io.h b/backends/platform/wince/missing/io.h index 96bc6a9ea1..de492cac68 100644 --- a/backends/platform/wince/missing/io.h +++ b/backends/platform/wince/missing/io.h @@ -1,7 +1,6 @@ /* Header is not present in Windows CE SDK */ /* This stuff will live here until port configuration file is in place */ -#define stricmp _stricmp #define strdup _strdup #ifndef _FILE_DEFINED diff --git a/backends/platform/wince/portdefs.h b/backends/platform/wince/portdefs.h index 1f8b9bb3ca..64aa80abf2 100644 --- a/backends/platform/wince/portdefs.h +++ b/backends/platform/wince/portdefs.h @@ -31,8 +31,6 @@ int isprint(int c); int isspace(int c); char *strrchr(const char *s, int c); char *strdup(const char *s); -int _stricmp(const char *string1, const char *string2); -int stricmp(const char *string1, const char *string2); void assert(void *expression); void assert(int expression); long int strtol(const char *nptr, char **endptr, int base); @@ -53,8 +51,6 @@ void GetCurrentDirectory(int len, char *buf); #include #undef GetCurrentDirectory extern "C" void GetCurrentDirectory(int len, char *buf); -#define stricmp _stricmp -#define strnicmp _strnicmp #define snprintf _snprintf #define strdup _strdup #define fopen wce_fopen diff --git a/common/scummsys.h b/common/scummsys.h index 2420349245..5cf3ba4dad 100644 --- a/common/scummsys.h +++ b/common/scummsys.h @@ -107,21 +107,6 @@ #include "config.h" #endif -// -// Define scumm_stricmp and scumm_strnicmp -// -#if defined(_WIN32_WCE) || defined(_MSC_VER) - #define scumm_stricmp stricmp - #define scumm_strnicmp _strnicmp - #define snprintf _snprintf -#elif defined(__MINGW32__) || defined(__GP32__) || defined(__DS__) - #define scumm_stricmp stricmp - #define scumm_strnicmp strnicmp -#else - #define scumm_stricmp strcasecmp - #define scumm_strnicmp strncasecmp -#endif - // In the following we configure various targets, in particular those // which can't use our "configure" tool and hence don't use config.h. @@ -404,6 +389,15 @@ #endif +// +// Define scumm_stricmp and scumm_strnicmp +// +extern int scumm_stricmp(const char *s1, const char *s2); +extern int scumm_strnicmp(const char *s1, const char *s2, uint n); +#if defined(_WIN32_WCE) || defined(_MSC_VER) + // FIXME: Why is this necessary? + #define snprintf _snprintf +#endif // diff --git a/common/str.cpp b/common/str.cpp index 08a6cb6822..740e7b6a06 100644 --- a/common/str.cpp +++ b/common/str.cpp @@ -833,3 +833,36 @@ size_t strlcat(char *dst, const char *src, size_t size) { } } // End of namespace Common + +// Portable implementation of stricmp / strcasecmp / strcmpi. +// TODO: Rename this to Common::strcasecmp +int scumm_stricmp(const char *s1, const char *s2) { + byte l1, l2; + do { + // Don't use ++ inside tolower, in case the macro uses its + // arguments more than once. + l1 = (byte)*s1++; + l1 = tolower(l1); + l2 = (byte)*s2++; + l2 = tolower(l2); + } while (l1 == l2 && l1 != 0); + return l1 - l2; +} + +// Portable implementation of strnicmp / strncasecmp / strncmpi. +// TODO: Rename this to Common::strncasecmp +int scumm_strnicmp(const char *s1, const char *s2, uint n) { + byte l1, l2; + do { + if (n-- == 0) + return 0; // no difference found so far -> signal equality + + // Don't use ++ inside tolower, in case the macro uses its + // arguments more than once. + l1 = (byte)*s1++; + l1 = tolower(l1); + l2 = (byte)*s2++; + l2 = tolower(l2); + } while (l1 == l2 && l1 != 0); + return l1 - l2; +} diff --git a/test/common/str.h b/test/common/str.h index 5d9fe29af9..0dee16a493 100644 --- a/test/common/str.h +++ b/test/common/str.h @@ -378,4 +378,21 @@ class StringTestSuite : public CxxTest::TestSuite TS_ASSERT_EQUALS(Common::strlcat(test4, appendString, 11), strlen(resultString)); TS_ASSERT_EQUALS(strcmp(test4, resultString), 0); } + + void test_scumm_stricmp() { + TS_ASSERT_EQUALS(scumm_stricmp("abCd", "abCd"), 0); + TS_ASSERT_EQUALS(scumm_stricmp("abCd", "ABCd"), 0); + TS_ASSERT_LESS_THAN(scumm_stricmp("abCd", "ABCe"), 0); + TS_ASSERT_LESS_THAN(scumm_stricmp("abCd", "ABCde"), 0); + } + + void test_scumm_strnicmp() { + TS_ASSERT_EQUALS(scumm_strnicmp("abCd", "abCd", 3), 0); + TS_ASSERT_EQUALS(scumm_strnicmp("abCd", "ABCd", 4), 0); + TS_ASSERT_EQUALS(scumm_strnicmp("abCd", "ABCd", 5), 0); + TS_ASSERT_EQUALS(scumm_strnicmp("abCd", "ABCe", 3), 0); + TS_ASSERT_LESS_THAN(scumm_strnicmp("abCd", "ABCe", 4), 0); + TS_ASSERT_EQUALS(scumm_strnicmp("abCd", "ABCde", 4), 0); + TS_ASSERT_LESS_THAN(scumm_strnicmp("abCd", "ABCde", 5), 0); + } }; -- cgit v1.2.3