extras/physfsrwops.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 20 Feb 2018 10:26:33 -0500
changeset 1631 1aec60db9326
parent 1630 5123a32160af
permissions -rw-r--r--
Remove deprecated PHYSFS_getLastError() from all the extras (thanks, Rob!).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/*
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
 * This code provides a glue layer between PhysicsFS and Simple Directmedia
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
 *  Layer's (SDL) RWops i/o abstraction.
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
 *
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
 * License: this code is public domain. I make no warranty that it is useful,
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
 *  correct, harmless, or environmentally safe.
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
 *
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
 * This particular file may be used however you like, including copying it
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
 *  verbatim into a closed-source project, exploiting it commercially, and
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
 *  removing any trace of my name from the source (although I hope you won't
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
 *  do that). I welcome enhancements and corrections to this file, but I do
576
5da65f8e9a50 Switched to zlib license.
Ryan C. Gordon <icculus@icculus.org>
parents: 132
diff changeset
    12
 *  not require you to send me patches if you make changes. This code has
5da65f8e9a50 Switched to zlib license.
Ryan C. Gordon <icculus@icculus.org>
parents: 132
diff changeset
    13
 *  NO WARRANTY.
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
 *
576
5da65f8e9a50 Switched to zlib license.
Ryan C. Gordon <icculus@icculus.org>
parents: 132
diff changeset
    15
 * Unless otherwise stated, the rest of PhysicsFS falls under the zlib license.
809
116b8fe30371 Renamed LICENSE to LICENSE.txt
Ryan C. Gordon <icculus@icculus.org>
parents: 767
diff changeset
    16
 *  Please see LICENSE.txt in the root of the source tree.
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
 *
1193
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
    18
 * SDL 1.2 falls under the LGPL license. SDL 1.3+ is zlib, like PhysicsFS.
1373
527ef3c6a2d6 HTTPS all the things.
Ryan C. Gordon <icculus@icculus.org>
parents: 1364
diff changeset
    19
 *  You can get SDL at https://www.libsdl.org/
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
 *
767
db29bf06d171 Changed my email address.
Ryan C. Gordon <icculus@icculus.org>
parents: 654
diff changeset
    21
 *  This file was written by Ryan C. Gordon. (icculus@icculus.org).
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
 */
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
#include <stdio.h>  /* used for SEEK_SET, SEEK_CUR, SEEK_END ... */
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
#include "physfsrwops.h"
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
1364
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
    27
/* SDL's RWOPS interface changed a little in SDL 2.0... */
1193
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
    28
#if defined(SDL_VERSION_ATLEAST)
1364
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
    29
#if SDL_VERSION_ATLEAST(2, 0, 0)
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
    30
#define TARGET_SDL2 1
1193
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
    31
#endif
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
    32
#endif
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
    33
1364
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
    34
#if !TARGET_SDL2
1630
5123a32160af Patched physfsrwops to compile against SDL 1.2 (thanks, Rob!).
Ryan C. Gordon <icculus@icculus.org>
parents: 1373
diff changeset
    35
#ifndef RW_SEEK_SET
1364
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
    36
#define RW_SEEK_SET SEEK_SET
1630
5123a32160af Patched physfsrwops to compile against SDL 1.2 (thanks, Rob!).
Ryan C. Gordon <icculus@icculus.org>
parents: 1373
diff changeset
    37
#endif
5123a32160af Patched physfsrwops to compile against SDL 1.2 (thanks, Rob!).
Ryan C. Gordon <icculus@icculus.org>
parents: 1373
diff changeset
    38
#ifndef RW_SEEK_CUR
1364
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
    39
#define RW_SEEK_CUR SEEK_CUR
1630
5123a32160af Patched physfsrwops to compile against SDL 1.2 (thanks, Rob!).
Ryan C. Gordon <icculus@icculus.org>
parents: 1373
diff changeset
    40
