src/file/SDL_rwops.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 17 Apr 2013 01:32:06 -0700
changeset 7070 65df21723f58
parent 7037 3fedf1f25b94
child 7191 75360622e65f
child 8464 a2a909304cfe
permissions -rw-r--r--
Fixed bug 1802 - NULL pointer dereference in SDL_AllocRW() if out of memory. Philipp Wiesemann There is a NULL pointer dereference in SDL_AllocRW() if the system is out of memory. The "type" field is always written. This may be fixed with an early return. Or an else{} or not writing the field and using slower SDL_calloc(). This fault was recently introduced (http://hg.libsdl.org/SDL/rev/681820ca0e78).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     2
  Simple DirectMedia Layer
6885
700f1b25f77f Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 6655
diff changeset
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     7
  arising from the use of this software.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    11
  freely, subject to the following restrictions:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    20
*/
6643
673aed697ce6 Fixed compiling fseek64o on Linux systems
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
    21
/* Need this so Linux systems define fseek64o, ftell64o and off64_t */
673aed697ce6 Fixed compiling fseek64o on Linux systems
Sam Lantinga <slouken@libsdl.org>
parents: 6642
diff changeset
    22
#define _LARGEFILE64_SOURCE
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    23
#include "SDL_config.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    24
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    25
/* This file provides a general interface for SDL to read and write
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    26
   data sources.  It can easily be extended to files, memory, etc.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    27
*/
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    28
1354
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
    29
#include "SDL_endian.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    30
#include "SDL_rwops.h"
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
    31
4447
947201caa46e Added automated test to Xcode project plus needed changes to SDL_RWFromFile to be OS X bundle aware.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 3697
diff changeset
    32
#ifdef __APPLE__
4454
be387681d876 Fixed Eric's changes to allow building from the command line.
Sam Lantinga <slouken@libsdl.org>
parents: 4447
diff changeset
    33
#include "cocoa/SDL_rwopsbundlesupport.h"
4447
947201caa46e Added automated test to Xcode project plus needed changes to SDL_RWFromFile to be OS X bundle aware.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 3697
diff changeset
    34
#endif /* __APPLE__ */
947201caa46e Added automated test to Xcode project plus needed changes to SDL_RWFromFile to be OS X bundle aware.
Eric Wing <ewing . public |-at-| gmail . com>
parents: 3697
diff changeset
    35
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    36
#ifdef ANDROID
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    37
#include "../core/android/SDL_android.h"
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    38
#endif
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    39
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5062
diff changeset
    40
#ifdef __WIN32__
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    41
1447
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
    42
/* Functions to read/write Win32 API file pointers */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    43
5090
327f181542f1 Include windows.h in a single point in the source, so we can be consistent about the definition of UNICODE and have core utility functions for Windows that all modules can share.
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
    44
#include "../core/windows/SDL_windows.h"
1446
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    45
1465
8dfa9a6d69a5 Updated WinCE support by Dmitry (with some tweaks)
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    46
#ifndef INVALID_SET_FILE_POINTER
8dfa9a6d69a5 Updated WinCE support by Dmitry (with some tweaks)
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    47
#define INVALID_SET_FILE_POINTER 0xFFFFFFFF
8dfa9a6d69a5 Updated WinCE support by Dmitry (with some tweaks)
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    48
#endif
8dfa9a6d69a5 Updated WinCE support by Dmitry (with some tweaks)
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    49
2159
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
    50
#define READAHEAD_BUFFER_SIZE	1024
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
    51
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    52
static int SDLCALL
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
    53
windows_file_open(SDL_RWops * context, const char *filename, const char *mode)
1465
8dfa9a6d69a5 Updated WinCE support by Dmitry (with some tweaks)
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    54
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    55
    UINT old_error_mode;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    56
    HANDLE h;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    57
    DWORD r_right, w_right;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    58
    DWORD must_exist, truncate;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    59
    int a_mode;
1446
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    60
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    61
    if (!context)
2164
d083a2e4433d Merged memory leak fix from SDL 1.2
Sam Lantinga <slouken@libsdl.org>
parents: 2161
diff changeset
    62
        return -1;              /* failed (invalid call) */
2159
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
    63
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
    64
    context->hidden.windowsio.h = INVALID_HANDLE_VALUE;   /* mark this as unusable */
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
    65
    context->hidden.windowsio.buffer.data = NULL;
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
    66
    context->hidden.windowsio.buffer.size = 0;
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
    67
    context->hidden.windowsio.buffer.left = 0;
2165
c2ffcc627701 Merged r3201:3204 from branches/SDL-1.2: win32 rwops tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 2164
diff changeset
    68
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    69
    /* "r" = reading, file must exist */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    70
    /* "w" = writing, truncate existing, file may not exist */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    71
    /* "r+"= reading or writing, file must exist            */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    72
    /* "a" = writing, append file may not exist             */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    73
    /* "a+"= append + read, file may not exist              */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    74
    /* "w+" = read, write, truncate. file may not exist    */
1446
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    75
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    76
    must_exist = (SDL_strchr(mode, 'r') != NULL) ? OPEN_EXISTING : 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    77
    truncate = (SDL_strchr(mode, 'w') != NULL) ? CREATE_ALWAYS : 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    78
    r_right = (SDL_strchr(mode, '+') != NULL
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    79
               || must_exist) ? GENERIC_READ : 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    80
    a_mode = (SDL_strchr(mode, 'a') != NULL) ? OPEN_ALWAYS : 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    81
    w_right = (a_mode || SDL_strchr(mode, '+')
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    82
               || truncate) ? GENERIC_WRITE : 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    83
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    84
    if (!r_right && !w_right)   /* inconsistent mode */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    85
        return -1;              /* failed (invalid call) */
1465
8dfa9a6d69a5 Updated WinCE support by Dmitry (with some tweaks)
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    86
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
    87
    context->hidden.windowsio.buffer.data =
2173
Sam Lantinga <slouken@libsdl.org>
parents: 2165
diff changeset
    88
        (char *) SDL_malloc(READAHEAD_BUFFER_SIZE);
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
    89
    if (!context->hidden.windowsio.buffer.data) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7011
diff changeset
    90
        return SDL_OutOfMemory();
2165
c2ffcc627701 Merged r3201:3204 from branches/SDL-1.2: win32 rwops tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 2164
diff changeset
    91
    }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    92
    /* Do not open a dialog box if failure */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    93
    old_error_mode =
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
    94
        SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
