[Scummvm-cvs-logs] SF.net SVN: scummvm:[33141] scummvm/trunk/backends/platform/wince/missing/ missing.cpp

knakos at users.sourceforge.net knakos at users.sourceforge.net
Sun Jul 20 18:55:52 CEST 2008


Revision: 33141
          http://scummvm.svn.sourceforge.net/scummvm/?rev=33141&view=rev
Author:   knakos
Date:     2008-07-20 16:55:51 +0000 (Sun, 20 Jul 2008)

Log Message:
-----------
modified patch #1882942 - optimize and kill code for really old platforms

Modified Paths:
--------------
    scummvm/trunk/backends/platform/wince/missing/missing.cpp

Modified: scummvm/trunk/backends/platform/wince/missing/missing.cpp
===================================================================
--- scummvm/trunk/backends/platform/wince/missing/missing.cpp	2008-07-20 16:52:25 UTC (rev 33140)
+++ scummvm/trunk/backends/platform/wince/missing/missing.cpp	2008-07-20 16:55:51 UTC (rev 33141)
@@ -40,11 +40,21 @@
 
 void *bsearch(const void *key, const void *base, size_t nmemb,
 			  size_t size, int (*compar)(const void *, const void *)) {
-	size_t i;
+	// Perform binary search
+	size_t lo = 0;
+	size_t hi = nmemb;
+	while (lo < hi) {
+		size_t mid = (lo + hi) / 2;
+		const void *p = ((const char *)base) + mid * size;
+		int tmp = (*compar)(key, p);
+		if (tmp < 0)
+			hi = mid;
+		else if (tmp > 0)
+			lo = mid + 1;
+		else
+			return (void *)p;
+	}
 
-	for (i=0; i<nmemb; i++)
-		if (compar(key, (void*)((size_t)base + size * i)) == 0)
-			return (void*)((size_t)base + size * i);
 	return NULL;
 }
 
@@ -290,12 +300,6 @@
 	return 1;
 }
 
-/* in our case unlink is the same as remove */
-int unlink(const char* path)
-{
-	return remove(path);
-}
-
 /* Make directory, Unix style */
 void mkdir(char* dirname, int mode)
 {
@@ -320,374 +324,41 @@
 	CreateDirectory(pathUnc, 0);
 }
 
-/* Used in DEVICES.C and UI.C for some purpose. Not critical in this port */
-int system(const char* path) { return 0; }
-
-#if 0
-
-char *tmpnam(char *string)
-{
-	TCHAR pTemp[MAX_PATH+1];
-	static char buffer[MAX_PATH+1];
-	GetTempFileName(TEXT("."), TEXT("A8_"), 0, pTemp);
-	WideCharToMultiByte(CP_ACP, 0, pTemp, -1, buffer, MAX_PATH, NULL, NULL);
-
-	if (string)
-	{
-		strcpy(string, buffer);
-		return string;
-	}
-	else
-		return buffer;
-}
-
-FILE *tmpfile()
-{
-	TCHAR pTemp[MAX_PATH+1];
-	if (!GetTempFileName(TEXT("."), TEXT("A8_"), 0, pTemp))
-		return _wfopen(pTemp, TEXT("w+b"));
-	else
-		return 0;
-}
-
-#endif
-
-void rewind(FILE *stream)
-{
-	fseek(stream, 0, SEEK_SET);
-}
-
-
-#if _WIN32_WCE < 300
-
-int isalnum(int c) {
-	return ((c >= 'A' && c <= 'Z') ||
-			(c >= 'a' && c <= 'z') ||
-			(c >= '0' && c <= '9'));
-}
-
-char *_strdup(const char *strSource)
-#else
 char *strdup(const char *strSource)
-#endif
 {
 	char* buffer;
-	buffer = (char*)malloc(strlen(strSource)+1);
+	size_z len = strlen(strSource)+1;
+	buffer = (char*)malloc(len);
 	if (buffer)
-		strcpy(buffer, strSource);
+		memcpy(buffer, strSource, len);
 	return buffer;
 }
 
