src/file/SDL_rwops.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 02 Nov 2012 03:08:40 -0700
changeset 6633 e36b94438fcf
parent 6629 8e2c731103e6
child 6642 b716011e53ce
permissions -rw-r--r--
Try opening relative path files from internal storage.

I'm not falling back to external storage because the application should be
aware of whether external storage is available and choose whether or not to
use it.
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@2160
   124
static long SDLCALL
slouken@5062
   125
windows_file_seek(SDL_RWops * context, long offset, int whence)
slouken@1465
   126
{
slouken@5062
   127
    DWORD windowswhence;
slouken@2160
   128
    long file_pos;
slouken@1446
   129
slouken@5062
   130
    if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE) {
slouken@5062
   131
        SDL_SetError("windows_file_seek: invalid context/file not opened");
slouken@1895
   132
        return -1;
slouken@1895
   133
    }
slouken@1895
   134
slouken@2159
   135
    /* FIXME: We may be able to satisfy the seek within buffered data */
slouken@5062
   136
    if (whence == RW_SEEK_CUR && context->hidden.windowsio.buffer.left) {
slouken@5062
   137
        offset -= (long)context->hidden.windowsio.buffer.left;
slouken@2159
   138
    }
slouken@5062
   139
    context->hidden.windowsio.buffer.left = 0;
slouken@2159
   140
slouken@1895
   141
    switch (whence) {
slouken@1895
   142
    case RW_SEEK_SET:
slouken@5062
   143
        windowswhence = FILE_BEGIN;
slouken@1895
   144
        break;
slouken@1895
   145
    case RW_SEEK_CUR:
slouken@5062
   146
        windowswhence = FILE_CURRENT;
slouken@1895
   147
        break;
slouken@1895
   148
    case RW_SEEK_END:
slouken@5062
   149
        windowswhence = FILE_END;
slouken@1895
   150
        break;
slouken@1895
   151
    default:
slouken@5062
   152
        SDL_SetError("windows_file_seek: Unknown value for 'whence'");
slouken@1895
   153
        return -1;
slouken@1895
   154
    }
slouken@1895
   155
slouken@1895
   156
    file_pos =
slouken@5062
   157
        SetFilePointer(context->hidden.windowsio.h, offset, NULL, windowswhence);
slouken@1895
   158
slouken@1895
   159
    if (file_pos != INVALID_SET_FILE_POINTER)
slouken@1895
   160
        return file_pos;        /* success */
slouken@1895
   161
slouken@1895
   162
    SDL_Error(SDL_EFSEEK);
slouken@1895
   163
    return -1;                  /* error */
slouken@1446
   164
}
slouken@2735
   165
slouken@2160
   166
static size_t SDLCALL
slouken@5062
   167
windows_file_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
slouken@1465
   168
{
slouken@2160
   169
    size_t total_need;
slouken@2160
   170
    size_t total_read = 0;
slouken@2160
   171
    size_t read_ahead;
slouken@2159
   172
    DWORD byte_read;
slouken@1895
   173
slouken@2159
   174
    total_need = size * maxnum;
slouken@1895
   175
slouken@5062
   176
    if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE
slouken@2160
   177
        || !total_need)
slouken@1895
   178
        return 0;
slouken@1895
   179
slouken@5062
   180
    if (context->hidden.windowsio.buffer.left > 0) {
slouken@5062
   181
        void *data = (char *) context->hidden.windowsio.buffer.data +
slouken@5062
   182
            context->hidden.windowsio.buffer.size -
slouken@5062
   183
            context->hidden.windowsio.buffer.left;
bob@2185
   184
        read_ahead =
slouken@5062
   185
            SDL_min(total_need, context->hidden.windowsio.buffer.left);
slouken@2159
   186
        SDL_memcpy(ptr, data, read_ahead);
slouken@5062
   187
        context->hidden.windowsio.buffer.left -= read_ahead;
slouken@2159
   188
slouken@2159
   189
        if (read_ahead == total_need) {
slouken@2159
   190
            return maxnum;
slouken@2159
   191
        }
slouken@2159
   192
        ptr = (char *) ptr + read_ahead;
slouken@2159
   193
        total_need -= read_ahead;
slouken@2159
   194
        total_read += read_ahead;
slouken@1895
   195
    }
slouken@2159
   196