1465
8dfa9a6d69a5 Updated WinCE support by Dmitry (with some tweaks)
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    95
5090
327f181542f1 Include windows.h in a single point in the source, so we can be consistent about the definition of UNICODE and have core utility functions for Windows that all modules can share.
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
    96
    {
327f181542f1 Include windows.h in a single point in the source, so we can be consistent about the definition of UNICODE and have core utility functions for Windows that all modules can share.
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
    97
        LPTSTR tstr = WIN_UTF8ToString(filename);
327f181542f1 Include windows.h in a single point in the source, so we can be consistent about the definition of UNICODE and have core utility functions for Windows that all modules can share.
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
    98
        h = CreateFile(tstr, (w_right | r_right),
327f181542f1 Include windows.h in a single point in the source, so we can be consistent about the definition of UNICODE and have core utility functions for Windows that all modules can share.
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
    99
                       (w_right) ? 0 : FILE_SHARE_READ, NULL,
327f181542f1 Include windows.h in a single point in the source, so we can be consistent about the definition of UNICODE and have core utility functions for Windows that all modules can share.
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
   100
                       (must_exist | truncate | a_mode),
327f181542f1 Include windows.h in a single point in the source, so we can be consistent about the definition of UNICODE and have core utility functions for Windows that all modules can share.
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
   101
                       FILE_ATTRIBUTE_NORMAL, NULL);
327f181542f1 Include windows.h in a single point in the source, so we can be consistent about the definition of UNICODE and have core utility functions for Windows that all modules can share.
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
   102
        SDL_free(tstr);
327f181542f1 Include windows.h in a single point in the source, so we can be consistent about the definition of UNICODE and have core utility functions for Windows that all modules can share.
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
   103
    }
1465
8dfa9a6d69a5 Updated WinCE support by Dmitry (with some tweaks)
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   104
5090
327f181542f1 Include windows.h in a single point in the source, so we can be consistent about the definition of UNICODE and have core utility functions for Windows that all modules can share.
Sam Lantinga <slouken@libsdl.org>
parents: 5086
diff changeset
   105
    /* restore old behavior */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   106
    SetErrorMode(old_error_mode);
1446
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   107
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   108
    if (h == INVALID_HANDLE_VALUE) {
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   109
        SDL_free(context->hidden.windowsio.buffer.data);
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   110
        context->hidden.windowsio.buffer.data = NULL;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   111
        SDL_SetError("Couldn't open %s", filename);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   112
        return -2;              /* failed (CreateFile) */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   113
    }
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   114
    context->hidden.windowsio.h = h;
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   115
    context->hidden.windowsio.append = a_mode ? SDL_TRUE : SDL_FALSE;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   116
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   117
    return 0;                   /* ok */
1446
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   118
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2185
diff changeset
   119
6642
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   120
static Sint64 SDLCALL
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   121
windows_file_size(SDL_RWops * context)
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   122
{
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   123
    LARGE_INTEGER size;
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   124
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   125
    if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7011
diff changeset
   126
        return SDL_SetError("windows_file_size: invalid context/file not opened");
6642
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   127
    }
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   128
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   129
    if (!GetFileSizeEx(context->hidden.windowsio.h, &size)) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7011
diff changeset
   130
        return WIN_SetError("windows_file_size");
6642
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   131
    }
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   132
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   133
    return size.QuadPart;
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   134
}
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   135
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   136
static Sint64 SDLCALL
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   137
windows_file_seek(SDL_RWops * context, Sint64 offset, int whence)
1465
8dfa9a6d69a5 Updated WinCE support by Dmitry (with some tweaks)
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   138
{
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   139
    DWORD windowswhence;
6642
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   140
    LARGE_INTEGER windowsoffset;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   141
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   142
    if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7011
diff changeset
   143
        return SDL_SetError("windows_file_seek: invalid context/file not opened");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   144
    }
1446
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   145
2159
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   146
    /* FIXME: We may be able to satisfy the seek within buffered data */
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   147
    if (whence == RW_SEEK_CUR && context->hidden.windowsio.buffer.left) {
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   148
        offset -= (long)context->hidden.windowsio.buffer.left;
2159
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   149
    }
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   150
    context->hidden.windowsio.buffer.left = 0;
2159
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   151
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   152
    switch (whence) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   153
    case RW_SEEK_SET:
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   154
        windowswhence = FILE_BEGIN;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   155
        break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   156
    case RW_SEEK_CUR:
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   157
        windowswhence = FILE_CURRENT;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   158
        break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   159
    case RW_SEEK_END:
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   160
        windowswhence = FILE_END;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   161
        break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   162
    default:
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7011
diff changeset
   163
        return SDL_SetError("windows_file_seek: Unknown value for 'whence'");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   164
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   165
6642
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   166
    windowsoffset.QuadPart = offset;
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   167
    if (!SetFilePointerEx(context->hidden.windowsio.h, windowsoffset, &windowsoffset, windowswhence)) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7011
diff changeset
   168
        return WIN_SetError("windows_file_seek");
6642
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   169
    }
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   170
    return windowsoffset.QuadPart;
1446
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   171
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2185
diff changeset
   172
2160
00adbaed3910 Updated to use size_t instead of int for amounts of data.
Sam Lantinga <slouken@libsdl.org>
parents: 2159
diff changeset
   173
static size_t SDLCALL
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   174
windows_file_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
1465
8dfa9a6d69a5 Updated WinCE support by Dmitry (with some tweaks)
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   175
{
2160
00adbaed3910 Updated to use size_t instead of int for amounts of data.
Sam Lantinga <slouken@libsdl.org>
parents: 2159
diff changeset
   176
    size_t total_need;
00adbaed3910 Updated to use size_t instead of int for amounts of data.
Sam Lantinga <slouken@libsdl.org>
parents: 2159
diff changeset
   177
    size_t total_read = 0;
00adbaed3910 Updated to use size_t instead of int for amounts of data.
Sam Lantinga <slouken@libsdl.org>
parents: 2159
diff changeset
   178
    size_t read_ahead;
2159
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   179
    DWORD byte_read;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   180
2159
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   181
    total_need = size * maxnum;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   182
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   183
    if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE
2160
00adbaed3910 Updated to use size_t instead of int for amounts of data.
Sam Lantinga <slouken@libsdl.org>
parents: 2159
diff changeset
   184
        || !total_need)
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   185
        return 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   186
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   187
    if (context->hidden.windowsio.buffer.left > 0) {
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   188
        void *data = (char *) context->hidden.windowsio.buffer.data +
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   189
            context->hidden.windowsio.buffer.size -
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   190
            context->hidden.windowsio.buffer.left;
2185
2032348afed1 This code adds support for DirectColor visuals to SDL 1.3. The support uses part of the Xmu library. To ensure that the library is
Bob Pendleton <bob@pendleton.com>
parents: 2179
diff changeset
   191
        read_ahead =
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   192
            SDL_min(total_need, context->hidden.windowsio.buffer.left);
2159
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   193
        SDL_memcpy(ptr, data, read_ahead);
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   194
        context->hidden.windowsio.buffer.left -= read_ahead;
2159
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   195
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   196
        if (read_ahead == total_need) {
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   197
            return maxnum;
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   198
        }
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   199
        ptr = (char *) ptr + read_ahead;
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   200
        total_need -= read_ahead;
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   201
        total_read += read_ahead;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   202
    }
