src/physfs_platform_windows.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 17 May 2020 01:41:52 -0400
changeset 1691 c66c9326b05d
parent 1664 b76a47b006f6
permissions -rw-r--r--
7z: Make error handling a little more robust.
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__
1589
e4f75163b60d Cleaned up some header inclusion politics.
Ryan C. Gordon <icculus@icculus.org>
parents: 1582
diff changeset
    10
#include "physfs_platforms.h"
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
    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
1590
5aa450209e6a windows: Disable _CRT_SECURE_NO_WARNINGS again.
Ryan C. Gordon <icculus@icculus.org>
parents: 1589
diff changeset
    19
#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
5aa450209e6a windows: Disable _CRT_SECURE_NO_WARNINGS again.
Ryan C. Gordon <icculus@icculus.org>
parents: 1589
diff changeset
    20
#define _CRT_SECURE_NO_WARNINGS 1
5aa450209e6a windows: Disable _CRT_SECURE_NO_WARNINGS again.
Ryan C. Gordon <icculus@icculus.org>
parents: 1589
diff changeset
    21
#endif
5aa450209e6a windows: Disable _CRT_SECURE_NO_WARNINGS again.
Ryan C. Gordon <icculus@icculus.org>
parents: 1589
diff changeset
    22
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
    23
#define WIN32_LEAN_AND_MEAN 1
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
#include <windows.h>
1518
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
    25
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
    26
#ifndef PHYSFS_PLATFORM_WINRT
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
    27
#include <userenv.h>
1244
528c98081b16 Patched to compile on Windows.
Ryan C. Gordon <icculus@icculus.org>
parents: 1242
diff changeset
    28
#include <shlobj.h>
1518
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
    29
#endif
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
    30
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
    31
#if !defined(PHYSFS_NO_CDROM_SUPPORT)
1217
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
    32
#include <dbt.h>
1518
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
    33
#endif
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
    34
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
    35
#include <errno.h>
76
d6405544e896 __PHYSFS_platformGetUserDir() checks some win32 standard environment variables,
Ryan C. Gordon <icculus@icculus.org>
parents: 71
diff changeset
    36
#include <ctype.h>
262
2d332d53c263 -Fixed file enumerator function (needed a wildcard '*' specification)
Gregory S. Read <zeph@clutteredmind.org>
parents: 240
diff changeset
    37
#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
    38
1492
4621d3c4aa37 Attempt to fix compilation against the latest Windows SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 1453
diff changeset
    39
#ifdef allocator  /* apparently Windows 10 SDK conflicts here. */
4621d3c4aa37 Attempt to fix compilation against the latest Windows SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 1453
diff changeset
    40
#undef allocator
4621d3c4aa37 Attempt to fix compilation against the latest Windows SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 1453
diff changeset
    41
#endif
1589
e4f75163b60d Cleaned up some header inclusion politics.
Ryan C. Gordon <icculus@icculus.org>
parents: 1582
diff changeset
    42
e4f75163b60d Cleaned up some header inclusion politics.
Ryan C. Gordon <icculus@icculus.org>
parents: 1582
diff changeset
    43
#include "physfs_internal.h"
1492
4621d3c4aa37 Attempt to fix compilation against the latest Windows SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 1453
diff changeset
    44
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
    45
/*
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
    46
 * 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
    47
 *  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
    48
 *  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
    49
 */
606
70cf47819dd2 Fixed Win32 seeking on > 2gig files, other Cygwin issues.
Ryan C. Gordon <icculus@icculus.org>
parents: 596
diff changeset
    50
#define PHYSFS_INVALID_SET_FILE_POINTER  0xFFFFFFFF
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
    52
/* just in case... */
606
70cf47819dd2 Fixed Win32 seeking on > 2gig files, other Cygwin issues.
Ryan C. Gordon <icculus@icculus.org>
parents: 596
diff changeset
    53
#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
    54
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
    55
/* Not defined before the Vista SDK. */
1420
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    56
#define PHYSFS_FILE_ATTRIBUTE_REPARSE_POINT 0x400
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
    57
#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
    58
6a5f06dbd423 Patched to compile, and work with pre-Vista versions of the Platform SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 874
diff changeset
    59
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1376
diff changeset
    60
#define UTF8_TO_UNICODE_STACK(w_assignto, str) { \
858
969e69b2822f Improved dynamic loader and initial Unicode work in windows.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 856
diff changeset
    61
    if (str == NULL) \
969e69b2822f Improved dynamic loader and initial Unicode work in windows.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 856
diff changeset
    62
        w_assignto = NULL; \
969e69b2822f Improved dynamic loader and initial Unicode work in windows.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 856
diff changeset
    63
    else { \
1582
526d606206fa Fixed some compiler warnings on Visual Studio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1564
diff changeset
    64
        const size_t 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
    65
        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
    66
        if (w_assignto != NULL) \
1237
6d8b32355a28 Use UTF-16 on Windows, not UCS-2.
Ryan C. Gordon <icculus@icculus.org>
parents: 1226
diff changeset
    67
            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
    68
    } \
969e69b2822f Improved dynamic loader and initial Unicode work in windows.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 856
diff changeset
    69
} \
969e69b2822f Improved dynamic loader and initial Unicode work in windows.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 856
diff changeset
    70
1237
6d8b32355a28 Use UTF-16 on Windows, not UCS-2.
Ryan C. Gordon <icculus@icculus.org>
parents: 1226
diff changeset
    71
