project_files/Android-build/SDL-android-project/jni/SDL/src/file/SDL_rwops.c
author koda
Tue, 21 Jan 2014 22:43:06 +0100
changeset 10017 de822cd3df3a
parent 6619 229b99faf580
permissions -rwxr-xr-x
fixwhitespace and dos2unix
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6619
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
     1
/*
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
     2
  Simple DirectMedia Layer
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
     4
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
     5
  This software is provided 'as-is', without any express or implied
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
     7
  arising from the use of this software.
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
     8
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    11
  freely, subject to the following restrictions:
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    12
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    14
     claim that you wrote the original software. If you use this software
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    16
     appreciated but is not required.
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    18
     misrepresented as being the original software.
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    20
*/
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    21
#include "SDL_config.h"
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    22
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    23
/* This file provides a general interface for SDL to read and write
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    24
   data sources.  It can easily be extended to files, memory, etc.
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    25
*/
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    26
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    27
#include "SDL_endian.h"
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    28
#include "SDL_rwops.h"
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    29
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    30
#ifdef __APPLE__
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    31
#include "cocoa/SDL_rwopsbundlesupport.h"
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    32
#endif /* __APPLE__ */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    33
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    34
#ifdef ANDROID
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    35
#include "../core/android/SDL_android.h"
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    36
#include <android/log.h>
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    37
#endif
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    38
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    39
#ifdef __NDS__
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    40
/* include libfat headers for fatInitDefault(). */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    41
#include <fat.h>
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    42
#endif /* __NDS__ */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    43
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    44
#ifdef __WIN32__
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    45
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    46
/* Functions to read/write Win32 API file pointers */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    47
/* Will not use it on WinCE because stdio is buffered, it means
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 6619
diff changeset
    48
   faster, and all stdio functions anyway are embedded in coredll.dll -
6619
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    49
   the main wince dll*/
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    50
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    51
#include "../core/windows/SDL_windows.h"
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    52
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    53
#ifndef INVALID_SET_FILE_POINTER
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    54
#define INVALID_SET_FILE_POINTER 0xFFFFFFFF
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    55
#endif
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    56
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 6619
diff changeset
    57
#define READAHEAD_BUFFER_SIZE   1024
6619
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    58
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    59
static int SDLCALL
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    60
windows_file_open(SDL_RWops * context, const char *filename, const char *mode)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    61
{
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    62
#ifndef _WIN32_WCE
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    63
    UINT old_error_mode;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    64
#endif
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    65
    HANDLE h;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    66
    DWORD r_right, w_right;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    67
    DWORD must_exist, truncate;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    68
    int a_mode;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    69
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    70
    if (!context)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    71
        return -1;              /* failed (invalid call) */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    72
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    73
    context->hidden.windowsio.h = INVALID_HANDLE_VALUE;   /* mark this as unusable */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    74
    context->hidden.windowsio.buffer.data = NULL;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    75
    context->hidden.windowsio.buffer.size = 0;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    76
    context->hidden.windowsio.buffer.left = 0;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    77
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    78
    /* "r" = reading, file must exist */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    79
    /* "w" = writing, truncate existing, file may not exist */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    80
    /* "r+"= reading or writing, file must exist            */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    81
    /* "a" = writing, append file may not exist             */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    82
    /* "a+"= append + read, file may not exist              */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    83
    /* "w+" = read, write, truncate. file may not exist    */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    84
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    85
    must_exist = (SDL_strchr(mode, 'r') != NULL) ? OPEN_EXISTING : 0;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    86
    truncate = (SDL_strchr(mode, 'w') != NULL) ? CREATE_ALWAYS : 0;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    87
    r_right = (SDL_strchr(mode, '+') != NULL
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    88
               || must_exist) ? GENERIC_READ : 0;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    89
    a_mode = (SDL_strchr(mode, 'a') != NULL) ? OPEN_ALWAYS : 0;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    90
    w_right = (a_mode || SDL_strchr(mode, '+')
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    91
               || truncate) ? GENERIC_WRITE : 0;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    92
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    93
    if (!r_right && !w_right)   /* inconsistent mode */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    94
        return -1;              /* failed (invalid call) */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    95
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    96
    context->hidden.windowsio.buffer.data =
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    97
        (char *) SDL_malloc(READAHEAD_BUFFER_SIZE);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    98
    if (!context->hidden.windowsio.buffer.data) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
    99
        SDL_OutOfMemory();
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   100
        return -1;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   101
    }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   102