2159
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   203
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   204
    if (total_need < READAHEAD_BUFFER_SIZE) {
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   205
        if (!ReadFile
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   206
            (context->hidden.windowsio.h, context->hidden.windowsio.buffer.data,
2159
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   207
             READAHEAD_BUFFER_SIZE, &byte_read, NULL)) {
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   208
            SDL_Error(SDL_EFREAD);
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   209
            return 0;
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   210
        }
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   211
        read_ahead = SDL_min(total_need, (int) byte_read);
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   212
        SDL_memcpy(ptr, context->hidden.windowsio.buffer.data, read_ahead);
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   213
        context->hidden.windowsio.buffer.size = byte_read;
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   214
        context->hidden.windowsio.buffer.left = byte_read - read_ahead;
2159
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   215
        total_read += read_ahead;
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   216
    } else {
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   217
        if (!ReadFile
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   218
            (context->hidden.windowsio.h, ptr, (DWORD)total_need, &byte_read, NULL)) {
2159
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   219
            SDL_Error(SDL_EFREAD);
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   220
            return 0;
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   221
        }
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   222
        total_read += byte_read;
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   223
    }
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   224
    return (total_read / size);
1446
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   225
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2185
diff changeset
   226
2160
00adbaed3910 Updated to use size_t instead of int for amounts of data.
Sam Lantinga <slouken@libsdl.org>
parents: 2159
diff changeset
   227
static size_t SDLCALL
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   228
windows_file_write(SDL_RWops * context, const void *ptr, size_t size,
2160
00adbaed3910 Updated to use size_t instead of int for amounts of data.
Sam Lantinga <slouken@libsdl.org>
parents: 2159
diff changeset
   229
                 size_t num)
1465
8dfa9a6d69a5 Updated WinCE support by Dmitry (with some tweaks)
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   230
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   231
2160
00adbaed3910 Updated to use size_t instead of int for amounts of data.
Sam Lantinga <slouken@libsdl.org>
parents: 2159
diff changeset
   232
    size_t total_bytes;
3253
5d7ef5970073 Fixed issues building 64-bit Windows binary
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   233
    DWORD byte_written;
5d7ef5970073 Fixed issues building 64-bit Windows binary
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   234
    size_t nwritten;
1446
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   235
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   236
    total_bytes = size * num;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   237
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   238
    if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   239
        || total_bytes <= 0 || !size)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   240
        return 0;
1446
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   241
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   242
    if (context->hidden.windowsio.buffer.left) {
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   243
        SetFilePointer(context->hidden.windowsio.h,
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   244
                       -(LONG)context->hidden.windowsio.buffer.left, NULL,
2159
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   245
                       FILE_CURRENT);
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   246
        context->hidden.windowsio.buffer.left = 0;
2159
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   247
    }
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   248
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   249
    /* if in append mode, we must go to the EOF before write */
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   250
    if (context->hidden.windowsio.append) {
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   251
        if (SetFilePointer(context->hidden.windowsio.h, 0L, NULL, FILE_END) ==
2159
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   252
            INVALID_SET_FILE_POINTER) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   253
            SDL_Error(SDL_EFWRITE);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   254
            return 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   255
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   256
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   257
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   258
    if (!WriteFile
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   259
        (context->hidden.windowsio.h, ptr, (DWORD)total_bytes, &byte_written, NULL)) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   260
        SDL_Error(SDL_EFWRITE);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   261
        return 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   262
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   263
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   264
    nwritten = byte_written / size;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   265
    return nwritten;
1446
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   266
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2185
diff changeset
   267
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   268
static int SDLCALL
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   269
windows_file_close(SDL_RWops * context)
1465
8dfa9a6d69a5 Updated WinCE support by Dmitry (with some tweaks)
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   270
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   271
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   272
    if (context) {
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   273
        if (context->hidden.windowsio.h != INVALID_HANDLE_VALUE) {
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   274
            CloseHandle(context->hidden.windowsio.h);
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   275
            context->hidden.windowsio.h = INVALID_HANDLE_VALUE;   /* to be sure */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   276
        }
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   277
        if (context->hidden.windowsio.buffer.data) {
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   278
            SDL_free(context->hidden.windowsio.buffer.data);
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   279
            context->hidden.windowsio.buffer.data = NULL;
2159
dd4753e47ed4 Merged read-ahead support for Win32 file I/O from SDL 1.2 revision 3183
Sam Lantinga <slouken@libsdl.org>
parents: 1978
diff changeset
   280
        }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   281
        SDL_FreeRW(context);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   282
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   283
    return (0);
1446
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   284
}
5086
c2539ff054c8 Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
Sam Lantinga <slouken@libsdl.org>
parents: 5062
diff changeset
   285
#endif /* __WIN32__ */
1446
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   286
1447
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   287
#ifdef HAVE_STDIO_H
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   288
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   289
/* Functions to read/write stdio file pointers */
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   290
6642
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   291
static Sint64 SDLCALL
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   292
stdio_size(SDL_RWops * context)
1447
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   293
{
6642
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   294
    Sint64 pos, size;
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   295
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   296
    pos = SDL_RWseek(context, 0, RW_SEEK_CUR);
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   297
    if (pos < 0) {
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   298
        return -1;
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   299
    }
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   300
    size = SDL_RWseek(context, 0, RW_SEEK_END);
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   301
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   302
    SDL_RWseek(context, pos, RW_SEEK_SET);
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   303
    return size;
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   304
}
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   305
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   306
static Sint64 SDLCALL
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   307
stdio_seek(SDL_RWops * context, Sint64 offset, int whence)
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   308
{
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   309
#ifdef HAVE_FSEEKO64
6644
eacc8b228cef Fixed symbol typo.
Ryan C. Gordon <icculus@icculus.org>
parents: 6643
diff changeset
   310
    if (fseeko64(context->hidden.stdio.fp, (off64_t)offset, whence) == 0) {
eacc8b228cef Fixed symbol typo.
Ryan C. Gordon <icculus@icculus.org>
parents: 6643
diff changeset
   311
        return ftello64(context->hidden.stdio.fp);
6642
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   312
    }
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   313
#elif defined(HAVE_FSEEKO)
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   314
    if (fseeko(context->hidden.stdio.fp, (off_t)offset, whence) == 0) {
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   315
        return ftello(context->hidden.stdio.fp);
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   316
    }
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   317
#else
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   318
    if (fseek(context->hidden.stdio.fp, offset, whence) == 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   319
        return (ftell(context->hidden.stdio.fp));
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   320
    }
6642
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   321
#endif
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7011
diff changeset
   322
    return SDL_Error(SDL_EFSEEK);
1447
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   323
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2185
diff changeset
   324
2160
00adbaed3910 Updated to use size_t instead of int for amounts of data.
Sam Lantinga <slouken@libsdl.org>
parents: 2159
diff changeset
   325
static size_t SDLCALL
00adbaed3910 Updated to use size_t instead of int for amounts of data.
Sam Lantinga <slouken@libsdl.org>
parents: 2159
diff changeset
   326
stdio_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
1447
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   327
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   328
    size_t nread;
1446
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   329
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   330
    nread = fread(ptr, size, maxnum, context->hidden.stdio.fp);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   331
    if (nread == 0 && ferror(context->hidden.stdio.fp)) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   332
        SDL_Error(SDL_EFREAD);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   333
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   334
    return (nread);
1447
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   335
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2185
diff changeset
   336
2160
00adbaed3910 Updated to use size_t instead of int for amounts of data.
Sam Lantinga <slouken@libsdl.org>
parents: 2159
diff changeset
   337
static size_t SDLCALL
00adbaed3910 Updated to use size_t instead of int for amounts of data.
Sam Lantinga <slouken@libsdl.org>
parents: 2159
diff changeset
   338
stdio_write(SDL_RWops * context, const void *ptr, size_t size, size_t num)
1447
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   339
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   340
    size_t nwrote;
1446
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   341
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   342
    nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   343
    if (nwrote == 0 && ferror(context->hidden.stdio.fp)) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   344
        SDL_Error(SDL_EFWRITE);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   345
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   346
    return (nwrote);
1447
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   347
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2185
diff changeset
   348
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   349
static int SDLCALL
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   350
stdio_close(SDL_RWops * context)
1447
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   351
{
2160
00adbaed3910 Updated to use size_t instead of int for amounts of data.
Sam Lantinga <slouken@libsdl.org>
parents: 2159
diff changeset
   352
    int status = 0;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   353
    if (context) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   354
        if (context->hidden.stdio.autoclose) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   355
            /* WARNING:  Check the return value here! */
2160
00adbaed3910 Updated to use size_t instead of int for amounts of data.
Sam Lantinga <slouken@libsdl.org>
parents: 2159
diff changeset
   356
            if (fclose(context->hidden.stdio.fp) != 0) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7011
diff changeset
   357
                status = SDL_Error(SDL_EFWRITE);
2160
00adbaed3910 Updated to use size_t instead of int for amounts of data.
Sam Lantinga <slouken@libsdl.org>
parents: 2159
diff changeset
   358
            }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   359
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   360
        SDL_FreeRW(context);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   361
    }
2160
00adbaed3910 Updated to use size_t instead of int for amounts of data.
Sam Lantinga <slouken@libsdl.org>
parents: 2159
diff changeset
   362
    return status;
1447
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   363
}
1446
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   364
#endif /* !HAVE_STDIO_H */
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
   365
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   366
/* Functions to read/write memory pointers */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   367
6642
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   368
static Sint64 SDLCALL
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   369
mem_size(SDL_RWops * context)
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   370
{
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   371
    return (Sint64)(context->hidden.mem.stop - context->hidden.mem.base);
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   372
}
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   373
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   374
static Sint64 SDLCALL
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   375
mem_seek(SDL_RWops * context, Sint64 offset, int whence)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   376
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   377
    Uint8 *newpos;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   378
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   379
    switch (whence) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   380
    case RW_SEEK_SET:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   381
        newpos = context->hidden.mem.base + offset;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   382
        break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   383
    case RW_SEEK_CUR:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   384
        newpos = context->hidden.mem.here + offset;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   385
        break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   386
    case RW_SEEK_END:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   387
        newpos = context->hidden.mem.stop + offset;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   388
        break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   389
    default:
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7011
diff changeset
   390
        return SDL_SetError("Unknown value for 'whence'");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   391
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   392
    if (newpos < context->hidden.mem.base) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   393
        newpos = context->hidden.mem.base;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   394
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   395
    if (newpos > context->hidden.mem.stop) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   396
        newpos = context->hidden.mem.stop;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   397
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   398
    context->hidden.mem.here = newpos;