slouken@2159
   197
    if (total_need < READAHEAD_BUFFER_SIZE) {
slouken@2159
   198
        if (!ReadFile
slouken@5062
   199
            (context->hidden.windowsio.h, context->hidden.windowsio.buffer.data,
slouken@2159
   200
             READAHEAD_BUFFER_SIZE, &byte_read, NULL)) {
slouken@2159
   201
            SDL_Error(SDL_EFREAD);
slouken@2159
   202
            return 0;
slouken@2159
   203
        }
slouken@2159
   204
        read_ahead = SDL_min(total_need, (int) byte_read);
slouken@5062
   205
        SDL_memcpy(ptr, context->hidden.windowsio.buffer.data, read_ahead);
slouken@5062
   206
        context->hidden.windowsio.buffer.size = byte_read;
slouken@5062
   207
        context->hidden.windowsio.buffer.left = byte_read - read_ahead;
slouken@2159
   208
        total_read += read_ahead;
slouken@2159
   209
    } else {
slouken@2159
   210
        if (!ReadFile
slouken@5062
   211
            (context->hidden.windowsio.h, ptr, (DWORD)total_need, &byte_read, NULL)) {
slouken@2159
   212
            SDL_Error(SDL_EFREAD);
slouken@2159
   213
            return 0;
slouken@2159
   214
        }
slouken@2159
   215
        total_read += byte_read;
slouken@2159
   216
    }
slouken@2159
   217
    return (total_read / size);
slouken@1446
   218
}
slouken@2735
   219
slouken@2160
   220
static size_t SDLCALL
slouken@5062
   221
windows_file_write(SDL_RWops * context, const void *ptr, size_t size,
slouken@2160
   222
                 size_t num)
slouken@1465
   223
{
slouken@1446
   224
slouken@2160
   225
    size_t total_bytes;
slouken@3253
   226
    DWORD byte_written;
slouken@3253
   227
    size_t nwritten;
slouken@1446
   228
slouken@1895
   229
    total_bytes = size * num;
slouken@1895
   230
slouken@5062
   231
    if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE
slouken@1895
   232
        || total_bytes <= 0 || !size)
slouken@1895
   233
        return 0;
slouken@1895
   234
slouken@5062
   235
    if (context->hidden.windowsio.buffer.left) {
slouken@5062
   236
        SetFilePointer(context->hidden.windowsio.h,
slouken@5062
   237
                       -(LONG)context->hidden.windowsio.buffer.left, NULL,
slouken@2159
   238
                       FILE_CURRENT);
slouken@5062
   239
        context->hidden.windowsio.buffer.left = 0;
slouken@2159
   240
    }
slouken@2159
   241
slouken@1895
   242
    /* if in append mode, we must go to the EOF before write */
slouken@5062
   243
    if (context->hidden.windowsio.append) {
slouken@5062
   244
        if (SetFilePointer(context->hidden.windowsio.h, 0L, NULL, FILE_END) ==
slouken@2159
   245
            INVALID_SET_FILE_POINTER) {
slouken@1895
   246
            SDL_Error(SDL_EFWRITE);
slouken@1895
   247
            return 0;
slouken@1895
   248
        }
slouken@1895
   249
    }
slouken@1895
   250
slouken@1895
   251
    if (!WriteFile
slouken@5062
   252
        (context->hidden.windowsio.h, ptr, (DWORD)total_bytes, &byte_written, NULL)) {
slouken@1895
   253
        SDL_Error(SDL_EFWRITE);
slouken@1895
   254
        return 0;
slouken@1895
   255
    }
slouken@1895
   256
slouken@1895
   257
    nwritten = byte_written / size;
slouken@1895
   258
    return nwritten;
slouken@1446
   259
}
slouken@2735
   260
slouken@1895
   261
static int SDLCALL
slouken@5062
   262
windows_file_close(SDL_RWops * context)
slouken@1465
   263
{
slouken@1895
   264
slouken@1895
   265
    if (context) {
slouken@5062
   266
        if (context->hidden.windowsio.h != INVALID_HANDLE_VALUE) {
slouken@5062
   267
            CloseHandle(context->hidden.windowsio.h);
slouken@5062
   268
            context->hidden.windowsio.h = INVALID_HANDLE_VALUE;   /* to be sure */
slouken@1895
   269
        }
slouken@5062
   270
        if (context->hidden.windowsio.buffer.data) {
slouken@5062
   271
            SDL_free(context->hidden.windowsio.buffer.data);
slouken@5062
   272
            context->hidden.windowsio.buffer.data = NULL;
slouken@2159
   273
        }
slouken@1895
   274
        SDL_FreeRW(context);
slouken@1895
   275
    }
slouken@1895
   276
    return (0);
slouken@1446
   277
}
slouken@5086
   278
