misc/libphysfs/platform_windows.c
author Wuzzy <Wuzzy2@mail.ru>
Wed, 18 Apr 2018 17:12:15 +0200
changeset 13341 afc089c39556
parent 12218 bb5522e88ab2
child 13875 a966b1914792
permissions -rw-r--r--
Removed tag 0.9.24.1-release
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     1
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     2
 * Windows support routines for PhysicsFS.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     3
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     4
 * Please see the file LICENSE.txt in the source's root directory.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     5
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     6
 *  This file written by Ryan C. Gordon, and made sane by Gregory S. Read.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     7
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     8
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     9
#define __PHYSICSFS_INTERNAL__
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    10
#include "physfs_platforms.h"
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    11
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    12
#ifdef PHYSFS_PLATFORM_WINDOWS
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
    13
#ifndef PHYSFS_PLATFORM_WINRT
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    14
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    15
/* Forcibly disable UNICODE macro, since we manage this ourselves. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    16
#ifdef UNICODE
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    17
#undef UNICODE
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    18
#endif
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    19
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    20
#define WIN32_LEAN_AND_MEAN 1
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    21
#include <windows.h>
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    22
#include <userenv.h>
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    23
#include <shlobj.h>
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    24
#include <dbt.h>
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    25
#include <errno.h>
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    26
#include <ctype.h>
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    27
#include <time.h>
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    28
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    29
#include "physfs_internal.h"
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    30
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    31
#define LOWORDER_UINT64(pos) ((PHYSFS_uint32) (pos & 0xFFFFFFFF))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    32
#define HIGHORDER_UINT64(pos) ((PHYSFS_uint32) ((pos >> 32) & 0xFFFFFFFF))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    33
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    34
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    35
 * Users without the platform SDK don't have this defined.  The original docs
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    36
 *  for SetFilePointer() just said to compare with 0xFFFFFFFF, so this should
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    37
 *  work as desired.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    38
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    39
#define PHYSFS_INVALID_SET_FILE_POINTER  0xFFFFFFFF
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    40
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    41
/* just in case... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    42
#define PHYSFS_INVALID_FILE_ATTRIBUTES   0xFFFFFFFF
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    43
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    44
/* Not defined before the Vista SDK. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    45
#define PHYSFS_IO_REPARSE_TAG_SYMLINK    0xA000000C
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    46
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    47
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    48
#define UTF8_TO_UNICODE_STACK_MACRO(w_assignto, str) { \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    49
    if (str == NULL) \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    50
        w_assignto = NULL; \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    51
    else { \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    52
        const PHYSFS_uint64 len = (PHYSFS_uint64) ((strlen(str) + 1) * 2); \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    53
        w_assignto = (WCHAR *) __PHYSFS_smallAlloc(len); \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    54
        if (w_assignto != NULL) \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    55
            PHYSFS_utf8ToUtf16(str, (PHYSFS_uint16 *) w_assignto, len); \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    56
    } \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    57
} \
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    58
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    59
/* Note this counts WCHARs, not codepoints! */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    60
static PHYSFS_uint64 wStrLen(const WCHAR *wstr)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    61
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    62
    PHYSFS_uint64 len = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    63
    while (*(wstr++))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    64
        len++;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    65
    return len;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    66
} /* wStrLen */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    67
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    68
static char *unicodeToUtf8Heap(const WCHAR *w_str)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    69
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    70
    char *retval = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    71
    if (w_str != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    72
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    73
        void *ptr = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    74
        const PHYSFS_uint64 len = (wStrLen(w_str) * 4) + 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    75
        retval = allocator.Malloc(len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    76
        BAIL_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    77
        PHYSFS_utf8FromUtf16((const PHYSFS_uint16 *) w_str, retval, len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    78
        ptr = allocator.Realloc(retval, strlen(retval) + 1); /* shrink. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    79
        if (ptr != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    80
            retval = (char *) ptr;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    81
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    82
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    83
} /* unicodeToUtf8Heap */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    84
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    85
/* !!! FIXME: do we really need readonly? If not, do we need this struct? */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    86
typedef struct
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    87
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    88
    HANDLE handle;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    89
    int readonly;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    90
} WinApiFile;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    91
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    92
static HANDLE detectCDThreadHandle = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    93
static HWND detectCDHwnd = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    94
static volatile int initialDiscDetectionComplete = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    95
static volatile DWORD drivesWithMediaBitmap = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    96
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    97
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    98
static PHYSFS_ErrorCode errcodeFromWinApiError(const DWORD err)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    99
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   100
    /*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   101
     * win32 error codes are sort of a tricky thing; Microsoft intentionally
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   102
     *  doesn't list which ones a given API might trigger, there are several
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   103
     *  with overlapping and unclear meanings...and there's 16 thousand of
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   104
     *  them in Windows 7. It looks like the ones we care about are in the
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   105
     *  first 500, but I can't say this list is perfect; we might miss
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   106
     *  important values or misinterpret others.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   107
     *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   108
     * Don't treat this list as anything other than a work in progress.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   109
     */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   110
    switch (err)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   111
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   112
        case ERROR_SUCCESS: return PHYSFS_ERR_OK;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   113
        case ERROR_ACCESS_DENIED: return PHYSFS_ERR_PERMISSION;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   114
        case ERROR_NETWORK_ACCESS_DENIED: return PHYSFS_ERR_PERMISSION;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   115
        case ERROR_NOT_READY: return PHYSFS_ERR_IO;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   116
        case ERROR_CRC: return PHYSFS_ERR_IO;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   117
        case ERROR_SEEK: return PHYSFS_ERR_IO;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   118
        case ERROR_SECTOR_NOT_FOUND: return PHYSFS_ERR_IO;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   119
        case ERROR_NOT_DOS_DISK: return PHYSFS_ERR_IO;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   120
        case ERROR_WRITE_FAULT: return PHYSFS_ERR_IO;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   121
        case ERROR_READ_FAULT: return PHYSFS_ERR_IO;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   122
        case ERROR_DEV_NOT_EXIST: return PHYSFS_ERR_IO;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   123
        /* !!! FIXME: ?? case ELOOP: return PHYSFS_ERR_SYMLINK_LOOP; */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   124
        case ERROR_BUFFER_OVERFLOW: return PHYSFS_ERR_BAD_FILENAME;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   125
        case ERROR_INVALID_NAME: return PHYSFS_ERR_BAD_FILENAME;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   126
        case ERROR_BAD_PATHNAME: return PHYSFS_ERR_BAD_FILENAME;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   127
        case ERROR_DIRECTORY: return PHYSFS_ERR_BAD_FILENAME;
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   128
        case ERROR_FILE_NOT_FOUND: return PHYSFS_ERR_NOT_FOUND;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   129
        case ERROR_PATH_NOT_FOUND: return PHYSFS_ERR_NOT_FOUND;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   130
        case ERROR_DELETE_PENDING: return PHYSFS_ERR_NOT_FOUND;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   131
        case ERROR_INVALID_DRIVE: return PHYSFS_ERR_NOT_FOUND;
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   132
        case ERROR_HANDLE_DISK_FULL: return PHYSFS_ERR_NO_SPACE;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   133
        case ERROR_DISK_FULL: return PHYSFS_ERR_NO_SPACE;
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   134
        /* !!! FIXME: ?? case ENOTDIR: return PHYSFS_ERR_NOT_FOUND; */
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   135
        /* !!! FIXME: ?? case EISDIR: return PHYSFS_ERR_NOT_A_FILE; */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   136
        case ERROR_WRITE_PROTECT: return PHYSFS_ERR_READ_ONLY;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   137
        case ERROR_LOCK_VIOLATION: return PHYSFS_ERR_BUSY;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   138
        case ERROR_SHARING_VIOLATION: return PHYSFS_ERR_BUSY;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   139
        case ERROR_CURRENT_DIRECTORY: return PHYSFS_ERR_BUSY;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   140
        case ERROR_DRIVE_LOCKED: return PHYSFS_ERR_BUSY;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   141
        case ERROR_PATH_BUSY: return PHYSFS_ERR_BUSY;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   142
        case ERROR_BUSY: return PHYSFS_ERR_BUSY;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   143
        case ERROR_NOT_ENOUGH_MEMORY: return PHYSFS_ERR_OUT_OF_MEMORY;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   144
        case ERROR_OUTOFMEMORY: return PHYSFS_ERR_OUT_OF_MEMORY;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   145
        case ERROR_DIR_NOT_EMPTY: return PHYSFS_ERR_DIR_NOT_EMPTY;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   146
        default: return PHYSFS_ERR_OS_ERROR;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   147
    } /* switch */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   148
} /* errcodeFromWinApiError */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   149
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   150
static inline PHYSFS_ErrorCode errcodeFromWinApi(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   151
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   152
    return errcodeFromWinApiError(GetLastError());
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   153
} /* errcodeFromWinApi */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   154
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   155
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   156
typedef BOOL (WINAPI *fnSTEM)(DWORD, LPDWORD b);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   157
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   158
static DWORD pollDiscDrives(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   159
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   160
    /* Try to use SetThreadErrorMode(), which showed up in Windows 7. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   161
    HANDLE lib = LoadLibraryA("kernel32.dll");
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   162
    fnSTEM stem = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   163
    char drive[4] = { 'x', ':', '\\', '\0' };
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   164
    DWORD oldErrorMode = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   165
    DWORD drives = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   166
    DWORD i;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   167
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   168
    if (lib)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   169
        stem = (fnSTEM) GetProcAddress(lib, "SetThreadErrorMode");
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   170
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   171
    if (stem)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   172
        stem(SEM_FAILCRITICALERRORS, &oldErrorMode);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   173
    else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   174
        oldErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   175
    
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   176
    /* Do detection. This may block if a disc is spinning up. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   177
    for (i = 'A'; i <= 'Z'; i++)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   178
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   179
        DWORD tmp = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   180
        drive[0] = (char) i;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   181
        if (GetDriveTypeA(drive) != DRIVE_CDROM)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   182
            continue;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   183
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   184
        /* If this function succeeds, there's media in the drive */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   185
        if (GetVolumeInformationA(drive, NULL, 0, NULL, NULL, &tmp, NULL, 0))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   186
            drives |= (1 << (i - 'A'));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   187
    } /* for */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   188
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   189
    if (stem)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   190
        stem(oldErrorMode, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   191
    else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   192
        SetErrorMode(oldErrorMode);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   193
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   194
    if (lib)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   195
        FreeLibrary(lib);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   196
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   197
    return drives;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   198
} /* pollDiscDrives */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   199
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   200
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   201
static LRESULT CALLBACK detectCDWndProc(HWND hwnd, UINT msg,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   202
                                        WPARAM wp, LPARAM lparam)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   203
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   204
    PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR) lparam;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   205
    PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME) lparam;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   206
    const int removed = (wp == DBT_DEVICEREMOVECOMPLETE);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   207
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   208
    if (msg == WM_DESTROY)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   209
        return 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   210
    else if ((msg != WM_DEVICECHANGE) ||
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   211
             ((wp != DBT_DEVICEARRIVAL) && (wp != DBT_DEVICEREMOVECOMPLETE)) ||
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   212
             (lpdb->dbch_devicetype != DBT_DEVTYP_VOLUME) ||
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   213
             ((lpdbv->dbcv_flags & DBTF_MEDIA) == 0))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   214
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   215
        return DefWindowProcW(hwnd, msg, wp, lparam);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   216
    } /* else if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   217
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   218
    if (removed)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   219
        drivesWithMediaBitmap &= ~lpdbv->dbcv_unitmask;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   220
    else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   221
        drivesWithMediaBitmap |= lpdbv->dbcv_unitmask;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   222
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   223
    return TRUE;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   224
} /* detectCDWndProc */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   225
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   226
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   227
static DWORD WINAPI detectCDThread(LPVOID lpParameter)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   228
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   229
    const char *classname = "PhysicsFSDetectCDCatcher";
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   230
    const char *winname = "PhysicsFSDetectCDMsgWindow";
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   231
    HINSTANCE hInstance = GetModuleHandleW(NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   232
    ATOM class_atom = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   233
    WNDCLASSEXA wce;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   234
    MSG msg;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   235
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   236
    memset(&wce, '\0', sizeof (wce));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   237
    wce.cbSize = sizeof (wce);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   238
    wce.lpfnWndProc = detectCDWndProc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   239
    wce.lpszClassName = classname;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   240
    wce.hInstance = hInstance;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   241
    class_atom = RegisterClassExA(&wce);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   242
    if (class_atom == 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   243
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   244
        initialDiscDetectionComplete = 1;  /* let main thread go on. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   245
        return 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   246
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   247
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   248
    detectCDHwnd = CreateWindowExA(0, classname, winname, WS_OVERLAPPEDWINDOW,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   249
                        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   250
                        CW_USEDEFAULT, HWND_DESKTOP, NULL, hInstance, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   251
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   252
    if (detectCDHwnd == NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   253
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   254
        initialDiscDetectionComplete = 1;  /* let main thread go on. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   255
        UnregisterClassA(classname, hInstance);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   256
        return 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   257
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   258
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   259
    /* We'll get events when discs come and go from now on. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   260
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   261
    /* Do initial detection, possibly blocking awhile... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   262
    drivesWithMediaBitmap = pollDiscDrives();
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   263
    initialDiscDetectionComplete = 1;  /* let main thread go on. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   264
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   265
    do
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   266
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   267
        const BOOL rc = GetMessageW(&msg, detectCDHwnd, 0, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   268
        if ((rc == 0) || (rc == -1))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   269
            break;  /* don't care if WM_QUIT or error break this loop. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   270
        TranslateMessage(&msg);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   271
        DispatchMessageW(&msg);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   272
    } while (1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   273
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   274
    /* we've been asked to quit. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   275
    DestroyWindow(detectCDHwnd);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   276
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   277
    do
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   278
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   279
        const BOOL rc = GetMessage(&msg, detectCDHwnd, 0, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   280
        if ((rc == 0) || (rc == -1))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   281
            break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   282
        TranslateMessage(&msg);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   283
        DispatchMessageW(&msg);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   284
    } while (1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   285
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   286
    UnregisterClassA(classname, hInstance);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   287
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   288
    return 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   289
} /* detectCDThread */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   290
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   291
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   292
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   293
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   294
    char drive_str[4] = { 'x', ':', '\\', '\0' };
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   295
    DWORD drives = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   296
    DWORD i;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   297
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   298
    /*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   299
     * If you poll a drive while a user is inserting a disc, the OS will
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   300
     *  block this thread until the drive has spun up. So we swallow the risk
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   301
     *  once for initial detection, and spin a thread that will get device
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   302
     *  events thereafter, for apps that use this interface to poll for
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   303
     *  disc insertion.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   304
     */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   305
    if (!detectCDThreadHandle)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   306
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   307
        initialDiscDetectionComplete = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   308
        detectCDThreadHandle = CreateThread(NULL,0,detectCDThread,NULL,0,NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   309
        if (detectCDThreadHandle == NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   310
            return;  /* oh well. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   311
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   312
        while (!initialDiscDetectionComplete)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   313
            Sleep(50);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   314
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   315
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   316
    drives = drivesWithMediaBitmap; /* whatever the thread has seen, we take. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   317
    for (i = 'A'; i <= 'Z'; i++)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   318
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   319
        if (drives & (1 << (i - 'A')))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   320
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   321
            drive_str[0] = (char) i;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   322
            cb(data, drive_str);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   323
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   324
    } /* for */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   325
} /* __PHYSFS_platformDetectAvailableCDs */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   326
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   327
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   328
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   329
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   330
    DWORD buflen = 64;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   331
    LPWSTR modpath = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   332
    char *retval = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   333
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   334
    while (1)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   335
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   336
        DWORD rc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   337
        void *ptr;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   338
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   339
        if ( (ptr = allocator.Realloc(modpath, buflen*sizeof(WCHAR))) == NULL )
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   340
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   341
            allocator.Free(modpath);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   342
            BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   343
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   344
        modpath = (LPWSTR) ptr;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   345
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   346
        rc = GetModuleFileNameW(NULL, modpath, buflen);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   347
        if (rc == 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   348
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   349
            allocator.Free(modpath);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   350
            BAIL_MACRO(errcodeFromWinApi(), NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   351
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   352
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   353
        if (rc < buflen)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   354
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   355
            buflen = rc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   356
            break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   357
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   358
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   359
        buflen *= 2;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   360
    } /* while */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   361
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   362
    if (buflen > 0)  /* just in case... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   363
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   364
        WCHAR *ptr = (modpath + buflen) - 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   365
        while (ptr != modpath)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   366
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   367
            if (*ptr == '\\')
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   368
                break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   369
            ptr--;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   370
        } /* while */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   371
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   372
        if ((ptr == modpath) && (*ptr != '\\'))
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   373
            PHYSFS_setErrorCode(PHYSFS_ERR_OTHER_ERROR);  /* oh well. */
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   374
        else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   375
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   376
            *(ptr+1) = '\0';  /* chop off filename. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   377
            retval = unicodeToUtf8Heap(modpath);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   378
        } /* else */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   379
    } /* else */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   380
    allocator.Free(modpath);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   381
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   382
    return retval;   /* w00t. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   383
} /* __PHYSFS_platformCalcBaseDir */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   384
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   385
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   386
char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   387
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   388
    /*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   389
     * Vista and later has a new API for this, but SHGetFolderPath works there,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   390
     *  and apparently just wraps the new API. This is the new way to do it:
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   391
     *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   392
     *     SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_CREATE,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   393
     *                          NULL, &wszPath);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   394
     */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   395
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   396
    WCHAR path[MAX_PATH];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   397
    char *utf8 = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   398
    size_t len = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   399
    char *retval = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   400
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   401
    if (!SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   402
                                   NULL, 0, path)))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   403
        BAIL_MACRO(PHYSFS_ERR_OS_ERROR, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   404
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   405
    utf8 = unicodeToUtf8Heap(path);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   406
    BAIL_IF_MACRO(!utf8, ERRPASS, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   407
    len = strlen(utf8) + strlen(org) + strlen(app) + 4;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   408
    retval = allocator.Malloc(len);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   409
    if (!retval)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   410
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   411
        allocator.Free(utf8);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   412
        BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   413
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   414
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   415
    sprintf(retval, "%s\\%s\\%s\\", utf8, org, app);
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   416
    allocator.Free(utf8);
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   417
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   418
} /* __PHYSFS_platformCalcPrefDir */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   419
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   420
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   421
char *__PHYSFS_platformCalcUserDir(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   422
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   423
    typedef BOOL (WINAPI *fnGetUserProfDirW)(HANDLE, LPWSTR, LPDWORD);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   424
    fnGetUserProfDirW pGetDir = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   425
    HANDLE lib = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   426
    HANDLE accessToken = NULL;       /* Security handle to process */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   427
    char *retval = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   428
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   429
    lib = LoadLibraryA("userenv.dll");
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   430
    BAIL_IF_MACRO(!lib, errcodeFromWinApi(), NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   431
    pGetDir=(fnGetUserProfDirW) GetProcAddress(lib,"GetUserProfileDirectoryW");
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   432
    GOTO_IF_MACRO(!pGetDir, errcodeFromWinApi(), done);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   433
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   434
    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &accessToken))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   435
        GOTO_MACRO(errcodeFromWinApi(), done);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   436
    else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   437
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   438
        DWORD psize = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   439
        WCHAR dummy = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   440
        LPWSTR wstr = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   441
        BOOL rc = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   442
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   443
        /*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   444
         * Should fail. Will write the size of the profile path in
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   445
         *  psize. Also note that the second parameter can't be
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   446
         *  NULL or the function fails.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   447
         */
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   448
    	rc = pGetDir(accessToken, &dummy, &psize);
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   449
        assert(!rc);  /* !!! FIXME: handle this gracefully. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   450
        (void) rc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   451
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   452
        /* Allocate memory for the profile directory */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   453
        wstr = (LPWSTR) __PHYSFS_smallAlloc((psize + 1) * sizeof (WCHAR));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   454
        if (wstr != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   455
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   456
            if (pGetDir(accessToken, wstr, &psize))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   457
            {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   458
                /* Make sure it ends in a dirsep. We allocated +1 for this. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   459
                if (wstr[psize - 2] != '\\')
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   460
                {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   461
                    wstr[psize - 1] = '\\';
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   462
                    wstr[psize - 0] = '\0';
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   463
                } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   464
                retval = unicodeToUtf8Heap(wstr);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   465
            } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   466
            __PHYSFS_smallFree(wstr);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   467
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   468
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   469
        CloseHandle(accessToken);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   470
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   471
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   472
done:
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   473
    FreeLibrary(lib);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   474
    return retval;  /* We made it: hit the showers. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   475
} /* __PHYSFS_platformCalcUserDir */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   476
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   477
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   478
void *__PHYSFS_platformGetThreadID(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   479
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   480
    return ( (void *) ((size_t) GetCurrentThreadId()) );
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   481
} /* __PHYSFS_platformGetThreadID */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   482
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   483
void __PHYSFS_platformEnumerateFiles(const char *dirname,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   484
                                     PHYSFS_EnumFilesCallback callback,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   485
                                     const char *origdir,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   486
                                     void *callbackdata)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   487
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   488
    HANDLE dir = INVALID_HANDLE_VALUE;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   489
    WIN32_FIND_DATAW entw;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   490
    size_t len = strlen(dirname);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   491
    char *searchPath = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   492
    WCHAR *wSearchPath = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   493
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   494
    /* Allocate a new string for path, maybe '\\', "*", and NULL terminator */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   495
    searchPath = (char *) __PHYSFS_smallAlloc(len + 3);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   496
    if (searchPath == NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   497
        return;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   498
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   499
    /* Copy current dirname */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   500
    strcpy(searchPath, dirname);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   501
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   502
    /* if there's no '\\' at the end of the path, stick one in there. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   503
    if (searchPath[len - 1] != '\\')
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   504
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   505
        searchPath[len++] = '\\';
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   506
        searchPath[len] = '\0';
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   507
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   508
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   509
    /* Append the "*" to the end of the string */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   510
    strcat(searchPath, "*");
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   511
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   512
    UTF8_TO_UNICODE_STACK_MACRO(wSearchPath, searchPath);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   513
    if (!wSearchPath)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   514
        return;  /* oh well. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   515
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   516
    dir = FindFirstFileW(wSearchPath, &entw);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   517
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   518
    __PHYSFS_smallFree(wSearchPath);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   519
    __PHYSFS_smallFree(searchPath);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   520
    if (dir == INVALID_HANDLE_VALUE)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   521
        return;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   522
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   523
    do
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   524
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   525
        const WCHAR *fn = entw.cFileName;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   526
        char *utf8;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   527
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   528
        if ((fn[0] == '.') && (fn[1] == '\0'))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   529
            continue;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   530
        if ((fn[0] == '.') && (fn[1] == '.') && (fn[2] == '\0'))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   531
            continue;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   532
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   533
        utf8 = unicodeToUtf8Heap(fn);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   534
        if (utf8 != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   535
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   536
            callback(callbackdata, origdir, utf8);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   537
            allocator.Free(utf8);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   538
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   539
    } while (FindNextFileW(dir, &entw) != 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   540
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   541
    FindClose(dir);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   542
} /* __PHYSFS_platformEnumerateFiles */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   543
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   544
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   545
int __PHYSFS_platformMkDir(const char *path)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   546
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   547
    WCHAR *wpath;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   548
    DWORD rc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   549
    UTF8_TO_UNICODE_STACK_MACRO(wpath, path);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   550
    rc = CreateDirectoryW(wpath, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   551
    __PHYSFS_smallFree(wpath);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   552
    BAIL_IF_MACRO(rc == 0, errcodeFromWinApi(), 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   553
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   554
} /* __PHYSFS_platformMkDir */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   555
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   556
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   557
int __PHYSFS_platformInit(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   558
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   559
    return 1;  /* It's all good */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   560
} /* __PHYSFS_platformInit */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   561
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   562
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   563
int __PHYSFS_platformDeinit(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   564
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   565
    if (detectCDThreadHandle)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   566
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   567
        if (detectCDHwnd)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   568
            PostMessageW(detectCDHwnd, WM_QUIT, 0, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   569
        CloseHandle(detectCDThreadHandle);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   570
        detectCDThreadHandle = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   571
        initialDiscDetectionComplete = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   572
        drivesWithMediaBitmap = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   573
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   574
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   575
    return 1; /* It's all good */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   576
} /* __PHYSFS_platformDeinit */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   577
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   578
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   579
static void *doOpen(const char *fname, DWORD mode, DWORD creation, int rdonly)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   580
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   581
    HANDLE fileh;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   582
    WinApiFile *retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   583
    WCHAR *wfname;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   584
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   585
    UTF8_TO_UNICODE_STACK_MACRO(wfname, fname);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   586
    BAIL_IF_MACRO(!wfname, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   587
    fileh = CreateFileW(wfname, mode, FILE_SHARE_READ | FILE_SHARE_WRITE,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   588
                             NULL, creation, FILE_ATTRIBUTE_NORMAL, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   589
    __PHYSFS_smallFree(wfname);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   590
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   591
    BAIL_IF_MACRO(fileh == INVALID_HANDLE_VALUE,errcodeFromWinApi(), NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   592
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   593
    retval = (WinApiFile *) allocator.Malloc(sizeof (WinApiFile));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   594
    if (!retval)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   595
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   596
        CloseHandle(fileh);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   597
        BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   598
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   599
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   600
    retval->readonly = rdonly;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   601
    retval->handle = fileh;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   602
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   603
} /* doOpen */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   604
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   605
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   606
void *__PHYSFS_platformOpenRead(const char *filename)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   607
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   608
    return doOpen(filename, GENERIC_READ, OPEN_EXISTING, 1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   609
} /* __PHYSFS_platformOpenRead */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   610
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   611
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   612
void *__PHYSFS_platformOpenWrite(const char *filename)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   613
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   614
    return doOpen(filename, GENERIC_WRITE, CREATE_ALWAYS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   615
} /* __PHYSFS_platformOpenWrite */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   616
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   617
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   618
void *__PHYSFS_platformOpenAppend(const char *filename)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   619
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   620
    void *retval = doOpen(filename, GENERIC_WRITE, OPEN_ALWAYS, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   621
    if (retval != NULL)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   622
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   623
        HANDLE h = ((WinApiFile *) retval)->handle;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   624
        DWORD rc = SetFilePointer(h, 0, NULL, FILE_END);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   625
        if (rc == PHYSFS_INVALID_SET_FILE_POINTER)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   626
        {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   627
            const PHYSFS_ErrorCode err = errcodeFromWinApi();
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   628
            CloseHandle(h);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   629
            allocator.Free(retval);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   630
            BAIL_MACRO(err, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   631
        } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   632
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   633
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   634
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   635
} /* __PHYSFS_platformOpenAppend */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   636
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   637
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   638
PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buf, PHYSFS_uint64 len)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   639
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   640
    HANDLE Handle = ((WinApiFile *) opaque)->handle;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   641
    PHYSFS_sint64 totalRead = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   642
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   643
    if (!__PHYSFS_ui64FitsAddressSpace(len))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   644
        BAIL_MACRO(PHYSFS_ERR_INVALID_ARGUMENT, -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   645
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   646
    while (len > 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   647
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   648
        const DWORD thislen = (len > 0xFFFFFFFF) ? 0xFFFFFFFF : (DWORD) len;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   649
        DWORD numRead = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   650
        if (!ReadFile(Handle, buf, thislen, &numRead, NULL))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   651
            BAIL_MACRO(errcodeFromWinApi(), -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   652
        len -= (PHYSFS_uint64) numRead;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   653
        totalRead += (PHYSFS_sint64) numRead;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   654
        if (numRead != thislen)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   655
            break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   656
    } /* while */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   657
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   658
    return totalRead;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   659
} /* __PHYSFS_platformRead */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   660
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   661
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   662
PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   663
                                     PHYSFS_uint64 len)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   664
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   665
    HANDLE Handle = ((WinApiFile *) opaque)->handle;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   666
    PHYSFS_sint64 totalWritten = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   667
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   668
    if (!__PHYSFS_ui64FitsAddressSpace(len))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   669
        BAIL_MACRO(PHYSFS_ERR_INVALID_ARGUMENT, -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   670
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   671
    while (len > 0)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   672
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   673
        const DWORD thislen = (len > 0xFFFFFFFF) ? 0xFFFFFFFF : (DWORD) len;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   674
        DWORD numWritten = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   675
        if (!WriteFile(Handle, buffer, thislen, &numWritten, NULL))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   676
            BAIL_MACRO(errcodeFromWinApi(), -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   677
        len -= (PHYSFS_uint64) numWritten;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   678
        totalWritten += (PHYSFS_sint64) numWritten;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   679
        if (numWritten != thislen)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   680
            break;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   681
    } /* while */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   682
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   683
    return totalWritten;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   684
} /* __PHYSFS_platformWrite */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   685
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   686
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   687
int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   688
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   689
    HANDLE Handle = ((WinApiFile *) opaque)->handle;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   690
    LONG HighOrderPos;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   691
    PLONG pHighOrderPos;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   692
    DWORD rc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   693
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   694
    /* Get the high order 32-bits of the position */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   695
    HighOrderPos = HIGHORDER_UINT64(pos);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   696
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   697
    /*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   698
     * MSDN: "If you do not need the high-order 32 bits, this
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   699
     *         pointer must be set to NULL."
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   700
     */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   701
    pHighOrderPos = (HighOrderPos) ? &HighOrderPos : NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   702
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   703
    /* Move pointer "pos" count from start of file */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   704
    rc = SetFilePointer(Handle, LOWORDER_UINT64(pos),
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   705
                        pHighOrderPos, FILE_BEGIN);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   706
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   707
    if ( (rc == PHYSFS_INVALID_SET_FILE_POINTER) &&
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   708
         (GetLastError() != NO_ERROR) )
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   709
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   710
        BAIL_MACRO(errcodeFromWinApi(), 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   711
    } /* if */
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   712
    
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   713
    return 1;  /* No error occured */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   714
} /* __PHYSFS_platformSeek */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   715
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   716
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   717
PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   718
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   719
    HANDLE Handle = ((WinApiFile *) opaque)->handle;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   720
    LONG HighPos = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   721
    DWORD LowPos;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   722
    PHYSFS_sint64 retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   723
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   724
    /* Get current position */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   725
    LowPos = SetFilePointer(Handle, 0, &HighPos, FILE_CURRENT);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   726
    if ( (LowPos == PHYSFS_INVALID_SET_FILE_POINTER) &&
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   727
         (GetLastError() != NO_ERROR) )
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   728
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   729
        BAIL_MACRO(errcodeFromWinApi(), -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   730
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   731
    else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   732
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   733
        /* Combine the high/low order to create the 64-bit position value */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   734
        retval = (((PHYSFS_uint64) HighPos) << 32) | LowPos;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   735
        assert(retval >= 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   736
    } /* else */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   737
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   738
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   739
} /* __PHYSFS_platformTell */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   740
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   741
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   742
PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   743
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   744
    HANDLE Handle = ((WinApiFile *) opaque)->handle;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   745
    DWORD SizeHigh;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   746
    DWORD SizeLow;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   747
    PHYSFS_sint64 retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   748
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   749
    SizeLow = GetFileSize(Handle, &SizeHigh);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   750
    if ( (SizeLow == PHYSFS_INVALID_SET_FILE_POINTER) &&
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   751
         (GetLastError() != NO_ERROR) )
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   752
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   753
        BAIL_MACRO(errcodeFromWinApi(), -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   754
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   755
    else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   756
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   757
        /* Combine the high/low order to create the 64-bit position value */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   758
        retval = (((PHYSFS_uint64) SizeHigh) << 32) | SizeLow;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   759
        assert(retval >= 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   760
    } /* else */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   761
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   762
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   763
} /* __PHYSFS_platformFileLength */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   764
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   765
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   766
int __PHYSFS_platformFlush(void *opaque)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   767
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   768
    WinApiFile *fh = ((WinApiFile *) opaque);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   769
    if (!fh->readonly)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   770
        BAIL_IF_MACRO(!FlushFileBuffers(fh->handle), errcodeFromWinApi(), 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   771
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   772
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   773
} /* __PHYSFS_platformFlush */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   774
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   775
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   776
void __PHYSFS_platformClose(void *opaque)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   777
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   778
    HANDLE Handle = ((WinApiFile *) opaque)->handle;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   779
    (void) CloseHandle(Handle); /* ignore errors. You should have flushed! */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   780
    allocator.Free(opaque);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   781
} /* __PHYSFS_platformClose */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   782
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   783
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   784
static int doPlatformDelete(LPWSTR wpath)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   785
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   786
    const int isdir = (GetFileAttributesW(wpath) & FILE_ATTRIBUTE_DIRECTORY);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   787
    const BOOL rc = (isdir) ? RemoveDirectoryW(wpath) : DeleteFileW(wpath);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   788
    BAIL_IF_MACRO(!rc, errcodeFromWinApi(), 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   789
    return 1;   /* if you made it here, it worked. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   790
} /* doPlatformDelete */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   791
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   792
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   793
int __PHYSFS_platformDelete(const char *path)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   794
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   795
    int retval = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   796
    LPWSTR wpath = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   797
    UTF8_TO_UNICODE_STACK_MACRO(wpath, path);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   798
    BAIL_IF_MACRO(!wpath, PHYSFS_ERR_OUT_OF_MEMORY, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   799
    retval = doPlatformDelete(wpath);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   800
    __PHYSFS_smallFree(wpath);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   801
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   802
} /* __PHYSFS_platformDelete */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   803
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   804
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   805
void *__PHYSFS_platformCreateMutex(void)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   806
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   807
    LPCRITICAL_SECTION lpcs;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   808
    lpcs = (LPCRITICAL_SECTION) allocator.Malloc(sizeof (CRITICAL_SECTION));
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   809
    BAIL_IF_MACRO(!lpcs, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   810
    InitializeCriticalSection(lpcs);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   811
    return lpcs;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   812
} /* __PHYSFS_platformCreateMutex */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   813
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   814
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   815
void __PHYSFS_platformDestroyMutex(void *mutex)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   816
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   817
    DeleteCriticalSection((LPCRITICAL_SECTION) mutex);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   818
    allocator.Free(mutex);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   819
} /* __PHYSFS_platformDestroyMutex */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   820
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   821
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   822
int __PHYSFS_platformGrabMutex(void *mutex)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   823
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   824
    EnterCriticalSection((LPCRITICAL_SECTION) mutex);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   825
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   826
} /* __PHYSFS_platformGrabMutex */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   827
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   828
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   829
void __PHYSFS_platformReleaseMutex(void *mutex)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   830
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   831
    LeaveCriticalSection((LPCRITICAL_SECTION) mutex);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   832
} /* __PHYSFS_platformReleaseMutex */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   833
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   834
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   835
static PHYSFS_sint64 FileTimeToPhysfsTime(const FILETIME *ft)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   836
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   837
    SYSTEMTIME st_utc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   838
    SYSTEMTIME st_localtz;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   839
    TIME_ZONE_INFORMATION tzi;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   840
    DWORD tzid;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   841
    PHYSFS_sint64 retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   842
    struct tm tm;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   843
    BOOL rc;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   844
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   845
    BAIL_IF_MACRO(!FileTimeToSystemTime(ft, &st_utc), errcodeFromWinApi(), -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   846
    tzid = GetTimeZoneInformation(&tzi);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   847
    BAIL_IF_MACRO(tzid == TIME_ZONE_ID_INVALID, errcodeFromWinApi(), -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   848
    rc = SystemTimeToTzSpecificLocalTime(&tzi, &st_utc, &st_localtz);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   849
    BAIL_IF_MACRO(!rc, errcodeFromWinApi(), -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   850
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   851
    /* Convert to a format that mktime() can grok... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   852
    tm.tm_sec = st_localtz.wSecond;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   853
    tm.tm_min = st_localtz.wMinute;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   854
    tm.tm_hour = st_localtz.wHour;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   855
    tm.tm_mday = st_localtz.wDay;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   856
    tm.tm_mon = st_localtz.wMonth - 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   857
    tm.tm_year = st_localtz.wYear - 1900;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   858
    tm.tm_wday = -1 /*st_localtz.wDayOfWeek*/;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   859
    tm.tm_yday = -1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   860
    tm.tm_isdst = -1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   861
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   862
    /* Convert to a format PhysicsFS can grok... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   863
    retval = (PHYSFS_sint64) mktime(&tm);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   864
    BAIL_IF_MACRO(retval == -1, PHYSFS_ERR_OS_ERROR, -1);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   865
    return retval;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   866
} /* FileTimeToPhysfsTime */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   867
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   868
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   869
int __PHYSFS_platformStat(const char *filename, PHYSFS_Stat *st)
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   870
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   871
    WIN32_FILE_ATTRIBUTE_DATA winstat;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   872
    WCHAR *wstr = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   873
    DWORD err = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   874
    BOOL rc = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   875
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   876
    UTF8_TO_UNICODE_STACK_MACRO(wstr, filename);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   877
    BAIL_IF_MACRO(!wstr, PHYSFS_ERR_OUT_OF_MEMORY, 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   878
    rc = GetFileAttributesExW(wstr, GetFileExInfoStandard, &winstat);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   879
    err = (!rc) ? GetLastError() : 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   880
    __PHYSFS_smallFree(wstr);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   881
    BAIL_IF_MACRO(!rc, errcodeFromWinApiError(err), 0);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   882
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   883
    st->modtime = FileTimeToPhysfsTime(&winstat.ftLastWriteTime);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   884
    st->accesstime = FileTimeToPhysfsTime(&winstat.ftLastAccessTime);
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   885
    st->createtime = FileTimeToPhysfsTime(&winstat.ftCreationTime);
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   886
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   887
    if(winstat.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   888
    {
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   889
        st->filetype = PHYSFS_FILETYPE_DIRECTORY;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   890
        st->filesize = 0;
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   891
    } /* if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   892
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   893
    else if(winstat.dwFileAttributes & (FILE_ATTRIBUTE_OFFLINE | FILE_ATTRIBUTE_DEVICE))
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   894
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   895
        /* !!! FIXME: what are reparse points? */
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   896
        st->filetype = PHYSFS_FILETYPE_OTHER;
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   897
        /* !!! FIXME: don't rely on this */
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   898
        st->filesize = 0;
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   899
    } /* else if */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   900
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   901
    /* !!! FIXME: check for symlinks on Vista. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   902
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   903
    else
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   904
    {
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   905
        st->filetype = PHYSFS_FILETYPE_REGULAR;
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   906
        st->filesize = (((PHYSFS_uint64) winstat.nFileSizeHigh) << 32) | winstat.nFileSizeLow;
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   907
    } /* else */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   908
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   909
    st->readonly = ((winstat.dwFileAttributes & FILE_ATTRIBUTE_READONLY) != 0);
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   910
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   911
    return 1;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   912
} /* __PHYSFS_platformStat */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   913
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   914
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   915
/* !!! FIXME: Don't use C runtime for allocators? */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   916
int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   917
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   918
    return 0;  /* just use malloc() and friends. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   919
} /* __PHYSFS_platformSetDefaultAllocator */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   920
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   921
#endif  /* PHYSFS_PLATFORM_WINDOWS */
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 10017
diff changeset
   922
#endif  /* PHYSFS_PLATFORM_WINRT */
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   923
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   924
/* end of windows.c ... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   925
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   926