6642
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   399
    return (Sint64)(context->hidden.mem.here - context->hidden.mem.base);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   400
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2185
diff changeset
   401
2160
00adbaed3910 Updated to use size_t instead of int for amounts of data.
Sam Lantinga <slouken@libsdl.org>
parents: 2159
diff changeset
   402
static size_t SDLCALL
00adbaed3910 Updated to use size_t instead of int for amounts of data.
Sam Lantinga <slouken@libsdl.org>
parents: 2159
diff changeset
   403
mem_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   404
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   405
    size_t total_bytes;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   406
    size_t mem_available;
1078
e2ef6b7001fd Patch from Antonio SJ Musumeci:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
   407
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   408
    total_bytes = (maxnum * size);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   409
    if ((maxnum <= 0) || (size <= 0)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   410
        || ((total_bytes / maxnum) != (size_t) size)) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   411
        return 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   412
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   413
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   414
    mem_available = (context->hidden.mem.stop - context->hidden.mem.here);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   415
    if (total_bytes > mem_available) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   416
        total_bytes = mem_available;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   417
    }
1078
e2ef6b7001fd Patch from Antonio SJ Musumeci:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
   418
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   419
    SDL_memcpy(ptr, context->hidden.mem.here, total_bytes);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   420
    context->hidden.mem.here += total_bytes;
1078
e2ef6b7001fd Patch from Antonio SJ Musumeci:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
   421
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   422
    return (total_bytes / size);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   423
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2185
diff changeset
   424
2160
00adbaed3910 Updated to use size_t instead of int for amounts of data.
Sam Lantinga <slouken@libsdl.org>
parents: 2159
diff changeset
   425
static size_t SDLCALL
00adbaed3910 Updated to use size_t instead of int for amounts of data.
Sam Lantinga <slouken@libsdl.org>
parents: 2159
diff changeset
   426
mem_write(SDL_RWops * context, const void *ptr, size_t size, size_t num)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   427
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   428
    if ((context->hidden.mem.here + (num * size)) > context->hidden.mem.stop) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   429
        num = (context->hidden.mem.stop - context->hidden.mem.here) / size;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   430
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   431
    SDL_memcpy(context->hidden.mem.here, ptr, num * size);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   432
    context->hidden.mem.here += num * size;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   433
    return (num);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   434
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2185
diff changeset
   435
2160
00adbaed3910 Updated to use size_t instead of int for amounts of data.
Sam Lantinga <slouken@libsdl.org>
parents: 2159
diff changeset
   436
static size_t SDLCALL
00adbaed3910 Updated to use size_t instead of int for amounts of data.
Sam Lantinga <slouken@libsdl.org>
parents: 2159
diff changeset
   437
