2001-08-23 17:22:52 +02:00
|
|
|
/*
|
2001-08-23 18:02:51 +02:00
|
|
|
* Win32 support routines for PhysicsFS.
|
2001-08-23 17:22:52 +02:00
|
|
|
*
|
|
|
|
* Please see the file LICENSE in the source's root directory.
|
|
|
|
*
|
2002-04-13 02:56:12 +02:00
|
|
|
* This file written by Ryan C. Gordon, and made sane by Gregory S. Read.
|
2001-08-23 17:22:52 +02:00
|
|
|
*/
|
|
|
|
|
2002-05-10 11:25:25 +02:00
|
|
|
#if HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2002-07-10 23:14:33 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
|
2001-08-23 17:22:52 +02:00
|
|
|
#include <windows.h>
|
|
|
|
#include <stdio.h>
|
2001-08-29 04:41:47 +02:00
|
|
|
#include <stdlib.h>
|
2002-06-30 00:05:12 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2001-08-29 04:41:47 +02:00
|
|
|
#include <ctype.h>
|
2002-06-07 04:33:45 +02:00
|
|
|
#include <time.h>
|
2002-06-10 08:33:15 +02:00
|
|
|
#include <assert.h>
|
2002-05-06 05:26:10 +02:00
|
|
|
|
2001-08-23 17:22:52 +02:00
|
|
|
#define __PHYSICSFS_INTERNAL__
|
|
|
|
#include "physfs_internal.h"
|
|
|
|
|
2002-06-30 00:05:12 +02:00
|
|
|
#ifdef _MSC_VER /* for Cygwin, etc. */
|
|
|
|
#define alloca _alloca
|
2002-06-29 12:23:17 +02:00
|
|
|
#endif
|
|
|
|
|
2002-04-03 06:15:24 +02:00
|
|
|
#define LOWORDER_UINT64(pos) (PHYSFS_uint32)(pos & 0x00000000FFFFFFFF)
|
|
|
|
#define HIGHORDER_UINT64(pos) (PHYSFS_uint32)(pos & 0xFFFFFFFF00000000)
|
|
|
|
|
2002-06-30 00:05:12 +02:00
|
|
|
/* GetUserProfileDirectory() is only available on >= NT4 (no 9x/ME systems!) */
|
|
|
|
typedef BOOL (STDMETHODCALLTYPE FAR * LPFNGETUSERPROFILEDIR) (
|
|
|
|
HANDLE hToken,
|
|
|
|
LPTSTR lpProfileDir,
|
|
|
|
LPDWORD lpcchSize);
|
|
|
|
|
|
|
|
/* GetFileAttributesEx() is only available on >= Win98 or WinNT4 ... */
|
|
|
|
typedef BOOL (STDMETHODCALLTYPE FAR * LPFNGETFILEATTRIBUTESEX) (
|
|
|
|
LPCTSTR lpFileName,
|
|
|
|
GET_FILEEX_INFO_LEVELS fInfoLevelId,
|
|
|
|
LPVOID lpFileInformation);
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
HANDLE handle;
|
|
|
|
int readonly;
|
|
|
|
} win32file;
|
2001-08-23 17:22:52 +02:00
|
|
|
|
2002-06-30 00:05:12 +02:00
|
|
|
const char *__PHYSFS_platformDirSeparator = "\\";
|
|
|
|
static LPFNGETFILEATTRIBUTESEX pGetFileAttributesEx = NULL;
|
|
|
|
static HANDLE libKernel32 = NULL;
|
|
|
|
static char *userDir = NULL;
|
2001-08-23 17:22:52 +02:00
|
|
|
|
2002-06-30 00:05:12 +02:00
|
|
|
/*
|
|
|
|
* Users without the platform SDK don't have this defined. The original docs
|
|
|
|
* for SetFilePointer() just said to compare with 0xFFFFFFFF, so this should
|
|
|
|
* work as desired
|
|
|
|
*/
|
2002-05-06 05:26:10 +02:00
|
|
|
#ifndef INVALID_SET_FILE_POINTER
|
2002-06-30 00:05:12 +02:00
|
|
|
# define INVALID_SET_FILE_POINTER 0xFFFFFFFF
|
2002-05-06 05:26:10 +02:00
|
|
|
#endif
|
|
|
|
|
2002-06-30 00:05:12 +02:00
|
|
|
/* just in case... */
|
|
|
|
#ifndef INVALID_FILE_ATTRIBUTES
|
|
|
|
# define INVALID_FILE_ATTRIBUTES 0xFFFFFFFF
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2002-06-10 08:33:15 +02:00
|
|
|
/*
|
|
|
|
* Figure out what the last failing Win32 API call was, and
|
|
|
|
* generate a human-readable string for the error message.
|
|
|
|
*
|
|
|
|
* The return value is a static buffer that is overwritten with
|
|
|
|
* each call to this function.
|
|
|
|
*/
|
|
|
|
static const char *win32strerror(void)
|
|
|
|
{
|
|
|
|
static TCHAR msgbuf[255];
|
2002-06-30 00:05:12 +02:00
|
|
|
TCHAR *ptr = msgbuf;
|
2002-06-10 08:33:15 +02:00
|
|
|
|
|
|
|
FormatMessage(
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL,
|
|
|
|
GetLastError(),
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
|
|
|
|
msgbuf,
|
|
|
|
sizeof (msgbuf) / sizeof (TCHAR),
|
|
|
|
NULL
|
|
|
|
);
|
|
|
|
|
2002-06-30 00:05:12 +02:00
|
|
|
/* chop off newlines. */
|
|
|
|
for (ptr = msgbuf; *ptr; ptr++)
|
|
|
|
{
|
|
|
|
if ((*ptr == '\n') || (*ptr == '\r'))
|
|
|
|
{
|
|
|
|
*ptr = ' ';
|
|
|
|
break;
|
|
|
|
} /* if */
|
|
|
|
} /* for */
|
|
|
|
|
2002-06-10 08:33:15 +02:00
|
|
|
return((const char *) msgbuf);
|
|
|
|
} /* win32strerror */
|
|
|
|
|
|
|
|
|
2002-06-30 00:05:12 +02:00
|
|
|
static char *getExePath(const char *argv0)
|
2002-06-07 04:33:45 +02:00
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
DWORD buflen;
|
|
|
|
int success = 0;
|
|
|
|
char *ptr = NULL;
|
|
|
|
char *retval = (char *) malloc(sizeof (TCHAR) * (MAX_PATH + 1));
|
|
|
|
|
|
|
|
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
|
|
|
|
|
|
|
|
retval[0] = '\0';
|
|
|
|
buflen = GetModuleFileName(NULL, retval, MAX_PATH + 1);
|
|
|
|
if (buflen <= 0)
|
|
|
|
__PHYSFS_setError(win32strerror());
|
|
|
|
else
|
|
|
|
{
|
|
|
|
retval[buflen] = '\0'; /* does API always null-terminate this? */
|
|
|
|
|
|
|
|
/* make sure the string was not truncated. */
|
|
|
|
if (__PHYSFS_platformStricmp(&retval[buflen - 4], ".exe") != 0)
|
2002-08-01 22:22:27 +02:00
|
|
|
__PHYSFS_setError(ERR_GETMODFN_TRUNC);
|
2002-06-30 00:05:12 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ptr = strrchr(retval, '\\');
|
|
|
|
if (ptr == NULL)
|
2002-07-28 23:03:27 +02:00
|
|
|
__PHYSFS_setError(ERR_GETMODFN_NO_DIR);
|
2002-06-30 00:05:12 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
*(ptr + 1) = '\0'; /* chop off filename. */
|
|
|
|
success = 1;
|
|
|
|
} /* else */
|
|
|
|
} /* else */
|
|
|
|
} /* else */
|
|
|
|
|
|
|
|
/* if any part of the previous approach failed, try SearchPath()... */
|
|
|
|
|
|
|
|
if (!success)
|
|
|
|
{
|
|
|
|
if (argv0 == NULL)
|
2002-07-28 23:03:27 +02:00
|
|
|
__PHYSFS_setError(ERR_ARGV0_IS_NULL);
|
2002-06-30 00:05:12 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
buflen = SearchPath(NULL, argv0, NULL, MAX_PATH+1, retval, &ptr);
|
|
|
|
if (buflen == 0)
|
|
|
|
__PHYSFS_setError(win32strerror());
|
|
|
|
else if (buflen > MAX_PATH)
|
2002-07-28 23:03:27 +02:00
|
|
|
__PHYSFS_setError(ERR_SEARCHPATH_TRUNC);
|
2002-06-30 00:05:12 +02:00
|
|
|
else
|
|
|
|
success = 1;
|
|
|
|
} /* else */
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
if (!success)
|
|
|
|
{
|
|
|
|
free(retval);
|
|
|
|
return(NULL); /* physfs error message will be set, above. */
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
/* free up the bytes we didn't actually use. */
|
|
|
|
ptr = (char *) realloc(retval, strlen(retval) + 1);
|
|
|
|
if (ptr != NULL)
|
|
|
|
retval = ptr;
|
|
|
|
|
|
|
|
return(retval); /* w00t. */
|
|
|
|
} /* getExePath */
|
2002-06-07 04:33:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2002-06-30 00:05:12 +02:00
|
|
|
* Try to make use of GetUserProfileDirectory(), which isn't available on
|
|
|
|
* some common variants of Win32. If we can't use this, we just punt and
|
|
|
|
* use the physfs base dir for the user dir, too.
|
2002-06-07 04:33:45 +02:00
|
|
|
*
|
2002-06-30 00:05:12 +02:00
|
|
|
* On success, module-scope variable (userDir) will have a pointer to
|
|
|
|
* a malloc()'d string of the user's profile dir, and a non-zero value is
|
|
|
|
* returned. If we can't determine the profile dir, (userDir) will
|
|
|
|
* be NULL, and zero is returned.
|
2002-06-07 04:33:45 +02:00
|
|
|
*/
|
2002-06-30 00:05:12 +02:00
|
|
|
static int determineUserDir(void)
|
2002-06-07 04:33:45 +02:00
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
DWORD psize = 0;
|
2002-06-10 08:33:15 +02:00
|
|
|
char dummy[1];
|
|
|
|
BOOL rc = 0;
|
2002-06-30 00:05:12 +02:00
|
|
|
HANDLE processHandle; /* Current process handle */
|
|
|
|
HANDLE accessToken = NULL; /* Security handle to process */
|
|
|
|
LPFNGETUSERPROFILEDIR GetUserProfileDirectory;
|
|
|
|
HMODULE lib;
|
2002-06-10 08:33:15 +02:00
|
|
|
|
2002-06-30 00:05:12 +02:00
|
|
|
assert(userDir == NULL);
|
2002-06-10 08:33:15 +02:00
|
|
|
|
2002-06-30 00:05:12 +02:00
|
|
|
/*
|
|
|
|
* GetUserProfileDirectory() is only available on NT 4.0 and later.
|
|
|
|
* This means Win95/98/ME (and CE?) users have to do without, so for
|
|
|
|
* them, we'll default to the base directory when we can't get the
|
|
|
|
* function pointer.
|
|
|
|
*/
|
2002-06-10 08:33:15 +02:00
|
|
|
|
2002-06-30 00:05:12 +02:00
|
|
|
lib = LoadLibrary("userenv.dll");
|
|
|
|
if (lib)
|
|
|
|
{
|
|
|
|
/* !!! FIXME: Handle Unicode? */
|
|
|
|
GetUserProfileDirectory = (LPFNGETUSERPROFILEDIR)
|
2002-06-10 08:33:15 +02:00
|
|
|
GetProcAddress(lib, "GetUserProfileDirectoryA");
|
2002-06-30 00:05:12 +02:00
|
|
|
if (GetUserProfileDirectory)
|
|
|
|
{
|
|
|
|
processHandle = GetCurrentProcess();
|
|
|
|
if (OpenProcessToken(processHandle, TOKEN_QUERY, &accessToken))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Should fail. Will write the size of the profile path in
|
|
|
|
* psize. Also note that the second parameter can't be
|
|
|
|
* NULL or the function fails.
|
|
|
|
*/
|
|
|
|
rc = GetUserProfileDirectory(accessToken, dummy, &psize);
|
|
|
|
assert(!rc); /* success?! */
|
|
|
|
|
|
|
|
/* Allocate memory for the profile directory */
|
|
|
|
userDir = (char *) malloc(psize);
|
|
|
|
if (userDir != NULL)
|
|
|
|
{
|
|
|
|
if (!GetUserProfileDirectory(accessToken, userDir, &psize))
|
|
|
|
{
|
|
|
|
free(userDir);
|
|
|
|
userDir = NULL;
|
|
|
|
} /* if */
|
|
|
|
} /* else */
|
|
|
|
} /* if */
|
2002-06-10 08:33:15 +02:00
|
|
|
|
2002-06-30 00:05:12 +02:00
|
|
|
CloseHandle(accessToken);
|
|
|
|
} /* if */
|
2002-06-10 08:33:15 +02:00
|
|
|
|
2002-06-30 00:05:12 +02:00
|
|
|
FreeLibrary(lib);
|
2002-06-10 08:33:15 +02:00
|
|
|
} /* if */
|
|
|
|
|
2002-06-30 00:05:12 +02:00
|
|
|
if (userDir == NULL) /* couldn't get profile for some reason. */
|
2002-06-07 04:33:45 +02:00
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
/* Might just be a non-NT system; resort to the basedir. */
|
|
|
|
userDir = getExePath(NULL);
|
|
|
|
BAIL_IF_MACRO(userDir == NULL, NULL, 0); /* STILL failed?! */
|
2002-06-10 08:33:15 +02:00
|
|
|
} /* if */
|
2002-06-07 04:33:45 +02:00
|
|
|
|
2002-06-30 00:05:12 +02:00
|
|
|
return(1); /* We made it: hit the showers. */
|
|
|
|
} /* determineUserDir */
|
2002-06-10 08:33:15 +02:00
|
|
|
|
2002-06-07 04:33:45 +02:00
|
|
|
|
2002-07-14 17:11:39 +02:00
|
|
|
static BOOL mediaInDrive(const char *drive)
|
2002-06-07 04:33:45 +02:00
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
UINT oldErrorMode;
|
2002-07-14 17:11:39 +02:00
|
|
|
DWORD tmp;
|
|
|
|
BOOL retval;
|
2002-06-07 04:33:45 +02:00
|
|
|
|
|
|
|
/* Prevent windows warning message to appear when checking media size */
|
2002-06-30 00:05:12 +02:00
|
|
|
oldErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
|
2002-06-07 04:33:45 +02:00
|
|
|
|
|
|
|
/* If this function succeeds, there's media in the drive */
|
2002-07-14 17:11:39 +02:00
|
|
|
retval = GetVolumeInformation(drive, NULL, 0, NULL, NULL, &tmp, NULL, 0);
|
2002-06-07 04:33:45 +02:00
|
|
|
|
|
|
|
/* Revert back to old windows error handler */
|
2002-06-30 00:05:12 +02:00
|
|
|
SetErrorMode(oldErrorMode);
|
|
|
|
|
2002-07-14 17:11:39 +02:00
|
|
|
return(retval);
|
2002-06-30 00:05:12 +02:00
|
|
|
} /* mediaInDrive */
|
2002-06-07 04:33:45 +02:00
|
|
|
|
2001-08-23 17:22:52 +02:00
|
|
|
|
|
|
|
char **__PHYSFS_platformDetectAvailableCDs(void)
|
|
|
|
{
|
|
|
|
char **retval = (char **) malloc(sizeof (char *));
|
|
|
|
int cd_count = 1; /* We count the NULL entry. */
|
|
|
|
char drive_str[4] = "x:\\";
|
|
|
|
|
|
|
|
for (drive_str[0] = 'A'; drive_str[0] <= 'Z'; drive_str[0]++)
|
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
if (GetDriveType(drive_str) == DRIVE_CDROM && mediaInDrive(drive_str))
|
2001-08-23 17:22:52 +02:00
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
char **tmp = realloc(retval, sizeof (char *) * (cd_count + 1));
|
2001-08-23 17:22:52 +02:00
|
|
|
if (tmp)
|
|
|
|
{
|
|
|
|
retval = tmp;
|
2002-06-30 00:05:12 +02:00
|
|
|
retval[cd_count - 1] = (char *) malloc(4);
|
|
|
|
if (retval[cd_count - 1])
|
2001-08-23 17:22:52 +02:00
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
strcpy(retval[cd_count - 1], drive_str);
|
2001-08-23 17:22:52 +02:00
|
|
|
cd_count++;
|
|
|
|
} /* if */
|
|
|
|
} /* if */
|
|
|
|
} /* if */
|
|
|
|
} /* for */
|
|
|
|
|
|
|
|
retval[cd_count - 1] = NULL;
|
|
|
|
return(retval);
|
|
|
|
} /* __PHYSFS_detectAvailableCDs */
|
|
|
|
|
|
|
|
|
2001-10-09 17:41:33 +02:00
|
|
|
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
|
|
|
|
{
|
|
|
|
if (strchr(argv0, '\\') != NULL) /* default behaviour can handle this. */
|
|
|
|
return(NULL);
|
|
|
|
|
2002-03-21 04:22:04 +01:00
|
|
|
return(getExePath(argv0));
|
2001-08-23 17:22:52 +02:00
|
|
|
} /* __PHYSFS_platformCalcBaseDir */
|
|
|
|
|
|
|
|
|
|
|
|
char *__PHYSFS_platformGetUserName(void)
|
|
|
|
{
|
2001-08-23 18:02:51 +02:00
|
|
|
DWORD bufsize = 0;
|
2001-08-23 17:22:52 +02:00
|
|
|
LPTSTR retval = NULL;
|
|
|
|
|
|
|
|
if (GetUserName(NULL, &bufsize) == 0) /* This SHOULD fail. */
|
|
|
|
{
|
|
|
|
retval = (LPTSTR) malloc(bufsize);
|
|
|
|
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
|
|
|
|
if (GetUserName(retval, &bufsize) == 0) /* ?! */
|
|
|
|
{
|
2002-06-10 08:33:15 +02:00
|
|
|
__PHYSFS_setError(win32strerror());
|
2001-08-23 17:22:52 +02:00
|
|
|
free(retval);
|
|
|
|
retval = NULL;
|
|
|
|
} /* if */
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
return((char *) retval);
|
|
|
|
} /* __PHYSFS_platformGetUserName */
|
|
|
|
|
|
|
|
|
|
|
|
char *__PHYSFS_platformGetUserDir(void)
|
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
char *retval = (char *) malloc(strlen(userDir) + 1);
|
2002-06-10 08:33:15 +02:00
|
|
|
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
|
2002-06-30 00:05:12 +02:00
|
|
|
strcpy(retval, userDir); /* calculated at init time. */
|
|
|
|
return(retval);
|
2001-08-23 17:22:52 +02:00
|
|
|
} /* __PHYSFS_platformGetUserDir */
|
|
|
|
|
|
|
|
|
2002-04-03 06:15:24 +02:00
|
|
|
PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
|
2001-08-23 17:22:52 +02:00
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
return((PHYSFS_uint64) GetCurrentThreadId());
|
2001-08-23 17:22:52 +02:00
|
|
|
} /* __PHYSFS_platformGetThreadID */
|
|
|
|
|
|
|
|
|
2001-08-23 18:02:51 +02:00
|
|
|
/* ...make this Cygwin AND Visual C friendly... */
|
2001-08-23 17:22:52 +02:00
|
|
|
int __PHYSFS_platformStricmp(const char *x, const char *y)
|
|
|
|
{
|
2002-04-13 01:48:31 +02:00
|
|
|
#if (defined _MSC_VER)
|
|
|
|
return(stricmp(x, y));
|
|
|
|
#else
|
2001-08-23 18:02:51 +02:00
|
|
|
int ux, uy;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
ux = toupper((int) *x);
|
|
|
|
uy = toupper((int) *y);
|
|
|
|
if (ux > uy)
|
|
|
|
return(1);
|
|
|
|
else if (ux < uy)
|
|
|
|
return(-1);
|
|
|
|
x++;
|
|
|
|
y++;
|
|
|
|
} while ((ux) && (uy));
|
|
|
|
|
|
|
|
return(0);
|
2002-04-13 01:48:31 +02:00
|
|
|
#endif
|
2001-08-23 17:22:52 +02:00
|
|
|
} /* __PHYSFS_platformStricmp */
|
|
|
|
|
|
|
|
|
|
|
|
int __PHYSFS_platformExists(const char *fname)
|
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
return(GetFileAttributes(fname) != INVALID_FILE_ATTRIBUTES);
|
2001-08-23 17:22:52 +02:00
|
|
|
} /* __PHYSFS_platformExists */
|
|
|
|
|
|
|
|
|
|
|
|
int __PHYSFS_platformIsSymLink(const char *fname)
|
|
|
|
{
|
|
|
|
return(0); /* no symlinks on win32. */
|
|
|
|
} /* __PHYSFS_platformIsSymlink */
|
|
|
|
|
|
|
|
|
|
|
|
int __PHYSFS_platformIsDirectory(const char *fname)
|
|
|
|
{
|
|
|
|
return((GetFileAttributes(fname) & FILE_ATTRIBUTE_DIRECTORY) != 0);
|
|
|
|
} /* __PHYSFS_platformIsDirectory */
|
|
|
|
|
|
|
|
|
|
|
|
char *__PHYSFS_platformCvtToDependent(const char *prepend,
|
|
|
|
const char *dirName,
|
|
|
|
const char *append)
|
|
|
|
{
|
|
|
|
int len = ((prepend) ? strlen(prepend) : 0) +
|
|
|
|
((append) ? strlen(append) : 0) +
|
|
|
|
strlen(dirName) + 1;
|
|
|
|
char *retval = malloc(len);
|
2001-08-23 18:02:51 +02:00
|
|
|
char *p;
|
2001-08-23 17:22:52 +02:00
|
|
|
|
|
|
|
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
|
|
|
|
|
|
|
|
if (prepend)
|
|
|
|
strcpy(retval, prepend);
|
|
|
|
else
|
|
|
|
retval[0] = '\0';
|
|
|
|
|
|
|
|
strcat(retval, dirName);
|
|
|
|
|
|
|
|
if (append)
|
|
|
|
strcat(retval, append);
|
|
|
|
|
|
|
|
for (p = strchr(retval, '/'); p != NULL; p = strchr(p + 1, '/'))
|
|
|
|
*p = '\\';
|
|
|
|
|
|
|
|
return(retval);
|
|
|
|
} /* __PHYSFS_platformCvtToDependent */
|
|
|
|
|
|
|
|
|
|
|
|
/* Much like my college days, try to sleep for 10 milliseconds at a time... */
|
|
|
|
void __PHYSFS_platformTimeslice(void)
|
|
|
|
{
|
|
|
|
Sleep(10);
|
|
|
|
} /* __PHYSFS_platformTimeslice */
|
|
|
|
|
|
|
|
|
|
|
|
LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname,
|
|
|
|
int omitSymLinks)
|
|
|
|
{
|
2002-07-23 09:46:36 +02:00
|
|
|
LinkedStringList *retval = NULL, *p = NULL;
|
2001-08-23 17:22:52 +02:00
|
|
|
HANDLE dir;
|
|
|
|
WIN32_FIND_DATA ent;
|
2002-06-07 04:33:45 +02:00
|
|
|
char *SearchPath;
|
2002-06-08 08:19:27 +02:00
|
|
|
size_t len = strlen(dirname);
|
2002-06-07 04:33:45 +02:00
|
|
|
|
2002-06-08 08:19:27 +02:00
|
|
|
/* Allocate a new string for path, maybe '\\', "*", and NULL terminator */
|
2002-06-30 00:05:12 +02:00
|
|
|
SearchPath = (char *) alloca(len + 3);
|
2002-06-10 08:33:15 +02:00
|
|
|
BAIL_IF_MACRO(SearchPath == NULL, ERR_OUT_OF_MEMORY, NULL);
|
|
|
|
|
2002-06-07 04:33:45 +02:00
|
|
|
/* Copy current dirname */
|
|
|
|
strcpy(SearchPath, dirname);
|
2002-06-08 08:19:27 +02:00
|
|
|
|
|
|
|
/* if there's no '\\' at the end of the path, stick one in there. */
|
2002-06-10 08:33:15 +02:00
|
|
|
if (SearchPath[len - 1] != '\\')
|
2002-06-08 08:19:27 +02:00
|
|
|
{
|
2002-06-10 08:33:15 +02:00
|
|
|
SearchPath[len++] = '\\';
|
|
|
|
SearchPath[len] = '\0';
|
2002-06-08 08:19:27 +02:00
|
|
|
} /* if */
|
|
|
|
|
2002-06-07 04:33:45 +02:00
|
|
|
/* Append the "*" to the end of the string */
|
|
|
|
strcat(SearchPath, "*");
|
2001-08-23 17:22:52 +02:00
|
|
|
|
2002-06-07 04:33:45 +02:00
|
|
|
dir = FindFirstFile(SearchPath, &ent);
|
2001-08-23 17:22:52 +02:00
|
|
|
BAIL_IF_MACRO(dir == INVALID_HANDLE_VALUE, win32strerror(), NULL);
|
|
|
|
|
2002-06-08 08:19:27 +02:00
|
|
|
do
|
2001-08-23 17:22:52 +02:00
|
|
|
{
|
|
|
|
if (strcmp(ent.cFileName, ".") == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (strcmp(ent.cFileName, "..") == 0)
|
|
|
|
continue;
|
|
|
|
|
2002-07-23 09:46:36 +02:00
|
|
|
retval = __PHYSFS_addToLinkedStringList(retval, &p, ent.cFileName, -1);
|
2002-06-08 08:19:27 +02:00
|
|
|
} while (FindNextFile(dir, &ent) != 0);
|
2001-08-23 17:22:52 +02:00
|
|
|
|
|
|
|
FindClose(dir);
|
|
|
|
return(retval);
|
|
|
|
} /* __PHYSFS_platformEnumerateFiles */
|
|
|
|
|
|
|
|
|
|
|
|
char *__PHYSFS_platformCurrentDir(void)
|
|
|
|
{
|
2001-08-23 18:02:51 +02:00
|
|
|
LPTSTR retval;
|
2001-08-23 17:22:52 +02:00
|
|
|
DWORD buflen = 0;
|
|
|
|
|
2001-08-23 18:02:51 +02:00
|
|
|
buflen = GetCurrentDirectory(buflen, NULL);
|
2001-09-02 01:40:22 +02:00
|
|
|
retval = (LPTSTR) malloc(sizeof (TCHAR) * (buflen + 2));
|
|
|
|
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
|
2001-08-23 18:02:51 +02:00
|
|
|
GetCurrentDirectory(buflen, retval);
|
2001-09-02 01:40:22 +02:00
|
|
|
|
|
|
|
if (retval[buflen - 2] != '\\')
|
|
|
|
strcat(retval, "\\");
|
|
|
|
|
2001-08-23 17:22:52 +02:00
|
|
|
return((char *) retval);
|
|
|
|
} /* __PHYSFS_platformCurrentDir */
|
|
|
|
|
|
|
|
|
2001-09-02 01:40:22 +02:00
|
|
|
/* this could probably use a cleanup. */
|
2001-08-23 17:22:52 +02:00
|
|
|
char *__PHYSFS_platformRealPath(const char *path)
|
|
|
|
{
|
2001-09-02 01:40:22 +02:00
|
|
|
char *retval = NULL;
|
|
|
|
char *p = NULL;
|
|
|
|
|
|
|
|
BAIL_IF_MACRO(path == NULL, ERR_INVALID_ARGUMENT, NULL);
|
|
|
|
BAIL_IF_MACRO(*path == '\0', ERR_INVALID_ARGUMENT, NULL);
|
|
|
|
|
|
|
|
retval = (char *) malloc(MAX_PATH);
|
|
|
|
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If in \\server\path format, it's already an absolute path.
|
|
|
|
* We'll need to check for "." and ".." dirs, though, just in case.
|
|
|
|
*/
|
|
|
|
if ((path[0] == '\\') && (path[1] == '\\'))
|
|
|
|
strcpy(retval, path);
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char *currentDir = __PHYSFS_platformCurrentDir();
|
|
|
|
if (currentDir == NULL)
|
|
|
|
{
|
|
|
|
free(retval);
|
|
|
|
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
if (path[1] == ':') /* drive letter specified? */
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Apparently, "D:mypath" is the same as "D:\\mypath" if
|
|
|
|
* D: is not the current drive. However, if D: is the
|
|
|
|
* current drive, then "D:mypath" is a relative path. Ugh.
|
|
|
|
*/
|
|
|
|
if (path[2] == '\\') /* maybe an absolute path? */
|
|
|
|
strcpy(retval, path);
|
|
|
|
else /* definitely an absolute path. */
|
|
|
|
{
|
|
|
|
if (path[0] == currentDir[0]) /* current drive; relative. */
|
|
|
|
{
|
|
|
|
strcpy(retval, currentDir);
|
|
|
|
strcat(retval, path + 2);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
else /* not current drive; absolute. */
|
|
|
|
{
|
|
|
|
retval[0] = path[0];
|
|
|
|
retval[1] = ':';
|
|
|
|
retval[2] = '\\';
|
|
|
|
strcpy(retval + 3, path + 2);
|
|
|
|
} /* else */
|
|
|
|
} /* else */
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
else /* no drive letter specified. */
|
|
|
|
{
|
|
|
|
if (path[0] == '\\') /* absolute path. */
|
|
|
|
{
|
|
|
|
retval[0] = currentDir[0];
|
|
|
|
retval[1] = ':';
|
|
|
|
strcpy(retval + 2, path);
|
|
|
|
} /* if */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strcpy(retval, currentDir);
|
|
|
|
strcat(retval, path);
|
|
|
|
} /* else */
|
|
|
|
} /* else */
|
|
|
|
|
|
|
|
free(currentDir);
|
|
|
|
} /* else */
|
|
|
|
|
|
|
|
/* (whew.) Ok, now take out "." and ".." path entries... */
|
|
|
|
|
|
|
|
p = retval;
|
|
|
|
while ( (p = strstr(p, "\\.")) != NULL)
|
|
|
|
{
|
|
|
|
/* it's a "." entry that doesn't end the string. */
|
|
|
|
if (p[2] == '\\')
|
|
|
|
memmove(p + 1, p + 3, strlen(p + 3) + 1);
|
|
|
|
|
|
|
|
/* it's a "." entry that ends the string. */
|
|
|
|
else if (p[2] == '\0')
|
|
|
|
p[0] = '\0';
|
|
|
|
|
|
|
|
/* it's a ".." entry. */
|
|
|
|
else if (p[2] == '.')
|
|
|
|
{
|
|
|
|
char *prevEntry = p - 1;
|
|
|
|
while ((prevEntry != retval) && (*prevEntry != '\\'))
|
|
|
|
prevEntry--;
|
|
|
|
|
|
|
|
if (prevEntry == retval) /* make it look like a "." entry. */
|
|
|
|
memmove(p + 1, p + 2, strlen(p + 2) + 1);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (p[3] != '\0') /* doesn't end string. */
|
|
|
|
*prevEntry = '\0';
|
|
|
|
else /* ends string. */
|
|
|
|
memmove(prevEntry + 1, p + 4, strlen(p + 4) + 1);
|
|
|
|
|
|
|
|
p = prevEntry;
|
|
|
|
} /* else */
|
|
|
|
} /* else if */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p++; /* look past current char. */
|
|
|
|
} /* else */
|
|
|
|
} /* while */
|
|
|
|
|
|
|
|
/* shrink the retval's memory block if possible... */
|
|
|
|
p = (char *) realloc(retval, strlen(retval) + 1);
|
|
|
|
if (p != NULL)
|
|
|
|
retval = p;
|
|
|
|
|
|
|
|
return(retval);
|
2001-08-23 17:22:52 +02:00
|
|
|
} /* __PHYSFS_platformRealPath */
|
|
|
|
|
|
|
|
|
|
|
|
int __PHYSFS_platformMkDir(const char *path)
|
|
|
|
{
|
2001-08-23 18:02:51 +02:00
|
|
|
DWORD rc = CreateDirectory(path, NULL);
|
2001-08-23 17:22:52 +02:00
|
|
|
BAIL_IF_MACRO(rc == 0, win32strerror(), 0);
|
|
|
|
return(1);
|
|
|
|
} /* __PHYSFS_platformMkDir */
|
|
|
|
|
2002-06-10 08:33:15 +02:00
|
|
|
|
2002-06-30 00:05:12 +02:00
|
|
|
/*
|
|
|
|
* Get OS info and save the important parts.
|
2002-03-24 20:42:21 +01:00
|
|
|
*
|
|
|
|
* Returns non-zero if successful, otherwise it returns zero on failure.
|
|
|
|
*/
|
2002-06-30 00:05:12 +02:00
|
|
|
static int getOSInfo(void)
|
2002-03-24 20:42:21 +01:00
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
#if 0 /* we don't actually use this at the moment, but may in the future. */
|
|
|
|
OSVERSIONINFO OSVersionInfo; /* Information about the OS */
|
2002-03-24 20:42:21 +01:00
|
|
|
OSVersionInfo.dwOSVersionInfoSize = sizeof(OSVersionInfo);
|
2002-06-10 08:33:15 +02:00
|
|
|
BAIL_IF_MACRO(!GetVersionEx(&OSVersionInfo), win32strerror(), 0);
|
2002-03-24 20:42:21 +01:00
|
|
|
|
|
|
|
/* Set to TRUE if we are runnign a WinNT based OS 4.0 or greater */
|
2002-06-30 00:05:12 +02:00
|
|
|
runningNT = ((OSVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) &&
|
|
|
|
(OSVersionInfo.dwMajorVersion >= 4));
|
|
|
|
#endif
|
2002-03-24 20:42:21 +01:00
|
|
|
|
2002-06-30 00:05:12 +02:00
|
|
|
return(1);
|
|
|
|
} /* getOSInfo */
|
2002-03-24 20:42:21 +01:00
|
|
|
|
2002-06-30 00:05:12 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Some things we want/need are in external DLLs that may or may not be
|
|
|
|
* available, based on the operating system, etc. This function loads those
|
|
|
|
* libraries and hunts down the needed pointers.
|
|
|
|
*
|
|
|
|
* Libraries that are one-shot deals, or better loaded as needed, are loaded
|
|
|
|
* elsewhere (see determineUserDir()).
|
|
|
|
*
|
|
|
|
* Returns zero if a needed library couldn't load, non-zero if we have enough
|
|
|
|
* to go on (which means some useful but non-crucial libraries may _NOT_ be
|
|
|
|
* loaded; check the related module-scope variables).
|
|
|
|
*/
|
|
|
|
static int loadLibraries(void)
|
2002-03-24 20:42:21 +01:00
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
/* !!! FIXME: Make this table driven? */
|
|
|
|
int allNeededLibrariesLoaded = 1; /* flip to zero as needed. */
|
2002-03-24 20:42:21 +01:00
|
|
|
|
2002-06-30 00:05:12 +02:00
|
|
|
libKernel32 = LoadLibrary("kernel32.dll");
|
|
|
|
if (libKernel32)
|
2002-03-24 20:42:21 +01:00
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
pGetFileAttributesEx = (LPFNGETFILEATTRIBUTESEX)
|
|
|
|
GetProcAddress(libKernel32, "GetFileAttributesExA");
|
2002-06-10 08:33:15 +02:00
|
|
|
} /* if */
|
2002-06-30 00:05:12 +02:00
|
|
|
|
|
|
|
/* add other DLLs here... */
|
|
|
|
|
|
|
|
|
|
|
|
/* see if there's any reason to keep kernel32.dll around... */
|
|
|
|
if (libKernel32)
|
2002-06-10 08:33:15 +02:00
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
if ((pGetFileAttributesEx == NULL) /* && (somethingElse == NULL) */ )
|
|
|
|
{
|
|
|
|
FreeLibrary(libKernel32);
|
|
|
|
libKernel32 = NULL;
|
|
|
|
} /* if */
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
return(allNeededLibrariesLoaded);
|
|
|
|
} /* loadLibraries */
|
|
|
|
|
|
|
|
|
|
|
|
int __PHYSFS_platformInit(void)
|
|
|
|
{
|
|
|
|
BAIL_IF_MACRO(!getOSInfo(), NULL, 0);
|
|
|
|
BAIL_IF_MACRO(!loadLibraries(), NULL, 0);
|
|
|
|
BAIL_IF_MACRO(!determineUserDir(), NULL, 0);
|
|
|
|
|
|
|
|
return(1); /* It's all good */
|
|
|
|
} /* __PHYSFS_platformInit */
|
2002-03-24 20:42:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
int __PHYSFS_platformDeinit(void)
|
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
if (userDir != NULL)
|
2002-06-10 08:33:15 +02:00
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
free(userDir);
|
|
|
|
userDir = NULL;
|
2002-06-10 08:33:15 +02:00
|
|
|
} /* if */
|
2002-03-24 20:42:21 +01:00
|
|
|
|
2002-06-30 00:23:30 +02:00
|
|
|
if (libKernel32)
|
|
|
|
{
|
|
|
|
FreeLibrary(libKernel32);
|
|
|
|
libKernel32 = NULL;
|
|
|
|
} /* if */
|
|
|
|
|
2002-06-30 00:05:12 +02:00
|
|
|
return(1); /* It's all good */
|
|
|
|
} /* __PHYSFS_platformDeinit */
|
|
|
|
|
|
|
|
|
|
|
|
static void *doOpen(const char *fname, DWORD mode, DWORD creation, int rdonly)
|
|
|
|
{
|
|
|
|
HANDLE fileHandle;
|
|
|
|
win32file *retval;
|
|
|
|
|
|
|
|
fileHandle = CreateFile(fname, mode, FILE_SHARE_READ, NULL,
|
|
|
|
creation, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
|
|
|
|
BAIL_IF_MACRO(fileHandle == INVALID_HANDLE_VALUE, win32strerror(), NULL);
|
|
|
|
|
|
|
|
retval = malloc(sizeof (win32file));
|
|
|
|
if (retval == NULL)
|
2002-06-10 08:33:15 +02:00
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
CloseHandle(fileHandle);
|
|
|
|
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
|
2002-06-10 08:33:15 +02:00
|
|
|
} /* if */
|
2002-03-24 20:42:21 +01:00
|
|
|
|
2002-06-30 00:05:12 +02:00
|
|
|
retval->readonly = rdonly;
|
|
|
|
retval->handle = fileHandle;
|
|
|
|
return(retval);
|
|
|
|
} /* doOpen */
|
|
|
|
|
2002-03-24 20:42:21 +01:00
|
|
|
|
2002-04-03 06:15:24 +02:00
|
|
|
void *__PHYSFS_platformOpenRead(const char *filename)
|
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
return(doOpen(filename, GENERIC_READ, OPEN_EXISTING, 1));
|
|
|
|
} /* __PHYSFS_platformOpenRead */
|
2002-04-03 06:15:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
void *__PHYSFS_platformOpenWrite(const char *filename)
|
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
return(doOpen(filename, GENERIC_WRITE, CREATE_ALWAYS, 0));
|
|
|
|
} /* __PHYSFS_platformOpenWrite */
|
2002-04-03 06:15:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
void *__PHYSFS_platformOpenAppend(const char *filename)
|
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
void *retval = doOpen(filename, GENERIC_WRITE, OPEN_ALWAYS, 0);
|
|
|
|
if (retval != NULL)
|
|
|
|
{
|
|
|
|
HANDLE h = ((win32file *) retval)->handle;
|
|
|
|
if (SetFilePointer(h, 0, NULL, FILE_END) == INVALID_SET_FILE_POINTER)
|
|
|
|
{
|
|
|
|
const char *err = win32strerror();
|
|
|
|
CloseHandle(h);
|
|
|
|
free(retval);
|
|
|
|
BAIL_MACRO(err, NULL);
|
|
|
|
} /* if */
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
return(retval);
|
|
|
|
} /* __PHYSFS_platformOpenAppend */
|
2002-04-03 06:15:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
|
|
|
|
PHYSFS_uint32 size, PHYSFS_uint32 count)
|
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
HANDLE FileHandle = ((win32file *) opaque)->handle;
|
2002-04-03 06:15:24 +02:00
|
|
|
DWORD CountOfBytesRead;
|
|
|
|
PHYSFS_sint64 retval;
|
|
|
|
|
|
|
|
/* Read data from the file */
|
2002-07-11 23:37:30 +02:00
|
|
|
/* !!! FIXME: uint32 might be a greater # than DWORD */
|
2002-04-03 06:15:24 +02:00
|
|
|
if(!ReadFile(FileHandle, buffer, count * size, &CountOfBytesRead, NULL))
|
|
|
|
{
|
2002-06-10 08:33:15 +02:00
|
|
|
BAIL_MACRO(win32strerror(), -1);
|
|
|
|
} /* if */
|
2002-04-03 06:15:24 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Return the number of "objects" read. */
|
2002-07-11 23:37:30 +02:00
|
|
|
/* !!! FIXME: What if not the right amount of bytes was read to make an object? */
|
2002-04-03 06:15:24 +02:00
|
|
|
retval = CountOfBytesRead / size;
|
2002-06-10 08:33:15 +02:00
|
|
|
} /* else */
|
2002-04-03 06:15:24 +02:00
|
|
|
|
2002-06-30 00:05:12 +02:00
|
|
|
return(retval);
|
|
|
|
} /* __PHYSFS_platformRead */
|
|
|
|
|
2002-04-03 06:15:24 +02:00
|
|
|
|
2002-06-29 12:23:17 +02:00
|
|
|
PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
|
2002-04-03 06:15:24 +02:00
|
|
|
PHYSFS_uint32 size, PHYSFS_uint32 count)
|
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
HANDLE FileHandle = ((win32file *) opaque)->handle;
|
2002-04-03 06:15:24 +02:00
|
|
|
DWORD CountOfBytesWritten;
|
|
|
|
PHYSFS_sint64 retval;
|
|
|
|
|
|
|
|
/* Read data from the file */
|
2002-07-11 23:37:30 +02:00
|
|
|
/* !!! FIXME: uint32 might be a greater # than DWORD */
|
2002-04-03 06:15:24 +02:00
|
|
|
if(!WriteFile(FileHandle, buffer, count * size, &CountOfBytesWritten, NULL))
|
|
|
|
{
|
2002-06-10 08:33:15 +02:00
|
|
|
BAIL_MACRO(win32strerror(), -1);
|
|
|
|
} /* if */
|
2002-04-03 06:15:24 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Return the number of "objects" read. */
|
2002-07-11 23:37:30 +02:00
|
|
|
/* !!! FIXME: What if not the right number of bytes was written? */
|
2002-04-03 06:15:24 +02:00
|
|
|
retval = CountOfBytesWritten / size;
|
2002-06-10 08:33:15 +02:00
|
|
|
} /* else */
|
2002-04-03 06:15:24 +02:00
|
|
|
|
2002-06-30 00:05:12 +02:00
|
|
|
return(retval);
|
|
|
|
} /* __PHYSFS_platformWrite */
|
|
|
|
|
2002-04-03 06:15:24 +02:00
|
|
|
|
|
|
|
int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
|
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
HANDLE FileHandle = ((win32file *) opaque)->handle;
|
2002-04-03 06:15:24 +02:00
|
|
|
DWORD HighOrderPos;
|
2002-06-30 00:05:12 +02:00
|
|
|
DWORD rc;
|
2002-04-03 06:15:24 +02:00
|
|
|
|
|
|
|
/* Get the high order 32-bits of the position */
|
|
|
|
HighOrderPos = HIGHORDER_UINT64(pos);
|
|
|
|
|
2002-07-11 23:37:30 +02:00
|
|
|
/* !!! FIXME: SetFilePointer needs a signed 64-bit value. */
|
2002-04-03 06:15:24 +02:00
|
|
|
/* Move pointer "pos" count from start of file */
|
2002-06-30 00:05:12 +02:00
|
|
|
rc = SetFilePointer(FileHandle, LOWORDER_UINT64(pos),
|
|
|
|
&HighOrderPos, FILE_BEGIN);
|
2002-04-03 06:15:24 +02:00
|
|
|
|
2002-06-30 00:05:12 +02:00
|
|
|
if ((rc == INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
|
|
|
|
BAIL_MACRO(win32strerror(), 0);
|
|
|
|
|
|
|
|
return(1); /* No error occured */
|
|
|
|
} /* __PHYSFS_platformSeek */
|
2002-04-03 06:15:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
|
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
HANDLE FileHandle = ((win32file *) opaque)->handle;
|
|
|
|
DWORD HighPos = 0;
|
|
|
|
DWORD LowPos;
|
2002-04-03 06:15:24 +02:00
|
|
|
PHYSFS_sint64 retval;
|
|
|
|
|
|
|
|
/* Get current position */
|
2002-06-30 00:05:12 +02:00
|
|
|
LowPos = SetFilePointer(FileHandle, 0, &HighPos, FILE_CURRENT);
|
|
|
|
if ((LowPos == INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
|
2002-04-03 06:15:24 +02:00
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
BAIL_MACRO(win32strerror(), 0);
|
|
|
|
} /* if */
|
2002-05-08 02:02:35 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Combine the high/low order to create the 64-bit position value */
|
2002-06-30 00:05:12 +02:00
|
|
|
retval = (((PHYSFS_uint64) HighPos) << 32) | LowPos;
|
|
|
|
assert(retval >= 0);
|
|
|
|
} /* else */
|
|
|
|
|
|
|
|
return(retval);
|
|
|
|
} /* __PHYSFS_platformTell */
|
2002-04-03 06:15:24 +02:00
|
|
|
|
|
|
|
|
2002-06-30 00:05:12 +02:00
|
|
|
PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
|
2002-04-03 06:15:24 +02:00
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
HANDLE FileHandle = ((win32file *) opaque)->handle;
|
|
|
|
DWORD SizeHigh;
|
|
|
|
DWORD SizeLow;
|
2002-04-03 06:15:24 +02:00
|
|
|
PHYSFS_sint64 retval;
|
|
|
|
|
2002-06-30 00:05:12 +02:00
|
|
|
SizeLow = GetFileSize(FileHandle, &SizeHigh);
|
|
|
|
if ((SizeLow == INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
|
2002-04-03 06:15:24 +02:00
|
|
|
{
|
2002-06-10 08:33:15 +02:00
|
|
|
BAIL_MACRO(win32strerror(), -1);
|
|
|
|
} /* if */
|
2002-05-08 02:02:35 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Combine the high/low order to create the 64-bit position value */
|
2002-06-30 00:05:12 +02:00
|
|
|
retval = (((PHYSFS_uint64) SizeHigh) << 32) | SizeLow;
|
|
|
|
assert(retval >= 0);
|
2002-06-10 08:33:15 +02:00
|
|
|
} /* else */
|
2002-04-03 06:15:24 +02:00
|
|
|
|
2002-06-30 00:05:12 +02:00
|
|
|
return(retval);
|
|
|
|
} /* __PHYSFS_platformFileLength */
|
|
|
|
|
2002-04-03 06:15:24 +02:00
|
|
|
|
|
|
|
int __PHYSFS_platformEOF(void *opaque)
|
|
|
|
{
|
|
|
|
PHYSFS_sint64 FilePosition;
|
|
|
|
int retval = 0;
|
|
|
|
|
|
|
|
/* Get the current position in the file */
|
2002-06-30 00:05:12 +02:00
|
|
|
if ((FilePosition = __PHYSFS_platformTell(opaque)) != 0)
|
2002-04-03 06:15:24 +02:00
|
|
|
{
|
2002-05-08 02:02:35 +02:00
|
|
|
/* Non-zero if EOF is equal to the file length */
|
|
|
|
retval = FilePosition == __PHYSFS_platformFileLength(opaque);
|
2002-06-30 00:05:12 +02:00
|
|
|
} /* if */
|
|
|
|
|
|
|
|
return(retval);
|
|
|
|
} /* __PHYSFS_platformEOF */
|
2002-04-03 06:15:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
int __PHYSFS_platformFlush(void *opaque)
|
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
win32file *fh = ((win32file *) opaque);
|
|
|
|
if (!fh->readonly)
|
|
|
|
BAIL_IF_MACRO(!FlushFileBuffers(fh->handle), win32strerror(), 0);
|
2002-04-03 06:15:24 +02:00
|
|
|
|
2002-06-30 00:05:12 +02:00
|
|
|
return(1);
|
|
|
|
} /* __PHYSFS_platformFlush */
|
2002-04-03 06:15:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
int __PHYSFS_platformClose(void *opaque)
|
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
HANDLE FileHandle = ((win32file *) opaque)->handle;
|
|
|
|
BAIL_IF_MACRO(!CloseHandle(FileHandle), win32strerror(), 0);
|
|
|
|
free(opaque);
|
|
|
|
return(1);
|
|
|
|
} /* __PHYSFS_platformClose */
|
2002-04-03 06:15:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
int __PHYSFS_platformDelete(const char *path)
|
|
|
|
{
|
|
|
|
/* If filename is a folder */
|
2002-06-30 00:05:12 +02:00
|
|
|
if (GetFileAttributes(path) == FILE_ATTRIBUTE_DIRECTORY)
|
2002-04-03 06:15:24 +02:00
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
BAIL_IF_MACRO(!RemoveDirectory(path), win32strerror(), 0);
|
|
|
|
} /* if */
|
2002-04-03 06:15:24 +02:00
|
|
|
else
|
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
BAIL_IF_MACRO(!DeleteFile(path), win32strerror(), 0);
|
|
|
|
} /* else */
|
2002-04-03 06:15:24 +02:00
|
|
|
|
2002-06-30 00:05:12 +02:00
|
|
|
return(1); /* if you got here, it worked. */
|
|
|
|
} /* __PHYSFS_platformDelete */
|
2002-04-03 06:15:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
void *__PHYSFS_platformCreateMutex(void)
|
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
return((void *) CreateMutex(NULL, FALSE, NULL));
|
|
|
|
} /* __PHYSFS_platformCreateMutex */
|
|
|
|
|
2002-04-03 06:15:24 +02:00
|
|
|
|
|
|
|
void __PHYSFS_platformDestroyMutex(void *mutex)
|
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
CloseHandle((HANDLE) mutex);
|
|
|
|
} /* __PHYSFS_platformDestroyMutex */
|
|
|
|
|
2002-04-03 06:15:24 +02:00
|
|
|
|
|
|
|
int __PHYSFS_platformGrabMutex(void *mutex)
|
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
return(WaitForSingleObject((HANDLE) mutex, INFINITE) != WAIT_FAILED);
|
|
|
|
} /* __PHYSFS_platformGrabMutex */
|
2002-04-03 06:15:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
void __PHYSFS_platformReleaseMutex(void *mutex)
|
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
ReleaseMutex((HANDLE) mutex);
|
|
|
|
} /* __PHYSFS_platformReleaseMutex */
|
|
|
|
|
2002-04-03 06:15:24 +02:00
|
|
|
|
2002-06-30 00:05:12 +02:00
|
|
|
static PHYSFS_sint64 FileTimeToPhysfsTime(const FILETIME *ft)
|
2002-06-10 08:33:15 +02:00
|
|
|
{
|
|
|
|
SYSTEMTIME st_utc;
|
|
|
|
SYSTEMTIME st_localtz;
|
2002-06-30 00:05:12 +02:00
|
|
|
TIME_ZONE_INFORMATION tzi;
|
|
|
|
DWORD tzid;
|
|
|
|
PHYSFS_sint64 retval;
|
2002-06-10 08:33:15 +02:00
|
|
|
struct tm tm;
|
|
|
|
|
2002-06-30 00:05:12 +02:00
|
|
|
BAIL_IF_MACRO(!FileTimeToSystemTime(ft, &st_utc), win32strerror(), -1);
|
|
|
|
tzid = GetTimeZoneInformation(&tzi);
|
|
|
|
BAIL_IF_MACRO(tzid == TIME_ZONE_ID_INVALID, win32strerror(), -1);
|
2002-06-10 08:33:15 +02:00
|
|
|
|
2002-06-30 00:05:12 +02:00
|
|
|
/* (This API is unsupported and fails on non-NT systems. */
|
|
|
|
if (!SystemTimeToTzSpecificLocalTime(&tzi, &st_utc, &st_localtz))
|
|
|
|
{
|
|
|
|
/* do it by hand. Grumble... */
|
|
|
|
ULARGE_INTEGER ui64;
|
|
|
|
FILETIME new_ft;
|
|
|
|
ui64.LowPart = ft->dwLowDateTime;
|
|
|
|
ui64.HighPart = ft->dwHighDateTime;
|
|
|
|
|
|
|
|
if (tzid == TIME_ZONE_ID_STANDARD)
|
|
|
|
tzi.Bias += tzi.StandardBias;
|
|
|
|
else if (tzid == TIME_ZONE_ID_DAYLIGHT)
|
|
|
|
tzi.Bias += tzi.DaylightBias;
|
|
|
|
|
|
|
|
/* convert from minutes to 100-nanosecond increments... */
|
|
|
|
#if 0 /* For compilers that puke on 64-bit math. */
|
|
|
|
/* goddamn this is inefficient... */
|
|
|
|
while (tzi.Bias > 0)
|
|
|
|
{
|
|
|
|
DWORD tmp = ui64.LowPart - 60000000;
|
|
|
|
if ((ui64.LowPart < tmp) && (tmp > 60000000))
|
|
|
|
ui64.HighPart--;
|
|
|
|
ui64.LowPart = tmp;
|
|
|
|
tzi.Bias--;
|
|
|
|
} /* while */
|
|
|
|
|
|
|
|
while (tzi.Bias < 0)
|
|
|
|
{
|
|
|
|
DWORD tmp = ui64.LowPart + 60000000;
|
|
|
|
if ((ui64.LowPart > tmp) && (tmp < 60000000))
|
|
|
|
ui64.HighPart++;
|
|
|
|
ui64.LowPart = tmp;
|
|
|
|
tzi.Bias++;
|
|
|
|
} /* while */
|
|
|
|
#else
|
|
|
|
ui64.QuadPart -= (((LONGLONG) tzi.Bias) * (600000000));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Move it back into a FILETIME structure... */
|
|
|
|
new_ft.dwLowDateTime = ui64.LowPart;
|
|
|
|
new_ft.dwHighDateTime = ui64.HighPart;
|
|
|
|
|
|
|
|
/* Convert to something human-readable... */
|
|
|
|
if (!FileTimeToSystemTime(&new_ft, &st_localtz))
|
|
|
|
BAIL_MACRO(win32strerror(), -1);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
/* Convert to a format that mktime() can grok... */
|
2002-06-10 08:33:15 +02:00
|
|
|
tm.tm_sec = st_localtz.wSecond;
|
|
|
|
tm.tm_min = st_localtz.wMinute;
|
|
|
|
tm.tm_hour = st_localtz.wHour;
|
|
|
|
tm.tm_mday = st_localtz.wDay;
|
|
|
|
tm.tm_mon = st_localtz.wMonth - 1;
|
|
|
|
tm.tm_year = st_localtz.wYear - 1900;
|
2002-06-30 00:05:12 +02:00
|
|
|
tm.tm_wday = -1 /*st_localtz.wDayOfWeek*/;
|
2002-06-10 08:33:15 +02:00
|
|
|
tm.tm_yday = -1;
|
|
|
|
tm.tm_isdst = -1;
|
2002-06-30 00:05:12 +02:00
|
|
|
|
|
|
|
/* Convert to a format PhysicsFS can grok... */
|
|
|
|
retval = (PHYSFS_sint64) mktime(&tm);
|
|
|
|
BAIL_IF_MACRO(retval == -1, strerror(errno), -1);
|
|
|
|
return(retval);
|
|
|
|
} /* FileTimeToPhysfsTime */
|
|
|
|
|
2002-06-10 08:33:15 +02:00
|
|
|
|
2002-06-07 04:33:45 +02:00
|
|
|
PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname)
|
2002-05-06 05:26:10 +02:00
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
PHYSFS_sint64 retval = -1;
|
|
|
|
WIN32_FILE_ATTRIBUTE_DATA attrData;
|
|
|
|
memset(&attrData, '\0', sizeof (attrData));
|
2002-05-06 05:26:10 +02:00
|
|
|
|
2002-06-30 00:23:30 +02:00
|
|
|
/* GetFileAttributesEx didn't show up until Win98 and NT4. */
|
2002-06-30 00:05:12 +02:00
|
|
|
if (pGetFileAttributesEx != NULL)
|
2002-05-06 05:26:10 +02:00
|
|
|
{
|
2002-06-30 00:05:12 +02:00
|
|
|
if (pGetFileAttributesEx(fname, GetFileExInfoStandard, &attrData))
|
|
|
|
{
|
|
|
|
/* 0 return value indicates an error or not supported */
|
|
|
|
if ( (attrData.ftLastWriteTime.dwHighDateTime != 0) ||
|
|
|
|
(attrData.ftLastWriteTime.dwLowDateTime != 0) )
|
|
|
|
{
|
|
|
|
retval = FileTimeToPhysfsTime(&attrData.ftLastWriteTime);
|
|
|
|
} /* if */
|
|
|
|
} /* if */
|
|
|
|
} /* if */
|
|
|
|
|
2002-06-30 00:23:30 +02:00
|
|
|
/* GetFileTime() has been in the Win32 API since the start. */
|
2002-06-30 00:05:12 +02:00
|
|
|
if (retval == -1) /* try a fallback... */
|
|
|
|
{
|
2002-06-30 00:23:30 +02:00
|
|
|
FILETIME ft;
|
|
|
|
BOOL rc;
|
|
|
|
const char *err;
|
|
|
|
win32file *f = (win32file *) __PHYSFS_platformOpenRead(fname);
|
|
|
|
BAIL_IF_MACRO(f == NULL, NULL, -1)
|
|
|
|
rc = GetFileTime(f->handle, NULL, NULL, &ft);
|
|
|
|
err = win32strerror();
|
|
|
|
CloseHandle(f->handle);
|
|
|
|
free(f);
|
|
|
|
BAIL_IF_MACRO(!rc, err, -1);
|
|
|
|
retval = FileTimeToPhysfsTime(&ft);
|
2002-06-30 00:05:12 +02:00
|
|
|
} /* if */
|
|
|
|
|
|
|
|
return(retval);
|
2002-05-25 11:41:14 +02:00
|
|
|
} /* __PHYSFS_platformGetLastModTime */
|
|
|
|
|
2002-07-10 23:14:33 +02:00
|
|
|
#endif
|
|
|
|
|
2001-08-23 17:22:52 +02:00
|
|
|
/* end of win32.c ... */
|
|
|
|
|