src/platform_windows.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 21 Mar 2012 23:30:50 -0400
changeset 1242 1e6db80d2393
parent 1240 22d4d1bd4e21
child 1244 528c98081b16
permissions -rw-r--r--
Added PHYSFS_getPrefDir().
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/*
810
fafcbe604881 Fixed some comments.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
     2
 * Windows support routines for PhysicsFS.
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
 *
809
116b8fe30371 Renamed LICENSE to LICENSE.txt
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
     4
 * Please see the file LICENSE.txt in the source's root directory.
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
 *
198
39274fb69c25 More cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 197
diff changeset
     6
 *  This file written by Ryan C. Gordon, and made sane by Gregory S. Read.
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
 */
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
818
e36f23f49042 Now compiles everything whether we need it or not, removing whole files with
Ryan C. Gordon <icculus@icculus.org>
parents: 810
diff changeset
     9
#define __PHYSICSFS_INTERNAL__
e36f23f49042 Now compiles everything whether we need it or not, removing whole files with
Ryan C. Gordon <icculus@icculus.org>
parents: 810
diff changeset
    10
#include "physfs_platforms.h"
e36f23f49042 Now compiles everything whether we need it or not, removing whole files with
Ryan C. Gordon <icculus@icculus.org>
parents: 810
diff changeset
    11
e36f23f49042 Now compiles everything whether we need it or not, removing whole files with
Ryan C. Gordon <icculus@icculus.org>
parents: 810
diff changeset
    12
#ifdef PHYSFS_PLATFORM_WINDOWS
327
76e207c6b4a7 Build system corrections for BeOS and Cygwin.
Ryan C. Gordon <icculus@icculus.org>
parents: 313
diff changeset
    13
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
    14
/* Forcibly disable UNICODE macro, since we manage this ourselves. */
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
    15
#ifdef UNICODE
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
    16
#undef UNICODE
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
    17
#endif
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
    18
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
    19
#define WIN32_LEAN_AND_MEAN 1
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
#include <windows.h>
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
    21
#include <userenv.h>
1217
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
    22
#include <dbt.h>
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
    23
#include <errno.h>
76
d6405544e896 __PHYSFS_platformGetUserDir() checks some win32 standard environment variables,
Ryan C. Gordon <icculus@icculus.org>
parents: 71
diff changeset
    24
#include <ctype.h>
262
2d332d53c263 -Fixed file enumerator function (needed a wildcard '*' specification)
Gregory S. Read <zeph@clutteredmind.org>
parents: 240
diff changeset
    25
#include <time.h>
210
f5507414f7b7 -Added optional DISABLE_NT_SUPPORT to not compile NT specific code
Gregory S. Read <zeph@clutteredmind.org>
parents: 198
diff changeset
    26
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
#include "physfs_internal.h"
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
908
662ca06432ef Mingw32 fix for 64-bit literals.
Ryan C. Gordon <icculus@icculus.org>
parents: 906
diff changeset
    29
#define LOWORDER_UINT64(pos) ((PHYSFS_uint32) (pos & 0xFFFFFFFF))
662ca06432ef Mingw32 fix for 64-bit literals.
Ryan C. Gordon <icculus@icculus.org>
parents: 906
diff changeset
    30
#define HIGHORDER_UINT64(pos) ((PHYSFS_uint32) ((pos >> 32) & 0xFFFFFFFF))
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
    31
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
    32
/*
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
    33
 * Users without the platform SDK don't have this defined.  The original docs
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
    34
 *  for SetFilePointer() just said to compare with 0xFFFFFFFF, so this should
784
fb4a01af9ff7 Silly comment typo.
Ryan C. Gordon <icculus@icculus.org>
parents: 763
diff changeset
    35
 *  work as desired.
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
    36
 */
606
70cf47819dd2 Fixed Win32 seeking on > 2gig files, other Cygwin issues.
Ryan C. Gordon <icculus@icculus.org>
parents: 596
diff changeset
    37
#define PHYSFS_INVALID_SET_FILE_POINTER  0xFFFFFFFF
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
    39
/* just in case... */
606
70cf47819dd2 Fixed Win32 seeking on > 2gig files, other Cygwin issues.
Ryan C. Gordon <icculus@icculus.org>
parents: 596
diff changeset
    40
#define PHYSFS_INVALID_FILE_ATTRIBUTES   0xFFFFFFFF
70cf47819dd2 Fixed Win32 seeking on > 2gig files, other Cygwin issues.
Ryan C. Gordon <icculus@icculus.org>
parents: 596
diff changeset
    41
875
6a5f06dbd423 Patched to compile, and work with pre-Vista versions of the Platform SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 874
diff changeset
    42
/* Not defined before the Vista SDK. */
6a5f06dbd423 Patched to compile, and work with pre-Vista versions of the Platform SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 874
diff changeset
    43
#define PHYSFS_IO_REPARSE_TAG_SYMLINK    0xA000000C
6a5f06dbd423 Patched to compile, and work with pre-Vista versions of the Platform SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 874
diff changeset
    44
6a5f06dbd423 Patched to compile, and work with pre-Vista versions of the Platform SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 874
diff changeset
    45
858
969e69b2822f Improved dynamic loader and initial Unicode work in windows.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 856
diff changeset
    46
#define UTF8_TO_UNICODE_STACK_MACRO(w_assignto, str) { \
969e69b2822f Improved dynamic loader and initial Unicode work in windows.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 856
diff changeset
    47
    if (str == NULL) \
969e69b2822f Improved dynamic loader and initial Unicode work in windows.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 856
diff changeset
    48
        w_assignto = NULL; \
969e69b2822f Improved dynamic loader and initial Unicode work in windows.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 856
diff changeset
    49
    else { \
915
1445c55cdbe6 Fixed allocation size when converting from UTF-8 to UCS-2.
Ryan C. Gordon <icculus@icculus.org>
parents: 910
diff changeset
    50
        const PHYSFS_uint64 len = (PHYSFS_uint64) ((strlen(str) + 1) * 2); \
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
    51
        w_assignto = (WCHAR *) __PHYSFS_smallAlloc(len); \
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
    52
        if (w_assignto != NULL) \
1237
6d8b32355a28 Use UTF-16 on Windows, not UCS-2.
Ryan C. Gordon <icculus@icculus.org>
parents: 1226
diff changeset
    53
            PHYSFS_utf8ToUtf16(str, (PHYSFS_uint16 *) w_assignto, len); \
858
969e69b2822f Improved dynamic loader and initial Unicode work in windows.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 856
diff changeset
    54
    } \
969e69b2822f Improved dynamic loader and initial Unicode work in windows.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 856
diff changeset
    55
} \
969e69b2822f Improved dynamic loader and initial Unicode work in windows.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 856
diff changeset
    56
1237
6d8b32355a28 Use UTF-16 on Windows, not UCS-2.
Ryan C. Gordon <icculus@icculus.org>
parents: 1226
diff changeset
    57
/* Note this counts WCHARs, not codepoints! */
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
    58
static PHYSFS_uint64 wStrLen(const WCHAR *wstr)
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
    59
{
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
    60
    PHYSFS_uint64 len = 0;
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
    61
    while (*(wstr++))
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
    62
        len++;
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1012
diff changeset
    63
    return len;
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
    64
} /* wStrLen */
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
    65
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
    66
static char *unicodeToUtf8Heap(const WCHAR *w_str)
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
    67
{
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
    68
    char *retval = NULL;
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
    69
    if (w_str != NULL)
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
    70
    {
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
    71
        void *ptr = NULL;
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
    72
        const PHYSFS_uint64 len = (wStrLen(w_str) * 4) + 1;
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
    73
        retval = allocator.Malloc(len);
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
    74
        BAIL_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
1237
6d8b32355a28 Use UTF-16 on Windows, not UCS-2.
Ryan C. Gordon <icculus@icculus.org>
parents: 1226
diff changeset
    75
        PHYSFS_utf8FromUtf16((const PHYSFS_uint16 *) w_str, retval, len);
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
    76
        ptr = allocator.Realloc(retval, strlen(retval) + 1); /* shrink. */
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
    77
        if (ptr != NULL)
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
    78
            retval = (char *) ptr;
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
    79
    } /* if */
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1012
diff changeset
    80
    return retval;
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
    81
} /* unicodeToUtf8Heap */
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
    82
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
    83
/* !!! FIXME: do we really need readonly? If not, do we need this struct? */
858
969e69b2822f Improved dynamic loader and initial Unicode work in windows.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 856
diff changeset
    84