mem_writeconst(SDL_RWops * context, const void *ptr, size_t size, size_t num)
764
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   438
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   439
    SDL_SetError("Can't write to read-only memory");
6996
bc0fd5bced25 Fix bug 1560 - SDL_RWFromConstMem write operation returns -1 but should return 0.
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6885
diff changeset
   440
    return (0);
764
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   441
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2185
diff changeset
   442
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   443
static int SDLCALL
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   444
mem_close(SDL_RWops * context)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   445
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   446
    if (context) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   447
        SDL_FreeRW(context);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   448
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   449
    return (0);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   450
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   451
1447
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   452
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   453
/* Functions to create SDL_RWops structures from various data sources */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   454
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   455
SDL_RWops *
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   456
SDL_RWFromFile(const char *file, const char *mode)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   457
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   458
    SDL_RWops *rwops = NULL;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   459
    if (!file || !*file || !mode || !*mode) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   460
        SDL_SetError("SDL_RWFromFile(): No file or no mode specified");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   461
        return NULL;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   462
    }
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   463
#if defined(ANDROID)
6629
8e2c731103e6 Try opening the file path directly before hitting the asset system.
Sam Lantinga <slouken@libsdl.org>
parents: 6430
diff changeset
   464
#ifdef HAVE_STDIO_H
8e2c731103e6 Try opening the file path directly before hitting the asset system.
Sam Lantinga <slouken@libsdl.org>
parents: 6430
diff changeset
   465
    /* Try to open the file on the filesystem first */
6633
e36b94438fcf Try opening relative path files from internal storage.
Sam Lantinga <slouken@libsdl.org>
parents: 6629
diff changeset
   466
    if (*file == '/') {
6629
8e2c731103e6 Try opening the file path directly before hitting the asset system.
Sam Lantinga <slouken@libsdl.org>
parents: 6430
diff changeset
   467
        FILE *fp = fopen(file, mode);
8e2c731103e6 Try opening the file path directly before hitting the asset system.
Sam Lantinga <slouken@libsdl.org>
parents: 6430
diff changeset
   468
        if (fp) {
8e2c731103e6 Try opening the file path directly before hitting the asset system.
Sam Lantinga <slouken@libsdl.org>
parents: 6430
diff changeset
   469
            return SDL_RWFromFP(fp, 1);
8e2c731103e6 Try opening the file path directly before hitting the asset system.
Sam Lantinga <slouken@libsdl.org>
parents: 6430
diff changeset
   470
        }
6633
e36b94438fcf Try opening relative path files from internal storage.
Sam Lantinga <slouken@libsdl.org>
parents: 6629
diff changeset
   471
    } else {
e36b94438fcf Try opening relative path files from internal storage.
Sam Lantinga <slouken@libsdl.org>
parents: 6629
diff changeset
   472
        /* Try opening it from internal storage if it's a relative path */
e36b94438fcf Try opening relative path files from internal storage.
Sam Lantinga <slouken@libsdl.org>
parents: 6629
diff changeset
   473
        char *path;
e36b94438fcf Try opening relative path files from internal storage.
Sam Lantinga <slouken@libsdl.org>
parents: 6629
diff changeset
   474
        FILE *fp;
e36b94438fcf Try opening relative path files from internal storage.
Sam Lantinga <slouken@libsdl.org>
parents: 6629
diff changeset
   475
e36b94438fcf Try opening relative path files from internal storage.
Sam Lantinga <slouken@libsdl.org>
parents: 6629
diff changeset
   476
        path = SDL_stack_alloc(char, PATH_MAX);
e36b94438fcf Try opening relative path files from internal storage.
Sam Lantinga <slouken@libsdl.org>
parents: 6629
diff changeset
   477
        if (path) {
e36b94438fcf Try opening relative path files from internal storage.
Sam Lantinga <slouken@libsdl.org>
parents: 6629
diff changeset
   478
            SDL_snprintf(path, PATH_MAX, "%s/%s",
e36b94438fcf Try opening relative path files from internal storage.
Sam Lantinga <slouken@libsdl.org>
parents: 6629
diff changeset
   479
                         SDL_AndroidGetInternalStoragePath(), file);
e36b94438fcf Try opening relative path files from internal storage.
Sam Lantinga <slouken@libsdl.org>
parents: 6629
diff changeset
   480
            fp = fopen(path, mode);
e36b94438fcf Try opening relative path files from internal storage.
Sam Lantinga <slouken@libsdl.org>
parents: 6629
diff changeset
   481
            SDL_stack_free(path);
e36b94438fcf Try opening relative path files from internal storage.
Sam Lantinga <slouken@libsdl.org>
parents: 6629
diff changeset
   482
            if (fp) {
e36b94438fcf Try opening relative path files from internal storage.
Sam Lantinga <slouken@libsdl.org>
parents: 6629
diff changeset
   483
                return SDL_RWFromFP(fp, 1);
e36b94438fcf Try opening relative path files from internal storage.
Sam Lantinga <slouken@libsdl.org>
parents: 6629
diff changeset
   484
            }
e36b94438fcf Try opening relative path files from internal storage.
Sam Lantinga <slouken@libsdl.org>
parents: 6629
diff changeset
   485
        }
6629
8e2c731103e6 Try opening the file path directly before hitting the asset system.
Sam Lantinga <slouken@libsdl.org>
parents: 6430
diff changeset
   486
    }
6633
e36b94438fcf Try opening relative path files from internal storage.
Sam Lantinga <slouken@libsdl.org>
parents: 6629
diff changeset
   487
#endif /* HAVE_STDIO_H */
6629
8e2c731103e6 Try opening the file path directly before hitting the asset system.
Sam Lantinga <slouken@libsdl.org>
parents: 6430
diff changeset
   488
8e2c731103e6 Try opening the file path directly before hitting the asset system.
Sam Lantinga <slouken@libsdl.org>
parents: 6430
diff changeset
   489
    /* Try to open the file from the asset system */
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   490
    rwops = SDL_AllocRW();
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   491
    if (!rwops)
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   492
        return NULL;            /* SDL_SetError already setup by SDL_AllocRW() */
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   493
    if (Android_JNI_FileOpen(rwops, file, mode) < 0) {
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   494
        SDL_FreeRW(rwops);
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   495
        return NULL;
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   496
    }
6642
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   497
    rwops->size = Android_JNI_FileSize;
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   498
    rwops->seek = Android_JNI_FileSeek;
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   499
    rwops->read = Android_JNI_FileRead;
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   500
    rwops->write = Android_JNI_FileWrite;
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   501
    rwops->close = Android_JNI_FileClose;
6999
681820ca0e78 Fix bug 122 - SDL_RWops bug fixes: set RWops.type field, add input validation, add test coverage
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6996
diff changeset
   502
    rwops->type = SDL_RWOPS_JNIFILE;