#endif /* __WIN32__ */
slouken@1446
   279
slouken@1447
   280
#ifdef HAVE_STDIO_H
slouken@1446
   281
slouken@1447
   282
/* Functions to read/write stdio file pointers */
slouken@1446
   283
slouken@2160
   284
static long SDLCALL
slouken@2160
   285
stdio_seek(SDL_RWops * context, long offset, int whence)
slouken@1447
   286
{
slouken@1895
   287
    if (fseek(context->hidden.stdio.fp, offset, whence) == 0) {
slouken@1895
   288
        return (ftell(context->hidden.stdio.fp));
slouken@1895
   289
    } else {
slouken@1895
   290
        SDL_Error(SDL_EFSEEK);
slouken@1895
   291
        return (-1);
slouken@1895
   292
    }
slouken@1447
   293
}
slouken@2735
   294
slouken@2160
   295
static size_t SDLCALL
slouken@2160
   296
stdio_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
slouken@1447
   297
{
slouken@1895
   298
    size_t nread;
slouken@1447
   299
slouken@1895
   300
    nread = fread(ptr, size, maxnum, context->hidden.stdio.fp);
slouken@1895
   301
    if (nread == 0 && ferror(context->hidden.stdio.fp)) {
slouken@1895
   302
        SDL_Error(SDL_EFREAD);
slouken@1895
   303
    }
slouken@1895
   304
    return (nread);
slouken@1447
   305
}
slouken@2735
   306
slouken@2160
   307
static size_t SDLCALL
slouken@2160
   308
stdio_write(SDL_RWops * context, const void *ptr, size_t size, size_t num)
slouken@1447
   309
{
slouken@1895
   310
    size_t nwrote;
slouken@1447
   311
slouken@1895
   312
    nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
slouken@1895
   313
    if (nwrote == 0 && ferror(context->hidden.stdio.fp)) {
slouken@1895
   314
        SDL_Error(SDL_EFWRITE);
slouken@1895
   315
    }
slouken@1895
   316
    return (nwrote);
slouken@1447
   317
}
slouken@2735
   318
slouken@1895
   319
static int SDLCALL
slouken@1895
   320
stdio_close(SDL_RWops * context)
slouken@1447
   321
{
slouken@2160
   322
    int status = 0;
slouken@1895
   323
    if (context) {
slouken@1895
   324
        if (context->hidden.stdio.autoclose) {
slouken@1895
   325
            /* WARNING:  Check the return value here! */
slouken@2160
   326
            if (fclose(context->hidden.stdio.fp) != 0) {
slouken@2160
   327
                SDL_Error(SDL_EFWRITE);
slouken@2160
   328
                status = -1;
slouken@2160
   329
            }
slouken@1895
   330
        }
slouken@1895
   331
        SDL_FreeRW(context);
slouken@1895
   332
    }
slouken@2160
   333
    return status;
slouken@1447
   334
}
slouken@1446
   335
#endif /* !HAVE_STDIO_H */
slouken@1330
   336
slouken@0
   337
/* Functions to read/write memory pointers */
slouken@0
   338
slouken@2160
   339
static long SDLCALL
slouken@2160
   340