#endif
5123a32160af Patched physfsrwops to compile against SDL 1.2 (thanks, Rob!).
Ryan C. Gordon <icculus@icculus.org>
parents: 1373
diff changeset
    41
#ifndef RW_SEEK_END
1364
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
    42
#define RW_SEEK_END SEEK_END
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
    43
#endif
1630
5123a32160af Patched physfsrwops to compile against SDL 1.2 (thanks, Rob!).
Ryan C. Gordon <icculus@icculus.org>
parents: 1373
diff changeset
    44
#endif
1364
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
    45
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
    46
#if TARGET_SDL2
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
    47
static Sint64 SDLCALL physfsrwops_size(struct SDL_RWops *rw)
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
    48
{
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
    49
    PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
    50
    return (Sint64) PHYSFS_fileLength(handle);
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
    51
} /* physfsrwops_size */
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
    52
#endif
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
    53
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
    54
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
    55
#if TARGET_SDL2
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
    56
static Sint64 SDLCALL physfsrwops_seek(struct SDL_RWops *rw, Sint64 offset, int whence)
1193
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
    57
#else
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
static int physfsrwops_seek(SDL_RWops *rw, int offset, int whence)
1193
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
    59
#endif
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
{
654
c0ae01de361d Changed PHYSFS_file to PHYSFS_File to match rest of API's naming
Ryan C. Gordon <icculus@icculus.org>
parents: 576
diff changeset
    61
    PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
1193
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
    62
    PHYSFS_sint64 pos = 0;
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
1364
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
    64
    if (whence == RW_SEEK_SET)
1193
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
    65
        pos = (PHYSFS_sint64) offset;
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
1364
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
    67
    else if (whence == RW_SEEK_CUR)
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
    {
1193
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
    69
        const PHYSFS_sint64 current = PHYSFS_tell(handle);
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
        if (current == -1)
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
        {
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
            SDL_SetError("Can't find position in file: %s",
1631
1aec60db9326 Remove deprecated PHYSFS_getLastError() from all the extras (thanks, Rob!).
Ryan C. Gordon <icculus@icculus.org>
parents: 1630
diff changeset
    73
                          PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    74
            return -1;
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
        } /* if */
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
1193
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
    77
        if (offset == 0)  /* this is a "tell" call. We're done. */
132
b53fa5093749 Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents: 123
diff changeset
    78
        {
1364
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
    79
            #if TARGET_SDL2
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
    80
            return (Sint64) current;
1193
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
    81
            #else
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
    82
            return (int) current;
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
    83
            #endif
132
b53fa5093749 Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents: 123
diff changeset
    84
        } /* if */
b53fa5093749 Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents: 123
diff changeset
    85
1193
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
    86
        pos = current + ((PHYSFS_sint64) offset);
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
    } /* else if */
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
1364
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
    89
    else if (whence == RW_SEEK_END)
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
    {
1193
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
    91
        const PHYSFS_sint64 len = PHYSFS_fileLength(handle);
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
        if (len == -1)
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
        {
1631
1aec60db9326 Remove deprecated PHYSFS_getLastError() from all the extras (thanks, Rob!).
Ryan C. Gordon <icculus@icculus.org>
parents: 1630
diff changeset
    94
            SDL_SetError("Can't find end of file: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
    95
            return -1;
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
        } /* if */
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
1193
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
    98
        pos = len + ((PHYSFS_sint64) offset);
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
    } /* else if */
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
    else
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
    {
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
        SDL_SetError("Invalid 'whence' parameter.");
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
   104
        return -1;
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
    } /* else */
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
132
b53fa5093749 Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents: 123
diff changeset
   107
    if ( pos < 0 )
b53fa5093749 Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents: 123
diff changeset
   108
    {
b53fa5093749 Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents: 123
diff changeset
   109
        SDL_SetError("Attempt to seek past start of file.");
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
   110
        return -1;
132
b53fa5093749 Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents: 123
diff changeset
   111
    } /* if */
b53fa5093749 Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents: 123
diff changeset
   112
    
b53fa5093749 Added typedefs and platform-specific i/o.
Ryan C. Gordon <icculus@icculus.org>
parents: 123
diff changeset
   113
    if (!PHYSFS_seek(handle, (PHYSFS_uint64) pos))
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
    {
1631
1aec60db9326 Remove deprecated PHYSFS_getLastError() from all the extras (thanks, Rob!).
Ryan C. Gordon <icculus@icculus.org>
parents: 1630
diff changeset
   115
        SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
   116
        return -1;
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
    } /* if */
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
1364
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
   119
    #if TARGET_SDL2
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
   120
    return (Sint64) pos;
1193
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
   121
    #else
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
   122
    return (int) pos;
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
   123
    #endif
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
} /* physfsrwops_seek */
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
1364
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
   127
#if TARGET_SDL2
1193
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
   128
static size_t SDLCALL physfsrwops_read(struct SDL_RWops *rw, void *ptr,
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
   129
                                       size_t size, size_t maxnum)
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
   130
#else
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
static int physfsrwops_read(SDL_RWops *rw, void *ptr, int size, int maxnum)
1193
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
   132
#endif
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
{
654
c0ae01de361d Changed PHYSFS_file to PHYSFS_File to match rest of API's naming
Ryan C. Gordon <icculus@icculus.org>
parents: 576
diff changeset
   134
    PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
1193
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
   135
    const PHYSFS_uint64 readlen = (PHYSFS_uint64) (maxnum * size);
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
   136
    const PHYSFS_sint64 rc = PHYSFS_readBytes(handle, ptr, readlen);
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
   137
    if (rc != ((PHYSFS_sint64) readlen))
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
    {
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
        if (!PHYSFS_eof(handle)) /* not EOF? Must be an error. */
1364
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
   140
        {
1631
1aec60db9326 Remove deprecated PHYSFS_getLastError() from all the extras (thanks, Rob!).
Ryan C. Gordon <icculus@icculus.org>
parents: 1630
diff changeset
   141
            SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
1364
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
   142
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
   143
            #if TARGET_SDL2
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
   144
            return 0;
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
   145
            #else
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
   146
            return -1;
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
   147
            #endif
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
   148
        } /* if */
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
    } /* if */
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
1364
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
   151
    #if TARGET_SDL2
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
   152
    return (size_t) rc / size;
1193
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
   153
    #else
1364
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
   154
    return (int) rc / size;
1193
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
   155
    #endif
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
} /* physfsrwops_read */
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
1364
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
   159
#if TARGET_SDL2
1193
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
   160
static size_t SDLCALL physfsrwops_write(struct SDL_RWops *rw, const void *ptr,
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
   161
                                        size_t size, size_t num)
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
   162
#else
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
static int physfsrwops_write(SDL_RWops *rw, const void *ptr, int size, int num)
1193
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
   164
#endif
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
{
654
c0ae01de361d Changed PHYSFS_file to PHYSFS_File to match rest of API's naming
Ryan C. Gordon <icculus@icculus.org>
parents: 576
diff changeset
   166
    PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
1193
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
   167
    const PHYSFS_uint64 writelen = (PHYSFS_uint64) (num * size);
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
   168
    const PHYSFS_sint64 rc = PHYSFS_writeBytes(handle, ptr, writelen);
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
   169
    if (rc != ((PHYSFS_sint64) writelen))
1631
1aec60db9326 Remove deprecated PHYSFS_getLastError() from all the extras (thanks, Rob!).
Ryan C. Gordon <icculus@icculus.org>
parents: 1630
diff changeset
   170
        SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
1364
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
   172
    #if TARGET_SDL2
1193
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
   173
    return (size_t) rc;
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
   174
    #else
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
   175
    return (int) rc;
e4cc35000826 Add support for SDL 1.3 to physfsrwops.
Ryan C. Gordon <icculus@icculus.org>
parents: 1192
diff changeset
   176
    #endif
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
} /* physfsrwops_write */
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   178
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
static int physfsrwops_close(SDL_RWops *rw)
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   181
{
654
c0ae01de361d Changed PHYSFS_file to PHYSFS_File to match rest of API's naming
Ryan C. Gordon <icculus@icculus.org>
parents: 576
diff changeset
   182
    PHYSFS_File *handle = (PHYSFS_File *) rw->hidden.unknown.data1;
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   183
    if (!PHYSFS_close(handle))
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
    {
1631
1aec60db9326 Remove deprecated PHYSFS_getLastError() from all the extras (thanks, Rob!).
Ryan C. Gordon <icculus@icculus.org>
parents: 1630
diff changeset
   185
        SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
   186
        return -1;
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   187
    } /* if */
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   188
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
    SDL_FreeRW(rw);
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
   190
    return 0;
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
} /* physfsrwops_close */
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
654
c0ae01de361d Changed PHYSFS_file to PHYSFS_File to match rest of API's naming
Ryan C. Gordon <icculus@icculus.org>
parents: 576
diff changeset
   194
static SDL_RWops *create_rwops(PHYSFS_File *handle)
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
{
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   196
    SDL_RWops *retval = NULL;
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   198
    if (handle == NULL)
1631
1aec60db9326 Remove deprecated PHYSFS_getLastError() from all the extras (thanks, Rob!).
Ryan C. Gordon <icculus@icculus.org>
parents: 1630
diff changeset
   199
        SDL_SetError("PhysicsFS error: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
    else
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   201
    {
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   202
        retval = SDL_AllocRW();
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
        if (retval != NULL)
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
        {
1364
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
   205
            #if TARGET_SDL2
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
   206
            retval->size  = physfsrwops_size;
61d312843192 Fixes to SDL_RWops bridge code.
Ryan C. Gordon <icculus@icculus.org>
parents: 1193
diff changeset
   207
            #endif
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   208
            retval->seek  = physfsrwops_seek;
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   209
            retval->read  = physfsrwops_read;
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   210
            retval->write = physfsrwops_write;
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   211
            retval->close = physfsrwops_close;
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   212
            retval->hidden.unknown.data1 = handle;
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   213
        } /* if */
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
    } /* else */
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
   216
    return retval;
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   217
} /* create_rwops */
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   218
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   219
654
c0ae01de361d Changed PHYSFS_file to PHYSFS_File to match rest of API's naming
Ryan C. Gordon <icculus@icculus.org>
parents: 576
diff changeset
   220
SDL_RWops *PHYSFSRWOPS_makeRWops(PHYSFS_File *handle)
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   221
{
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   222
    SDL_RWops *retval = NULL;
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   223
    if (handle == NULL)
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   224
        SDL_SetError("NULL pointer passed to PHYSFSRWOPS_makeRWops().");
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   225
    else
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   226
        retval = create_rwops(handle);
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   227
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
   228
    return retval;
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
} /* PHYSFSRWOPS_makeRWops */
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   230
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   231
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   232
SDL_RWops *PHYSFSRWOPS_openRead(const char *fname)
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
{
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
   234
    return create_rwops(PHYSFS_openRead(fname));
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   235
} /* PHYSFSRWOPS_openRead */
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   236
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   237
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   238
SDL_RWops *PHYSFSRWOPS_openWrite(const char *fname)
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   239
{
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
   240
    return create_rwops(PHYSFS_openWrite(fname));
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
} /* PHYSFSRWOPS_openWrite */
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   242
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   243
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   244
SDL_RWops *PHYSFSRWOPS_openAppend(const char *fname)
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   245
{
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 809
diff changeset
   246
    return create_rwops(PHYSFS_openAppend(fname));
123
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   247
} /* PHYSFSRWOPS_openAppend */
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   248
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
/* end of physfsrwops.c ... */
53305c50c541 Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251