5582
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   503
1281a3f1f0a6 Allow Android platforms to read from .apk files via the RWOPS interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   504
#elif defined(__WIN32__)
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   505
    rwops = SDL_AllocRW();
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   506
    if (!rwops)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   507
        return NULL;            /* SDL_SetError already setup by SDL_AllocRW() */
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   508
    if (windows_file_open(rwops, file, mode) < 0) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   509
        SDL_FreeRW(rwops);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   510
        return NULL;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   511
    }
6642
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   512
    rwops->size = windows_file_size;
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   513
    rwops->seek = windows_file_seek;
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   514
    rwops->read = windows_file_read;
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   515
    rwops->write = windows_file_write;
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4454
diff changeset
   516
    rwops->close = windows_file_close;
6999
681820ca0e78 Fix bug 122 - SDL_RWops bug fixes: set RWops.type field, add input validation, add test coverage
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6996
diff changeset
   517
    rwops->type = SDL_RWOPS_WINFILE;
1447
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   518
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   519
#elif HAVE_STDIO_H
6375
93f9a24d1c02 Fixed some minor compiler warnings.
Ryan C. Gordon <icculus@icculus.org>
parents: 6138
diff changeset
   520
    {
93f9a24d1c02 Fixed some minor compiler warnings.
Ryan C. Gordon <icculus@icculus.org>
parents: 6138
diff changeset
   521
    	#ifdef __APPLE__
93f9a24d1c02 Fixed some minor compiler warnings.
Ryan C. Gordon <icculus@icculus.org>
parents: 6138
diff changeset
   522
    	FILE *fp = SDL_OpenFPFromBundleOrFallback(file, mode);
93f9a24d1c02 Fixed some minor compiler warnings.
Ryan C. Gordon <icculus@icculus.org>
parents: 6138
diff changeset
   523
        #else
93f9a24d1c02 Fixed some minor compiler warnings.
Ryan C. Gordon <icculus@icculus.org>
parents: 6138
diff changeset
   524
    	FILE *fp = fopen(file, mode);
93f9a24d1c02 Fixed some minor compiler warnings.
Ryan C. Gordon <icculus@icculus.org>
parents: 6138
diff changeset
   525
    	#endif
93f9a24d1c02 Fixed some minor compiler warnings.
Ryan C. Gordon <icculus@icculus.org>
parents: 6138
diff changeset
   526
    	if (fp == NULL) {
93f9a24d1c02 Fixed some minor compiler warnings.
Ryan C. Gordon <icculus@icculus.org>
parents: 6138
diff changeset
   527
            SDL_SetError("Couldn't open %s", file);
93f9a24d1c02 Fixed some minor compiler warnings.
Ryan C. Gordon <icculus@icculus.org>
parents: 6138
diff changeset
   528
        } else {
93f9a24d1c02 Fixed some minor compiler warnings.
Ryan C. Gordon <icculus@icculus.org>
parents: 6138
diff changeset
   529
            rwops = SDL_RWFromFP(fp, 1);
93f9a24d1c02 Fixed some minor compiler warnings.
Ryan C. Gordon <icculus@icculus.org>
parents: 6138
diff changeset
   530
        }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   531
    }
1447
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   532
#else
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   533
    SDL_SetError("SDL not compiled with stdio support");
1446
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   534
#endif /* !HAVE_STDIO_H */
1447
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   535
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   536
    return (rwops);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   537
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   538
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
   539
#ifdef HAVE_STDIO_H
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   540
SDL_RWops *
2161
e635db5b45ef Oh yeah, they're boolean values...
Sam Lantinga <slouken@libsdl.org>
parents: 2160
diff changeset
   541
SDL_RWFromFP(FILE * fp, SDL_bool autoclose)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   542
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   543
    SDL_RWops *rwops = NULL;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   544
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   545
    rwops = SDL_AllocRW();
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   546
    if (rwops != NULL) {
6642
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   547
        rwops->size = stdio_size;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   548
        rwops->seek = stdio_seek;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   549
        rwops->read = stdio_read;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   550
        rwops->write = stdio_write;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   551
        rwops->close = stdio_close;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   552
        rwops->hidden.stdio.fp = fp;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   553
        rwops->hidden.stdio.autoclose = autoclose;
6999
681820ca0e78 Fix bug 122 - SDL_RWops bug fixes: set RWops.type field, add input validation, add test coverage
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6996
diff changeset
   554
        rwops->type = SDL_RWOPS_STDFILE;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   555
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   556
    return (rwops);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   557
}
3564
d264d99576c1 Add a dummy function if SDL doesn't have STDIO support
Sam Lantinga <slouken@libsdl.org>
parents: 3253
diff changeset
   558
#else
d264d99576c1 Add a dummy function if SDL doesn't have STDIO support
Sam Lantinga <slouken@libsdl.org>
parents: 3253
diff changeset
   559
SDL_RWops *
d264d99576c1 Add a dummy function if SDL doesn't have STDIO support
Sam Lantinga <slouken@libsdl.org>
parents: 3253
diff changeset
   560
SDL_RWFromFP(void * fp, SDL_bool autoclose)
d264d99576c1 Add a dummy function if SDL doesn't have STDIO support
Sam Lantinga <slouken@libsdl.org>
parents: 3253
diff changeset
   561
{
d264d99576c1 Add a dummy function if SDL doesn't have STDIO support
Sam Lantinga <slouken@libsdl.org>
parents: 3253
diff changeset
   562
    SDL_SetError("SDL not compiled with stdio support");
d264d99576c1 Add a dummy function if SDL doesn't have STDIO support
Sam Lantinga <slouken@libsdl.org>
parents: 3253
diff changeset
   563
    return NULL;
d264d99576c1 Add a dummy function if SDL doesn't have STDIO support
Sam Lantinga <slouken@libsdl.org>
parents: 3253
diff changeset
   564
}
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
   565