-/* Very limited implementation of sys/time.h */
-void usleep(long usec)
-{
-	long msec = usec/1000;
-	if (msec <= 0)
-		Sleep(0);
-	else
-		Sleep(msec);
-}
-
-/* This may provide for better sync mechanism */
-unsigned int clock()
-{
-	return GetTickCount();
-}
-
-/* And why do people use this? */
-#if _WIN32_WCE >= 300
-void abort()
-{
-	exit(1);
-}
-#endif
-
-/*
-IMHO, no project should use this one, it is not portable at all. This implementation
-at least allows some projects to work.
-*/
-char* getenv(char* name)
-{
-	static char buffer[MAX_PATH+1];
-	if (strcmp(name, "HOME") == 0 || strcmp(name, "HOMEDIR") == 0)
-	{
-		getcwd(buffer, MAX_PATH);
-		return buffer;
-	}
-	else
-		return "";
-}
-
-#if _WIN32_WCE < 300 || defined(_TEST_HPC_STDIO)
-
-void *calloc(size_t n, size_t s) {
-	void *result = malloc(n * s);
-	if (result)
-		memset(result, 0, n * s);
-
-	return result;
-}
-
-char *strpbrk(const char *s, const char *accept) {
-	int i;
-
-	if (!s || !accept)
-		return NULL;
-
-	for (i=0; i<strlen(s); i++) {
-		int j;
-		for (j=0; j<strlen(accept); j++)
-			if (s[i] == accept[j])
-				return (char*)&s[i];
-	}
-
-	return NULL;
-}
-
-#ifndef _TEST_HPC_STDIO
-
-int isdigit(int c) {
-	return (c >='0' && c <= '9');
-}
-
-int isprint(int c) {
-	return (c >= ' ' && c <= '~');
-}
-
-int isspace(int c) {
-	return (c == ' ');
-}
-
-#endif
-
-#ifndef WIN32_PLATFORM_HPCPRO
-
-
-int printf(const char *format, ...) {
-	// useless anyway :)
-	return 0;
-}
-
-FILE *fopen(const char *path, const char *mode) {
-	TCHAR tempo[MAX_PATH];
-	HANDLE result;
-	bool writeAccess = (mode[0] == 'W' || mode[0] == 'w');
-
-	MultiByteToWideChar(CP_ACP, 0, path, strlen(path) + 1, tempo, sizeof(tempo));
-
-	result = CreateFile(tempo, ( writeAccess ? GENERIC_WRITE : GENERIC_READ), 0, NULL, (writeAccess ? CREATE_ALWAYS : OPEN_EXISTING), FILE_ATTRIBUTE_NORMAL, NULL);
-	if (result == INVALID_HANDLE_VALUE)
-		return NULL;
-	else
-		return (FILE*)result;
-}
-
-FILE * _wfopen(const TCHAR *path, const TCHAR *mode) {
-	HANDLE result;
-	bool writeAccess = (mode[0] == 'W' || mode[0] == 'w');
-	result = CreateFile(path, ( writeAccess ? GENERIC_WRITE : GENERIC_READ), 0, NULL, (writeAccess ? CREATE_ALWAYS : OPEN_EXISTING), FILE_ATTRIBUTE_NORMAL, NULL);
-	if (result == INVALID_HANDLE_VALUE)
-		return NULL;
-	else
-		return (FILE*)result;
-}
-
-FILE *_wfreopen(const TCHAR *path, const TCHAR *mode, FILE *stream) {
-	fclose(stream);
-	stream = _wfopen(path, mode);
-	return stream;
-}
-
-int fclose(FILE *stream) {
-	CloseHandle((HANDLE)stream);
-	return 1;
-}
-
-int fseek(FILE *stream, long offset, int whence) {
-	SetFilePointer((HANDLE)stream, offset, NULL, (whence == SEEK_CUR ? FILE_CURRENT : whence == SEEK_END ? FILE_END : FILE_BEGIN));
-	return 0;
-}
-
-long ftell(FILE *stream) {
-	return (SetFilePointer((HANDLE)stream, 0, NULL, FILE_CURRENT));
-}
-
-size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) {
-	DWORD sizeWritten;
-
-	WriteFile((HANDLE)stream, ptr, size * nmemb, &sizeWritten, NULL);
-
-	if (size != 0)
-		return sizeWritten / size;
-	else
-		return 0;
-}
-
-size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
-	DWORD sizeRead;
-
-	ReadFile((HANDLE)stream, ptr, size * nmemb, &sizeRead, NULL);
-
-	if (size != 0)
-		return sizeRead / size;
-	else
-		return 0;
-}
-
-int fgetc(FILE *stream) {
-	unsigned char c;
-	if (fread(&c, 1, 1, stream) != 1)
-		return -1;
-	else
-		return c;
-}
-
-char *fgets(char *s, int size, FILE *stream) {
-	int i = 0;
-	char tempo[1];
-
-	memset(s, 0, size);
-	while (fread(tempo, 1, 1, stream)) {
-		//if (tempo[0] == '\r')
-		//	break;
-		if (tempo[0] == '\r')
-			continue;
-		s[i++] = tempo[0];
-		if (tempo[0] == '\n')
-			break;
-		if (i == size)
-			break;
-	}
-	if (!i)
-		return NULL;
-	else
-		return s;
-}
-
-int feof(FILE *stream) {
-	DWORD fileSize;
-	DWORD filePos;
-	fileSize = GetFileSize((HANDLE)stream, NULL);
-	filePos = SetFilePointer((HANDLE)stream, 0, 0, FILE_CURRENT);
-	return (filePos == 0xFFFFFFFF || filePos > (fileSize - 1));
-}
-
-int ferror(FILE *stream) {
-	return 0; // FIXME !
-}
-
-int fprintf(FILE *stream, const char *format, ...) {
-	char buf[1024];
-	va_list va;
-
-	va_start(va, format);
-	vsnprintf(buf, 1024, format, va);
-	va_end(va);
-
-	if (buf[strlen(buf) - 1] == '\n') {
-		int i = strlen(buf) - 1;
-		buf[i] = '\r';
-		buf[i + 1] = '\n';
-		buf[i + 2] = 0;
-	}
-
-	return fwrite(buf, 1, strlen(buf), stream);
-}
-
-FILE* _getstdfilex(int) {
-	return NULL;
-}
-
-void clearerr(FILE *stream) {
-}
-
-int fflush(FILE *stream) {
-	return 0;
-}
-
-#endif
-
-int stricmp( const char *string1, const char *string2 ) {
-	char src[4096];
-	char dest[4096];
-	int i;
-
-	for (i=0; i<strlen(string1); i++)
-		if (string1[i] >= 'A' && string1[i] <= 'Z')
-			src[i] = string1[i] + 32;
-		else
-			src[i] = string1[i];
-	src[i] = 0;
-
-	for (i=0; i<strlen(string2); i++)
-		if (string2[i] >= 'A' && string2[i] <= 'Z')
-			dest[i] = string2[i] + 32;
-		else
-			dest[i] = string2[i];
-	dest[i] = 0;
-
-	return strcmp(src, dest);
-}
-
-char *strrchr(const char *s, int c) {
-	int i;
-
-	for (i = strlen(s) - 1; i > 0; i--)
-		if (s[i] == c)
-			return (char*)(s + i);
-
-	return NULL;
-}
-
-long int strtol(const char *nptr, char **endptr, int base) {
-	// not correct but that's all we are using
-
-	long int result;
-	sscanf(nptr, "%ld", &result);
-	return result;
-}
-
-
-#endif
-
-
 // gcc build only functions follow
 #else // defined(__GNUC__)
 
 #ifndef __MINGW32CE__
