From b50342ad13c68cc1daf5deaddd00e103397c9704 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Sun, 18 May 2003 07:52:28 +0000 Subject: [PATCH] PocketPC fixes (thanks, David Hedbor!) --- archivers/zip.c | 11 ++ physfs_byteorder.c | 2 +- platform/pocketpc.c | 288 +++++++++++++++++++++++++------------------- 3 files changed, 177 insertions(+), 124 deletions(-) diff --git a/archivers/zip.c b/archivers/zip.c index c8bcd15..383b482 100644 --- a/archivers/zip.c +++ b/archivers/zip.c @@ -16,8 +16,10 @@ #include #include #include +#ifndef _WIN32_WCE #include #include +#endif #include "physfs.h" #include "zlib.h" @@ -186,7 +188,9 @@ static const char *zlib_error_string(int rc) { case Z_OK: return(NULL); /* not an error. */ case Z_STREAM_END: return(NULL); /* not an error. */ +#ifndef _WIN32_WCE case Z_ERRNO: return(strerror(errno)); +#endif case Z_NEED_DICT: return(ERR_ZLIB_NEED_DICT); case Z_DATA_ERROR: return(ERR_ZLIB_DATA_ERROR); case Z_MEM_ERROR: return(ERR_ZLIB_MEMORY_ERROR); @@ -894,6 +898,12 @@ static int zip_has_symlink_attr(ZIPentry *entry, PHYSFS_uint32 extern_attr) static PHYSFS_sint64 zip_dos_time_to_physfs_time(PHYSFS_uint32 dostime) { +#ifdef _WIN32_WCE + /* We have no struct tm and no mktime right now. + FIXME: This should probably be fixed at some point. + */ + return -1; +#else PHYSFS_uint32 dosdate; struct tm unixtime; memset(&unixtime, '\0', sizeof (unixtime)); @@ -915,6 +925,7 @@ static PHYSFS_sint64 zip_dos_time_to_physfs_time(PHYSFS_uint32 dostime) unixtime.tm_isdst = -1; return((PHYSFS_sint64) mktime(&unixtime)); +#endif } /* zip_dos_time_to_physfs_time */ diff --git a/physfs_byteorder.c b/physfs_byteorder.c index 989051b..593acf8 100644 --- a/physfs_byteorder.c +++ b/physfs_byteorder.c @@ -24,7 +24,7 @@ #if defined(__i386__) || defined(__ia64__) || defined(WIN32) || \ (defined(__alpha__) || defined(__alpha)) || \ - defined(__arm__) || \ + defined(__arm__) || defined(ARM) || \ (defined(__mips__) && defined(__MIPSEL__)) || \ defined(__SYMBIAN32__) || \ defined(__x86_64__) || \ diff --git a/platform/pocketpc.c b/platform/pocketpc.c index 3561e9d..78c9470 100644 --- a/platform/pocketpc.c +++ b/platform/pocketpc.c @@ -26,6 +26,7 @@ typedef struct const char *__PHYSFS_platformDirSeparator = "\\"; +static char *userDir = NULL; /* * Figure out what the last failing Win32 API call was, and @@ -40,17 +41,17 @@ static const char *win32strerror(void) TCHAR *ptr = msgbuf; FormatMessage( - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, - GetLastError(), - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */ - msgbuf, - sizeof (msgbuf) / sizeof (TCHAR), - NULL - ); + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + GetLastError(), + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */ + msgbuf, + sizeof (msgbuf) / sizeof (TCHAR), + NULL + ); - /* chop off newlines. */ + /* chop off newlines. */ for (ptr = msgbuf; *ptr; ptr++) { if ((*ptr == '\n') || (*ptr == '\r')) @@ -66,62 +67,112 @@ static const char *win32strerror(void) static char *UnicodeToAsc(const wchar_t *w_str) { - char *str=NULL; + char *str=NULL; - if(w_str!=NULL) - { - int len=wcslen(w_str)+1; - str=(char *)malloc(len); - - if(WideCharToMultiByte(CP_ACP,0,w_str,-1,str,len,NULL,NULL)==0) - { //Conversion failed - free(str); - return NULL; - } - else - { //Conversion successful - return(str); - } + if(w_str!=NULL) + { + int len=wcslen(w_str)+1; + str=(char *)malloc(len); + if(WideCharToMultiByte(CP_ACP,0,w_str,-1,str,len,NULL,NULL)==0) + { //Conversion failed + free(str); + return NULL; } else - { //Given NULL string - return NULL; + { //Conversion successful + return(str); } + + } + else + { //Given NULL string + return NULL; + } } static wchar_t *AscToUnicode(const char *str) { - wchar_t *w_str=NULL; - if(str!=NULL) + wchar_t *w_str=NULL; + if(str!=NULL) + { + int len=strlen(str)+1; + w_str=(wchar_t *)malloc(sizeof(wchar_t)*len); + if(MultiByteToWideChar(CP_ACP,0,str,-1,w_str,len)==0) { - int len=strlen(str)+1; - w_str=(wchar_t *)malloc(sizeof(wchar_t)*len); - if(MultiByteToWideChar(CP_ACP,0,str,-1,w_str,len)==0) - { - free(w_str); - return NULL; - } - else - { - return(w_str); - } + free(w_str); + return NULL; } else { - return NULL; + return(w_str); } + } + else + { + return NULL; + } } +static char *getExePath() +{ + DWORD buflen; + int success = 0; + TCHAR *ptr = NULL; + TCHAR *retval = (TCHAR *) malloc(sizeof (TCHAR) * (MAX_PATH + 1)); + char *charretval; + BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL); + + retval[0] = _T('\0'); + buflen = GetModuleFileName(NULL, retval, MAX_PATH + 1); + if (buflen <= 0) { + __PHYSFS_setError(win32strerror()); + } else { + retval[buflen] = '\0'; /* does API always null-terminate this? */ + ptr = retval+buflen; + while( ptr != retval ) + { + if( *ptr != _T('\\') ) { + *ptr-- = _T('\0'); + } else { + break; + } + } + success = 1; + } /* else */ + + if (!success) + { + free(retval); + return(NULL); /* physfs error message will be set, above. */ + } /* if */ + + /* free up the bytes we didn't actually use. */ + ptr = (TCHAR *) realloc(retval, sizeof(TCHAR) * _tcslen(retval) + 1); + if (ptr != NULL) + retval = ptr; + + charretval = UnicodeToAsc(retval); + free(retval); + if(charretval == NULL) { + BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL); + } + + return(charretval); /* w00t. */ +} /* getExePath */ + int __PHYSFS_platformInit(void) { + userDir = getExePath(); + BAIL_IF_MACRO(userDir == NULL, NULL, 0); /* failed? */ return(1); /* always succeed. */ } /* __PHYSFS_platformInit */ int __PHYSFS_platformDeinit(void) { + free(userDir); return(1); /* always succeed. */ } /* __PHYSFS_platformDeinit */ @@ -134,8 +185,7 @@ char **__PHYSFS_platformDetectAvailableCDs(void) char *__PHYSFS_platformCalcBaseDir(const char *argv0) { - return("\\"); -// BAIL_MACRO(ERR_NOT_IMPLEMENTED, NULL); + return(getExePath()); } /* __PHYSFS_platformCalcBaseDir */ @@ -147,7 +197,7 @@ char *__PHYSFS_platformGetUserName(void) char *__PHYSFS_platformGetUserDir(void) { - return("\\"); + return userDir; BAIL_MACRO(ERR_NOT_IMPLEMENTED, NULL); } /* __PHYSFS_platformGetUserDir */ @@ -160,32 +210,24 @@ PHYSFS_uint64 __PHYSFS_platformGetThreadID(void) int __PHYSFS_platformStricmp(const char *x, const char *y) { - const char *p1 = x, *p2 = y; - int r = 0; - - while((*p1) && (*p2) && (toupper(*p1++) == toupper(*p2++))) ++r; - - r = (!((*p1) || (*p2)) ? (0) : ((toupper(*p1) > toupper(*p2)) ? - (r + 1) : -(r + 1))); - - return(r); + return(_stricmp(x, y)); } /* __PHYSFS_platformStricmp */ int __PHYSFS_platformExists(const char *fname) { - int retval=0; + int retval=0; - wchar_t *w_fname=AscToUnicode(fname); + wchar_t *w_fname=AscToUnicode(fname); - if(w_fname!=NULL) - { - retval=(GetFileAttributes(w_fname) != INVALID_FILE_ATTRIBUTES); - free(w_fname); - } + if(w_fname!=NULL) + { + retval=(GetFileAttributes(w_fname) != INVALID_FILE_ATTRIBUTES); + free(w_fname); + } - return(retval); + return(retval); } /* __PHYSFS_platformExists */ @@ -197,17 +239,17 @@ int __PHYSFS_platformIsSymLink(const char *fname) int __PHYSFS_platformIsDirectory(const char *fname) { - int retval=0; + int retval=0; - wchar_t *w_fname=AscToUnicode(fname); + wchar_t *w_fname=AscToUnicode(fname); - if(w_fname!=NULL) - { - retval=((GetFileAttributes(w_fname) & FILE_ATTRIBUTE_DIRECTORY) != 0); - free(w_fname); - } + if(w_fname!=NULL) + { + retval=((GetFileAttributes(w_fname) & FILE_ATTRIBUTE_DIRECTORY) != 0); + free(w_fname); + } - return(retval); + return(retval); } /* __PHYSFS_platformIsDirectory */ @@ -216,8 +258,8 @@ char *__PHYSFS_platformCvtToDependent(const char *prepend, const char *append) { int len = ((prepend) ? strlen(prepend) : 0) + - ((append) ? strlen(append) : 0) + - strlen(dirName) + 1; + ((append) ? strlen(append) : 0) + + strlen(dirName) + 1; char *retval = malloc(len); char *p; @@ -255,7 +297,7 @@ LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname, HANDLE dir; WIN32_FIND_DATA ent; char *SearchPath; - wchar_t *w_SearchPath; + wchar_t *w_SearchPath; size_t len = strlen(dirname); /* Allocate a new string for path, maybe '\\', "*", and NULL terminator */ @@ -275,16 +317,16 @@ LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname, /* Append the "*" to the end of the string */ strcat(SearchPath, "*"); - w_SearchPath=AscToUnicode(SearchPath); + w_SearchPath=AscToUnicode(SearchPath); dir = FindFirstFile(w_SearchPath, &ent); - free(w_SearchPath); - free(SearchPath); + free(w_SearchPath); + free(SearchPath); - if(dir == INVALID_HANDLE_VALUE) - { - return NULL; - } + if(dir == INVALID_HANDLE_VALUE) + { + return NULL; + } do { @@ -298,7 +340,7 @@ LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname, if (l == NULL) break; - l->str=UnicodeToAsc(ent.cFileName); + l->str=UnicodeToAsc(ent.cFileName); if (l->str == NULL) { @@ -323,38 +365,38 @@ LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname, char *__PHYSFS_platformCurrentDir(void) { - return("\\"); + return("\\"); } /* __PHYSFS_platformCurrentDir */ char *__PHYSFS_platformRealPath(const char *path) { - char *retval=(char *)malloc(strlen(path)+1); + char *retval=(char *)malloc(strlen(path)+1); - strcpy(retval,path); + strcpy(retval,path); - return(retval); + return(retval); } /* __PHYSFS_platformRealPath */ int __PHYSFS_platformMkDir(const char *path) { - wchar_t *w_path = AscToUnicode(path); - if(w_path!=NULL) + wchar_t *w_path = AscToUnicode(path); + if(w_path!=NULL) + { + DWORD rc = CreateDirectory(w_path, NULL); + free(w_path); + if(rc==0) { - DWORD rc = CreateDirectory(w_path, NULL); - free(w_path); - if(rc==0) - { - return(0); - } - return(1); - } - else - { - return(0); + return(0); } + return(1); + } + else + { + return(0); + } } /* __PHYSFS_platformMkDir */ @@ -362,30 +404,30 @@ static void *doOpen(const char *fname, DWORD mode, DWORD creation, int rdonly) { HANDLE fileHandle; winCEfile *retval; - wchar_t *w_fname=AscToUnicode(fname); + wchar_t *w_fname=AscToUnicode(fname); fileHandle = CreateFile(w_fname, mode, FILE_SHARE_READ, NULL, creation, FILE_ATTRIBUTE_NORMAL, NULL); - free(w_fname); + free(w_fname); - if(fileHandle==INVALID_HANDLE_VALUE) - { - return NULL; - } + if(fileHandle==INVALID_HANDLE_VALUE) + { + return NULL; + } BAIL_IF_MACRO(fileHandle == INVALID_HANDLE_VALUE, win32strerror(), NULL); - retval = malloc(sizeof (winCEfile)); - if (retval == NULL) - { - CloseHandle(fileHandle); - BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL); - } /* if */ + retval = malloc(sizeof (winCEfile)); + if (retval == NULL) + { + CloseHandle(fileHandle); + BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL); + } /* if */ - retval->readonly = rdonly; - retval->handle = fileHandle; - return(retval); + retval->readonly = rdonly; + retval->handle = fileHandle; + return(retval); } /* doOpen */ @@ -432,7 +474,7 @@ PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer, /*!!! - uint32 might be a greater # than DWORD */ if(!ReadFile(FileHandle, buffer, count * size, &CountOfBytesRead, NULL)) { - retval=-1; + retval=-1; } /* if */ else { @@ -457,7 +499,7 @@ PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer, /*!!! - uint32 might be a greater # than DWORD */ if(!WriteFile(FileHandle, buffer, count * size, &CountOfBytesWritten, NULL)) { - retval=-1; + retval=-1; } /* if */ else { @@ -466,7 +508,7 @@ PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer, retval = CountOfBytesWritten / size; } /* else */ - return(retval); + return(retval); } /* __PHYSFS_platformWrite */ @@ -479,7 +521,7 @@ int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos) /* Get the high order 32-bits of the position */ //HighOrderPos = HIGHORDER_UINT64(pos); - HighOrderPos = (unsigned long)(pos>>32); + HighOrderPos = (unsigned long)(pos>>32); /*!!! SetFilePointer needs a signed 64-bit value. */ /* Move pointer "pos" count from start of file */ @@ -487,9 +529,9 @@ int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos) &HighOrderPos, FILE_BEGIN); if ((rc == INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR)) - { + { BAIL_MACRO(win32strerror(), 0); - } + } return(1); /* No error occured */ } /* __PHYSFS_platformSeek */ @@ -579,19 +621,19 @@ int __PHYSFS_platformClose(void *opaque) int __PHYSFS_platformDelete(const char *path) { - wchar_t *w_path=AscToUnicode(path); + wchar_t *w_path=AscToUnicode(path); /* If filename is a folder */ if (GetFileAttributes(w_path) == FILE_ATTRIBUTE_DIRECTORY) { - int retval=!RemoveDirectory(w_path); - free(w_path); + int retval=!RemoveDirectory(w_path); + free(w_path); BAIL_IF_MACRO(retval, win32strerror(), 0); } /* if */ else { - int retval=!DeleteFile(w_path); - free(w_path); + int retval=!DeleteFile(w_path); + free(w_path); BAIL_IF_MACRO(retval, win32strerror(), 0); } /* else */