typedef struct
969e69b2822f Improved dynamic loader and initial Unicode work in windows.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 856
diff changeset
    85
{
969e69b2822f Improved dynamic loader and initial Unicode work in windows.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 856
diff changeset
    86
    HANDLE handle;
969e69b2822f Improved dynamic loader and initial Unicode work in windows.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 856
diff changeset
    87
    int readonly;
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
    88
} WinApiFile;
858
969e69b2822f Improved dynamic loader and initial Unicode work in windows.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 856
diff changeset
    89
864
ce0fcbbba8a3 Man, I knew I would need getOSInfo() as soon as I deleted it!
Ryan C. Gordon <icculus@icculus.org>
parents: 862
diff changeset
    90
1242
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
    91
/* !!! FIXME: we cache userDir in physfs.c during PHYSFS_init(), too. */
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
    92
static char *userDir = NULL;
858
969e69b2822f Improved dynamic loader and initial Unicode work in windows.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 856
diff changeset
    93
static HANDLE libUserEnv = NULL;
1217
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
    94
static HANDLE detectCDThreadHandle = NULL;
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
    95
static HWND detectCDHwnd = 0;
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
    96
static volatile int initialDiscDetectionComplete = 0;
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
    97
static volatile DWORD drivesWithMediaBitmap = 0;
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
    98
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   100
static PHYSFS_ErrorCode errcodeFromWinApiError(const DWORD err)
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   101
{
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   102
    /*
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   103
     * win32 error codes are sort of a tricky thing; Microsoft intentionally
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   104
     *  doesn't list which ones a given API might trigger, there are several
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   105
     *  with overlapping and unclear meanings...and there's 16 thousand of
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   106
     *  them in Windows 7. It looks like the ones we care about are in the
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   107
     *  first 500, but I can't say this list is perfect; we might miss
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   108
     *  important values or misinterpret others.
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   109
     *
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   110
     * Don't treat this list as anything other than a work in progress.
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   111
     */
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   112
    switch (err)
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   113
    {
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   114
        case ERROR_SUCCESS: return PHYSFS_ERR_OK;
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   115
        case ERROR_ACCESS_DENIED: return PHYSFS_ERR_PERMISSION;
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   116
        case ERROR_NETWORK_ACCESS_DENIED: return PHYSFS_ERR_PERMISSION;
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   117
        case ERROR_DISK_RESOURCES_EXHAUSTED: return PHYSFS_ERR_NO_SPACE;
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   118
        case ERROR_NOT_READY: return PHYSFS_ERR_IO;
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   119
        case ERROR_CRC: return PHYSFS_ERR_IO;
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   120
        case ERROR_SEEK: return PHYSFS_ERR_IO;
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   121
        case ERROR_SECTOR_NOT_FOUND: return PHYSFS_ERR_IO;
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   122
        case ERROR_NOT_DOS_DISK: return PHYSFS_ERR_IO;
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   123
        case ERROR_WRITE_FAULT: return PHYSFS_ERR_IO;
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   124
        case ERROR_READ_FAULT: return PHYSFS_ERR_IO;
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   125
        case ERROR_DEV_NOT_EXIST: return PHYSFS_ERR_IO;
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   126
        case ERROR_DATA_CHECKSUM_ERROR: return PHYSFS_ERR_IO;
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   127
        /* !!! FIXME: ?? case ELOOP: return PHYSFS_ERR_SYMLINK_LOOP; */
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   128
        case ERROR_BUFFER_OVERFLOW: return PHYSFS_ERR_BAD_FILENAME;
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   129
        case ERROR_INVALID_NAME: return PHYSFS_ERR_BAD_FILENAME;
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   130
        case ERROR_BAD_PATHNAME: return PHYSFS_ERR_BAD_FILENAME;
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   131
        case ERROR_DIRECTORY: return PHYSFS_ERR_BAD_FILENAME;
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   132
        case ERROR_FILE_NOT_FOUND: return PHYSFS_ERR_NO_SUCH_PATH;
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   133
        case ERROR_PATH_NOT_FOUND: return PHYSFS_ERR_NO_SUCH_PATH;
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   134
        case ERROR_DELETE_PENDING: return PHYSFS_ERR_NO_SUCH_PATH;
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   135
        case ERROR_INVALID_DRIVE: return PHYSFS_ERR_NO_SUCH_PATH;
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   136
        case ERROR_HANDLE_DISK_FULL: return PHYSFS_ERR_NO_SPACE;
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   137
        case ERROR_DISK_FULL: return PHYSFS_ERR_NO_SPACE;
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   138
        /* !!! FIXME: ?? case ENOTDIR: return PHYSFS_ERR_NO_SUCH_PATH; */
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   139
        /* !!! FIXME: ?? case EISDIR: return PHYSFS_ERR_NOT_A_FILE; */
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   140
        case ERROR_WRITE_PROTECT: return PHYSFS_ERR_READ_ONLY;
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   141
        case ERROR_LOCK_VIOLATION: return PHYSFS_ERR_BUSY;
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   142
        case ERROR_SHARING_VIOLATION: return PHYSFS_ERR_BUSY;
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   143
        case ERROR_CURRENT_DIRECTORY: return PHYSFS_ERR_BUSY;
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   144
        case ERROR_DRIVE_LOCKED: return PHYSFS_ERR_BUSY;
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   145
        case ERROR_PATH_BUSY: return PHYSFS_ERR_BUSY;
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   146
        case ERROR_BUSY: return PHYSFS_ERR_BUSY;
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   147
        case ERROR_NOT_ENOUGH_MEMORY: return PHYSFS_ERR_OUT_OF_MEMORY;
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   148
        case ERROR_OUTOFMEMORY: return PHYSFS_ERR_OUT_OF_MEMORY;
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   149
        case ERROR_DIR_NOT_EMPTY: return PHYSFS_ERR_DIR_NOT_EMPTY;
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   150
        default: return PHYSFS_ERR_OS_ERROR;
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   151
    } /* switch */
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   152
} /* errcodeFromWinApiError */
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   153
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   154
static inline PHYSFS_ErrorCode errcodeFromWinApi(void)
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   155
{
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   156
    return errcodeFromWinApiError(GetLastError());
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   157
} /* errcodeFromWinApi */
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   158
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   159
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   161
/*
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   162
 * On success, module-scope variable (userDir) will have a pointer to
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   163
 *  a malloc()'d string of the user's profile dir, and a non-zero value is
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   164
 *  returned. If we can't determine the profile dir, (userDir) will
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   165
 *  be NULL, and zero is returned.
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   166
 */
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   167
static int determineUserDir(void)
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   168
{
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   169
    typedef BOOL (WINAPI *fnGetUserProfDirW)(HANDLE, LPWSTR, LPDWORD);
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   170
    fnGetUserProfDirW pGetDir = NULL;
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   172
    HANDLE accessToken = NULL;       /* Security handle to process */
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   173
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   174
    if (userDir != NULL)
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   175
        return 1;  /* already good to go. */
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   176
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   177
    pGetDir = (fnGetUserProfDirW)
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   178
        GetProcAddress(libUserEnv, "GetUserProfileDirectoryW");
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   179
    BAIL_IF_MACRO(pGetDir == NULL, errcodeFromWinApi(), 0);
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   180
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   181
    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &accessToken))
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   182
        BAIL_MACRO(errcodeFromWinApi(), 0);
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   183
    else
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   184
    {
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   185
        DWORD psize = 0;
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   186
        WCHAR dummy = 0;
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   187
        LPWSTR wstr = NULL;
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   188
        BOOL rc = 0;
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   189
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   190
        /*
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   191
         * Should fail. Will write the size of the profile path in
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   192
         *  psize. Also note that the second parameter can't be
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   193
         *  NULL or the function fails.
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   194
         */
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   195
    	rc = pGetDir(accessToken, &dummy, &psize);
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   196
        assert(!rc);  /* !!! FIXME: handle this gracefully. */
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   197
        (void) rc;
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   199
        /* Allocate memory for the profile directory */
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   200
        wstr = (LPWSTR) __PHYSFS_smallAlloc(psize * sizeof (WCHAR));
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   201
        if (wstr != NULL)
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   202
        {
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   203
            if (pGetDir(accessToken, wstr, &psize))
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   204
                userDir = unicodeToUtf8Heap(wstr);
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   205
            __PHYSFS_smallFree(wstr);
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   206
        } /* if */
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   207
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   208
        CloseHandle(accessToken);
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   209
    } /* if */
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   210
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   211
    return 1;  /* We made it: hit the showers. */
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   212
} /* determineUserDir */
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   213
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   214
1217
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   215
typedef BOOL (WINAPI *fnSTEM)(DWORD, LPDWORD b);
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   216
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   217
static DWORD pollDiscDrives(void)
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   218
{
1217
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   219
    /* Try to use SetThreadErrorMode(), which showed up in Windows 7. */
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   220
    HANDLE lib = LoadLibraryA("kernel32.dll");
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   221
    fnSTEM stem = NULL;
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   222
    char drive[4] = { 'x', ':', '\\', '\0' };
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   223
    DWORD oldErrorMode = 0;
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   224
    DWORD drives = 0;
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   225
    DWORD i;
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   226
1217
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   227
    if (lib)
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   228
        stem = (fnSTEM) GetProcAddress(lib, "SetThreadErrorMode");
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   229
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   230
    if (stem)
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   231
        stem(SEM_FAILCRITICALERRORS, &oldErrorMode);
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   232
    else
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   233
        oldErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   234
    
1217
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   235
    /* Do detection. This may block if a disc is spinning up. */
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   236
    for (i = 'A'; i <= 'Z'; i++)
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   237
    {
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   238
        DWORD tmp = 0;
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   239
        drive[0] = (char) i;
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   240
        if (GetDriveTypeA(drive) != DRIVE_CDROM)
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   241
            continue;
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   242
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   243
        /* If this function succeeds, there's media in the drive */
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   244
        if (GetVolumeInformationA(drive, NULL, 0, NULL, NULL, &tmp, NULL, 0))
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   245
            drives |= (1 << (i - 'A'));
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   246
    } /* for */
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   247
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   248
    if (stem)
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   249
        stem(oldErrorMode, NULL);
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   250
    else
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   251
        SetErrorMode(oldErrorMode);
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   252
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   253
    if (lib)
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   254
        FreeLibrary(lib);
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   255
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   256
    return drives;
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   257
} /* pollDiscDrives */
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   258
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   259
1217
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   260
static LRESULT CALLBACK detectCDWndProc(HWND hwnd, UINT msg,
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   261
                                        WPARAM wp, LPARAM lparam)
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   262
{
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   263
    PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR) lparam;
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   264
    PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME) lparam;
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   265
    const int removed = (wp == DBT_DEVICEREMOVECOMPLETE);
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   266
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   267
    if (msg == WM_DESTROY)
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   268
        return 0;
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   269
    else if ((msg != WM_DEVICECHANGE) ||
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   270
             ((wp != DBT_DEVICEARRIVAL) && (wp != DBT_DEVICEREMOVECOMPLETE)) ||
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   271
             (lpdb->dbch_devicetype != DBT_DEVTYP_VOLUME) ||
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   272
             ((lpdbv->dbcv_flags & DBTF_MEDIA) == 0))
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   273
    {
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   274
        return DefWindowProcW(hwnd, msg, wp, lparam);
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   275
    } /* else if */
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   276
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   277
    if (removed)
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   278
        drivesWithMediaBitmap &= ~lpdbv->dbcv_unitmask;
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   279
    else
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   280
        drivesWithMediaBitmap |= lpdbv->dbcv_unitmask;
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   281
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   282
    return TRUE;
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   283
} /* detectCDWndProc */
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   284
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   285
1217
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   286
static DWORD WINAPI detectCDThread(LPVOID lpParameter)
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   287
{
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   288
    const char *classname = "PhysicsFSDetectCDCatcher";
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   289
    const char *winname = "PhysicsFSDetectCDMsgWindow";
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   290
    HINSTANCE hInstance = GetModuleHandleW(NULL);
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   291
    ATOM class_atom = 0;
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   292
    WNDCLASSEXA wce;
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   293
    MSG msg;
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   294
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   295
    memset(&wce, '\0', sizeof (wce));
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   296
    wce.cbSize = sizeof (wce);
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   297
    wce.lpfnWndProc = detectCDWndProc;
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   298
    wce.lpszClassName = classname;
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   299
    wce.hInstance = hInstance;
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   300
    class_atom = RegisterClassExA(&wce);
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   301
    if (class_atom == 0)
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   302
    {
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   303
        initialDiscDetectionComplete = 1;  /* let main thread go on. */
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   304
        return 0;
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   305
    } /* if */
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   306
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   307
    detectCDHwnd = CreateWindowExA(0, classname, winname, WS_OVERLAPPEDWINDOW,
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   308
                        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   309
                        CW_USEDEFAULT, HWND_DESKTOP, NULL, hInstance, NULL);
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   310
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   311
    if (detectCDHwnd == NULL)
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   312
    {
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   313
        initialDiscDetectionComplete = 1;  /* let main thread go on. */
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   314
        UnregisterClassA(classname, hInstance);
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   315
        return 0;
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   316
    } /* if */
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   317
1217
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   318
    /* We'll get events when discs come and go from now on. */
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   319
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   320
    /* Do initial detection, possibly blocking awhile... */
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   321
    drivesWithMediaBitmap = pollDiscDrives();
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   322
    initialDiscDetectionComplete = 1;  /* let main thread go on. */
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   323
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   324
    do
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   325
    {
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   326
        const BOOL rc = GetMessageW(&msg, detectCDHwnd, 0, 0);
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   327
        if ((rc == 0) || (rc == -1))
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   328
            break;  /* don't care if WM_QUIT or error break this loop. */
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   329
        TranslateMessage(&msg);
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   330
        DispatchMessageW(&msg);
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   331
    } while (1);
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   332
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   333
    /* we've been asked to quit. */
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   334
    DestroyWindow(detectCDHwnd);
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   335
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   336
    do
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   337
    {
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   338
        const BOOL rc = GetMessage(&msg, detectCDHwnd, 0, 0);
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   339
        if ((rc == 0) || (rc == -1))
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   340
            break;
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   341
        TranslateMessage(&msg);
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   342
        DispatchMessageW(&msg);
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   343
    } while (1);
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   344
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   345
    UnregisterClassA(classname, hInstance);
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   346
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   347
    return 0;
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   348
} /* detectCDThread */
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   349
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   350
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   351
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   352
{
1217
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   353
    char drive_str[4] = { 'x', ':', '\\', '\0' };
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   354
    DWORD drives = 0;
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   355
    DWORD i;
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   356
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   357
    /*
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   358
     * If you poll a drive while a user is inserting a disc, the OS will
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   359
     *  block this thread until the drive has spun up. So we swallow the risk
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   360
     *  once for initial detection, and spin a thread that will get device
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   361
     *  events thereafter, for apps that use this interface to poll for
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   362
     *  disc insertion.
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   363
     */
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   364
    if (!detectCDThreadHandle)
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   365
    {
1217
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   366
        initialDiscDetectionComplete = 0;
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   367
        detectCDThreadHandle = CreateThread(NULL,0,detectCDThread,NULL,0,NULL);
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   368
        if (detectCDThreadHandle == NULL)
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   369
            return;  /* oh well. */
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   370
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   371
        while (!initialDiscDetectionComplete)
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   372
            Sleep(50);
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   373
    } /* if */
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   374
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   375
    drives = drivesWithMediaBitmap; /* whatever the thread has seen, we take. */
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   376
    for (i = 'A'; i <= 'Z'; i++)
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   377
    {
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   378
        if (drives & (1 << (i - 'A')))
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   379
        {
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   380
            drive_str[0] = (char) i;
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   381
            cb(data, drive_str);
1217
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   382
        } /* if */
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   383
    } /* for */
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   384
} /* __PHYSFS_platformDetectAvailableCDs */
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   385
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   386
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   387
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
275
42f0e962abbb Lots of cleanups, fixes, and corrections. Should now work on all win32
Ryan C. Gordon <icculus@icculus.org>
parents: 268
diff changeset
   388
{
859
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   389
    DWORD buflen = 64;
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   390
    LPWSTR modpath = NULL;
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   391
    char *retval = NULL;
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   392
859
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   393
    while (1)
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   394
    {
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   395
        DWORD rc;
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   396
        void *ptr;
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   397
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   398
        if ( (ptr = allocator.Realloc(modpath, buflen*sizeof(WCHAR))) == NULL )
859
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   399
        {
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   400
            allocator.Free(modpath);
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   401
            BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
859
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   402
        } /* if */
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   403
        modpath = (LPWSTR) ptr;
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   404
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   405
        rc = GetModuleFileNameW(NULL, modpath, buflen);
859
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   406
        if (rc == 0)
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   407
        {
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   408
            allocator.Free(modpath);
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   409
            BAIL_MACRO(errcodeFromWinApi(), NULL);
859
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   410
        } /* if */
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   411
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   412
        if (rc < buflen)
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   413
        {
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   414
            buflen = rc;
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   415
            break;
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   416
        } /* if */
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   417
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   418
        buflen *= 2;
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   419
    } /* while */
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   420
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   421
    if (buflen > 0)  /* just in case... */
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   422
    {
859
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   423
        WCHAR *ptr = (modpath + buflen) - 1;
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   424
        while (ptr != modpath)
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   425
        {
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   426
            if (*ptr == '\\')
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   427
                break;
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   428
            ptr--;
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   429
        } /* while */
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   430
859
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   431
        if ((ptr == modpath) && (*ptr != '\\'))
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   432
            __PHYSFS_setError(ERR_GETMODFN_NO_DIR);
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   433
        else
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   434
        {
859
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   435
            *(ptr + 1) = '\0';  /* chop off filename. */
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   436
            retval = unicodeToUtf8Heap(modpath);
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   437
        } /* else */
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   438
    } /* else */
859
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   439
    allocator.Free(modpath);
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   440
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1012
diff changeset
   441
    return retval;   /* w00t. */
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   442
} /* __PHYSFS_platformCalcBaseDir */
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   443
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   444
1242
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   445
char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app)
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   446
{
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   447
    // Vista and later has a new API for this, but SHGetFolderPath works there,
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   448
    //  and apparently just wraps the new API. This is the new way to do it:
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   449
    // SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_CREATE,
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   450
    //                      NULL, &wszPath);
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   451
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   452
    WCHAR path[MAX_PATH];
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   453
    if (!SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE,
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   454
                                   NULL, 0, path)))
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   455
        BAIL_MACRO(PHYSFS_ERR_OS_ERROR, NULL);
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   456
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   457
    return unicodeToUtf8Heap(path);
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   458
} /* __PHYSFS_platformCalcPrefDir */
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   459
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   460
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   461
char *__PHYSFS_platformGetUserName(void)
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   462
{
71
105bdb041c03 More win32 updates.
Ryan C. Gordon <icculus@icculus.org>
parents: 68
diff changeset
   463
    DWORD bufsize = 0;
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   464
    char *retval = NULL;
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   465
    
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   466
    if (GetUserNameW(NULL, &bufsize) == 0)  /* This SHOULD fail. */
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   467
    {
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   468
        LPWSTR wbuf = (LPWSTR) __PHYSFS_smallAlloc(bufsize * sizeof (WCHAR));
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   469
        BAIL_IF_MACRO(!wbuf, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   470
        if (GetUserNameW(wbuf, &bufsize) == 0)  /* ?! */
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   471
            __PHYSFS_setError(errcodeFromWinApi());
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   472
        else
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   473
            retval = unicodeToUtf8Heap(wbuf);
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   474
        __PHYSFS_smallFree(wbuf);
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   475
    } /* if */
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   476
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1012
diff changeset
   477
    return retval;
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   478
} /* __PHYSFS_platformGetUserName */
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   479
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   480
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   481
char *__PHYSFS_platformGetUserDir(void)
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   482
{
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   483
    char *retval = (char *) allocator.Malloc(strlen(userDir) + 1);
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   484
    BAIL_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   485
    strcpy(retval, userDir); /* calculated at init time. */
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1012
diff changeset
   486
    return retval;
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   487
} /* __PHYSFS_platformGetUserDir */
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   488
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   489
1012
f254870dd7dd Attempt to clean up the thread ID mess in platform_unix ...
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   490
void *__PHYSFS_platformGetThreadID(void)
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   491
{
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1012
diff changeset
   492
    return ( (void *) ((size_t) GetCurrentThreadId()) );
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   493
} /* __PHYSFS_platformGetThreadID */
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   494
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   495
875
6a5f06dbd423 Patched to compile, and work with pre-Vista versions of the Platform SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 874
diff changeset
   496
static int isSymlinkAttrs(const DWORD attr, const DWORD tag)
874
ad804f24633f Symlink support for Windows Vista. Untested code.
Ryan C. Gordon <icculus@icculus.org>
parents: 864
diff changeset
   497
{
875
6a5f06dbd423 Patched to compile, and work with pre-Vista versions of the Platform SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 874
diff changeset
   498
    return ( (attr & FILE_ATTRIBUTE_REPARSE_POINT) && 
6a5f06dbd423 Patched to compile, and work with pre-Vista versions of the Platform SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 874
diff changeset
   499
             (tag == PHYSFS_IO_REPARSE_TAG_SYMLINK) );
874
ad804f24633f Symlink support for Windows Vista. Untested code.
Ryan C. Gordon <icculus@icculus.org>
parents: 864
diff changeset
   500
} /* isSymlinkAttrs */
ad804f24633f Symlink support for Windows Vista. Untested code.
Ryan C. Gordon <icculus@icculus.org>
parents: 864
diff changeset
   501
ad804f24633f Symlink support for Windows Vista. Untested code.
Ryan C. Gordon <icculus@icculus.org>
parents: 864
diff changeset
   502
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 651
diff changeset
   503
void __PHYSFS_platformEnumerateFiles(const char *dirname,
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 651
diff changeset
   504
                                     int omitSymLinks,
754
e7cd7411eadf API BREAKAGE: Changed PHYSFS_enumerateFilesCallback() to pass the originally
Ryan C. Gordon <icculus@icculus.org>
parents: 748
diff changeset
   505
                                     PHYSFS_EnumFilesCallback callback,
e7cd7411eadf API BREAKAGE: Changed PHYSFS_enumerateFilesCallback() to pass the originally
Ryan C. Gordon <icculus@icculus.org>
parents: 748
diff changeset
   506
                                     const char *origdir,
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 651
diff changeset
   507
                                     void *callbackdata)
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   508
{
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   509
    HANDLE dir = INVALID_HANDLE_VALUE;
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   510
    WIN32_FIND_DATAW entw;
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 651
diff changeset
   511
    size_t len = strlen(dirname);
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   512
    char *searchPath = NULL;
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   513
    WCHAR *wSearchPath = NULL;
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   514
268
0f47386615f0 Enumerate files code: cleaned up memory leak, Doesn't throw away file entry
Ryan C. Gordon <icculus@icculus.org>
parents: 262
diff changeset
   515
    /* Allocate a new string for path, maybe '\\', "*", and NULL terminator */
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   516
    searchPath = (char *) __PHYSFS_smallAlloc(len + 3);
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   517
    if (searchPath == NULL)
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 651
diff changeset
   518
        return;
275
42f0e962abbb Lots of cleanups, fixes, and corrections. Should now work on all win32
Ryan C. Gordon <icculus@icculus.org>
parents: 268
diff changeset
   519
262
2d332d53c263 -Fixed file enumerator function (needed a wildcard '*' specification)
Gregory S. Read <zeph@clutteredmind.org>
parents: 240
diff changeset
   520
    /* Copy current dirname */
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   521
    strcpy(searchPath, dirname);
268
0f47386615f0 Enumerate files code: cleaned up memory leak, Doesn't throw away file entry
Ryan C. Gordon <icculus@icculus.org>
parents: 262
diff changeset
   522
0f47386615f0 Enumerate files code: cleaned up memory leak, Doesn't throw away file entry
Ryan C. Gordon <icculus@icculus.org>
parents: 262
diff changeset
   523
    /* if there's no '\\' at the end of the path, stick one in there. */
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   524
    if (searchPath[len - 1] != '\\')
268
0f47386615f0 Enumerate files code: cleaned up memory leak, Doesn't throw away file entry
Ryan C. Gordon <icculus@icculus.org>
parents: 262
diff changeset
   525
    {
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   526
        searchPath[len++] = '\\';
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   527
        searchPath[len] = '\0';
268
0f47386615f0 Enumerate files code: cleaned up memory leak, Doesn't throw away file entry
Ryan C. Gordon <icculus@icculus.org>
parents: 262
diff changeset
   528
    } /* if */
0f47386615f0 Enumerate files code: cleaned up memory leak, Doesn't throw away file entry
Ryan C. Gordon <icculus@icculus.org>
parents: 262
diff changeset
   529
262
2d332d53c263 -Fixed file enumerator function (needed a wildcard '*' specification)
Gregory S. Read <zeph@clutteredmind.org>
parents: 240
diff changeset
   530
    /* Append the "*" to the end of the string */
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   531
    strcat(searchPath, "*");
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   532
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   533
    UTF8_TO_UNICODE_STACK_MACRO(wSearchPath, searchPath);
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   534
    if (!wSearchPath)
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   535
        return;  /* oh well. */
262
2d332d53c263 -Fixed file enumerator function (needed a wildcard '*' specification)
Gregory S. Read <zeph@clutteredmind.org>
parents: 240
diff changeset
   536
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   537
    dir = FindFirstFileW(wSearchPath, &entw);
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   538
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   539
    __PHYSFS_smallFree(wSearchPath);
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   540
    __PHYSFS_smallFree(searchPath);
657
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 651
diff changeset
   541
    if (dir == INVALID_HANDLE_VALUE)
dad3b5c307a9 Added callback APIs and ripped up the internals everywhere to use them.
Ryan C. Gordon <icculus@icculus.org>
parents: 651
diff changeset
   542
        return;
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   543
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   544
    do
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   545
    {
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   546
        const DWORD attr = entw.dwFileAttributes;
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   547
        const DWORD tag = entw.dwReserved0;
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   548
        const WCHAR *fn = entw.cFileName;
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   549
        char *utf8;
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   550
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   551
        if ((fn[0] == '.') && (fn[1] == '\0'))
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   552
            continue;
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   553
        if ((fn[0] == '.') && (fn[1] == '.') && (fn[2] == '\0'))
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   554
            continue;
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   555
        if ((omitSymLinks) && (isSymlinkAttrs(attr, tag)))
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   556
            continue;
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   557
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   558
        utf8 = unicodeToUtf8Heap(fn);
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   559
        if (utf8 != NULL)
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   560
        {
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   561
            callback(callbackdata, origdir, utf8);
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   562
            allocator.Free(utf8);
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   563
        } /* if */
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   564
    } while (FindNextFileW(dir, &entw) != 0);
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   565
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   566
    FindClose(dir);
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   567
} /* __PHYSFS_platformEnumerateFiles */
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   568
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   569
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   570
int __PHYSFS_platformMkDir(const char *path)
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   571
{
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   572
    WCHAR *wpath;
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   573
    DWORD rc;
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   574
    UTF8_TO_UNICODE_STACK_MACRO(wpath, path);
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   575
    rc = CreateDirectoryW(wpath, NULL);
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   576
    __PHYSFS_smallFree(wpath);
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   577
    BAIL_IF_MACRO(rc == 0, errcodeFromWinApi(), 0);
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1012
diff changeset
   578
    return 1;
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   579
} /* __PHYSFS_platformMkDir */
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   580
275
42f0e962abbb Lots of cleanups, fixes, and corrections. Should now work on all win32
Ryan C. Gordon <icculus@icculus.org>
parents: 268
diff changeset
   581
131
69b093825303 -Added init and deinit routines per physfs_internal.h changes.
Gregory S. Read <zeph@clutteredmind.org>
parents: 127
diff changeset
   582
int __PHYSFS_platformInit(void)
69b093825303 -Added init and deinit routines per physfs_internal.h changes.
Gregory S. Read <zeph@clutteredmind.org>
parents: 127
diff changeset
   583
{
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   584
    libUserEnv = LoadLibraryA("userenv.dll");
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   585
    BAIL_IF_MACRO(libUserEnv == NULL, errcodeFromWinApi(), 0);
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   586
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   587
    /* !!! FIXME: why do we precalculate this? */
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   588
    BAIL_IF_MACRO(!determineUserDir(), ERRPASS, 0);
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1012
diff changeset
   589
    return 1;  /* It's all good */
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   590
} /* __PHYSFS_platformInit */
131
69b093825303 -Added init and deinit routines per physfs_internal.h changes.
Gregory S. Read <zeph@clutteredmind.org>
parents: 127
diff changeset
   591
69b093825303 -Added init and deinit routines per physfs_internal.h changes.
Gregory S. Read <zeph@clutteredmind.org>
parents: 127
diff changeset
   592
69b093825303 -Added init and deinit routines per physfs_internal.h changes.
Gregory S. Read <zeph@clutteredmind.org>
parents: 127
diff changeset
   593
int __PHYSFS_platformDeinit(void)
69b093825303 -Added init and deinit routines per physfs_internal.h changes.
Gregory S. Read <zeph@clutteredmind.org>
parents: 127
diff changeset
   594
{
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   595
    if (detectCDThreadHandle)
1217
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   596
    {
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   597
        if (detectCDHwnd)
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   598
            PostMessageW(detectCDHwnd, WM_QUIT, 0, 0);
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   599
        CloseHandle(detectCDThreadHandle);
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   600
        detectCDThreadHandle = NULL;
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   601
        initialDiscDetectionComplete = 0;
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   602
        drivesWithMediaBitmap = 0;
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   603
    } /* if */
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   604
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   605
    if (libUserEnv)
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   606
        FreeLibrary(libUserEnv);
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   607
    libUserEnv = NULL;
858
969e69b2822f Improved dynamic loader and initial Unicode work in windows.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 856
diff changeset
   608
    allocator.Free(userDir);
969e69b2822f Improved dynamic loader and initial Unicode work in windows.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 856
diff changeset
   609
    userDir = NULL;
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1012
diff changeset
   610
    return 1; /* It's all good */
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   611
} /* __PHYSFS_platformDeinit */
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   612
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   613
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   614
static void *doOpen(const char *fname, DWORD mode, DWORD creation, int rdonly)
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   615
{
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   616
    HANDLE fileh;
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   617
    WinApiFile *retval;
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   618
    WCHAR *wfname;
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   619
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   620
    UTF8_TO_UNICODE_STACK_MACRO(wfname, fname);
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   621
    BAIL_IF_MACRO(!wfname, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   622
    fileh = CreateFileW(wfname, mode, FILE_SHARE_READ | FILE_SHARE_WRITE,
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   623
                             NULL, creation, FILE_ATTRIBUTE_NORMAL, NULL);
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   624
    __PHYSFS_smallFree(wfname);
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   625
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   626
    BAIL_IF_MACRO(fileh == INVALID_HANDLE_VALUE,errcodeFromWinApi(), NULL);
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   627
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   628
    retval = (WinApiFile *) allocator.Malloc(sizeof (WinApiFile));
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   629
    if (!retval)
275
42f0e962abbb Lots of cleanups, fixes, and corrections. Should now work on all win32
Ryan C. Gordon <icculus@icculus.org>
parents: 268
diff changeset
   630
    {
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   631
        CloseHandle(fileh);
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   632
        BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
275
42f0e962abbb Lots of cleanups, fixes, and corrections. Should now work on all win32
Ryan C. Gordon <icculus@icculus.org>
parents: 268
diff changeset
   633
    } /* if */
131
69b093825303 -Added init and deinit routines per physfs_internal.h changes.
Gregory S. Read <zeph@clutteredmind.org>
parents: 127
diff changeset
   634
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   635
    retval->readonly = rdonly;
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   636
    retval->handle = fileh;
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1012
diff changeset
   637
    return retval;
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   638
} /* doOpen */
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   639
131
69b093825303 -Added init and deinit routines per physfs_internal.h changes.
Gregory S. Read <zeph@clutteredmind.org>
parents: 127
diff changeset
   640
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   641
void *__PHYSFS_platformOpenRead(const char *filename)
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   642
{
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1012
diff changeset
   643
    return doOpen(filename, GENERIC_READ, OPEN_EXISTING, 1);
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   644
} /* __PHYSFS_platformOpenRead */
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   645
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   646
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   647
void *__PHYSFS_platformOpenWrite(const char *filename)
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   648
{
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1012
diff changeset
   649
    return doOpen(filename, GENERIC_WRITE, CREATE_ALWAYS, 0);
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   650
} /* __PHYSFS_platformOpenWrite */
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   651
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   652
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   653
void *__PHYSFS_platformOpenAppend(const char *filename)
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   654
{
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   655
    void *retval = doOpen(filename, GENERIC_WRITE, OPEN_ALWAYS, 0);
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   656
    if (retval != NULL)
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   657
    {
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   658
        HANDLE h = ((WinApiFile *) retval)->handle;
606
70cf47819dd2 Fixed Win32 seeking on > 2gig files, other Cygwin issues.
Ryan C. Gordon <icculus@icculus.org>
parents: 596
diff changeset
   659
        DWORD rc = SetFilePointer(h, 0, NULL, FILE_END);
70cf47819dd2 Fixed Win32 seeking on > 2gig files, other Cygwin issues.
Ryan C. Gordon <icculus@icculus.org>
parents: 596
diff changeset
   660
        if (rc == PHYSFS_INVALID_SET_FILE_POINTER)
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   661
        {
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   662
            const PHYSFS_ErrorCode err = errcodeFromWinApi();
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   663
            CloseHandle(h);
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   664
            allocator.Free(retval);
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   665
            BAIL_MACRO(err, NULL);
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   666
        } /* if */
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   667
    } /* if */
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   668
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1012
diff changeset
   669
    return retval;
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   670
} /* __PHYSFS_platformOpenAppend */
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   671
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   672
1135
3b71a0c08665 Patched to compile on Windows.
Ryan C. Gordon <icculus@icculus.org>
parents: 1129
diff changeset
   673
/* !!! FIXME: this function fails if len > 0xFFFFFFFF. */
1098
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1067
diff changeset
   674
PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buf, PHYSFS_uint64 len)
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   675
{
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   676
    HANDLE Handle = ((WinApiFile *) opaque)->handle;
1098
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1067
diff changeset
   677
    DWORD CountOfBytesRead = 0;
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   678
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   679
    if (!__PHYSFS_ui64FitsAddressSpace(len))
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   680
        BAIL_MACRO(PHYSFS_ERR_INVALID_ARGUMENT, -1);
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   681
    else if(!ReadFile(Handle, buf, (DWORD) len, &CountOfBytesRead, NULL))
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   682
        BAIL_MACRO(errcodeFromWinApi(), -1);
1098
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1067
diff changeset
   683
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1067
diff changeset
   684
    return (PHYSFS_sint64) CountOfBytesRead;
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   685
} /* __PHYSFS_platformRead */
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   686
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   687
1135
3b71a0c08665 Patched to compile on Windows.
Ryan C. Gordon <icculus@icculus.org>
parents: 1129
diff changeset
   688
/* !!! FIXME: this function fails if len > 0xFFFFFFFF. */
310
f8bca4a93fd5 Patched to compile on Cygwin.
Ryan C. Gordon <icculus@icculus.org>
parents: 275
diff changeset
   689
PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
1135
3b71a0c08665 Patched to compile on Windows.
Ryan C. Gordon <icculus@icculus.org>
parents: 1129
diff changeset
   690
                                     PHYSFS_uint64 len)
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   691
{
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   692
    HANDLE Handle = ((WinApiFile *) opaque)->handle;
1098
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1067
diff changeset
   693
    DWORD CountOfBytesWritten = 0;
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   694
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   695
    if (!__PHYSFS_ui64FitsAddressSpace(len))
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   696
        BAIL_MACRO(PHYSFS_ERR_INVALID_ARGUMENT, -1);
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   697
    else if(!WriteFile(Handle, buffer, (DWORD) len, &CountOfBytesWritten, NULL))
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   698
        BAIL_MACRO(errcodeFromWinApi(), -1);
1098
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1067
diff changeset
   699
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1067
diff changeset
   700
    return (PHYSFS_sint64) CountOfBytesWritten;
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   701
} /* __PHYSFS_platformWrite */
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   702
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   703
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   704
int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   705
{
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   706
    HANDLE Handle = ((WinApiFile *) opaque)->handle;
930
45ad625f03bc Fixed compiler warnings in win32 API calls (thanks, Dennis!).
Ryan C. Gordon <icculus@icculus.org>
parents: 926
diff changeset
   707
    LONG HighOrderPos;
45ad625f03bc Fixed compiler warnings in win32 API calls (thanks, Dennis!).
Ryan C. Gordon <icculus@icculus.org>
parents: 926
diff changeset
   708
    PLONG pHighOrderPos;
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   709
    DWORD rc;
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   710
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   711
    /* Get the high order 32-bits of the position */
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   712
    HighOrderPos = HIGHORDER_UINT64(pos);
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   713
606
70cf47819dd2 Fixed Win32 seeking on > 2gig files, other Cygwin issues.
Ryan C. Gordon <icculus@icculus.org>
parents: 596
diff changeset
   714
    /*
70cf47819dd2 Fixed Win32 seeking on > 2gig files, other Cygwin issues.
Ryan C. Gordon <icculus@icculus.org>
parents: 596
diff changeset
   715
     * MSDN: "If you do not need the high-order 32 bits, this
70cf47819dd2 Fixed Win32 seeking on > 2gig files, other Cygwin issues.
Ryan C. Gordon <icculus@icculus.org>
parents: 596
diff changeset
   716
     *         pointer must be set to NULL."
70cf47819dd2 Fixed Win32 seeking on > 2gig files, other Cygwin issues.
Ryan C. Gordon <icculus@icculus.org>
parents: 596
diff changeset
   717
     */
70cf47819dd2 Fixed Win32 seeking on > 2gig files, other Cygwin issues.
Ryan C. Gordon <icculus@icculus.org>
parents: 596
diff changeset
   718
    pHighOrderPos = (HighOrderPos) ? &HighOrderPos : NULL;
70cf47819dd2 Fixed Win32 seeking on > 2gig files, other Cygwin issues.
Ryan C. Gordon <icculus@icculus.org>
parents: 596
diff changeset
   719
70cf47819dd2 Fixed Win32 seeking on > 2gig files, other Cygwin issues.
Ryan C. Gordon <icculus@icculus.org>
parents: 596
diff changeset
   720
    /*
70cf47819dd2 Fixed Win32 seeking on > 2gig files, other Cygwin issues.
Ryan C. Gordon <icculus@icculus.org>
parents: 596
diff changeset
   721
     * !!! FIXME: MSDN: "Windows Me/98/95:  If the pointer
70cf47819dd2 Fixed Win32 seeking on > 2gig files, other Cygwin issues.
Ryan C. Gordon <icculus@icculus.org>
parents: 596
diff changeset
   722
     * !!! FIXME:  lpDistanceToMoveHigh is not NULL, then it must
70cf47819dd2 Fixed Win32 seeking on > 2gig files, other Cygwin issues.
Ryan C. Gordon <icculus@icculus.org>
parents: 596
diff changeset
   723
     * !!! FIXME:  point to either 0, INVALID_SET_FILE_POINTER, or
70cf47819dd2 Fixed Win32 seeking on > 2gig files, other Cygwin issues.
Ryan C. Gordon <icculus@icculus.org>
parents: 596
diff changeset
   724
     * !!! FIXME:  the sign extension of the value of lDistanceToMove.
70cf47819dd2 Fixed Win32 seeking on > 2gig files, other Cygwin issues.
Ryan C. Gordon <icculus@icculus.org>
parents: 596
diff changeset
   725
     * !!! FIXME:  Any other value will be rejected."
70cf47819dd2 Fixed Win32 seeking on > 2gig files, other Cygwin issues.
Ryan C. Gordon <icculus@icculus.org>
parents: 596
diff changeset
   726
     */
70cf47819dd2 Fixed Win32 seeking on > 2gig files, other Cygwin issues.
Ryan C. Gordon <icculus@icculus.org>
parents: 596
diff changeset
   727
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   728
    /* Move pointer "pos" count from start of file */
651
cbe4ea4c7e8e Changed vars named "FileHandle" to "Handle" to not cause confusion with
Ryan C. Gordon <icculus@icculus.org>
parents: 615
diff changeset
   729
    rc = SetFilePointer(Handle, LOWORDER_UINT64(pos),
606
70cf47819dd2 Fixed Win32 seeking on > 2gig files, other Cygwin issues.
Ryan C. Gordon <icculus@icculus.org>
parents: 596
diff changeset
   730
                        pHighOrderPos, FILE_BEGIN);
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   731
606
70cf47819dd2 Fixed Win32 seeking on > 2gig files, other Cygwin issues.
Ryan C. Gordon <icculus@icculus.org>
parents: 596
diff changeset
   732
    if ( (rc == PHYSFS_INVALID_SET_FILE_POINTER) &&
70cf47819dd2 Fixed Win32 seeking on > 2gig files, other Cygwin issues.
Ryan C. Gordon <icculus@icculus.org>
parents: 596
diff changeset
   733
         (GetLastError() != NO_ERROR) )
70cf47819dd2 Fixed Win32 seeking on > 2gig files, other Cygwin issues.
Ryan C. Gordon <icculus@icculus.org>
parents: 596
diff changeset
   734
    {
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   735
        BAIL_MACRO(errcodeFromWinApi(), 0);
606
70cf47819dd2 Fixed Win32 seeking on > 2gig files, other Cygwin issues.
Ryan C. Gordon <icculus@icculus.org>
parents: 596
diff changeset
   736
    } /* if */
70cf47819dd2 Fixed Win32 seeking on > 2gig files, other Cygwin issues.
Ryan C. Gordon <icculus@icculus.org>
parents: 596
diff changeset
   737
    
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1012
diff changeset
   738
    return 1;  /* No error occured */
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   739
} /* __PHYSFS_platformSeek */
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   740
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   741
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   742
PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   743
{
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   744
    HANDLE Handle = ((WinApiFile *) opaque)->handle;
930
45ad625f03bc Fixed compiler warnings in win32 API calls (thanks, Dennis!).
Ryan C. Gordon <icculus@icculus.org>
parents: 926
diff changeset
   745
    LONG HighPos = 0;
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   746
    DWORD LowPos;
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   747
    PHYSFS_sint64 retval;
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   748
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   749
    /* Get current position */
651
cbe4ea4c7e8e Changed vars named "FileHandle" to "Handle" to not cause confusion with
Ryan C. Gordon <icculus@icculus.org>
parents: 615
diff changeset
   750
    LowPos = SetFilePointer(Handle, 0, &HighPos, FILE_CURRENT);
606
70cf47819dd2 Fixed Win32 seeking on > 2gig files, other Cygwin issues.
Ryan C. Gordon <icculus@icculus.org>
parents: 596
diff changeset
   751
    if ( (LowPos == PHYSFS_INVALID_SET_FILE_POINTER) &&
70cf47819dd2 Fixed Win32 seeking on > 2gig files, other Cygwin issues.
Ryan C. Gordon <icculus@icculus.org>
parents: 596
diff changeset
   752
         (GetLastError() != NO_ERROR) )
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   753
    {
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   754
        BAIL_MACRO(errcodeFromWinApi(), -1);
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   755
    } /* if */
211
e9a1d8b11e85 -Fixed a lot of the file functions error handling. Many were handling
Gregory S. Read <zeph@clutteredmind.org>
parents: 210
diff changeset
   756
    else
e9a1d8b11e85 -Fixed a lot of the file functions error handling. Many were handling
Gregory S. Read <zeph@clutteredmind.org>
parents: 210
diff changeset
   757
    {
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   758
        /* Combine the high/low order to create the 64-bit position value */
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   759
        retval = (((PHYSFS_uint64) HighPos) << 32) | LowPos;
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   760
        assert(retval >= 0);
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   761
    } /* else */
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   762
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1012
diff changeset
   763
    return retval;
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   764
} /* __PHYSFS_platformTell */
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   765
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   766
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   767
PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   768
{
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   769
    HANDLE Handle = ((WinApiFile *) opaque)->handle;
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   770
    DWORD SizeHigh;
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   771
    DWORD SizeLow;
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   772
    PHYSFS_sint64 retval;
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   773
651
cbe4ea4c7e8e Changed vars named "FileHandle" to "Handle" to not cause confusion with
Ryan C. Gordon <icculus@icculus.org>
parents: 615
diff changeset
   774
    SizeLow = GetFileSize(Handle, &SizeHigh);
606
70cf47819dd2 Fixed Win32 seeking on > 2gig files, other Cygwin issues.
Ryan C. Gordon <icculus@icculus.org>
parents: 596
diff changeset
   775
    if ( (SizeLow == PHYSFS_INVALID_SET_FILE_POINTER) &&
70cf47819dd2 Fixed Win32 seeking on > 2gig files, other Cygwin issues.
Ryan C. Gordon <icculus@icculus.org>
parents: 596
diff changeset
   776
         (GetLastError() != NO_ERROR) )
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   777
    {
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   778
        BAIL_MACRO(errcodeFromWinApi(), -1);
275
42f0e962abbb Lots of cleanups, fixes, and corrections. Should now work on all win32
Ryan C. Gordon <icculus@icculus.org>
parents: 268
diff changeset
   779
    } /* if */
211
e9a1d8b11e85 -Fixed a lot of the file functions error handling. Many were handling
Gregory S. Read <zeph@clutteredmind.org>
parents: 210
diff changeset
   780
    else
e9a1d8b11e85 -Fixed a lot of the file functions error handling. Many were handling
Gregory S. Read <zeph@clutteredmind.org>
parents: 210
diff changeset
   781
    {
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   782
        /* Combine the high/low order to create the 64-bit position value */
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   783
        retval = (((PHYSFS_uint64) SizeHigh) << 32) | SizeLow;
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   784
        assert(retval >= 0);
275
42f0e962abbb Lots of cleanups, fixes, and corrections. Should now work on all win32
Ryan C. Gordon <icculus@icculus.org>
parents: 268
diff changeset
   785
    } /* else */
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   786
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1012
diff changeset
   787
    return retval;
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   788
} /* __PHYSFS_platformFileLength */
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   789
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   790
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   791
int __PHYSFS_platformFlush(void *opaque)
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   792
{
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   793
    WinApiFile *fh = ((WinApiFile *) opaque);
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   794
    if (!fh->readonly)
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   795
        BAIL_IF_MACRO(!FlushFileBuffers(fh->handle), errcodeFromWinApi(), 0);
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   796
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1012
diff changeset
   797
    return 1;
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   798
} /* __PHYSFS_platformFlush */
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   799
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   800
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   801
void __PHYSFS_platformClose(void *opaque)
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   802
{
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   803
    HANDLE Handle = ((WinApiFile *) opaque)->handle;
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   804
    (void) CloseHandle(Handle); /* ignore errors. You should have flushed! */
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   805
    allocator.Free(opaque);
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   806
} /* __PHYSFS_platformClose */
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   807
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   808
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   809
static int doPlatformDelete(LPWSTR wpath)
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   810
{
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   811
    const int isdir = (GetFileAttributesW(wpath) & FILE_ATTRIBUTE_DIRECTORY);
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   812
    const BOOL rc = (isdir) ? RemoveDirectoryW(wpath) : DeleteFileW(wpath);
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   813
    BAIL_IF_MACRO(!rc, errcodeFromWinApi(), 0);
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1012
diff changeset
   814
    return 1;   /* if you made it here, it worked. */
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   815
} /* doPlatformDelete */
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   816
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   817
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   818
int __PHYSFS_platformDelete(const char *path)
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   819
{
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   820
    int retval = 0;
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   821
    LPWSTR wpath = NULL;
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   822
    UTF8_TO_UNICODE_STACK_MACRO(wpath, path);
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   823
    BAIL_IF_MACRO(!wpath, PHYSFS_ERR_OUT_OF_MEMORY, 0);
861
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   824
    retval = doPlatformDelete(wpath);
9b405bf6a95d Tons of Unicode work in windows.c ... should now use UCS-2 on NT/XP/Vista/etc
Ryan C. Gordon <icculus@icculus.org>
parents: 859
diff changeset
   825
    __PHYSFS_smallFree(wpath);
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1012
diff changeset
   826
    return retval;
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   827
} /* __PHYSFS_platformDelete */
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   828
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   829
853
8df352657911 Added some FIXME comments.
Ryan C. Gordon <icculus@icculus.org>
parents: 852
diff changeset
   830