-int islower(int c)
-{
+int islower(int c) {
 	return (c>='a' && c<='z');
 }
 
-int isspace(int c)
-{
+int isspace(int c) {
 	return (c==' ' || c=='\f' || c=='\n' || c=='\r' || c=='\t' || c=='\v');
 }
 
-int isalpha(int c)
-{
-	return (islower(c) || (c>='A' && c<='Z'));
+int isalpha(int c) {
+	return ((c>='a' && c<='z') || (c>='A' && c<='Z'));
 }
 
-int isalnum(int c)
-{
-	return (isalpha(c) || (c>='0' && c<='9'));
+int isalnum(int c) {
+	return ((c>='a' && c<='z') || (c>='A' && c<='Z') || (c>='0' && c<='9'));
 }
 
-int isprint(int c)
-{
-	static char punct[] = "!\"#%&'();<=>?[\\]*+,-./:^_{|}~";
-	int i = 0, flag = 0;
-	while ((punct[i] != 0) && (flag = (punct[i] != c)))
-		i++;
-	return (isalnum(c) || flag);
+int isprint(int c) {
+	//static const char punct[] = "!\"#%&'();<=>?[\\]*+,-./:^_{|}~";
+	//return (isalnum(c) || strchr(punct, c));
+	return (32 <= c && c <= 126);	// based on BSD manpage
 }
-
-extern "C" int atexit(void (*function)(void))
-{
-	return 0;
-}
 #endif
 
 #endif


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list