mem_seek(SDL_RWops * context, long offset, int whence)
slouken@0
   341
{
slouken@1895
   342
    Uint8 *newpos;
slouken@0
   343
slouken@1895
   344
    switch (whence) {
slouken@1895
   345
    case RW_SEEK_SET:
slouken@1895
   346
        newpos = context->hidden.mem.base + offset;
slouken@1895
   347
        break;
slouken@1895
   348
    case RW_SEEK_CUR:
slouken@1895
   349
        newpos = context->hidden.mem.here + offset;
slouken@1895
   350
        break;
slouken@1895
   351
    case RW_SEEK_END:
slouken@1895
   352
        newpos = context->hidden.mem.stop + offset;
slouken@1895
   353
        break;
slouken@1895
   354
    default:
slouken@1895
   355
        SDL_SetError("Unknown value for 'whence'");
slouken@1895
   356
        return (-1);
slouken@1895
   357
    }
slouken@1895
   358
    if (newpos < context->hidden.mem.base) {
slouken@1895
   359
        newpos = context->hidden.mem.base;
slouken@1895
   360
    }
slouken@1895
   361
    if (newpos > context->hidden.mem.stop) {
slouken@1895
   362
        newpos = context->hidden.mem.stop;
slouken@1895
   363
    }
slouken@1895
   364
    context->hidden.mem.here = newpos;
slouken@3253
   365
    return (long)(context->hidden.mem.here - context->hidden.mem.base);
slouken@0
   366
}
slouken@2735
   367
slouken@2160
   368
static size_t SDLCALL
slouken@2160
   369
mem_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
slouken@0
   370
{
slouken@1895
   371
    size_t total_bytes;
slouken@1895
   372
    size_t mem_available;
slouken@0
   373
slouken@1895
   374
    total_bytes = (maxnum * size);
slouken@1895
   375
    if ((maxnum <= 0) || (size <= 0)
slouken@1895
   376
        || ((total_bytes / maxnum) != (size_t) size)) {
slouken@1895
   377
        return 0;
slouken@1895
   378
    }
icculus@1078
   379
slouken@1895
   380
    mem_available = (context->hidden.mem.stop - context->hidden.mem.here);
slouken@1895
   381
    if (total_bytes > mem_available) {
slouken@1895
   382
        total_bytes = mem_available;
slouken@1895
   383
    }
icculus@1078
   384
slouken@1895
   385
    SDL_memcpy(ptr, context->hidden.mem.here, total_bytes);
slouken@1895
   386
    context->hidden.mem.here += total_bytes;
icculus@1078
   387
slouken@1895
   388
    return (total_bytes / size);
slouken@0
   389
}
slouken@2735
   390
slouken@2160
   391
static size_t SDLCALL
slouken@2160
   392
mem_write(SDL_RWops * context, const void *ptr, size_t size, size_t num)
slouken@0
   393
{
slouken@1895
   394
    if ((context->hidden.mem.here + (num * size)) > context->hidden.mem.stop) {
slouken@1895
   395
        num = (context->hidden.mem.stop - context->hidden.mem.here) / size;
slouken@1895
   396
    }
slouken@1895
   397
    SDL_memcpy(context->hidden.mem.here, ptr, num * size);
slouken@1895
   398
    context->hidden.mem.here += num * size;
slouken@1895
   399
    return (num);
slouken@0
   400
}
slouken@2735
   401
slouken@2160
   402
static size_t SDLCALL
slouken@2160
   403
mem_writeconst(SDL_RWops * context, const void *ptr, size_t size, size_t num)
slouken@764
   404
{
slouken@1895
   405
    SDL_SetError("Can't write to read-only memory");
slouken@1895
   406
    return (-1);
slouken@764
   407
}
slouken@2735
   408
slouken@1895
   409
static int SDLCALL
slouken@1895
   410
mem_close(SDL_RWops * context)
slouken@0
   411
{
slouken@1895
   412
    if (context) {
slouken@1895
   413
        SDL_FreeRW(context);
slouken@1895
   414
    }
slouken@1895
   415
    return (0);
slouken@0
   416
}
slouken@0
   417
slouken@1447
   418
slouken@0
   419
/* Functions to create SDL_RWops structures from various data sources */
slouken@0
   420
slouken@1895
   421
SDL_RWops *
slouken@1895
   422
