Skip to content

Commit

Permalink
COMMON: Provide our own implementations for scumm_str(n)icmp
Browse files Browse the repository at this point in the history
This takes up a tiny little bit of extra binary size, but gets
rid of some awful #ifdef hackery.
  • Loading branch information
fingolfin committed May 23, 2011
1 parent 3931e1d commit 8e3aafd
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 20 deletions.
1 change: 0 additions & 1 deletion 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
Expand Down
4 changes: 0 additions & 4 deletions backends/platform/wince/portdefs.h
Expand Up @@ -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);
Expand All @@ -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
Expand Down
24 changes: 9 additions & 15 deletions common/scummsys.h
Expand Up @@ -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.
Expand Down Expand Up @@ -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

This comment has been minimized.

Copy link
@lordhoto

lordhoto May 23, 2011

Contributor

Probably because MSVC's libc only features a function called _snprintf, but not sprintf (http://msdn.microsoft.com/en-us/library/2ts7cx93%28v=VS.100%29.aspx). I am not sure why we need it for WinCE though, maybe cegcc's libc only features _snprintf too.

This comment has been minimized.

Copy link
@fingolfin

fingolfin via email May 24, 2011

Author Contributor

This comment has been minimized.

Copy link
@lordhoto

lordhoto May 24, 2011

Contributor

Thanks, that explains it. I thought snprintf was part of ISO C... but I guess I either was wrong, or else Microsoft does not care about standards... Guess neither would surprise me :)

snprintf is C99. But I don't think MSVC even aims at supporting that ;-).

Well, or maybe it's just a leftover from the days the WinCE port was compiled using MSVC (the idea would be that first somebody added the define for WINCE, then somebody else noticed it was also needed on windows, and used the MSVC thing... alas, this speculation is rather moot ;).

Just tried with my local cegcc installation, it seems it supports both _snprintf and snprintf, so I guess it might really be a leftover from the eMSVC days.

#endif


//
Expand Down
33 changes: 33 additions & 0 deletions common/str.cpp
Expand Up @@ -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;
}
17 changes: 17 additions & 0 deletions test/common/str.h
Expand Up @@ -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);
}
};

0 comments on commit 8e3aafd

Please sign in to comment.