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