#endif /* HAVE_STDIO_H */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   566
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   567
SDL_RWops *
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   568
SDL_RWFromMem(void *mem, int size)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   569
{
6999
681820ca0e78 Fix bug 122 - SDL_RWops bug fixes: set RWops.type field, add input validation, add test coverage
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6996
diff changeset
   570
    SDL_RWops *rwops = NULL;
681820ca0e78 Fix bug 122 - SDL_RWops bug fixes: set RWops.type field, add input validation, add test coverage
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6996
diff changeset
   571
    if (!mem) {
681820ca0e78 Fix bug 122 - SDL_RWops bug fixes: set RWops.type field, add input validation, add test coverage
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6996
diff changeset
   572
      SDL_InvalidParamError("mem");
681820ca0e78 Fix bug 122 - SDL_RWops bug fixes: set RWops.type field, add input validation, add test coverage
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6996
diff changeset
   573
      return (rwops);
681820ca0e78 Fix bug 122 - SDL_RWops bug fixes: set RWops.type field, add input validation, add test coverage
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6996
diff changeset
   574
    }
681820ca0e78 Fix bug 122 - SDL_RWops bug fixes: set RWops.type field, add input validation, add test coverage
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6996
diff changeset
   575
    if (!size) {
681820ca0e78 Fix bug 122 - SDL_RWops bug fixes: set RWops.type field, add input validation, add test coverage
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6996
diff changeset
   576
      SDL_InvalidParamError("size");
681820ca0e78 Fix bug 122 - SDL_RWops bug fixes: set RWops.type field, add input validation, add test coverage
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6996
diff changeset
   577
      return (rwops);
681820ca0e78 Fix bug 122 - SDL_RWops bug fixes: set RWops.type field, add input validation, add test coverage
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6996
diff changeset
   578
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   579
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   580
    rwops = SDL_AllocRW();
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   581
    if (rwops != NULL) {
6642
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   582
        rwops->size = mem_size;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   583
        rwops->seek = mem_seek;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   584
        rwops->read = mem_read;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   585
        rwops->write = mem_write;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   586
        rwops->close = mem_close;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   587
        rwops->hidden.mem.base = (Uint8 *) mem;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   588
        rwops->hidden.mem.here = rwops->hidden.mem.base;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   589
        rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
6999
681820ca0e78 Fix bug 122 - SDL_RWops bug fixes: set RWops.type field, add input validation, add test coverage
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6996
diff changeset
   590
        rwops->type = SDL_RWOPS_MEMORY;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   591
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   592
    return (rwops);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   593
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   594
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   595
SDL_RWops *
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   596
SDL_RWFromConstMem(const void *mem, int size)
764
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   597
{
6999
681820ca0e78 Fix bug 122 - SDL_RWops bug fixes: set RWops.type field, add input validation, add test coverage
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6996
diff changeset
   598
    SDL_RWops *rwops = NULL;
681820ca0e78 Fix bug 122 - SDL_RWops bug fixes: set RWops.type field, add input validation, add test coverage
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6996
diff changeset
   599
    if (!mem) {
681820ca0e78 Fix bug 122 - SDL_RWops bug fixes: set RWops.type field, add input validation, add test coverage
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6996
diff changeset
   600
      SDL_InvalidParamError("mem");
681820ca0e78 Fix bug 122 - SDL_RWops bug fixes: set RWops.type field, add input validation, add test coverage
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6996
diff changeset
   601
      return (rwops);
681820ca0e78 Fix bug 122 - SDL_RWops bug fixes: set RWops.type field, add input validation, add test coverage
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6996
diff changeset
   602
    }
681820ca0e78 Fix bug 122 - SDL_RWops bug fixes: set RWops.type field, add input validation, add test coverage
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6996
diff changeset
   603
    if (!size) {
681820ca0e78 Fix bug 122 - SDL_RWops bug fixes: set RWops.type field, add input validation, add test coverage
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6996
diff changeset
   604
      SDL_InvalidParamError("size");
681820ca0e78 Fix bug 122 - SDL_RWops bug fixes: set RWops.type field, add input validation, add test coverage
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6996
diff changeset
   605
      return (rwops);
681820ca0e78 Fix bug 122 - SDL_RWops bug fixes: set RWops.type field, add input validation, add test coverage
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6996
diff changeset
   606
    }
764
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   607
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   608
    rwops = SDL_AllocRW();
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   609
    if (rwops != NULL) {
6642
b716011e53ce Added an API to get the size of a file - WARNING! ABI CHANGE!
Sam Lantinga <slouken@libsdl.org>
parents: 6633
diff changeset
   610
        rwops->size = mem_size;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   611
        rwops->seek = mem_seek;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   612
        rwops->read = mem_read;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   613
        rwops->write = mem_writeconst;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   614
        rwops->close = mem_close;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   615
        rwops->hidden.mem.base = (Uint8 *) mem;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   616
        rwops->hidden.mem.here = rwops->hidden.mem.base;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   617
        rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
6999
681820ca0e78 Fix bug 122 - SDL_RWops bug fixes: set RWops.type field, add input validation, add test coverage
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 6996
diff changeset
   618
        rwops->type = SDL_RWOPS_MEMORY_RO;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   619
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   620
    return (rwops);
764
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   621
}
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   622
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   623
SDL_RWops *
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   624
SDL_AllocRW(void)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   625
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   626
    SDL_RWops *area;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   627
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   628
    area = (SDL_RWops *) SDL_malloc(sizeof *area);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   629
    if (area == NULL) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   630
        SDL_OutOfMemory();
7070
65df21723f58 Fixed bug 1802 - NULL pointer dereference in SDL_AllocRW() if out of memory.
Sam Lantinga <slouken@libsdl.org>
parents: 7037
diff changeset
   631
    } else {
65df21723f58 Fixed bug 1802 - NULL pointer dereference in SDL_AllocRW() if out of memory.
Sam Lantinga <slouken@libsdl.org>
parents: 7037
diff changeset
   632
        area->type = SDL_RWOPS_UNKNOWN;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   633
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   634
    return (area);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   635
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   636
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   637
void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   638
SDL_FreeRW(SDL_RWops * area)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   639
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   640
    SDL_free(area);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   641
}
1354
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   642
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   643
/* Functions for dynamically reading and writing endian-specific values */
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   644
6655
6a3e741ea70d Added SDL_ReadU8() and SDL_WriteU8() for consistency with the rest of the API.
Sam Lantinga <slouken@libsdl.org>
parents: 6644
diff changeset
   645
Uint8
6a3e741ea70d Added SDL_ReadU8() and SDL_WriteU8() for consistency with the rest of the API.
Sam Lantinga <slouken@libsdl.org>
parents: 6644
diff changeset
   646
SDL_ReadU8(SDL_RWops * src)
6a3e741ea70d Added SDL_ReadU8() and SDL_WriteU8() for consistency with the rest of the API.
Sam Lantinga <slouken@libsdl.org>
parents: 6644
diff changeset
   647
{
6a3e741ea70d Added SDL_ReadU8() and SDL_WriteU8() for consistency with the rest of the API.
Sam Lantinga <slouken@libsdl.org>
parents: 6644
diff changeset
   648
    Uint8 value = 0;
6a3e741ea70d Added SDL_ReadU8() and SDL_WriteU8() for consistency with the rest of the API.
Sam Lantinga <slouken@libsdl.org>
parents: 6644
diff changeset
   649
6a3e741ea70d Added SDL_ReadU8() and SDL_WriteU8() for consistency with the rest of the API.
Sam Lantinga <slouken@libsdl.org>
parents: 6644
diff changeset
   650
    SDL_RWread(src, &value, (sizeof value), 1);
6a3e741ea70d Added SDL_ReadU8() and SDL_WriteU8() for consistency with the rest of the API.
Sam Lantinga <slouken@libsdl.org>
parents: 6644
diff changeset
   651
    return value;
6a3e741ea70d Added SDL_ReadU8() and SDL_WriteU8() for consistency with the rest of the API.
Sam Lantinga <slouken@libsdl.org>
parents: 6644
diff changeset
   652
}
6a3e741ea70d Added SDL_ReadU8() and SDL_WriteU8() for consistency with the rest of the API.
Sam Lantinga <slouken@libsdl.org>
parents: 6644
diff changeset
   653
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   654
Uint16
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   655
SDL_ReadLE16(SDL_RWops * src)
1354
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   656
{
6655
6a3e741ea70d Added SDL_ReadU8() and SDL_WriteU8() for consistency with the rest of the API.
Sam Lantinga <slouken@libsdl.org>
parents: 6644
diff changeset
   657
    Uint16 value = 0;
1354
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   658
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   659
    SDL_RWread(src, &value, (sizeof value), 1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   660
    return (SDL_SwapLE16(value));
1354
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   661
}
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   662
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   663
Uint16
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   664
SDL_ReadBE16(SDL_RWops * src)
1354
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   665
{
6655
6a3e741ea70d Added SDL_ReadU8() and SDL_WriteU8() for consistency with the rest of the API.
Sam Lantinga <slouken@libsdl.org>
parents: 6644
diff changeset
   666
    Uint16 value = 0;
1354
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   667
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   668
    SDL_RWread(src, &value, (sizeof value), 1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   669
    return (SDL_SwapBE16(value));
1354
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   670
}
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   671
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   672
Uint32
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   673
SDL_ReadLE32(SDL_RWops * src)
1354
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   674
{
6655
6a3e741ea70d Added SDL_ReadU8() and SDL_WriteU8() for consistency with the rest of the API.
Sam Lantinga <slouken@libsdl.org>
parents: 6644
diff changeset
   675
    Uint32 value = 0;
1354
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   676
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   677
    SDL_RWread(src, &value, (sizeof value), 1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   678
    return (SDL_SwapLE32(value));
1354
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   679
}
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   680
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   681
Uint32
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   682
SDL_ReadBE32(SDL_RWops * src)
1354
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   683
{
6655
6a3e741ea70d Added SDL_ReadU8() and SDL_WriteU8() for consistency with the rest of the API.
Sam Lantinga <slouken@libsdl.org>
parents: 6644
diff changeset
   684
    Uint32 value = 0;
1354
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   685
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   686
    SDL_RWread(src, &value, (sizeof value), 1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   687
    return (SDL_SwapBE32(value));
1354
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   688
}
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   689
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   690
Uint64
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   691
SDL_ReadLE64(SDL_RWops * src)
1354
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   692
{
6655
6a3e741ea70d Added SDL_ReadU8() and SDL_WriteU8() for consistency with the rest of the API.
Sam Lantinga <slouken@libsdl.org>
parents: 6644
diff changeset
   693
    Uint64 value = 0;
1354
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   694
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   695
    SDL_RWread(src, &value, (sizeof value), 1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   696
    return (SDL_SwapLE64(value));
1354
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   697
}
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   698
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   699
Uint64
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   700
SDL_ReadBE64(SDL_RWops * src)
1354
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   701
{
6655
6a3e741ea70d Added SDL_ReadU8() and SDL_WriteU8() for consistency with the rest of the API.
Sam Lantinga <slouken@libsdl.org>
parents: 6644
diff changeset
   702
    Uint64 value = 0;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   703
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   704
    SDL_RWread(src, &value, (sizeof value), 1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   705
    return (SDL_SwapBE64(value));
1354
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   706
}
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   707
3253
5d7ef5970073 Fixed issues building 64-bit Windows binary
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   708
size_t
6655
6a3e741ea70d Added SDL_ReadU8() and SDL_WriteU8() for consistency with the rest of the API.
Sam Lantinga <slouken@libsdl.org>
parents: 6644
diff changeset
   709
SDL_WriteU8(SDL_RWops * dst, Uint8 value)
6a3e741ea70d Added SDL_ReadU8() and SDL_WriteU8() for consistency with the rest of the API.
Sam Lantinga <slouken@libsdl.org>
parents: 6644
diff changeset
   710
{
6a3e741ea70d Added SDL_ReadU8() and SDL_WriteU8() for consistency with the rest of the API.
Sam Lantinga <slouken@libsdl.org>
parents: 6644
diff changeset
   711
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
6a3e741ea70d Added SDL_ReadU8() and SDL_WriteU8() for consistency with the rest of the API.
Sam Lantinga <slouken@libsdl.org>
parents: 6644
diff changeset
   712
}
6a3e741ea70d Added SDL_ReadU8() and SDL_WriteU8() for consistency with the rest of the API.
Sam Lantinga <slouken@libsdl.org>
parents: 6644
diff changeset
   713
6a3e741ea70d Added SDL_ReadU8() and SDL_WriteU8() for consistency with the rest of the API.
Sam Lantinga <slouken@libsdl.org>
parents: 6644
diff changeset
   714
size_t
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   715
SDL_WriteLE16(SDL_RWops * dst, Uint16 value)
1354
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   716
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   717
    value = SDL_SwapLE16(value);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   718
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   719
}
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   720
3253
5d7ef5970073 Fixed issues building 64-bit Windows binary
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   721
size_t
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   722
SDL_WriteBE16(SDL_RWops * dst, Uint16 value)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   723
{
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   724
    value = SDL_SwapBE16(value);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   725
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
1354
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   726
}
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   727
3253
5d7ef5970073 Fixed issues building 64-bit Windows binary
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   728
size_t
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   729
SDL_WriteLE32(SDL_RWops * dst, Uint32 value)
1354
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   730
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   731
    value = SDL_SwapLE32(value);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   732
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
1354
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   733
}
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   734
3253
5d7ef5970073 Fixed issues building 64-bit Windows binary
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   735
size_t
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   736
SDL_WriteBE32(SDL_RWops * dst, Uint32 value)
1354
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   737
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   738
    value = SDL_SwapBE32(value);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   739
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   740
}
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   741
3253
5d7ef5970073 Fixed issues building 64-bit Windows binary
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   742
size_t
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   743
SDL_WriteLE64(SDL_RWops * dst, Uint64 value)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   744
{
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   745
    value = SDL_SwapLE64(value);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   746
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
1354
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   747
}
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   748
3253
5d7ef5970073 Fixed issues building 64-bit Windows binary
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   749
size_t
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   750
SDL_WriteBE64(SDL_RWops * dst, Uint64 value)
1354
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   751
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   752
    value = SDL_SwapBE64(value);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   753
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
1354
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   754
}
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   755
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   756
/* vi: set ts=4 sw=4 expandtab: */