SDL_RWFromFile(const char *file, const char *mode)
slouken@0
   423
{
slouken@1895
   424
    SDL_RWops *rwops = NULL;
slouken@1895
   425
    if (!file || !*file || !mode || !*mode) {
slouken@1895
   426
        SDL_SetError("SDL_RWFromFile(): No file or no mode specified");
slouken@1895
   427
        return NULL;
slouken@1895
   428
    }
icculus@5582
   429
#if defined(ANDROID)
slouken@6629
   430
#ifdef HAVE_STDIO_H
slouken@6629
   431
    /* Try to open the file on the filesystem first */
slouken@6633
   432
    if (*file == '/') {
slouken@6629
   433
        FILE *fp = fopen(file, mode);
slouken@6629
   434
        if (fp) {
slouken@6629
   435
            return SDL_RWFromFP(fp, 1);
slouken@6629
   436
        }
slouken@6633
   437
    } else {
slouken@6633
   438
        /* Try opening it from internal storage if it's a relative path */
slouken@6633
   439
        char *path;
slouken@6633
   440
        FILE *fp;
slouken@6633
   441
slouken@6633
   442
        path = SDL_stack_alloc(char, PATH_MAX);
slouken@6633
   443
        if (path) {
slouken@6633
   444
            SDL_snprintf(path, PATH_MAX, "%s/%s",
slouken@6633
   445
                         SDL_AndroidGetInternalStoragePath(), file);
slouken@6633
   446
            fp = fopen(path, mode);
slouken@6633
   447
            SDL_stack_free(path);
slouken@6633
   448
            if (fp) {
slouken@6633
   449
                return SDL_RWFromFP(fp, 1);
slouken@6633
   450
            }
slouken@6633
   451
        }
slouken@6629
   452
    }
slouken@6633
   453
#endif /* HAVE_STDIO_H */
slouken@6629
   454
slouken@6629
   455
    /* Try to open the file from the asset system */
icculus@5582
   456
    rwops = SDL_AllocRW();
icculus@5582
   457
    if (!rwops)
icculus@5582
   458
        return NULL;            /* SDL_SetError already setup by SDL_AllocRW() */
icculus@5582
   459
    if (Android_JNI_FileOpen(rwops, file, mode) < 0) {
icculus@5582
   460
        SDL_FreeRW(rwops);
icculus@5582
   461
        return NULL;
icculus@5582
   462
    }
icculus@5582
   463
    rwops->seek = Android_JNI_FileSeek;
icculus@5582
   464
    rwops->read = Android_JNI_FileRead;
icculus@5582
   465
    rwops->write = Android_JNI_FileWrite;
icculus@5582
   466
    rwops->close = Android_JNI_FileClose;
icculus@5582
   467
icculus@5582
   468
#elif defined(__WIN32__)
slouken@1895
   469
    rwops = SDL_AllocRW();
slouken@1895
   470
    if (!rwops)
slouken@1895
   471
        return NULL;            /* SDL_SetError already setup by SDL_AllocRW() */
slouken@5062
   472
    if (windows_file_open(rwops, file, mode) < 0) {
slouken@1895
   473
        SDL_FreeRW(rwops);
slouken@1895
   474
        return NULL;
slouken@1895
   475
    }
slouken@5062
   476
    rwops->seek = windows_file_seek;
slouken@5062
   477
    rwops->read = windows_file_read;
slouken@5062
   478
    rwops->write = windows_file_write;
slouken@5062
   479
    rwops->close = windows_file_close;
slouken@1447
   480
slouken@1447
   481
#elif HAVE_STDIO_H
icculus@6375
   482
    {
icculus@6375
   483
    	#ifdef __APPLE__
icculus@6375
   484
    	FILE *fp = SDL_OpenFPFromBundleOrFallback(file, mode);
icculus@6375
   485
        #else
icculus@6375
   486
    	FILE *fp = fopen(file, mode);
icculus@6375
   487
    	#endif
icculus@6375
   488
    	if (fp == NULL) {
icculus@6375
   489
            SDL_SetError("Couldn't open %s", file);
icculus@6375
   490
        } else {
icculus@6375
   491
            rwops = SDL_RWFromFP(fp, 1);
icculus@6375
   492
        }
slouken@1895
   493
    }
slouken@0
   494
#else
slouken@1895
   495
    SDL_SetError("SDL not compiled with stdio support");
slouken@1446
   496
#endif /* !HAVE_STDIO_H */
slouken@1447
   497
slouken@1895
   498
    return (rwops);
slouken@0
   499
}
slouken@0
   500
slouken@1330
   501
#ifdef HAVE_STDIO_H
slouken@1895
   502
SDL_RWops *
slouken@2161
   503
