platform/pocketpc.c
changeset 566 1e640d4acab9
parent 501 fa1298d1157b
child 578 bff1af8455ca
--- a/platform/pocketpc.c	Fri May 16 15:32:46 2003 +0000
+++ b/platform/pocketpc.c	Sun May 18 07:52:28 2003 +0000
@@ -26,6 +26,7 @@
 
 
 const char *__PHYSFS_platformDirSeparator = "\\";
+static char *userDir = NULL;
 
 /*
  * Figure out what the last failing Win32 API call was, and
@@ -40,17 +41,17 @@
     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 char *UnicodeToAsc(const wchar_t *w_str)
 {
-	char *str=NULL;
-
-	if(w_str!=NULL)
-	{
-		int len=wcslen(w_str)+1;
-		str=(char *)malloc(len);
+    char *str=NULL;
 
-		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_platformCalcBaseDir(const char *argv0)
 {
-	return("\\");
-//    BAIL_MACRO(ERR_NOT_IMPLEMENTED, NULL);
+    return(getExePath());
 } /* __PHYSFS_platformCalcBaseDir */
 
 
@@ -147,7 +197,7 @@
 
 char *__PHYSFS_platformGetUserDir(void)
 {
-	return("\\");
+    return userDir;
     BAIL_MACRO(ERR_NOT_IMPLEMENTED, NULL);
 } /* __PHYSFS_platformGetUserDir */
 
@@ -160,32 +210,24 @@
 
 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_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 @@
                                       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 @@
     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 @@
     /* 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 @@
         if (l == NULL)
             break;
 
-		l->str=UnicodeToAsc(ent.cFileName);
+	l->str=UnicodeToAsc(ent.cFileName);
 
         if (l->str == NULL)
         {
@@ -323,38 +365,38 @@
 
 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);
+	    return(0);
 	}
-	else
-	{
-		return(0);
-	}
+	return(1);
+    }
+    else
+    {
+	return(0);
+    }
 } /* __PHYSFS_platformMkDir */
 
 
@@ -362,30 +404,30 @@
 {
     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 @@
     /*!!! - 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 @@
     /*!!! - 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 @@
         retval = CountOfBytesWritten / size;
     } /* else */
 
-	return(retval);
+    return(retval);
 
 } /* __PHYSFS_platformWrite */
 
@@ -479,7 +521,7 @@
 
     /* 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 @@
                         &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_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 */