#ifdef _WIN32_WCE
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   103
    {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   104
        LPTSTR tstr = WIN_UTF8ToString(filename);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   105
        h = CreateFile(tstr, (w_right | r_right),
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   106
                       (w_right) ? 0 : FILE_SHARE_READ, NULL,
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   107
                       (must_exist | truncate | a_mode),
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   108
                       FILE_ATTRIBUTE_NORMAL, NULL);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   109
        SDL_free(tstr);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   110
    }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   111
#else
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   112
    /* Do not open a dialog box if failure */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   113
    old_error_mode =
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   114
        SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   115
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   116
    {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   117
        LPTSTR tstr = WIN_UTF8ToString(filename);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   118
        h = CreateFile(tstr, (w_right | r_right),
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   119
                       (w_right) ? 0 : FILE_SHARE_READ, NULL,
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   120
                       (must_exist | truncate | a_mode),
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   121
                       FILE_ATTRIBUTE_NORMAL, NULL);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   122
        SDL_free(tstr);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   123
    }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   124
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   125
    /* restore old behavior */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   126
    SetErrorMode(old_error_mode);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   127
#endif /* _WIN32_WCE */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   128
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   129
    if (h == INVALID_HANDLE_VALUE) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   130
        SDL_free(context->hidden.windowsio.buffer.data);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   131
        context->hidden.windowsio.buffer.data = NULL;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   132
        SDL_SetError("Couldn't open %s", filename);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   133
        return -2;              /* failed (CreateFile) */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   134
    }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   135
    context->hidden.windowsio.h = h;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   136
    context->hidden.windowsio.append = a_mode ? SDL_TRUE : SDL_FALSE;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   137
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   138
    return 0;                   /* ok */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   139
}
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   140
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   141
static long SDLCALL
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   142
windows_file_seek(SDL_RWops * context, long offset, int whence)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   143
{
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   144
    DWORD windowswhence;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   145
    long file_pos;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   146
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   147
    if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   148
        SDL_SetError("windows_file_seek: invalid context/file not opened");
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   149
        return -1;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   150
    }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   151
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   152
    /* FIXME: We may be able to satisfy the seek within buffered data */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   153
    if (whence == RW_SEEK_CUR && context->hidden.windowsio.buffer.left) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   154
        offset -= (long)context->hidden.windowsio.buffer.left;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   155
    }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   156
    context->hidden.windowsio.buffer.left = 0;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   157
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   158
    switch (whence) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   159
    case RW_SEEK_SET:
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   160
        windowswhence = FILE_BEGIN;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   161
        break;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   162
    case RW_SEEK_CUR:
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   163
        windowswhence = FILE_CURRENT;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   164
        break;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   165
    case RW_SEEK_END:
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   166
        windowswhence = FILE_END;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   167
        break;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   168
    default:
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   169
        SDL_SetError("windows_file_seek: Unknown value for 'whence'");
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   170
        return -1;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   171
    }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   172
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   173
    file_pos =
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   174
        SetFilePointer(context->hidden.windowsio.h, offset, NULL, windowswhence);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   175
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   176
    if (file_pos != INVALID_SET_FILE_POINTER)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   177
        return file_pos;        /* success */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   178
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   179
    SDL_Error(SDL_EFSEEK);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   180
    return -1;                  /* error */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   181
}
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   182
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   183
static size_t SDLCALL
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   184
windows_file_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   185
{
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   186
    size_t total_need;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   187
    size_t total_read = 0;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   188
    size_t read_ahead;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   189
    DWORD byte_read;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   190
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   191
    total_need = size * maxnum;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   192
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   193
    if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   194
        || !total_need)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   195
        return 0;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   196
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   197
    if (context->hidden.windowsio.buffer.left > 0) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   198
        void *data = (char *) context->hidden.windowsio.buffer.data +
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   199
            context->hidden.windowsio.buffer.size -
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   200
            context->hidden.windowsio.buffer.left;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   201
        read_ahead =
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   202
            SDL_min(total_need, context->hidden.windowsio.buffer.left);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   203
        SDL_memcpy(ptr, data, read_ahead);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   204
        context->hidden.windowsio.buffer.left -= read_ahead;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   205
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   206
        if (read_ahead == total_need) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   207
            return maxnum;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   208
        }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   209
        ptr = (char *) ptr + read_ahead;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   210
        total_need -= read_ahead;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   211
        total_read += read_ahead;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   212
    }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   213
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   214
    if (total_need < READAHEAD_BUFFER_SIZE) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   215
        if (!ReadFile
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   216
            (context->hidden.windowsio.h, context->hidden.windowsio.buffer.data,
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   217
             READAHEAD_BUFFER_SIZE, &byte_read, NULL)) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   218
            SDL_Error(SDL_EFREAD);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   219
            return 0;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   220
        }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   221
        read_ahead = SDL_min(total_need, (int) byte_read);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   222
        SDL_memcpy(ptr, context->hidden.windowsio.buffer.data, read_ahead);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   223
        context->hidden.windowsio.buffer.size = byte_read;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   224
        context->hidden.windowsio.buffer.left = byte_read - read_ahead;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   225
        total_read += read_ahead;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   226
    } else {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   227
        if (!ReadFile
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   228
            (context->hidden.windowsio.h, ptr, (DWORD)total_need, &byte_read, NULL)) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   229
            SDL_Error(SDL_EFREAD);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   230
            return 0;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   231
        }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   232
        total_read += byte_read;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   233
    }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   234
    return (total_read / size);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   235
}
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   236
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   237
static size_t SDLCALL
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   238
windows_file_write(SDL_RWops * context, const void *ptr, size_t size,
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   239
                 size_t num)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   240
{
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   241
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   242
    size_t total_bytes;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   243
    DWORD byte_written;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   244
    size_t nwritten;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   245
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   246
    total_bytes = size * num;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   247
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   248
    if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   249
        || total_bytes <= 0 || !size)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   250
        return 0;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   251
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   252
    if (context->hidden.windowsio.buffer.left) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   253
        SetFilePointer(context->hidden.windowsio.h,
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   254
                       -(LONG)context->hidden.windowsio.buffer.left, NULL,
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   255
                       FILE_CURRENT);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   256
        context->hidden.windowsio.buffer.left = 0;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   257
    }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   258
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   259
    /* if in append mode, we must go to the EOF before write */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   260
    if (context->hidden.windowsio.append) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   261
        if (SetFilePointer(context->hidden.windowsio.h, 0L, NULL, FILE_END) ==
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   262
            INVALID_SET_FILE_POINTER) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   263
            SDL_Error(SDL_EFWRITE);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   264
            return 0;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   265
        }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   266
    }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   267
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   268
    if (!WriteFile
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   269
        (context->hidden.windowsio.h, ptr, (DWORD)total_bytes, &byte_written, NULL)) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   270
        SDL_Error(SDL_EFWRITE);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   271
        return 0;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   272
    }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   273
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   274
    nwritten = byte_written / size;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   275
    return nwritten;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   276
}
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   277
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   278
static int SDLCALL
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   279
windows_file_close(SDL_RWops * context)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   280
{
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   281
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   282
    if (context) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   283
        if (context->hidden.windowsio.h != INVALID_HANDLE_VALUE) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   284
            CloseHandle(context->hidden.windowsio.h);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   285
            context->hidden.windowsio.h = INVALID_HANDLE_VALUE;   /* to be sure */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   286
        }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   287
        if (context->hidden.windowsio.buffer.data) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   288
            SDL_free(context->hidden.windowsio.buffer.data);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   289
            context->hidden.windowsio.buffer.data = NULL;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   290
        }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   291
        SDL_FreeRW(context);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   292
    }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   293
    return (0);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   294
}
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   295
#endif /* __WIN32__ */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   296
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   297
#ifdef HAVE_STDIO_H
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   298
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   299
/* Functions to read/write stdio file pointers */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   300
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   301
static long SDLCALL
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   302
stdio_seek(SDL_RWops * context, long offset, int whence)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   303
{
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   304
    if (fseek(context->hidden.stdio.fp, offset, whence) == 0) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   305
        return (ftell(context->hidden.stdio.fp));
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   306
    } else {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   307
        SDL_Error(SDL_EFSEEK);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   308
        return (-1);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   309
    }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   310
}
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   311
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   312
static size_t SDLCALL
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   313
stdio_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   314
{
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   315
    size_t nread;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   316
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   317
    nread = fread(ptr, size, maxnum, context->hidden.stdio.fp);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   318
    if (nread == 0 && ferror(context->hidden.stdio.fp)) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   319
        SDL_Error(SDL_EFREAD);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   320
    }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   321
    return (nread);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   322
}
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   323
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   324
static size_t SDLCALL
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   325
stdio_write(SDL_RWops * context, const void *ptr, size_t size, size_t num)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   326
{
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   327
    size_t nwrote;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   328
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   329
    nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   330
    if (nwrote == 0 && ferror(context->hidden.stdio.fp)) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   331
        SDL_Error(SDL_EFWRITE);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   332
    }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   333
    return (nwrote);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   334
}
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   335
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   336
static int SDLCALL
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   337
stdio_close(SDL_RWops * context)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   338
{
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   339
    int status = 0;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   340
    if (context) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   341
        if (context->hidden.stdio.autoclose) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   342
            /* WARNING:  Check the return value here! */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   343
            if (fclose(context->hidden.stdio.fp) != 0) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   344
                SDL_Error(SDL_EFWRITE);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   345
                status = -1;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   346
            }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   347
        }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   348
        SDL_FreeRW(context);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   349
    }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   350
    return status;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   351
}
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   352
#endif /* !HAVE_STDIO_H */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   353
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   354
/* Functions to read/write memory pointers */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   355
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   356
static long SDLCALL
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   357
mem_seek(SDL_RWops * context, long offset, int whence)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   358
{
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   359
    Uint8 *newpos;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   360
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   361
    switch (whence) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   362
    case RW_SEEK_SET:
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   363
        newpos = context->hidden.mem.base + offset;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   364
        break;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   365
    case RW_SEEK_CUR:
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   366
        newpos = context->hidden.mem.here + offset;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   367
        break;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   368
    case RW_SEEK_END:
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   369
        newpos = context->hidden.mem.stop + offset;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   370
        break;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   371
    default:
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   372
        SDL_SetError("Unknown value for 'whence'");
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   373
        return (-1);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   374
    }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   375
    if (newpos < context->hidden.mem.base) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   376
        newpos = context->hidden.mem.base;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   377
    }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   378
    if (newpos > context->hidden.mem.stop) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   379
        newpos = context->hidden.mem.stop;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   380
    }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   381
    context->hidden.mem.here = newpos;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   382
    return (long)(context->hidden.mem.here - context->hidden.mem.base);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   383
}
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   384
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   385
static size_t SDLCALL
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   386
mem_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   387
{
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   388
    size_t total_bytes;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   389
    size_t mem_available;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   390
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   391
    total_bytes = (maxnum * size);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   392
    if ((maxnum <= 0) || (size <= 0)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   393
        || ((total_bytes / maxnum) != (size_t) size)) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   394
        return 0;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   395
    }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   396
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   397
    mem_available = (context->hidden.mem.stop - context->hidden.mem.here);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   398
    if (total_bytes > mem_available) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   399
        total_bytes = mem_available;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   400
    }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   401
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   402
    SDL_memcpy(ptr, context->hidden.mem.here, total_bytes);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   403
    context->hidden.mem.here += total_bytes;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   404
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   405
    return (total_bytes / size);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   406
}
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   407
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   408
static size_t SDLCALL
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   409
mem_write(SDL_RWops * context, const void *ptr, size_t size, size_t num)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   410
{
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   411
    if ((context->hidden.mem.here + (num * size)) > context->hidden.mem.stop) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   412
        num = (context->hidden.mem.stop - context->hidden.mem.here) / size;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   413
    }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   414
    SDL_memcpy(context->hidden.mem.here, ptr, num * size);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   415
    context->hidden.mem.here += num * size;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   416
    return (num);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   417
}
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   418
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   419
static size_t SDLCALL
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   420
mem_writeconst(SDL_RWops * context, const void *ptr, size_t size, size_t num)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   421
{
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   422
    SDL_SetError("Can't write to read-only memory");
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   423
    return (-1);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   424
}
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   425
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   426
static int SDLCALL
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   427
mem_close(SDL_RWops * context)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   428
{
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   429
    if (context) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   430
        SDL_FreeRW(context);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   431
    }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   432
    return (0);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   433
}
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   434
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   435
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   436
/* Functions to create SDL_RWops structures from various data sources */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   437
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   438
SDL_RWops *
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   439
SDL_RWFromFile(const char *file, const char *mode)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   440
{
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   441
    SDL_RWops *rwops = NULL;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   442
#ifdef HAVE_STDIO_H
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   443
    FILE *fp = NULL;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   444
#endif
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   445
    if (!file || !*file || !mode || !*mode) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   446
        SDL_SetError("SDL_RWFromFile(): No file or no mode specified");
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   447
        return NULL;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   448
    }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   449