/* 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
    72
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
    73
{
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
    74
    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
    75
    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
    76
        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
    77
    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
    78
} /* 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
    79
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
    80
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
    81
{
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
    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
    83
    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
    84
    {
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
    85
        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
    86
        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
    87
        retval = allocator.Malloc(len);
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1376
diff changeset
    88
        BAIL_IF(!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
    89
        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
    90
        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
    91
        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
    92
            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
    93
    } /* 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
    94
    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
    95
} /* 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
    96
1518
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
    97
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
    98
/* Some older APIs aren't in WinRT (only the "Ex" version, etc).
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
    99
   Since non-WinRT might not have the "Ex" version, we tapdance to use
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   100
   the perfectly-fine-and-available-even-on-Win95 API on non-WinRT targets. */
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   101
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   102
static inline HANDLE winFindFirstFileW(const WCHAR *path, LPWIN32_FIND_DATAW d)
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   103
{
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   104
    #ifdef PHYSFS_PLATFORM_WINRT
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   105
    return FindFirstFileExW(path, FindExInfoStandard, d,
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   106
                            FindExSearchNameMatch, NULL, 0);
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   107
    #else
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   108
    return FindFirstFileW(path, d);
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   109
    #endif
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   110
} /* winFindFirstFileW */
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   111
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   112
static inline BOOL winInitializeCriticalSection(LPCRITICAL_SECTION lpcs)
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   113
{
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   114
    #ifdef PHYSFS_PLATFORM_WINRT
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   115
    return InitializeCriticalSectionEx(lpcs, 2000, 0);
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   116
    #else
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   117
    InitializeCriticalSection(lpcs);
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   118
    return TRUE;
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   119
    #endif
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   120
} /* winInitializeCriticalSection */
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   121
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   122
static inline HANDLE winCreateFileW(const WCHAR *wfname, const DWORD mode,
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   123
                                    const DWORD creation)
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   124
{
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   125
    const DWORD share = FILE_SHARE_READ | FILE_SHARE_WRITE;
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   126
    #ifdef PHYSFS_PLATFORM_WINRT
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   127
    return CreateFile2(wfname, mode, share, creation, NULL);
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   128
    #else
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   129
    return CreateFileW(wfname, mode, share, NULL, creation,
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   130
                       FILE_ATTRIBUTE_NORMAL, NULL);
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   131
    #endif
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   132
} /* winCreateFileW */
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   133
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   134
static BOOL winSetFilePointer(HANDLE h, const PHYSFS_sint64 pos,
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   135
                              PHYSFS_sint64 *_newpos, const DWORD whence)
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   136
{
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   137
    #ifdef PHYSFS_PLATFORM_WINRT
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   138
    LARGE_INTEGER lipos;
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   139
    LARGE_INTEGER linewpos;
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   140
    BOOL rc;
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   141
    lipos.QuadPart = (LONGLONG) pos;
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   142
    rc = SetFilePointerEx(h, lipos, &linewpos, whence);
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   143
    if (_newpos)
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   144
        *_newpos = (PHYSFS_sint64) linewpos.QuadPart;
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   145
    return rc;
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   146
    #else
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   147
    const LONG low = (LONG) (pos & 0xFFFFFFFF);
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   148
    LONG high = (LONG) ((pos >> 32) & 0xFFFFFFFF);
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   149
    const DWORD rc = SetFilePointer(h, low, &high, whence);
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   150
    /* 0xFFFFFFFF could be valid, so you have to check GetLastError too! */
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   151
    if (_newpos)
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   152
        *_newpos = ((PHYSFS_sint64) rc) | (((PHYSFS_sint64) high) << 32);
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   153
    if ((rc == PHYSFS_INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   154
        return FALSE;
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   155
    return TRUE;
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   156
    #endif
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   157
} /* winSetFilePointer */
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   158
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   159
static PHYSFS_sint64 winGetFileSize(HANDLE h)
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   160
{
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   161
    #ifdef PHYSFS_PLATFORM_WINRT
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   162
    FILE_STANDARD_INFO info;
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   163
    const BOOL rc = GetFileInformationByHandleEx(h, FileStandardInfo,
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   164
                                                 &info, sizeof (info));
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   165
    return rc ? (PHYSFS_sint64) info.EndOfFile.QuadPart : -1;
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   166
    #else
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   167
    DWORD high = 0;
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   168
    const DWORD rc = GetFileSize(h, &high);
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   169
    if ((rc == PHYSFS_INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   170
        return -1;
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   171
    return (PHYSFS_sint64) ((((PHYSFS_uint64) high) << 32) | rc);
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   172
    #endif
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   173
} /* winGetFileSize */
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
   174
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
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
   176
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
   177
{
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   178
    /*
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
     * 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
   180
     *  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
   181
     *  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
   182
     *  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
   183
     *  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
   184
     *  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
   185
     *
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   186
     * 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
   187
     */
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   188
    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
   189
    {
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
   190
        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
   191
        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
   192
        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
   193
        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
   194
        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
   195
        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
   196
        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
   197
        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
   198
        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
   199
        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
   200
        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
   201
        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
   202
        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
   203
        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
   204
        case ERROR_DIRECTORY: return PHYSFS_ERR_BAD_FILENAME;
1322
5476917b8ddf Allow application-supplied archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 1264
diff changeset
   205
        case ERROR_FILE_NOT_FOUND: return PHYSFS_ERR_NOT_FOUND;
5476917b8ddf Allow application-supplied archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 1264
diff changeset
   206
        case ERROR_PATH_NOT_FOUND: return PHYSFS_ERR_NOT_FOUND;
5476917b8ddf Allow application-supplied archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 1264
diff changeset
   207
        case ERROR_DELETE_PENDING: return PHYSFS_ERR_NOT_FOUND;
5476917b8ddf Allow application-supplied archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 1264
diff changeset
   208
        case ERROR_INVALID_DRIVE: return PHYSFS_ERR_NOT_FOUND;
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
   209
        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
   210
        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
   211
        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
   212
        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
   213
        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
   214
        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
   215
        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
   216
        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
   217
        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
   218
        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
   219
        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
   220
        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
   221
        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
   222
    } /* 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
   223
} /* errcodeFromWinApiError */
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   224
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
   225
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
   226
{
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1237
diff changeset
   227
    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
   228
} /* errcodeFromWinApi */
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   229
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
   230
1518
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   231
#if defined(PHYSFS_NO_CDROM_SUPPORT)
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   232
#define detectAvailableCDs(cb, data)
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   233
#define deinitCDThread()
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   234
#else
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   235
static HANDLE detectCDThreadHandle = NULL;
1534
2035d523fc6f windows: Use a real synchronization primitive for CD detection thread init.
Ryan C. Gordon <icculus@icculus.org>
parents: 1533
diff changeset
   236
static HWND detectCDHwnd = NULL;
1518
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   237
static volatile DWORD drivesWithMediaBitmap = 0;
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   238
1217
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   239
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
   240
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   241
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
   242
{
1217
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   243
    /* 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
   244
    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
   245
    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
   246
    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
   247
    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
   248
    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
   249
    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
   250
1217
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   251
    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
   252
        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
   253
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   254
    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
   255
        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
   256
    else
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   257
        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
   258
    
1217
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   259
    /* 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
   260
    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
   261
    {
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   262
        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
   263
        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
   264
        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
   265
            continue;
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 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
   268
        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
   269
            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
   270
    } /* for */
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   271
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   272
    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
   273
        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
   274
    else
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   275
        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
   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 (lib)
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   278
        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
   279
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   280
    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
   281
} /* pollDiscDrives */
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   282
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   283
1217
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   284
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
   285
                                        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
   286
{
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   287
    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
   288
    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
   289
    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
   290
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   291
    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
   292
        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
   293
    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
   294
             ((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
   295
             (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
   296
             ((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
   297
    {
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   298
        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
   299
    } /* 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
   300
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 (removed)
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   302
        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
   303
    else
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   304
        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
   305
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   306
    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
   307
} /* detectCDWndProc */
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   308
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   309
1534
2035d523fc6f windows: Use a real synchronization primitive for CD detection thread init.
Ryan C. Gordon <icculus@icculus.org>
parents: 1533
diff changeset
   310
static DWORD WINAPI detectCDThread(LPVOID arg)
1217
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   311
{
1534
2035d523fc6f windows: Use a real synchronization primitive for CD detection thread init.
Ryan C. Gordon <icculus@icculus.org>
parents: 1533
diff changeset
   312
    HANDLE initialDiscDetectionComplete = *((HANDLE *) arg);
1217
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   313
    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
   314
    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
   315
    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
   316
    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
   317
    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
   318
    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
   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
    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
   321
    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
   322
    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
   323
    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
   324
    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
   325
    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
   326
    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
   327
    {
1534
2035d523fc6f windows: Use a real synchronization primitive for CD detection thread init.
Ryan C. Gordon <icculus@icculus.org>
parents: 1533
diff changeset
   328
        SetEvent(initialDiscDetectionComplete);  /* let main thread go on. */
1217
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   329
        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
   330
    } /* if */
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   331
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   332
    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
   333
                        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
   334
                        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
   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
    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
   337
    {
1534
2035d523fc6f windows: Use a real synchronization primitive for CD detection thread init.
Ryan C. Gordon <icculus@icculus.org>
parents: 1533
diff changeset
   338
        SetEvent(initialDiscDetectionComplete);  /* let main thread go on. */
1217
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   339
        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
   340
        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
   341
    } /* if */
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   342
1217
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   343
    /* 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
   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
    /* 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
   346
    drivesWithMediaBitmap = pollDiscDrives();
1534
2035d523fc6f windows: Use a real synchronization primitive for CD detection thread init.
Ryan C. Gordon <icculus@icculus.org>
parents: 1533
diff changeset
   347
2035d523fc6f windows: Use a real synchronization primitive for CD detection thread init.
Ryan C. Gordon <icculus@icculus.org>
parents: 1533
diff changeset
   348
    SetEvent(initialDiscDetectionComplete);  /* let main thread go on. */
1217
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
    do
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   351
    {
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   352
        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
   353
        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
   354
            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
   355
        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
   356
        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
   357
    } 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
   358
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   359
    /* 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
   360
    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
   361
    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
   362
    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
   363
} /* detectCDThread */
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   364
1518
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   365
static void detectAvailableCDs(PHYSFS_StringCallback cb, void *data)
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   366
{
1217
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   367
    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
   368
    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
   369
    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
   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
    /*
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   372
     * 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
   373
     *  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
   374
     *  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
   375
     *  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
   376
     *  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
   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 (!detectCDThreadHandle)
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   379
    {
1534
2035d523fc6f windows: Use a real synchronization primitive for CD detection thread init.
Ryan C. Gordon <icculus@icculus.org>
parents: 1533
diff changeset
   380
        HANDLE initialDetectDone = CreateEvent(NULL, TRUE, FALSE, NULL);
2035d523fc6f windows: Use a real synchronization primitive for CD detection thread init.
Ryan C. Gordon <icculus@icculus.org>
parents: 1533
diff changeset
   381
        if (!initialDetectDone)
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
            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
   383
1534
2035d523fc6f windows: Use a real synchronization primitive for CD detection thread init.
Ryan C. Gordon <icculus@icculus.org>
parents: 1533
diff changeset
   384
        detectCDThreadHandle = CreateThread(NULL, 0, detectCDThread,
2035d523fc6f windows: Use a real synchronization primitive for CD detection thread init.
Ryan C. Gordon <icculus@icculus.org>
parents: 1533
diff changeset
   385
                                            &initialDetectDone, 0, NULL);
2035d523fc6f windows: Use a real synchronization primitive for CD detection thread init.
Ryan C. Gordon <icculus@icculus.org>
parents: 1533
diff changeset
   386
        if (detectCDThreadHandle)
2035d523fc6f windows: Use a real synchronization primitive for CD detection thread init.
Ryan C. Gordon <icculus@icculus.org>
parents: 1533
diff changeset
   387
            WaitForSingleObject(initialDetectDone, INFINITE);
2035d523fc6f windows: Use a real synchronization primitive for CD detection thread init.
Ryan C. Gordon <icculus@icculus.org>
parents: 1533
diff changeset
   388
        CloseHandle(initialDetectDone);
2035d523fc6f windows: Use a real synchronization primitive for CD detection thread init.
Ryan C. Gordon <icculus@icculus.org>
parents: 1533
diff changeset
   389
2035d523fc6f windows: Use a real synchronization primitive for CD detection thread init.
Ryan C. Gordon <icculus@icculus.org>
parents: 1533
diff changeset
   390
        if (!detectCDThreadHandle)
2035d523fc6f windows: Use a real synchronization primitive for CD detection thread init.
Ryan C. Gordon <icculus@icculus.org>
parents: 1533
diff changeset
   391
            return;  /* oh well. */
1217
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   392
    } /* if */
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   393
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   394
    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
   395
    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
   396
    {
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   397
        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
   398
        {
e38ffad216e8 Move Windows CD-ROM detection to another thread that uses device notifications.
Ryan C. Gordon <icculus@icculus.org>
parents: 1213
diff changeset
   399
            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
   400
            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
   401
        } /* if */
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   402
    } /* for */
1518
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   403
} /* detectAvailableCDs */
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   404
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   405
static void deinitCDThread(void)
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   406
{
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   407
    if (detectCDThreadHandle)
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   408
    {
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   409
        if (detectCDHwnd)
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   410
            PostMessageW(detectCDHwnd, WM_QUIT, 0, 0);
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   411
        CloseHandle(detectCDThreadHandle);
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   412
        detectCDThreadHandle = NULL;
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   413
        drivesWithMediaBitmap = 0;
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   414
    } /* if */
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   415
} /* deinitCDThread */
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   416
#endif
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   417
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   418
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   419
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   420
{
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   421
    detectAvailableCDs(cb, data);
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   422
} /* __PHYSFS_platformDetectAvailableCDs */
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   423
1518
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   424
#ifdef PHYSFS_PLATFORM_WINRT
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   425
static char *calcDirAppendSep(const WCHAR *wdir)
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   426
{
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   427
    size_t len;
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   428
    void *ptr;
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   429
    char *retval;
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   430
    BAIL_IF(!wdir, errcodeFromWinApi(), NULL);
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   431
    retval = unicodeToUtf8Heap(wdir);
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   432
    BAIL_IF_ERRPASS(!retval, NULL);
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   433
    len = strlen(retval);
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   434
    ptr = allocator.Realloc(retval, len + 2);
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   435
    if (!ptr)
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   436
    {
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   437
        allocator.Free(retval);
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   438
        BAIL(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   439
    } /* if */
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   440
    retval = (char *) ptr;
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   441
    retval[len] = '\\';
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   442
    retval[len+1] = '\0';
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   443
    return retval;
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   444
} /* calcDirAppendSep */
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   445
#endif
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   446
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   447
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
   448
{
1518
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   449
#ifdef PHYSFS_PLATFORM_WINRT
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   450
    return calcDirAppendSep((const WCHAR *) __PHYSFS_winrtCalcBaseDir());
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   451
#else
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   452
    char *retval = NULL;
859
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   453
    DWORD buflen = 64;
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   454
    LPWSTR modpath = NULL;
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   455
859
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   456
    while (1)
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   457
    {
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   458
        DWORD rc;
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   459
        void *ptr;
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   460
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   461
        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
   462
        {
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   463
            allocator.Free(modpath);
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1376
diff changeset
   464
            BAIL(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
   465
        } /* if */
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   466
        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
   467
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   468
        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
   469
        if (rc == 0)
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   470
        {
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   471
            allocator.Free(modpath);
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1376
diff changeset
   472
            BAIL(errcodeFromWinApi(), NULL);
859
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   473
        } /* if */
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   474
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   475
        if (rc < buflen)
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   476
        {
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   477
            buflen = rc;
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   478
            break;
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   479
        } /* if */
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   480
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   481
        buflen *= 2;
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   482
    } /* while */
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   483
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   484
    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
   485
    {
859
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   486
        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
   487
        while (ptr != modpath)
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   488
        {
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   489
            if (*ptr == '\\')
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   490
                break;
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   491
            ptr--;
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   492
        } /* while */
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   493
859
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   494
        if ((ptr == modpath) && (*ptr != '\\'))
1328
a5314f07614a Remove __PHYSFS_setError(), use the new public API instead.
Ryan C. Gordon <icculus@icculus.org>
parents: 1327
diff changeset
   495
            PHYSFS_setErrorCode(PHYSFS_ERR_OTHER_ERROR);  /* oh well. */
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   496
        else
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   497
        {
1264
03715ba4603d Make sure base dir always has a dirsep at the end of it.
Ryan C. Gordon <icculus@icculus.org>
parents: 1256
diff changeset
   498
            *(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
   499
            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
   500
        } /* else */
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   501
    } /* else */
859
7702965ac1f4 Reworked getExePath() to remove some FIXMEs and respect Unicode.
Ryan C. Gordon <icculus@icculus.org>
parents: 858
diff changeset
   502
    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
   503
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
   504
    return retval;   /* w00t. */
1518
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   505
#endif
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   506
} /* __PHYSFS_platformCalcBaseDir */
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   507
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   508
1242
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   509
char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app)
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   510
{
1518
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   511
#ifdef PHYSFS_PLATFORM_WINRT
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   512
    return calcDirAppendSep((const WCHAR *) __PHYSFS_winrtCalcPrefDir());
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   513
#else
1245
1709a0f75ee3 Fixed some brainfarts in the Windows version of PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1244
diff changeset
   514
    /*
1709a0f75ee3 Fixed some brainfarts in the Windows version of PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1244
diff changeset
   515
     * Vista and later has a new API for this, but SHGetFolderPath works there,
1709a0f75ee3 Fixed some brainfarts in the Windows version of PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1244
diff changeset
   516
     *  and apparently just wraps the new API. This is the new way to do it:
1709a0f75ee3 Fixed some brainfarts in the Windows version of PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1244
diff changeset
   517
     *
1709a0f75ee3 Fixed some brainfarts in the Windows version of PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1244
diff changeset
   518
     *     SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_CREATE,
1709a0f75ee3 Fixed some brainfarts in the Windows version of PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1244
diff changeset
   519
     *                          NULL, &wszPath);
1709a0f75ee3 Fixed some brainfarts in the Windows version of PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1244
diff changeset
   520
     */
1242
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   521
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   522
    WCHAR path[MAX_PATH];
1245
1709a0f75ee3 Fixed some brainfarts in the Windows version of PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1244
diff changeset
   523
    char *utf8 = NULL;
1709a0f75ee3 Fixed some brainfarts in the Windows version of PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1244
diff changeset
   524
    size_t len = 0;
1709a0f75ee3 Fixed some brainfarts in the Windows version of PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1244
diff changeset
   525
    char *retval = NULL;
1709a0f75ee3 Fixed some brainfarts in the Windows version of PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1244
diff changeset
   526
1242
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   527
    if (!SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE,
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   528
                                   NULL, 0, path)))
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1376
diff changeset
   529
        BAIL(PHYSFS_ERR_OS_ERROR, NULL);
1242
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   530
1245
1709a0f75ee3 Fixed some brainfarts in the Windows version of PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1244
diff changeset
   531
    utf8 = unicodeToUtf8Heap(path);
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1376
diff changeset
   532
    BAIL_IF_ERRPASS(!utf8, NULL);
1246
4fa9fe3e0b52 Make sure the prefdir has a final dirsep on it.
Ryan C. Gordon <icculus@icculus.org>
parents: 1245
diff changeset
   533
    len = strlen(utf8) + strlen(org) + strlen(app) + 4;
1245
1709a0f75ee3 Fixed some brainfarts in the Windows version of PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1244
diff changeset
   534
    retval = allocator.Malloc(len);
1709a0f75ee3 Fixed some brainfarts in the Windows version of PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1244
diff changeset
   535
    if (!retval)
1709a0f75ee3 Fixed some brainfarts in the Windows version of PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1244
diff changeset
   536
    {
1709a0f75ee3 Fixed some brainfarts in the Windows version of PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1244
diff changeset
   537
        allocator.Free(utf8);
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1376
diff changeset
   538
        BAIL(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
1245
1709a0f75ee3 Fixed some brainfarts in the Windows version of PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1244
diff changeset
   539
    } /* if */
1709a0f75ee3 Fixed some brainfarts in the Windows version of PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1244
diff changeset
   540
1424
1114f181e444 Replaced sprintf() calls with snprintf().
Ryan C. Gordon <icculus@icculus.org>
parents: 1421
diff changeset
   541
    snprintf(retval, len, "%s\\%s\\%s\\", utf8, org, app);
1334
8214ccf21d36 Fixed memory leak.
Ryan C. Gordon <icculus@icculus.org>
parents: 1328
diff changeset
   542
    allocator.Free(utf8);
1245
1709a0f75ee3 Fixed some brainfarts in the Windows version of PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1244
diff changeset
   543
    return retval;
1518
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   544
#endif
1242
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   545
} /* __PHYSFS_platformCalcPrefDir */
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   546
1e6db80d2393 Added PHYSFS_getPrefDir().
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
   547
1248
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   548
char *__PHYSFS_platformCalcUserDir(void)
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   549
{
1518
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   550
#ifdef PHYSFS_PLATFORM_WINRT
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   551
    return calcDirAppendSep((const WCHAR *) __PHYSFS_winrtCalcPrefDir());
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   552
#else
1248
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   553
    typedef BOOL (WINAPI *fnGetUserProfDirW)(HANDLE, LPWSTR, LPDWORD);
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   554
    fnGetUserProfDirW pGetDir = NULL;
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   555
    HANDLE lib = NULL;
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   556
    HANDLE accessToken = NULL;       /* Security handle to process */
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   557
    char *retval = NULL;
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   558
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   559
    lib = LoadLibraryA("userenv.dll");
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1376
diff changeset
   560
    BAIL_IF(!lib, errcodeFromWinApi(), NULL);
1248
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   561
    pGetDir=(fnGetUserProfDirW) GetProcAddress(lib,"GetUserProfileDirectoryW");
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1376
diff changeset
   562
    GOTO_IF(!pGetDir, errcodeFromWinApi(), done);
1248
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   563
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   564
    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &accessToken))
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1376
diff changeset
   565
        GOTO(errcodeFromWinApi(), done);
1248
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   566
    else
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   567
    {
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   568
        DWORD psize = 0;
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   569
        LPWSTR wstr = NULL;
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   570
        BOOL rc = 0;
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   571
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   572
        /*
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   573
         * Should fail. Will write the size of the profile path in
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   574
         *  psize. Also note that the second parameter can't be
1663
7564353d225f windows: Workaround for WinXP systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 1646
diff changeset
   575
         *  NULL or the function fails on Windows XP, but has to be NULL on
7564353d225f windows: Workaround for WinXP systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 1646
diff changeset
   576
         *  Windows 10 or it will fail.  :(
1248
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   577
         */
1646
daea7f45a2f2 windows: Workaround GetUserProfileDirectory's API change in Win10 build 1809.
Ryan C. Gordon <icculus@icculus.org>
parents: 1620
diff changeset
   578
        rc = pGetDir(accessToken, NULL, &psize);
1533
49e7fb6d5b37 More FIXME cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1527
diff changeset
   579
        GOTO_IF(rc, PHYSFS_ERR_OS_ERROR, done);  /* should have failed! */
1248
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   580
1663
7564353d225f windows: Workaround for WinXP systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 1646
diff changeset
   581
        if (psize == 0)  /* probably on Windows XP, try a different way. */
7564353d225f windows: Workaround for WinXP systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 1646
diff changeset
   582
        {
1664
b76a47b006f6 Fixed compiler warning.
Ryan C. Gordon <icculus@icculus.org>
parents: 1663
diff changeset
   583
            WCHAR x = 0;
1663
7564353d225f windows: Workaround for WinXP systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 1646
diff changeset
   584
            rc = pGetDir(accessToken, &x, &psize);
7564353d225f windows: Workaround for WinXP systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 1646
diff changeset
   585
            GOTO_IF(rc, PHYSFS_ERR_OS_ERROR, done);  /* should have failed! */
7564353d225f windows: Workaround for WinXP systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 1646
diff changeset
   586
            GOTO_IF(!psize, PHYSFS_ERR_OS_ERROR, done);  /* Uhoh... */
7564353d225f windows: Workaround for WinXP systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 1646
diff changeset
   587
        } /* if */
7564353d225f windows: Workaround for WinXP systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 1646
diff changeset
   588
1248
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   589
        /* Allocate memory for the profile directory */
1264
03715ba4603d Make sure base dir always has a dirsep at the end of it.
Ryan C. Gordon <icculus@icculus.org>
parents: 1256
diff changeset
   590
        wstr = (LPWSTR) __PHYSFS_smallAlloc((psize + 1) * sizeof (WCHAR));
1248
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   591
        if (wstr != NULL)
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   592
        {
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   593
            if (pGetDir(accessToken, wstr, &psize))
1264
03715ba4603d Make sure base dir always has a dirsep at the end of it.
Ryan C. Gordon <icculus@icculus.org>
parents: 1256
diff changeset
   594
            {
03715ba4603d Make sure base dir always has a dirsep at the end of it.
Ryan C. Gordon <icculus@icculus.org>
parents: 1256
diff changeset
   595
                /* Make sure it ends in a dirsep. We allocated +1 for this. */
03715ba4603d Make sure base dir always has a dirsep at the end of it.
Ryan C. Gordon <icculus@icculus.org>
parents: 1256
diff changeset
   596
                if (wstr[psize - 2] != '\\')
03715ba4603d Make sure base dir always has a dirsep at the end of it.
Ryan C. Gordon <icculus@icculus.org>
parents: 1256
diff changeset
   597
                {
03715ba4603d Make sure base dir always has a dirsep at the end of it.
Ryan C. Gordon <icculus@icculus.org>
parents: 1256
diff changeset
   598
                    wstr[psize - 1] = '\\';
03715ba4603d Make sure base dir always has a dirsep at the end of it.
Ryan C. Gordon <icculus@icculus.org>
parents: 1256
diff changeset
   599
                    wstr[psize - 0] = '\0';
03715ba4603d Make sure base dir always has a dirsep at the end of it.
Ryan C. Gordon <icculus@icculus.org>
parents: 1256
diff changeset
   600
                } /* if */
1248
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   601
                retval = unicodeToUtf8Heap(wstr);
1264
03715ba4603d Make sure base dir always has a dirsep at the end of it.
Ryan C. Gordon <icculus@icculus.org>
parents: 1256
diff changeset
   602
            } /* if */
1248
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   603
            __PHYSFS_smallFree(wstr);
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   604
        } /* if */
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   605
    } /* if */
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   606
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   607
done:
1533
49e7fb6d5b37 More FIXME cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1527
diff changeset
   608
    if (accessToken)
49e7fb6d5b37 More FIXME cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1527
diff changeset
   609
        CloseHandle(accessToken);
1248
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   610
    FreeLibrary(lib);
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   611
    return retval;  /* We made it: hit the showers. */
1518
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   612
#endif
1248
632895970ed6 Cleaned up some FIXMEs.
Ryan C. Gordon <icculus@icculus.org>
parents: 1246
diff changeset
   613
} /* __PHYSFS_platformCalcUserDir */
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   614
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   615
1518
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   616
int __PHYSFS_platformInit(void)
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   617
{
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   618
    return 1;  /* It's all good */
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   619
} /* __PHYSFS_platformInit */
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   620
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   621
1536
c791c9aaa2a2 __PHYSFS_platformDeinit() should return void.
Ryan C. Gordon <icculus@icculus.org>
parents: 1535
diff changeset
   622
void __PHYSFS_platformDeinit(void)
1518
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   623
{
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   624
    deinitCDThread();
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   625
} /* __PHYSFS_platformDeinit */
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   626
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   627
1012
f254870dd7dd Attempt to clean up the thread ID mess in platform_unix ...
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   628
void *__PHYSFS_platformGetThreadID(void)
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   629
{
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
   630
    return ( (void *) ((size_t) GetCurrentThreadId()) );
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   631
} /* __PHYSFS_platformGetThreadID */
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   632
1518
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   633
1591
97d3641bfba3 Make PHYSFS_EnumerateCallback return an enum instead of an int.
Ryan C. Gordon <icculus@icculus.org>
parents: 1590
diff changeset
   634
PHYSFS_EnumerateCallbackResult __PHYSFS_platformEnumerate(const char *dirname,
1564
62c27f6efeed Patched to compile on various platforms and compilers.
Ryan C. Gordon <icculus@icculus.org>
parents: 1559
diff changeset
   635
                               PHYSFS_EnumerateCallback callback,
62c27f6efeed Patched to compile on various platforms and compilers.
Ryan C. Gordon <icculus@icculus.org>
parents: 1559
diff changeset
   636
                               const char *origdir, void *callbackdata)
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   637
{
1591
97d3641bfba3 Make PHYSFS_EnumerateCallback return an enum instead of an int.
Ryan C. Gordon <icculus@icculus.org>
parents: 1590
diff changeset
   638
    PHYSFS_EnumerateCallbackResult retval = PHYSFS_ENUM_OK;
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
   639
    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
   640
    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
   641
    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
   642
    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
   643
    WCHAR *wSearchPath = NULL;
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   644
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
   645
    /* 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
   646
    searchPath = (char *) __PHYSFS_smallAlloc(len + 3);
1591
97d3641bfba3 Make PHYSFS_EnumerateCallback return an enum instead of an int.
Ryan C. Gordon <icculus@icculus.org>
parents: 1590
diff changeset
   647
    BAIL_IF(!searchPath, PHYSFS_ERR_OUT_OF_MEMORY, PHYSFS_ENUM_ERROR);
275
42f0e962abbb Lots of cleanups, fixes, and corrections. Should now work on all win32
Ryan C. Gordon <icculus@icculus.org>
parents: 268
diff changeset
   648
262
2d332d53c263 -Fixed file enumerator function (needed a wildcard '*' specification)
Gregory S. Read <zeph@clutteredmind.org>
parents: 240
diff changeset
   649
    /* 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
   650
    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
   651
0f47386615f0 Enumerate files code: cleaned up memory leak, Doesn't throw away file entry
Ryan C. Gordon <icculus@icculus.org>
parents: 262
diff changeset
   652
    /* 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
   653
    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
   654
    {
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
   655
        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
   656
        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
   657
    } /* 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
   658
262
2d332d53c263 -Fixed file enumerator function (needed a wildcard '*' specification)
Gregory S. Read <zeph@clutteredmind.org>
parents: 240
diff changeset
   659
    /* 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
   660
    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
   661
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1376
diff changeset
   662
    UTF8_TO_UNICODE_STACK(wSearchPath, searchPath);
1559
aaee2b83291f Reworked enumeration to be more powerful.
Ryan C. Gordon <icculus@icculus.org>
parents: 1543
diff changeset
   663
    __PHYSFS_smallFree(searchPath);
1591
97d3641bfba3 Make PHYSFS_EnumerateCallback return an enum instead of an int.
Ryan C. Gordon <icculus@icculus.org>
parents: 1590
diff changeset
   664
    BAIL_IF_ERRPASS(!wSearchPath, PHYSFS_ENUM_ERROR);
262
2d332d53c263 -Fixed file enumerator function (needed a wildcard '*' specification)
Gregory S. Read <zeph@clutteredmind.org>
parents: 240
diff changeset
   665
1518
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   666
    dir = winFindFirstFileW(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
   667
    __PHYSFS_smallFree(wSearchPath);
1591
97d3641bfba3 Make PHYSFS_EnumerateCallback return an enum instead of an int.
Ryan C. Gordon <icculus@icculus.org>
parents: 1590
diff changeset
   668
    BAIL_IF(dir==INVALID_HANDLE_VALUE, errcodeFromWinApi(), PHYSFS_ENUM_ERROR);
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   669
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   670
    do
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   671
    {
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   672
        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
   673
        char *utf8;
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   674
1559
aaee2b83291f Reworked enumeration to be more powerful.
Ryan C. Gordon <icculus@icculus.org>
parents: 1543
diff changeset
   675
        if (fn[0] == '.')  /* ignore "." and ".." */
aaee2b83291f Reworked enumeration to be more powerful.
Ryan C. Gordon <icculus@icculus.org>
parents: 1543
diff changeset
   676
        {
aaee2b83291f Reworked enumeration to be more powerful.
Ryan C. Gordon <icculus@icculus.org>
parents: 1543
diff changeset
   677
            if ((fn[1] == '\0') || ((fn[1] == '.') && (fn[2] == '\0')))
aaee2b83291f Reworked enumeration to be more powerful.
Ryan C. Gordon <icculus@icculus.org>
parents: 1543
diff changeset
   678
                continue;
aaee2b83291f Reworked enumeration to be more powerful.
Ryan C. Gordon <icculus@icculus.org>
parents: 1543
diff changeset
   679
        } /* if */
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   680
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   681
        utf8 = unicodeToUtf8Heap(fn);
1559
aaee2b83291f Reworked enumeration to be more powerful.
Ryan C. Gordon <icculus@icculus.org>
parents: 1543
diff changeset
   682
        if (utf8 == NULL)
aaee2b83291f Reworked enumeration to be more powerful.
Ryan C. Gordon <icculus@icculus.org>
parents: 1543
diff changeset
   683
            retval = -1;
aaee2b83291f Reworked enumeration to be more powerful.
Ryan C. Gordon <icculus@icculus.org>
parents: 1543
diff changeset
   684
        else
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
   685
        {
1559
aaee2b83291f Reworked enumeration to be more powerful.
Ryan C. Gordon <icculus@icculus.org>
parents: 1543
diff changeset
   686
            retval = callback(callbackdata, origdir, utf8);
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   687
            allocator.Free(utf8);
1591
97d3641bfba3 Make PHYSFS_EnumerateCallback return an enum instead of an int.
Ryan C. Gordon <icculus@icculus.org>
parents: 1590
diff changeset
   688
            if (retval == PHYSFS_ENUM_ERROR)
1564
62c27f6efeed Patched to compile on various platforms and compilers.
Ryan C. Gordon <icculus@icculus.org>
parents: 1559
diff changeset
   689
                PHYSFS_setErrorCode(PHYSFS_ERR_APP_CALLBACK);
1559
aaee2b83291f Reworked enumeration to be more powerful.
Ryan C. Gordon <icculus@icculus.org>
parents: 1543
diff changeset
   690
        } /* else */
1591
97d3641bfba3 Make PHYSFS_EnumerateCallback return an enum instead of an int.
Ryan C. Gordon <icculus@icculus.org>
parents: 1590
diff changeset
   691
    } while ((retval == PHYSFS_ENUM_OK) && (FindNextFileW(dir, &entw) != 0));
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   692
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   693
    FindClose(dir);
1559
aaee2b83291f Reworked enumeration to be more powerful.
Ryan C. Gordon <icculus@icculus.org>
parents: 1543
diff changeset
   694
aaee2b83291f Reworked enumeration to be more powerful.
Ryan C. Gordon <icculus@icculus.org>
parents: 1543
diff changeset
   695
    return retval;
aaee2b83291f Reworked enumeration to be more powerful.
Ryan C. Gordon <icculus@icculus.org>
parents: 1543
diff changeset
   696
} /* __PHYSFS_platformEnumerate */
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   697
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   698
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   699
int __PHYSFS_platformMkDir(const char *path)
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   700
{
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
   701
    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
   702
    DWORD rc;
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1376
diff changeset
   703
    UTF8_TO_UNICODE_STACK(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
   704
    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
   705
    __PHYSFS_smallFree(wpath);
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1376
diff changeset
   706
    BAIL_IF(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
   707
    return 1;
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   708
} /* __PHYSFS_platformMkDir */
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   709
275
42f0e962abbb Lots of cleanups, fixes, and corrections. Should now work on all win32
Ryan C. Gordon <icculus@icculus.org>
parents: 268
diff changeset
   710
1526
982e1137ec7d windows: Just pass HANDLEs around instead of wrapping in a struct.
Ryan C. Gordon <icculus@icculus.org>
parents: 1525
diff changeset
   711
static HANDLE doOpen(const char *fname, DWORD mode, DWORD creation)
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   712
{
1198
4cdb856021dd Removed the non-Unicode fallbacks from platform_windows.c, other cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 1180
diff changeset
   713
    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
   714
    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
   715
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1376
diff changeset
   716
    UTF8_TO_UNICODE_STACK(wfname, fname);
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1376
diff changeset
   717
    BAIL_IF(!wfname, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
1518
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   718
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   719
    fileh = winCreateFileW(wfname, mode, creation);
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
   720
    __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
   721
1526
982e1137ec7d windows: Just pass HANDLEs around instead of wrapping in a struct.
Ryan C. Gordon <icculus@icculus.org>
parents: 1525
diff changeset
   722
    if (fileh == INVALID_HANDLE_VALUE)
982e1137ec7d windows: Just pass HANDLEs around instead of wrapping in a struct.
Ryan C. Gordon <icculus@icculus.org>
parents: 1525
diff changeset
   723
        BAIL(errcodeFromWinApi(), INVALID_HANDLE_VALUE);
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   724
1526
982e1137ec7d windows: Just pass HANDLEs around instead of wrapping in a struct.
Ryan C. Gordon <icculus@icculus.org>
parents: 1525
diff changeset
   725
    return fileh;
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   726
} /* doOpen */
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   727
131
69b093825303 -Added init and deinit routines per physfs_internal.h changes.
Gregory S. Read <zeph@clutteredmind.org>
parents: 127
diff changeset
   728
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   729
void *__PHYSFS_platformOpenRead(const char *filename)
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   730
{
1526
982e1137ec7d windows: Just pass HANDLEs around instead of wrapping in a struct.
Ryan C. Gordon <icculus@icculus.org>
parents: 1525
diff changeset
   731
    HANDLE h = doOpen(filename, GENERIC_READ, OPEN_EXISTING);
982e1137ec7d windows: Just pass HANDLEs around instead of wrapping in a struct.
Ryan C. Gordon <icculus@icculus.org>
parents: 1525
diff changeset
   732
    return (h == INVALID_HANDLE_VALUE) ? NULL : (void *) h;
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   733
} /* __PHYSFS_platformOpenRead */
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   734
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   735
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   736
void *__PHYSFS_platformOpenWrite(const char *filename)
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   737
{
1526
982e1137ec7d windows: Just pass HANDLEs around instead of wrapping in a struct.
Ryan C. Gordon <icculus@icculus.org>
parents: 1525
diff changeset
   738
    HANDLE h = doOpen(filename, GENERIC_WRITE, CREATE_ALWAYS);
982e1137ec7d windows: Just pass HANDLEs around instead of wrapping in a struct.
Ryan C. Gordon <icculus@icculus.org>
parents: 1525
diff changeset
   739
    return (h == INVALID_HANDLE_VALUE) ? NULL : (void *) h;
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   740
} /* __PHYSFS_platformOpenWrite */
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
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   743
void *__PHYSFS_platformOpenAppend(const char *filename)
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   744
{
1526
982e1137ec7d windows: Just pass HANDLEs around instead of wrapping in a struct.
Ryan C. Gordon <icculus@icculus.org>
parents: 1525
diff changeset
   745
    HANDLE h = doOpen(filename, GENERIC_WRITE, OPEN_ALWAYS);
982e1137ec7d windows: Just pass HANDLEs around instead of wrapping in a struct.
Ryan C. Gordon <icculus@icculus.org>
parents: 1525
diff changeset
   746
    BAIL_IF_ERRPASS(h == INVALID_HANDLE_VALUE, NULL);
982e1137ec7d windows: Just pass HANDLEs around instead of wrapping in a struct.
Ryan C. Gordon <icculus@icculus.org>
parents: 1525
diff changeset
   747
982e1137ec7d windows: Just pass HANDLEs around instead of wrapping in a struct.
Ryan C. Gordon <icculus@icculus.org>
parents: 1525
diff changeset
   748
    if (!winSetFilePointer(h, 0, NULL, FILE_END))
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   749
    {
1526
982e1137ec7d windows: Just pass HANDLEs around instead of wrapping in a struct.
Ryan C. Gordon <icculus@icculus.org>
parents: 1525
diff changeset
   750
        const PHYSFS_ErrorCode err = errcodeFromWinApi();
982e1137ec7d windows: Just pass HANDLEs around instead of wrapping in a struct.
Ryan C. Gordon <icculus@icculus.org>
parents: 1525
diff changeset
   751
        CloseHandle(h);
982e1137ec7d windows: Just pass HANDLEs around instead of wrapping in a struct.
Ryan C. Gordon <icculus@icculus.org>
parents: 1525
diff changeset
   752
        BAIL(err, NULL);
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   753
    } /* if */
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   754
1526
982e1137ec7d windows: Just pass HANDLEs around instead of wrapping in a struct.
Ryan C. Gordon <icculus@icculus.org>
parents: 1525
diff changeset
   755
    return (void *) h;
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   756
} /* __PHYSFS_platformOpenAppend */
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   757
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   758
1098
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1067
diff changeset
   759
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
   760
{
1526
982e1137ec7d windows: Just pass HANDLEs around instead of wrapping in a struct.
Ryan C. Gordon <icculus@icculus.org>
parents: 1525
diff changeset
   761
    HANDLE h = (HANDLE) opaque;
1256
6c166b1e4243 Allow Windows to read/write more than 4 gigs to a file in one shot.
Ryan C. Gordon <icculus@icculus.org>
parents: 1252
diff changeset
   762
    PHYSFS_sint64 totalRead = 0;
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   763
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
   764
    if (!__PHYSFS_ui64FitsAddressSpace(len))
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1376
diff changeset
   765
        BAIL(PHYSFS_ERR_INVALID_ARGUMENT, -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
   766
1256
6c166b1e4243 Allow Windows to read/write more than 4 gigs to a file in one shot.
Ryan C. Gordon <icculus@icculus.org>
parents: 1252
diff changeset
   767
    while (len > 0)
6c166b1e4243 Allow Windows to read/write more than 4 gigs to a file in one shot.
Ryan C. Gordon <icculus@icculus.org>
parents: 1252
diff changeset
   768
    {
6c166b1e4243 Allow Windows to read/write more than 4 gigs to a file in one shot.
Ryan C. Gordon <icculus@icculus.org>
parents: 1252
diff changeset
   769
        const DWORD thislen = (len > 0xFFFFFFFF) ? 0xFFFFFFFF : (DWORD) len;
6c166b1e4243 Allow Windows to read/write more than 4 gigs to a file in one shot.
Ryan C. Gordon <icculus@icculus.org>
parents: 1252
diff changeset
   770
        DWORD numRead = 0;
1526
982e1137ec7d windows: Just pass HANDLEs around instead of wrapping in a struct.
Ryan C. Gordon <icculus@icculus.org>
parents: 1525
diff changeset
   771
        if (!ReadFile(h, buf, thislen, &numRead, NULL))
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1376
diff changeset
   772
            BAIL(errcodeFromWinApi(), -1);
1256
6c166b1e4243 Allow Windows to read/write more than 4 gigs to a file in one shot.
Ryan C. Gordon <icculus@icculus.org>
parents: 1252
diff changeset
   773
        len -= (PHYSFS_uint64) numRead;
6c166b1e4243 Allow Windows to read/write more than 4 gigs to a file in one shot.
Ryan C. Gordon <icculus@icculus.org>
parents: 1252
diff changeset
   774
        totalRead += (PHYSFS_sint64) numRead;
6c166b1e4243 Allow Windows to read/write more than 4 gigs to a file in one shot.
Ryan C. Gordon <icculus@icculus.org>
parents: 1252
diff changeset
   775
        if (numRead != thislen)
6c166b1e4243 Allow Windows to read/write more than 4 gigs to a file in one shot.
Ryan C. Gordon <icculus@icculus.org>
parents: 1252
diff changeset
   776
            break;
6c166b1e4243 Allow Windows to read/write more than 4 gigs to a file in one shot.
Ryan C. Gordon <icculus@icculus.org>
parents: 1252
diff changeset
   777
    } /* while */
6c166b1e4243 Allow Windows to read/write more than 4 gigs to a file in one shot.
Ryan C. Gordon <icculus@icculus.org>
parents: 1252
diff changeset
   778
6c166b1e4243 Allow Windows to read/write more than 4 gigs to a file in one shot.
Ryan C. Gordon <icculus@icculus.org>
parents: 1252
diff changeset
   779
    return totalRead;
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   780
} /* __PHYSFS_platformRead */
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   781
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   782
310
f8bca4a93fd5 Patched to compile on Cygwin.
Ryan C. Gordon <icculus@icculus.org>
parents: 275
diff changeset
   783
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
   784
                                     PHYSFS_uint64 len)
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   785
{
1526
982e1137ec7d windows: Just pass HANDLEs around instead of wrapping in a struct.
Ryan C. Gordon <icculus@icculus.org>
parents: 1525
diff changeset
   786
    HANDLE h = (HANDLE) opaque;
1256
6c166b1e4243 Allow Windows to read/write more than 4 gigs to a file in one shot.
Ryan C. Gordon <icculus@icculus.org>
parents: 1252
diff changeset
   787
    PHYSFS_sint64 totalWritten = 0;
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   788
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
   789
    if (!__PHYSFS_ui64FitsAddressSpace(len))
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1376
diff changeset
   790
        BAIL(PHYSFS_ERR_INVALID_ARGUMENT, -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
   791
1256
6c166b1e4243 Allow Windows to read/write more than 4 gigs to a file in one shot.
Ryan C. Gordon <icculus@icculus.org>
parents: 1252
diff changeset
   792
    while (len > 0)
6c166b1e4243 Allow Windows to read/write more than 4 gigs to a file in one shot.
Ryan C. Gordon <icculus@icculus.org>
parents: 1252
diff changeset
   793
    {
6c166b1e4243 Allow Windows to read/write more than 4 gigs to a file in one shot.
Ryan C. Gordon <icculus@icculus.org>
parents: 1252
diff changeset
   794
        const DWORD thislen = (len > 0xFFFFFFFF) ? 0xFFFFFFFF : (DWORD) len;
6c166b1e4243 Allow Windows to read/write more than 4 gigs to a file in one shot.
Ryan C. Gordon <icculus@icculus.org>
parents: 1252
diff changeset
   795
        DWORD numWritten = 0;
1526
982e1137ec7d windows: Just pass HANDLEs around instead of wrapping in a struct.
Ryan C. Gordon <icculus@icculus.org>
parents: 1525
diff changeset
   796
        if (!WriteFile(h, buffer, thislen, &numWritten, NULL))
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1376
diff changeset
   797
            BAIL(errcodeFromWinApi(), -1);
1256
6c166b1e4243 Allow Windows to read/write more than 4 gigs to a file in one shot.
Ryan C. Gordon <icculus@icculus.org>
parents: 1252
diff changeset
   798
        len -= (PHYSFS_uint64) numWritten;
6c166b1e4243 Allow Windows to read/write more than 4 gigs to a file in one shot.
Ryan C. Gordon <icculus@icculus.org>
parents: 1252
diff changeset
   799
        totalWritten += (PHYSFS_sint64) numWritten;
6c166b1e4243 Allow Windows to read/write more than 4 gigs to a file in one shot.
Ryan C. Gordon <icculus@icculus.org>
parents: 1252
diff changeset
   800
        if (numWritten != thislen)
6c166b1e4243 Allow Windows to read/write more than 4 gigs to a file in one shot.
Ryan C. Gordon <icculus@icculus.org>
parents: 1252
diff changeset
   801
            break;
6c166b1e4243 Allow Windows to read/write more than 4 gigs to a file in one shot.
Ryan C. Gordon <icculus@icculus.org>
parents: 1252
diff changeset
   802
    } /* while */
6c166b1e4243 Allow Windows to read/write more than 4 gigs to a file in one shot.
Ryan C. Gordon <icculus@icculus.org>
parents: 1252
diff changeset
   803
6c166b1e4243 Allow Windows to read/write more than 4 gigs to a file in one shot.
Ryan C. Gordon <icculus@icculus.org>
parents: 1252
diff changeset
   804
    return totalWritten;
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   805
} /* __PHYSFS_platformWrite */
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   806
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
int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   809
{
1526
982e1137ec7d windows: Just pass HANDLEs around instead of wrapping in a struct.
Ryan C. Gordon <icculus@icculus.org>
parents: 1525
diff changeset
   810
    HANDLE h = (HANDLE) opaque;
1518
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   811
    const PHYSFS_sint64 spos = (PHYSFS_sint64) pos;
1525
a7df4293a48b windows: Fixed logic bug.
Ryan C. Gordon <icculus@icculus.org>
parents: 1518
diff changeset
   812
    BAIL_IF(!winSetFilePointer(h,spos,NULL,FILE_BEGIN), 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
   813
    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
   814
} /* __PHYSFS_platformSeek */
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   815
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   816
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   817
PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   818
{
1526
982e1137ec7d windows: Just pass HANDLEs around instead of wrapping in a struct.
Ryan C. Gordon <icculus@icculus.org>
parents: 1525
diff changeset
   819
    HANDLE h = (HANDLE) opaque;
1518
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   820
    PHYSFS_sint64 pos = 0;
1525
a7df4293a48b windows: Fixed logic bug.
Ryan C. Gordon <icculus@icculus.org>
parents: 1518
diff changeset
   821
    BAIL_IF(!winSetFilePointer(h,0,&pos,FILE_CURRENT), errcodeFromWinApi(), -1);
1518
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   822
    return pos;
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   823
} /* __PHYSFS_platformTell */
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   824
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   825
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   826
PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   827
{
1526
982e1137ec7d windows: Just pass HANDLEs around instead of wrapping in a struct.
Ryan C. Gordon <icculus@icculus.org>
parents: 1525
diff changeset
   828
    HANDLE h = (HANDLE) opaque;
1518
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   829
    const PHYSFS_sint64 retval = winGetFileSize(h);
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   830
    BAIL_IF(retval < 0, errcodeFromWinApi(), -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
   831
    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
   832
} /* __PHYSFS_platformFileLength */
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   833
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   834
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   835
int __PHYSFS_platformFlush(void *opaque)
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   836
{
1526
982e1137ec7d windows: Just pass HANDLEs around instead of wrapping in a struct.
Ryan C. Gordon <icculus@icculus.org>
parents: 1525
diff changeset
   837
    HANDLE h = (HANDLE) opaque;
982e1137ec7d windows: Just pass HANDLEs around instead of wrapping in a struct.
Ryan C. Gordon <icculus@icculus.org>
parents: 1525
diff changeset
   838
    BAIL_IF(!FlushFileBuffers(h), 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
   839
    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
   840
} /* __PHYSFS_platformFlush */
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   841
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   842
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
   843
void __PHYSFS_platformClose(void *opaque)
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   844
{
1526
982e1137ec7d windows: Just pass HANDLEs around instead of wrapping in a struct.
Ryan C. Gordon <icculus@icculus.org>
parents: 1525
diff changeset
   845
    HANDLE h = (HANDLE) opaque;
982e1137ec7d windows: Just pass HANDLEs around instead of wrapping in a struct.
Ryan C. Gordon <icculus@icculus.org>
parents: 1525
diff changeset
   846
    (void) CloseHandle(h); /* ignore errors. You should have flushed! */
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   847
} /* __PHYSFS_platformClose */
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   848
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   849
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
   850
static int doPlatformDelete(LPWSTR wpath)
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   851
{
1518
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   852
    WIN32_FILE_ATTRIBUTE_DATA info;
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   853
    if (!GetFileAttributesExW(wpath, GetFileExInfoStandard, &info))
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   854
        BAIL(errcodeFromWinApi(), 0);
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   855
    else
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   856
    {
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   857
        const int isdir = (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   858
        const BOOL rc = isdir ? RemoveDirectoryW(wpath) : DeleteFileW(wpath);
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   859
        BAIL_IF(!rc, errcodeFromWinApi(), 0);
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   860
    } /* else */
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
   861
    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
   862
} /* 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
   863
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
   864
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
   865
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
   866
{
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
   867
    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
   868
    LPWSTR wpath = NULL;
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1376
diff changeset
   869
    UTF8_TO_UNICODE_STACK(wpath, path);
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1376
diff changeset
   870
    BAIL_IF(!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
   871
    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
   872
    __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
   873
    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
   874
} /* __PHYSFS_platformDelete */
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   875
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   876
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   877
void *__PHYSFS_platformCreateMutex(void)
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   878
{
1250
53b2f31a01c4 Windows: Use cross-thread Critical Sections instead of cross-process Mutexes.
Ryan C. Gordon <icculus@icculus.org>
parents: 1249
diff changeset
   879
    LPCRITICAL_SECTION lpcs;
53b2f31a01c4 Windows: Use cross-thread Critical Sections instead of cross-process Mutexes.
Ryan C. Gordon <icculus@icculus.org>
parents: 1249
diff changeset
   880
    lpcs = (LPCRITICAL_SECTION) allocator.Malloc(sizeof (CRITICAL_SECTION));
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1376
diff changeset
   881
    BAIL_IF(!lpcs, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
1518
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   882
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   883
    if (!winInitializeCriticalSection(lpcs))
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   884
    {
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   885
        allocator.Free(lpcs);
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   886
        BAIL(errcodeFromWinApi(), NULL);
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   887
    } /* if */
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   888
1250
53b2f31a01c4 Windows: Use cross-thread Critical Sections instead of cross-process Mutexes.
Ryan C. Gordon <icculus@icculus.org>
parents: 1249
diff changeset
   889
    return lpcs;
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   890
} /* __PHYSFS_platformCreateMutex */
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   891
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   892
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   893
void __PHYSFS_platformDestroyMutex(void *mutex)
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   894
{
1250
53b2f31a01c4 Windows: Use cross-thread Critical Sections instead of cross-process Mutexes.
Ryan C. Gordon <icculus@icculus.org>
parents: 1249
diff changeset
   895
    DeleteCriticalSection((LPCRITICAL_SECTION) mutex);
53b2f31a01c4 Windows: Use cross-thread Critical Sections instead of cross-process Mutexes.
Ryan C. Gordon <icculus@icculus.org>
parents: 1249
diff changeset
   896
    allocator.Free(mutex);
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   897
} /* __PHYSFS_platformDestroyMutex */
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   898
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   899
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   900
int __PHYSFS_platformGrabMutex(void *mutex)
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   901
{
1250
53b2f31a01c4 Windows: Use cross-thread Critical Sections instead of cross-process Mutexes.
Ryan C. Gordon <icculus@icculus.org>
parents: 1249
diff changeset
   902
    EnterCriticalSection((LPCRITICAL_SECTION) mutex);
53b2f31a01c4 Windows: Use cross-thread Critical Sections instead of cross-process Mutexes.
Ryan C. Gordon <icculus@icculus.org>
parents: 1249
diff changeset
   903
    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
   904
} /* __PHYSFS_platformGrabMutex */
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   905
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   906
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   907
void __PHYSFS_platformReleaseMutex(void *mutex)
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   908
{
1250
53b2f31a01c4 Windows: Use cross-thread Critical Sections instead of cross-process Mutexes.
Ryan C. Gordon <icculus@icculus.org>
parents: 1249
diff changeset
   909
    LeaveCriticalSection((LPCRITICAL_SECTION) mutex);
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   910
} /* __PHYSFS_platformReleaseMutex */
159
a893392b1628 -Implemented PHYSFS file routines
Gregory S. Read <zeph@clutteredmind.org>
parents: 131
diff changeset
   911
312
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   912
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   913
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
   914
{
42f0e962abbb Lots of cleanups, fixes, and corrections. Should now work on all win32
Ryan C. Gordon <icculus@icculus.org>
parents: 268
diff changeset
   915
    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
   916
    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
   917
    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
   918
    DWORD tzid;
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   919
    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
   920
    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
   921
    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
   922
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1376
diff changeset
   923
    BAIL_IF(!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
   924
    tzid = GetTimeZoneInformation(&tzi);
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1376
diff changeset
   925
    BAIL_IF(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
   926
    rc = SystemTimeToTzSpecificLocalTime(&tzi, &st_utc, &st_localtz);
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1376
diff changeset
   927
    BAIL_IF(!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
   928
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   929
    /* 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
   930
    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
   931
    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
   932
    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
   933
    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
   934
    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
   935
    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
   936
    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
   937
    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
   938
    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
   939
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   940
    /* 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
   941
    retval = (PHYSFS_sint64) mktime(&tm);
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1376
diff changeset
   942
    BAIL_IF(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
   943
    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
   944
} /* FileTimeToPhysfsTime */
4996c48861db _LOTS_ of cleanups, rewrites, fixes, and improvements. Builds on Cygwin again,
Ryan C. Gordon <icculus@icculus.org>
parents: 310
diff changeset
   945
1327
998351d485d9 Cleaned out the "exists" nonsense in the stat() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 1324
diff changeset
   946
1420
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
   947
/* check for symlinks. These exist in NTFS 3.1 (WinXP), even though
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
   948
   they aren't really available to userspace before Vista. I wonder
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
   949
   what would happen if you put an NTFS disk with a symlink on it
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
   950
   into an XP machine, though; would this flag get set?
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
   951
   NTFS symlinks are a form of "reparse point" (junction, volume mount,
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
   952
   etc), so if the REPARSE_POINT attribute is set, check for the symlink
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
   953
   tag thereafter. This assumes you already read in the file attributes. */
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
   954
static int isSymlink(const WCHAR *wpath, const DWORD attr)
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
   955
{
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
   956
    WIN32_FIND_DATAW w32dw;
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
   957
    HANDLE h;
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
   958
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
   959
    if ((attr & PHYSFS_FILE_ATTRIBUTE_REPARSE_POINT) == 0)
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
   960
        return 0;  /* not a reparse point? Definitely not a symlink. */
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
   961
1518
a6ac19814d53 Serious cleanup and update of Windows RT support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   962
    h = winFindFirstFileW(wpath, &w32dw);
1420
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
   963
    if (h == INVALID_HANDLE_VALUE)
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
   964
        return 0;  /* ...maybe the file just vanished...? */
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
   965
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
   966
    FindClose(h);
1421
d7477826b424 Patched to compile on Windows.
Ryan C. Gordon <icculus@icculus.org>
parents: 1420
diff changeset
   967
    return (w32dw.dwReserved0 == PHYSFS_IO_REPARSE_TAG_SYMLINK);
1420
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
   968
} /* isSymlink */
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
   969
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
   970
1620
f3459eaad51b Fixed mounting a symlink to a real directory.
Ryan C. Gordon <icculus@icculus.org>
parents: 1591
diff changeset
   971
int __PHYSFS_platformStat(const char *filename, PHYSFS_Stat *st, const int follow)
1054
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   972
{
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   973
    WIN32_FILE_ATTRIBUTE_DATA winstat;
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   974
    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
   975
    DWORD err = 0;
1054
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   976
    BOOL rc = 0;
1420
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
   977
    int issymlink = 0;
1054
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   978
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1376
diff changeset
   979
    UTF8_TO_UNICODE_STACK(wstr, filename);
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1376
diff changeset
   980
    BAIL_IF(!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
   981
    rc = GetFileAttributesExW(wstr, GetFileExInfoStandard, &winstat);
1420
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
   982
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
   983
    if (!rc)
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
   984
        err = GetLastError();
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
   985
    else  /* check for symlink while wstr is still available */
1620
f3459eaad51b Fixed mounting a symlink to a real directory.
Ryan C. Gordon <icculus@icculus.org>
parents: 1591
diff changeset
   986
        issymlink = !follow && isSymlink(wstr, winstat.dwFileAttributes);
1420
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
   987
1054
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   988
    __PHYSFS_smallFree(wstr);
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1376
diff changeset
   989
    BAIL_IF(!rc, errcodeFromWinApiError(err), 0);
1054
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   990
1327
998351d485d9 Cleaned out the "exists" nonsense in the stat() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 1324
diff changeset
   991
    st->modtime = FileTimeToPhysfsTime(&winstat.ftLastWriteTime);
998351d485d9 Cleaned out the "exists" nonsense in the stat() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 1324
diff changeset
   992
    st->accesstime = FileTimeToPhysfsTime(&winstat.ftLastAccessTime);
998351d485d9 Cleaned out the "exists" nonsense in the stat() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 1324
diff changeset
   993
    st->createtime = FileTimeToPhysfsTime(&winstat.ftCreationTime);
1054
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   994
1420
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
   995
    if (issymlink)
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
   996
    {
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
   997
        st->filetype = PHYSFS_FILETYPE_SYMLINK;
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
   998
        st->filesize = 0;
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
   999
    } /* if */
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
  1000
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
  1001
    else if (winstat.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1054
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
  1002
    {
1327
998351d485d9 Cleaned out the "exists" nonsense in the stat() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 1324
diff changeset
  1003
        st->filetype = PHYSFS_FILETYPE_DIRECTORY;
998351d485d9 Cleaned out the "exists" nonsense in the stat() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 1324
diff changeset
  1004
        st->filesize = 0;
1420
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
  1005
    } /* else if */
1054
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
  1006
1420
09e18c545d56 windows: (re)added support for symbolic links.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
  1007
    else if (winstat.dwFileAttributes & (FILE_ATTRIBUTE_OFFLINE | FILE_ATTRIBUTE_DEVICE))
1054
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
  1008
    {
1327
998351d485d9 Cleaned out the "exists" nonsense in the stat() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 1324
diff changeset
  1009
        st->filetype = PHYSFS_FILETYPE_OTHER;
1539
e965964f054a windows: PHYSFS_FILETYPE_OTHER files now report real filesize in PHYSFS_stat().
Ryan C. Gordon <icculus@icculus.org>
parents: 1536
diff changeset
  1010
        st->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
  1011
    } /* else if */
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
  1012
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
  1013
    else
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
  1014
    {
1327
998351d485d9 Cleaned out the "exists" nonsense in the stat() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 1324
diff changeset
  1015
        st->filetype = PHYSFS_FILETYPE_REGULAR;
998351d485d9 Cleaned out the "exists" nonsense in the stat() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 1324
diff changeset
  1016
        st->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
  1017
    } /* else */
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
  1018
1327
998351d485d9 Cleaned out the "exists" nonsense in the stat() API.
Ryan C. Gordon <icculus@icculus.org>
parents: 1324
diff changeset
  1019
    st->readonly = ((winstat.dwFileAttributes & FILE_ATTRIBUTE_READONLY) != 0);
1054
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
  1020
1106
94c3669d0311 Fixed PHYSFS_stat()'s return value to match rest of PhysicsFS API.
Ryan C. Gordon <icculus@icculus.org>
parents: 1104
diff changeset
  1021
    return 1;
1054
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
  1022
} /* __PHYSFS_platformStat */
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
  1023
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
  1024
#endif  /* PHYSFS_PLATFORM_WINDOWS */
327
76e207c6b4a7 Build system corrections for BeOS and Cygwin.
Ryan C. Gordon <icculus@icculus.org>
parents: 313
diff changeset
  1025
1507
8953b8a171e7 Update filename comments.
Ryan C. Gordon <icculus@icculus.org>
parents: 1506
diff changeset
  1026
/* end of physfs_platform_windows.c ... */
68
c9a10b5d5f4f Initial add; NOT complete.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1027
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
  1028