SDL_RWFromFP(FILE * fp, SDL_bool autoclose)
slouken@0
   504
{
slouken@1895
   505
    SDL_RWops *rwops = NULL;
slouken@0
   506
slouken@2735
   507
#if 0
slouken@2735
   508
/*#ifdef __NDS__*/
slouken@2735
   509
    /* set it up so we can use stdio file function */
slouken@2735
   510
    fatInitDefault();
slouken@2735
   511
    printf("called fatInitDefault()");
slouken@2735
   512
#endif /* __NDS__ */
slouken@2735
   513
slouken@1895
   514
    rwops = SDL_AllocRW();
slouken@1895
   515
    if (rwops != NULL) {
slouken@1895
   516
        rwops->seek = stdio_seek;
slouken@1895
   517
        rwops->read = stdio_read;
slouken@1895
   518
        rwops->write = stdio_write;
slouken@1895
   519
        rwops->close = stdio_close;
slouken@1895
   520
        rwops->hidden.stdio.fp = fp;
slouken@1895
   521
        rwops->hidden.stdio.autoclose = autoclose;
slouken@1895
   522
    }
slouken@1895
   523
    return (rwops);
slouken@0
   524
}
slouken@3564
   525
#else
slouken@3564
   526
SDL_RWops *
slouken@3564
   527
SDL_RWFromFP(void * fp, SDL_bool autoclose)
slouken@3564
   528
{
slouken@3564
   529
    SDL_SetError("SDL not compiled with stdio support");
slouken@3564
   530
    return NULL;
slouken@3564
   531
}
slouken@1330
   532
#endif /* HAVE_STDIO_H */
slouken@0
   533
slouken@1895
   534
SDL_RWops *
slouken@1895
   535
SDL_RWFromMem(void *mem, int size)
slouken@0
   536
{
slouken@1895
   537
    SDL_RWops *rwops;
slouken@0
   538
slouken@1895
   539
    rwops = SDL_AllocRW();
slouken@1895
   540
    if (rwops != NULL) {
slouken@1895
   541
        rwops->seek = mem_seek;
slouken@1895
   542
        rwops->read = mem_read;
slouken@1895
   543
        rwops->write = mem_write;
slouken@1895
   544
        rwops->close = mem_close;
slouken@1895
   545
        rwops->hidden.mem.base = (Uint8 *) mem;
slouken@1895
   546
        rwops->hidden.mem.here = rwops->hidden.mem.base;
slouken@1895
   547
        rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
slouken@1895
   548
    }
slouken@1895
   549
    return (rwops);
slouken@0
   550
}
slouken@0
   551
slouken@1895
   552
SDL_RWops *
slouken@1895
   553
SDL_RWFromConstMem(const void *mem, int size)
slouken@764
   554
{
slouken@1895
   555
    SDL_RWops *rwops;
slouken@764
   556
slouken@1895
   557
    rwops = SDL_AllocRW();
slouken@1895
   558
    if (rwops != NULL) {
slouken@1895
   559
        rwops->seek = mem_seek;
slouken@1895
   560
        rwops->read = mem_read;
slouken@1895
   561
        rwops->write = mem_writeconst;
slouken@1895
   562
        rwops->close = mem_close;
slouken@1895
   563
        rwops->hidden.mem.base = (Uint8 *) mem;
slouken@1895
   564
        rwops->hidden.mem.here = rwops->hidden.mem.base;
slouken@1895
   565
        rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
slouken@1895
   566
    }
slouken@1895
   567
    return (rwops);
slouken@764
   568
}
slouken@764
   569
slouken@1895
   570
SDL_RWops *
slouken@1895
   571
SDL_AllocRW(void)
slouken@0
   572
{
slouken@1895
   573
    SDL_RWops *area;
slouken@0
   574
slouken@1895
   575
    area = (SDL_RWops *) SDL_malloc(sizeof *area);
slouken@1895
   576
    if (area == NULL) {
slouken@1895
   577
        SDL_OutOfMemory();
slouken@1895
   578
    }
slouken@1895
   579
    return (area);
slouken@0
   580
}
slouken@0
   581
slouken@1895
   582
void
slouken@1895
   583
SDL_FreeRW(SDL_RWops * area)
slouken@0
   584
{
slouken@1895
   585
    SDL_free(area);
slouken@0
   586
}
slouken@1354
   587
slouken@1354
   588
/* Functions for dynamically reading and writing endian-specific values */
slouken@1354
   589
slouken@1895
   590
Uint16
slouken@1895
   591