#if defined(ANDROIDXELI) //Xeli: dont use the android assets, TODO create fallback system
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   450
    rwops = SDL_AllocRW();
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   451
    if (!rwops)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   452
        return NULL;            /* SDL_SetError already setup by SDL_AllocRW() */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   453
    if (Android_JNI_FileOpen(rwops, file, mode) < 0) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   454
        SDL_FreeRW(rwops);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   455
        return NULL;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   456
    }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   457
    rwops->seek = Android_JNI_FileSeek;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   458
    rwops->read = Android_JNI_FileRead;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   459
    rwops->write = Android_JNI_FileWrite;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   460
    rwops->close = Android_JNI_FileClose;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   461
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   462
#elif defined(__WIN32__)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   463
    rwops = SDL_AllocRW();
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   464
    if (!rwops)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   465
        return NULL;            /* SDL_SetError already setup by SDL_AllocRW() */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   466
    if (windows_file_open(rwops, file, mode) < 0) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   467
        SDL_FreeRW(rwops);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   468
        return NULL;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   469
    }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   470
    rwops->seek = windows_file_seek;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   471
    rwops->read = windows_file_read;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   472
    rwops->write = windows_file_write;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   473
    rwops->close = windows_file_close;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   474
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   475
#elif HAVE_STDIO_H
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 6619
diff changeset
   476
    #ifdef __APPLE__
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 6619
diff changeset
   477
    fp = SDL_OpenFPFromBundleOrFallback(file, mode);
