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.
This commit is contained in:
Max Horn 2011-05-23 18:32:42 +02:00
parent 3931e1dc50
commit 8e3aafd30d
5 changed files with 59 additions and 20 deletions

View file

@ -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

View file

@ -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 <math.h>
#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

View file

@ -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
//

View file

@ -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;
}

View file

@ -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);
}
};