SDL_ReadLE16(SDL_RWops * src)
slouken@1354
   592
{
slouken@1895
   593
    Uint16 value;
slouken@1354
   594
slouken@1895
   595
    SDL_RWread(src, &value, (sizeof value), 1);
slouken@1895
   596
    return (SDL_SwapLE16(value));
slouken@1354
   597
}
slouken@1354
   598
slouken@1895
   599
Uint16
slouken@1895
   600
SDL_ReadBE16(SDL_RWops * src)
slouken@1354
   601
{
slouken@1895
   602
    Uint16 value;
slouken@1895
   603
slouken@1895
   604
    SDL_RWread(src, &value, (sizeof value), 1);
slouken@1895
   605
    return (SDL_SwapBE16(value));
slouken@1354
   606
}
slouken@1895
   607
slouken@1895
   608
Uint32
slouken@1895
   609
SDL_ReadLE32(SDL_RWops * src)
slouken@1354
   610
{
slouken@1895
   611
    Uint32 value;
slouken@1895
   612
slouken@1895
   613
    SDL_RWread(src, &value, (sizeof value), 1);
slouken@1895
   614
    return (SDL_SwapLE32(value));
slouken@1354
   615
}
slouken@1895
   616
slouken@1895
   617
Uint32
slouken@1895
   618
SDL_ReadBE32(SDL_RWops * src)
slouken@1354
   619
{
slouken@1895
   620
    Uint32 value;
slouken@1895
   621
slouken@1895
   622
    SDL_RWread(src, &value, (sizeof value), 1);
slouken@1895
   623
    return (SDL_SwapBE32(value));
slouken@1354
   624
}
slouken@1895
   625
slouken@1895
   626
Uint64
slouken@1895
   627
SDL_ReadLE64(SDL_RWops * src)
slouken@1354
   628
{
slouken@1895
   629
    Uint64 value;
slouken@1895
   630
slouken@1895
   631
    SDL_RWread(src, &value, (sizeof value), 1);
slouken@1895
   632
    return (SDL_SwapLE64(value));
slouken@1354
   633
}
slouken@1895
   634
slouken@1895
   635
Uint64
slouken@1895
   636
SDL_ReadBE64(SDL_RWops * src)
slouken@1354
   637
{
slouken@1895
   638
    Uint64 value;
slouken@1895
   639
slouken@1895
   640
    SDL_RWread(src, &value, (sizeof value), 1);
slouken@1895
   641
    return (SDL_SwapBE64(value));
slouken@1354
   642
}
slouken@1895
   643
slouken@3253
   644
size_t
slouken@1895
   645
SDL_WriteLE16(SDL_RWops * dst, Uint16 value)
slouken@1354
   646
{
slouken@1895
   647
    value = SDL_SwapLE16(value);
slouken@1895
   648
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1354
   649
}
slouken@1895
   650
slouken@3253
   651
size_t
slouken@1895
   652
SDL_WriteBE16(SDL_RWops * dst, Uint16 value)
slouken@1895
   653
{
slouken@1895
   654
    value = SDL_SwapBE16(value);
slouken@1895
   655
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1895
   656
}
slouken@1895
   657
slouken@3253
   658
size_t
slouken@1895
   659
SDL_WriteLE32(SDL_RWops * dst, Uint32 value)
slouken@1895
   660
{
slouken@1895
   661
    value = SDL_SwapLE32(value);
slouken@1895
   662
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1895
   663
}
slouken@1895
   664
slouken@3253
   665
size_t
slouken@1895
   666
SDL_WriteBE32(SDL_RWops * dst, Uint32 value)
slouken@1895
   667
{
slouken@1895
   668
    value = SDL_SwapBE32(value);
slouken@1895
   669
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1895
   670
}
slouken@1895
   671
slouken@3253
   672
size_t
slouken@1895
   673
SDL_WriteLE64(SDL_RWops * dst, Uint64 value)
slouken@1895
   674
{
slouken@1895
   675
    value = SDL_SwapLE64(value);
slouken@1895
   676
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1895
   677
}
slouken@1895
   678
slouken@3253
   679
size_t
slouken@1895
   680
SDL_WriteBE64(SDL_RWops * dst, Uint64 value)
slouken@1895
   681
{
slouken@1895
   682
    value = SDL_SwapBE64(value);
slouken@1895
   683
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1895
   684
}
slouken@1895
   685
slouken@1895
   686
/* vi: set ts=4 sw=4 expandtab: */