6619
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   478
    #else
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 6619
diff changeset
   479
    fp = fopen(file, mode);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 6619
diff changeset
   480
    #endif
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 6619
diff changeset
   481
    if (fp == NULL) {
6619
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   482
        SDL_SetError("Couldn't open %s", file);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   483
    } else {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   484
        rwops = SDL_RWFromFP(fp, 1);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   485
    }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   486
#else
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   487
    SDL_SetError("SDL not compiled with stdio support");
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   488
#endif /* !HAVE_STDIO_H */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   489
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   490
    return (rwops);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   491
}
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   492
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   493
#ifdef HAVE_STDIO_H
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   494
SDL_RWops *
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   495
SDL_RWFromFP(FILE * fp, SDL_bool autoclose)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   496
{
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   497
    SDL_RWops *rwops = NULL;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   498
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   499
#if 0
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   500
/*#ifdef __NDS__*/
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   501
    /* set it up so we can use stdio file function */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   502
    fatInitDefault();
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   503
    printf("called fatInitDefault()");
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   504
#endif /* __NDS__ */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   505
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   506
    rwops = SDL_AllocRW();
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   507
    if (rwops != NULL) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   508
        rwops->seek = stdio_seek;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   509
        rwops->read = stdio_read;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   510
        rwops->write = stdio_write;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   511
        rwops->close = stdio_close;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   512
        rwops->hidden.stdio.fp = fp;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   513
        rwops->hidden.stdio.autoclose = autoclose;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   514
    }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   515
    return (rwops);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   516
}
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   517
#else
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   518
SDL_RWops *
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   519
SDL_RWFromFP(void * fp, SDL_bool autoclose)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   520
{
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   521
    SDL_SetError("SDL not compiled with stdio support");
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   522
    return NULL;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   523
}
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   524
#endif /* HAVE_STDIO_H */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   525
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   526
SDL_RWops *
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   527
SDL_RWFromMem(void *mem, int size)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   528
{
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   529
    SDL_RWops *rwops;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   530
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   531
    rwops = SDL_AllocRW();
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   532
    if (rwops != NULL) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   533
        rwops->seek = mem_seek;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   534
        rwops->read = mem_read;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   535
        rwops->write = mem_write;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   536
        rwops->close = mem_close;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   537
        rwops->hidden.mem.base = (Uint8 *) mem;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   538
        rwops->hidden.mem.here = rwops->hidden.mem.base;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   539
        rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   540
    }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   541
    return (rwops);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   542
}
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   543
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   544
SDL_RWops *
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   545
SDL_RWFromConstMem(const void *mem, int size)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   546
{
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   547
    SDL_RWops *rwops;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   548
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   549
    rwops = SDL_AllocRW();
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   550
    if (rwops != NULL) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   551
        rwops->seek = mem_seek;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   552
        rwops->read = mem_read;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   553
        rwops->write = mem_writeconst;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   554
        rwops->close = mem_close;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   555
        rwops->hidden.mem.base = (Uint8 *) mem;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   556
        rwops->hidden.mem.here = rwops->hidden.mem.base;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   557
        rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   558
    }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   559
    return (rwops);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   560
}
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   561
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   562
SDL_RWops *
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   563
SDL_AllocRW(void)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   564
{
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   565
    SDL_RWops *area;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   566
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   567
    area = (SDL_RWops *) SDL_malloc(sizeof *area);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   568
    if (area == NULL) {
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   569
        SDL_OutOfMemory();
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   570
    }
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   571
    return (area);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   572
}
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   573
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   574
void
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   575
SDL_FreeRW(SDL_RWops * area)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   576
{
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   577
    SDL_free(area);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   578
}
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   579
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   580
/* Functions for dynamically reading and writing endian-specific values */
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   581
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   582
Uint16
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   583
SDL_ReadLE16(SDL_RWops * src)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   584
{
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   585
    Uint16 value;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   586
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   587
    SDL_RWread(src, &value, (sizeof value), 1);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   588
    return (SDL_SwapLE16(value));
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   589
}
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   590
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   591
Uint16
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   592
SDL_ReadBE16(SDL_RWops * src)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   593
{
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   594
    Uint16 value;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   595
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   596
    SDL_RWread(src, &value, (sizeof value), 1);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   597
    return (SDL_SwapBE16(value));
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   598
}
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   599
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   600
Uint32
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   601
SDL_ReadLE32(SDL_RWops * src)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   602
{
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   603
    Uint32 value;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   604
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   605
    SDL_RWread(src, &value, (sizeof value), 1);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   606
    return (SDL_SwapLE32(value));
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   607
}
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   608
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   609
Uint32
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   610
SDL_ReadBE32(SDL_RWops * src)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   611
{
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   612
    Uint32 value;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   613
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   614
    SDL_RWread(src, &value, (sizeof value), 1);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   615
    return (SDL_SwapBE32(value));
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   616
}
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   617
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   618
Uint64
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   619
SDL_ReadLE64(SDL_RWops * src)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   620
{
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   621
    Uint64 value;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   622
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   623
    SDL_RWread(src, &value, (sizeof value), 1);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   624
    return (SDL_SwapLE64(value));
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   625
}
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   626
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   627
Uint64
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   628
SDL_ReadBE64(SDL_RWops * src)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   629
{
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   630
    Uint64 value;
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   631
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   632
    SDL_RWread(src, &value, (sizeof value), 1);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   633
    return (SDL_SwapBE64(value));
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   634
}
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   635
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   636
size_t
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   637
SDL_WriteLE16(SDL_RWops * dst, Uint16 value)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   638
{
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   639
    value = SDL_SwapLE16(value);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   640
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   641
}
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   642
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   643
size_t
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   644
SDL_WriteBE16(SDL_RWops * dst, Uint16 value)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   645
{
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   646
    value = SDL_SwapBE16(value);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   647
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   648
}
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   649
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   650
size_t
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   651
SDL_WriteLE32(SDL_RWops * dst, Uint32 value)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   652
{
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   653
    value = SDL_SwapLE32(value);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   654
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   655
}
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   656
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   657
size_t
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   658
SDL_WriteBE32(SDL_RWops * dst, Uint32 value)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   659
{
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   660
    value = SDL_SwapBE32(value);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   661
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   662
}
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   663
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   664
size_t
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   665
SDL_WriteLE64(SDL_RWops * dst, Uint64 value)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   666
{
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   667
    value = SDL_SwapLE64(value);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   668
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   669
}
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   670
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   671
size_t
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   672
SDL_WriteBE64(SDL_RWops * dst, Uint64 value)
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   673
{
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   674
    value = SDL_SwapBE64(value);
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   675
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   676
}
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   677
229b99faf580 disable using files from the assets dir, and use fopen()
Xeli
parents:
diff changeset
   678
/* vi: set ts=4 sw=4 expandtab: */