/*
8df352657911 Added some FIXME comments.
Ryan C. Gordon <icculus@icculus.org>
parents: 852
diff changeset
   831
 * !!! FIXME: why aren't we using Critical Sections instead of Mutexes?
8df352657911 Added some FIXME comments.
Ryan C. Gordon <icculus@icculus.org>
parents: 852
diff changeset
   832
 * !!! FIXME:  mutexes on Windows are for cross-process sync. CritSects are
8df352657911 Added some FIXME comments.
Ryan C. Gordon <icculus@icculus.org>
parents: 852
diff changeset
   833
 * !!! FIXME:  mutexes for threads in a single process and are faster.
8df352657911 Added some FIXME comments.
Ryan C. Gordon <icculus@icculus.org>
parents: 852
diff changeset
   834
 */
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   835
void *__PHYSFS_platformCreateMutex(void)
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   836
{
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1012
diff changeset
   837
    return ((void *) CreateMutex(NULL, FALSE, NULL));
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   838
} /* __PHYSFS_platformCreateMutex */
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   839
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   840
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   841
void __PHYSFS_platformDestroyMutex(void *mutex)
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   842
{
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   843
    CloseHandle((HANDLE) mutex);
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   844
} /* __PHYSFS_platformDestroyMutex */
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   845
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   846
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   847
int __PHYSFS_platformGrabMutex(void *mutex)
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   848
{
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1012
diff changeset
   849
    return (WaitForSingleObject((HANDLE) mutex, INFINITE) != WAIT_FAILED);
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   850
} /* __PHYSFS_platformGrabMutex */
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   851
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   852
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   853
void __PHYSFS_platformReleaseMutex(void *mutex)
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   854
{
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   855
    ReleaseMutex((HANDLE) mutex);
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   856
} /* __PHYSFS_platformReleaseMutex */
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   857
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   858
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   859
static PHYSFS_sint64 FileTimeToPhysfsTime(const FILETIME *ft)
275
42f0e962abbb Lots of cleanups, fixes, and corrections. Should now work on all win32
Ryan C. Gordon <icculus@icculus.org>
parents: 268
diff changeset
   860
{
42f0e962abbb Lots of cleanups, fixes, and corrections. Should now work on all win32
Ryan C. Gordon <icculus@icculus.org>
parents: 268
diff changeset
   861
    SYSTEMTIME st_utc;
42f0e962abbb Lots of cleanups, fixes, and corrections. Should now work on all win32
Ryan C. Gordon <icculus@icculus.org>
parents: 268
diff changeset
   862
    SYSTEMTIME st_localtz;
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   863
    TIME_ZONE_INFORMATION tzi;
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   864
    DWORD tzid;
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   865
    PHYSFS_sint64 retval;
275
42f0e962abbb Lots of cleanups, fixes, and corrections. Should now work on all win32
Ryan C. Gordon <icculus@icculus.org>
parents: 268
diff changeset
   866
    struct tm tm;
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   867
    BOOL rc;
275
42f0e962abbb Lots of cleanups, fixes, and corrections. Should now work on all win32
Ryan C. Gordon <icculus@icculus.org>
parents: 268
diff changeset
   868
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   869
    BAIL_IF_MACRO(!FileTimeToSystemTime(ft, &st_utc), errcodeFromWinApi(), -1);
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   870
    tzid = GetTimeZoneInformation(&tzi);
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   871
    BAIL_IF_MACRO(tzid == TIME_ZONE_ID_INVALID, errcodeFromWinApi(), -1);
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   872
    rc = SystemTimeToTzSpecificLocalTime(&tzi, &st_utc, &st_localtz);
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   873
    BAIL_IF_MACRO(!rc, errcodeFromWinApi(), -1);
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   874
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   875
    /* Convert to a format that mktime() can grok... */
275
42f0e962abbb Lots of cleanups, fixes, and corrections. Should now work on all win32
Ryan C. Gordon <icculus@icculus.org>
parents: 268
diff changeset
   876
    tm.tm_sec = st_localtz.wSecond;
42f0e962abbb Lots of cleanups, fixes, and corrections. Should now work on all win32
Ryan C. Gordon <icculus@icculus.org>
parents: 268
diff changeset
   877
    tm.tm_min = st_localtz.wMinute;
42f0e962abbb Lots of cleanups, fixes, and corrections. Should now work on all win32
Ryan C. Gordon <icculus@icculus.org>
parents: 268
diff changeset
   878
    tm.tm_hour = st_localtz.wHour;
42f0e962abbb Lots of cleanups, fixes, and corrections. Should now work on all win32
Ryan C. Gordon <icculus@icculus.org>
parents: 268
diff changeset
   879
    tm.tm_mday = st_localtz.wDay;
42f0e962abbb Lots of cleanups, fixes, and corrections. Should now work on all win32
Ryan C. Gordon <icculus@icculus.org>
parents: 268
diff changeset
   880
    tm.tm_mon = st_localtz.wMonth - 1;
42f0e962abbb Lots of cleanups, fixes, and corrections. Should now work on all win32
Ryan C. Gordon <icculus@icculus.org>
parents: 268
diff changeset
   881
    tm.tm_year = st_localtz.wYear - 1900;
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   882
    tm.tm_wday = -1 /*st_localtz.wDayOfWeek*/;
275
42f0e962abbb Lots of cleanups, fixes, and corrections. Should now work on all win32
Ryan C. Gordon <icculus@icculus.org>
parents: 268
diff changeset
   883
    tm.tm_yday = -1;
42f0e962abbb Lots of cleanups, fixes, and corrections. Should now work on all win32
Ryan C. Gordon <icculus@icculus.org>
parents: 268
diff changeset
   884
    tm.tm_isdst = -1;
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   885
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   886
    /* Convert to a format PhysicsFS can grok... */
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   887
    retval = (PHYSFS_sint64) mktime(&tm);
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   888
    BAIL_IF_MACRO(retval == -1, PHYSFS_ERR_OS_ERROR, -1);
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1012
diff changeset
   889
    return retval;
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   890
} /* FileTimeToPhysfsTime */
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   891
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   892
int __PHYSFS_platformStat(const char *filename, int *exists, PHYSFS_Stat *stat)
1054
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   893
{
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   894
    WIN32_FILE_ATTRIBUTE_DATA winstat;
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   895
    WCHAR *wstr = NULL;
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   896
    DWORD err = 0;
1054
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   897
    BOOL rc = 0;
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   898
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   899
    UTF8_TO_UNICODE_STACK_MACRO(wstr, filename);
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   900
    BAIL_IF_MACRO(!wstr, PHYSFS_ERR_OUT_OF_MEMORY, 0);
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   901
    rc = GetFileAttributesExW(wstr, GetFileExInfoStandard, &winstat);
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   902
    err = (!rc) ? GetLastError() : 0;
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   903
    *exists = ((err != ERROR_FILE_NOT_FOUND) && (err != ERROR_PATH_NOT_FOUND));
1054
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   904
    __PHYSFS_smallFree(wstr);
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   905
    BAIL_IF_MACRO(!rc, errcodeFromWinApiError(err), 0);
1054
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   906
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   907
    stat->modtime = FileTimeToPhysfsTime(&winstat.ftLastWriteTime);
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   908
    stat->accesstime = FileTimeToPhysfsTime(&winstat.ftLastAccessTime);
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   909
    stat->createtime = FileTimeToPhysfsTime(&winstat.ftCreationTime);
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   910
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   911
    if(winstat.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   912
    {
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   913
        stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   914
        stat->filesize = 0;
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   915
    } /* if */
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   916
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   917
    else if(winstat.dwFileAttributes & (FILE_ATTRIBUTE_OFFLINE | FILE_ATTRIBUTE_DEVICE))
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   918
    {
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   919
        /* !!! FIXME: what are reparse points? */
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   920
        stat->filetype = PHYSFS_FILETYPE_OTHER;
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   921
        /* !!! FIXME: don't rely on this */
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   922
        stat->filesize = 0;
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   923
    } /* else if */
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   924
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   925
    /* !!! FIXME: check for symlinks on Vista. */
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   926
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   927
    else
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   928
    {
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   929
        stat->filetype = PHYSFS_FILETYPE_REGULAR;
1055
8974a4a352fd Patched to compile (thanks, Christoph!).
Ryan C. Gordon <icculus@icculus.org>
parents: 1054
diff changeset
   930
        stat->filesize = (((PHYSFS_uint64) winstat.nFileSizeHigh) << 32) | winstat.nFileSizeLow;
1054
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   931
    } /* else */
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   932
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   933
    stat->readonly = ((winstat.dwFileAttributes & FILE_ATTRIBUTE_READONLY) != 0);
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   934
1106
94c3669d0311 Fixed PHYSFS_stat()'s return value to match rest of PhysicsFS API.
Ryan C. Gordon <icculus@icculus.org>
parents: 1104
diff changeset
   935
    return 1;
1054
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   936
} /* __PHYSFS_platformStat */
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   937
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   938
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   939
/* !!! FIXME: Don't use C runtime for allocators? */
845
3f150ffcf50c Since all the platform layers were using the same cut-and-paste of the
Ryan C. Gordon <icculus@icculus.org>
parents: 844
diff changeset
   940
int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   941
{
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1012
diff changeset
   942
    return 0;  /* just use malloc() and friends. */
845
3f150ffcf50c Since all the platform layers were using the same cut-and-paste of the
Ryan C. Gordon <icculus@icculus.org>
parents: 844
diff changeset
   943
} /* __PHYSFS_platformSetDefaultAllocator */
691
71d9affe0d8a All memory management now goes through allocation hooks instead of directly to
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   944
818
e36f23f49042 Now compiles everything whether we need it or not, removing whole files with
Ryan C. Gordon <icculus@icculus.org>
parents: 810
diff changeset
   945
#endif  /* PHYSFS_PLATFORM_WINDOWS */
327
76e207c6b4a7 Build system corrections for BeOS and Cygwin.
Ryan C. Gordon <icculus@icculus.org>
parents: 313
diff changeset
   946
791
43051d05bcc3 Renamed win32.c to windows.c (since it would be used for Win64, too.)
Ryan C. Gordon <icculus@icculus.org>
parents: 784
diff changeset
   947
/* end of windows.c ... */
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   948
818
e36f23f49042 Now compiles everything whether we need it or not, removing whole files with
Ryan C. Gordon <icculus@icculus.org>
parents: 810
diff changeset
   949