src/file/SDL_rwops.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 20 Jul 2012 13:33:15 -0400
changeset 6375 93f9a24d1c02
parent 6138 4c64952a58fb
child 6430 48d519500f7e
permissions -rwxr-xr-x
Fixed some minor compiler warnings.
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@1465
    46
/* Will not use it on WinCE because stdio is buffered, it means
slouken@1465
    47
   faster, and all stdio functions anyway are embedded in coredll.dll - 
slouken@1465
    48
   the main wince dll*/
slouken@0
    49
slouken@5090
    50
#include "../core/windows/SDL_windows.h"
slouken@1446
    51
slouken@1465
    52
#ifndef INVALID_SET_FILE_POINTER
slouken@1465
    53
#define INVALID_SET_FILE_POINTER 0xFFFFFFFF
slouken@1465
    54
#endif
slouken@1465
    55
slouken@2159
    56
#define READAHEAD_BUFFER_SIZE	1024
slouken@2159
    57
slouken@1895
    58
static int SDLCALL
slouken@5062
    59
windows_file_open(SDL_RWops * context, const char *filename, const char *mode)
slouken@1465
    60
{
slouken@1465
    61
#ifndef _WIN32_WCE
slouken@1895
    62
    UINT old_error_mode;
slouken@1465
    63
#endif
slouken@1895
    64
    HANDLE h;
slouken@1895
    65
    DWORD r_right, w_right;
slouken@1895
    66
    DWORD must_exist, truncate;
slouken@1895
    67
    int a_mode;
slouken@1446
    68
slouken@1895
    69
    if (!context)
slouken@2164
    70
        return -1;              /* failed (invalid call) */
slouken@2159
    71
slouken@5062
    72
    context->hidden.windowsio.h = INVALID_HANDLE_VALUE;   /* mark this as unusable */
slouken@5062
    73
    context->hidden.windowsio.buffer.data = NULL;
slouken@5062
    74
    context->hidden.windowsio.buffer.size = 0;
slouken@5062
    75
    context->hidden.windowsio.buffer.left = 0;
icculus@2165
    76
slouken@1895
    77
    /* "r" = reading, file must exist */
slouken@1895
    78
    /* "w" = writing, truncate existing, file may not exist */
slouken@1895
    79
    /* "r+"= reading or writing, file must exist            */
slouken@1895
    80
    /* "a" = writing, append file may not exist             */
slouken@1895
    81
    /* "a+"= append + read, file may not exist              */
slouken@1895
    82
    /* "w+" = read, write, truncate. file may not exist    */
slouken@1895
    83
slouken@1895
    84
    must_exist = (SDL_strchr(mode, 'r') != NULL) ? OPEN_EXISTING : 0;
slouken@1895
    85
    truncate = (SDL_strchr(mode, 'w') != NULL) ? CREATE_ALWAYS : 0;
slouken@1895
    86
    r_right = (SDL_strchr(mode, '+') != NULL
slouken@1895
    87
               || must_exist) ? GENERIC_READ : 0;
slouken@1895
    88
    a_mode = (SDL_strchr(mode, 'a') != NULL) ? OPEN_ALWAYS : 0;
slouken@1895
    89
    w_right = (a_mode || SDL_strchr(mode, '+')
slouken@1895
    90
               || truncate) ? GENERIC_WRITE : 0;
slouken@1895
    91
slouken@1895
    92
    if (!r_right && !w_right)   /* inconsistent mode */
slouken@1895
    93
        return -1;              /* failed (invalid call) */
slouken@1465
    94
slouken@5062
    95
    context->hidden.windowsio.buffer.data =
slouken@2173
    96
        (char *) SDL_malloc(READAHEAD_BUFFER_SIZE);
slouken@5062
    97
    if (!context->hidden.windowsio.buffer.data) {
icculus@2165
    98
        SDL_OutOfMemory();
icculus@2165
    99
        return -1;
icculus@2165
   100
    }
slouken@1465
   101
#ifdef _WIN32_WCE
slouken@1895
   102
    {
slouken@5090
   103
        LPTSTR tstr = WIN_UTF8ToString(filename);
slouken@5090
   104
        h = CreateFile(tstr, (w_right | r_right),
slouken@1895
   105
                       (w_right) ? 0 : FILE_SHARE_READ, NULL,
slouken@1895
   106
                       (must_exist | truncate | a_mode),
slouken@1895
   107
                       FILE_ATTRIBUTE_NORMAL, NULL);
slouken@5090
   108
        SDL_free(tstr);
slouken@1895
   109
    }
slouken@1465
   110
#else
slouken@1895
   111
    /* Do not open a dialog box if failure */
slouken@1895
   112
    old_error_mode =
slouken@1895
   113
        SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
slouken@1465
   114
slouken@5090
   115
    {
slouken@5090
   116
        LPTSTR tstr = WIN_UTF8ToString(filename);
slouken@5090
   117
        h = CreateFile(tstr, (w_right | r_right),
slouken@5090
   118
                       (w_right) ? 0 : FILE_SHARE_READ, NULL,
slouken@5090
   119
                       (must_exist | truncate | a_mode),
slouken@5090
   120
                       FILE_ATTRIBUTE_NORMAL, NULL);
slouken@5090
   121
        SDL_free(tstr);
slouken@5090
   122
    }
slouken@1465
   123
slouken@5090
   124
    /* restore old behavior */
slouken@1895
   125
    SetErrorMode(old_error_mode);
slouken@1465
   126
#endif /* _WIN32_WCE */
slouken@1446
   127
slouken@1895
   128
    if (h == INVALID_HANDLE_VALUE) {
slouken@5062
   129
        SDL_free(context->hidden.windowsio.buffer.data);
slouken@5062
   130
        context->hidden.windowsio.buffer.data = NULL;
slouken@1895
   131
        SDL_SetError("Couldn't open %s", filename);
slouken@1895
   132
        return -2;              /* failed (CreateFile) */
slouken@1895
   133
    }
slouken@5062
   134
    context->hidden.windowsio.h = h;
slouken@5062
   135
    context->hidden.windowsio.append = a_mode ? SDL_TRUE : SDL_FALSE;
slouken@1895
   136
slouken@1895
   137
    return 0;                   /* ok */
slouken@1446
   138
}
slouken@2735
   139
slouken@2160
   140
static long SDLCALL
slouken@5062
   141
windows_file_seek(SDL_RWops * context, long offset, int whence)
slouken@1465
   142
{
slouken@5062
   143
    DWORD windowswhence;
slouken@2160
   144
    long file_pos;
slouken@1446
   145
slouken@5062
   146
    if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE) {
slouken@5062
   147
        SDL_SetError("windows_file_seek: invalid context/file not opened");
slouken@1895
   148
        return -1;
slouken@1895
   149
    }
slouken@1895
   150
slouken@2159
   151
    /* FIXME: We may be able to satisfy the seek within buffered data */
slouken@5062
   152
    if (whence == RW_SEEK_CUR && context->hidden.windowsio.buffer.left) {
slouken@5062
   153
        offset -= (long)context->hidden.windowsio.buffer.left;
slouken@2159
   154
    }
slouken@5062
   155
    context->hidden.windowsio.buffer.left = 0;
slouken@2159
   156
slouken@1895
   157
    switch (whence) {
slouken@1895
   158
    case RW_SEEK_SET:
slouken@5062
   159
        windowswhence = FILE_BEGIN;
slouken@1895
   160
        break;
slouken@1895
   161
    case RW_SEEK_CUR:
slouken@5062
   162
        windowswhence = FILE_CURRENT;
slouken@1895
   163
        break;
slouken@1895
   164
    case RW_SEEK_END:
slouken@5062
   165
        windowswhence = FILE_END;
slouken@1895
   166
        break;
slouken@1895
   167
    default:
slouken@5062
   168
        SDL_SetError("windows_file_seek: Unknown value for 'whence'");
slouken@1895
   169
        return -1;
slouken@1895
   170
    }
slouken@1895
   171
slouken@1895
   172
    file_pos =
slouken@5062
   173
        SetFilePointer(context->hidden.windowsio.h, offset, NULL, windowswhence);
slouken@1895
   174
slouken@1895
   175
    if (file_pos != INVALID_SET_FILE_POINTER)
slouken@1895
   176
        return file_pos;        /* success */
slouken@1895
   177
slouken@1895
   178
    SDL_Error(SDL_EFSEEK);
slouken@1895
   179
    return -1;                  /* error */
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@2160
   300
static long SDLCALL
slouken@2160
   301
stdio_seek(SDL_RWops * context, long offset, int whence)
slouken@1447
   302
{
slouken@1895
   303
    if (fseek(context->hidden.stdio.fp, offset, whence) == 0) {
slouken@1895
   304
        return (ftell(context->hidden.stdio.fp));
slouken@1895
   305
    } else {
slouken@1895
   306
        SDL_Error(SDL_EFSEEK);
slouken@1895
   307
        return (-1);
slouken@1895
   308
    }
slouken@1447
   309
}
slouken@2735
   310
slouken@2160
   311
static size_t SDLCALL
slouken@2160
   312
stdio_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
slouken@1447
   313
{
slouken@1895
   314
    size_t nread;
slouken@1447
   315
slouken@1895
   316
    nread = fread(ptr, size, maxnum, context->hidden.stdio.fp);
slouken@1895
   317
    if (nread == 0 && ferror(context->hidden.stdio.fp)) {
slouken@1895
   318
        SDL_Error(SDL_EFREAD);
slouken@1895
   319
    }
slouken@1895
   320
    return (nread);
slouken@1447
   321
}
slouken@2735
   322
slouken@2160
   323
static size_t SDLCALL
slouken@2160
   324
stdio_write(SDL_RWops * context, const void *ptr, size_t size, size_t num)
slouken@1447
   325
{
slouken@1895
   326
    size_t nwrote;
slouken@1447
   327
slouken@1895
   328
    nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
slouken@1895
   329
    if (nwrote == 0 && ferror(context->hidden.stdio.fp)) {
slouken@1895
   330
        SDL_Error(SDL_EFWRITE);
slouken@1895
   331
    }
slouken@1895
   332
    return (nwrote);
slouken@1447
   333
}
slouken@2735
   334
slouken@1895
   335
static int SDLCALL
slouken@1895
   336
stdio_close(SDL_RWops * context)
slouken@1447
   337
{
slouken@2160
   338
    int status = 0;
slouken@1895
   339
    if (context) {
slouken@1895
   340
        if (context->hidden.stdio.autoclose) {
slouken@1895
   341
            /* WARNING:  Check the return value here! */
slouken@2160
   342
            if (fclose(context->hidden.stdio.fp) != 0) {
slouken@2160
   343
                SDL_Error(SDL_EFWRITE);
slouken@2160
   344
                status = -1;
slouken@2160
   345
            }
slouken@1895
   346
        }
slouken@1895
   347
        SDL_FreeRW(context);
slouken@1895
   348
    }
slouken@2160
   349
    return status;
slouken@1447
   350
}
slouken@1446
   351
#endif /* !HAVE_STDIO_H */
slouken@1330
   352
slouken@0
   353
/* Functions to read/write memory pointers */
slouken@0
   354
slouken@2160
   355
static long SDLCALL
slouken@2160
   356
mem_seek(SDL_RWops * context, long offset, int whence)
slouken@0
   357
{
slouken@1895
   358
    Uint8 *newpos;
slouken@0
   359
slouken@1895
   360
    switch (whence) {
slouken@1895
   361
    case RW_SEEK_SET:
slouken@1895
   362
        newpos = context->hidden.mem.base + offset;
slouken@1895
   363
        break;
slouken@1895
   364
    case RW_SEEK_CUR:
slouken@1895
   365
        newpos = context->hidden.mem.here + offset;
slouken@1895
   366
        break;
slouken@1895
   367
    case RW_SEEK_END:
slouken@1895
   368
        newpos = context->hidden.mem.stop + offset;
slouken@1895
   369
        break;
slouken@1895
   370
    default:
slouken@1895
   371
        SDL_SetError("Unknown value for 'whence'");
slouken@1895
   372
        return (-1);
slouken@1895
   373
    }
slouken@1895
   374
    if (newpos < context->hidden.mem.base) {
slouken@1895
   375
        newpos = context->hidden.mem.base;
slouken@1895
   376
    }
slouken@1895
   377
    if (newpos > context->hidden.mem.stop) {
slouken@1895
   378
        newpos = context->hidden.mem.stop;
slouken@1895
   379
    }
slouken@1895
   380
    context->hidden.mem.here = newpos;
slouken@3253
   381
    return (long)(context->hidden.mem.here - context->hidden.mem.base);
slouken@0
   382
}
slouken@2735
   383
slouken@2160
   384
static size_t SDLCALL
slouken@2160
   385
mem_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
slouken@0
   386
{
slouken@1895
   387
    size_t total_bytes;
slouken@1895
   388
    size_t mem_available;
slouken@0
   389
slouken@1895
   390
    total_bytes = (maxnum * size);
slouken@1895
   391
    if ((maxnum <= 0) || (size <= 0)
slouken@1895
   392
        || ((total_bytes / maxnum) != (size_t) size)) {
slouken@1895
   393
        return 0;
slouken@1895
   394
    }
icculus@1078
   395
slouken@1895
   396
    mem_available = (context->hidden.mem.stop - context->hidden.mem.here);
slouken@1895
   397
    if (total_bytes > mem_available) {
slouken@1895
   398
        total_bytes = mem_available;
slouken@1895
   399
    }
icculus@1078
   400
slouken@1895
   401
    SDL_memcpy(ptr, context->hidden.mem.here, total_bytes);
slouken@1895
   402
    context->hidden.mem.here += total_bytes;
icculus@1078
   403
slouken@1895
   404
    return (total_bytes / size);
slouken@0
   405
}
slouken@2735
   406
slouken@2160
   407
static size_t SDLCALL
slouken@2160
   408
mem_write(SDL_RWops * context, const void *ptr, size_t size, size_t num)
slouken@0
   409
{
slouken@1895
   410
    if ((context->hidden.mem.here + (num * size)) > context->hidden.mem.stop) {
slouken@1895
   411
        num = (context->hidden.mem.stop - context->hidden.mem.here) / size;
slouken@1895
   412
    }
slouken@1895
   413
    SDL_memcpy(context->hidden.mem.here, ptr, num * size);
slouken@1895
   414
    context->hidden.mem.here += num * size;
slouken@1895
   415
    return (num);
slouken@0
   416
}
slouken@2735
   417
slouken@2160
   418
static size_t SDLCALL
slouken@2160
   419
mem_writeconst(SDL_RWops * context, const void *ptr, size_t size, size_t num)
slouken@764
   420
{
slouken@1895
   421
    SDL_SetError("Can't write to read-only memory");
slouken@1895
   422
    return (-1);
slouken@764
   423
}
slouken@2735
   424
slouken@1895
   425
static int SDLCALL
slouken@1895
   426
mem_close(SDL_RWops * context)
slouken@0
   427
{
slouken@1895
   428
    if (context) {
slouken@1895
   429
        SDL_FreeRW(context);
slouken@1895
   430
    }
slouken@1895
   431
    return (0);
slouken@0
   432
}
slouken@0
   433
slouken@1447
   434
slouken@0
   435
/* Functions to create SDL_RWops structures from various data sources */
slouken@0
   436
slouken@1895
   437
SDL_RWops *
slouken@1895
   438
SDL_RWFromFile(const char *file, const char *mode)
slouken@0
   439
{
slouken@1895
   440
    SDL_RWops *rwops = NULL;
slouken@1895
   441
    if (!file || !*file || !mode || !*mode) {
slouken@1895
   442
        SDL_SetError("SDL_RWFromFile(): No file or no mode specified");
slouken@1895
   443
        return NULL;
slouken@1895
   444
    }
icculus@5582
   445
#if defined(ANDROID)
icculus@5582
   446
    rwops = SDL_AllocRW();
icculus@5582
   447
    if (!rwops)
icculus@5582
   448
        return NULL;            /* SDL_SetError already setup by SDL_AllocRW() */
icculus@5582
   449
    if (Android_JNI_FileOpen(rwops, file, mode) < 0) {
icculus@5582
   450
        SDL_FreeRW(rwops);
icculus@5582
   451
        return NULL;
icculus@5582
   452
    }
icculus@5582
   453
    rwops->seek = Android_JNI_FileSeek;
icculus@5582
   454
    rwops->read = Android_JNI_FileRead;
icculus@5582
   455
    rwops->write = Android_JNI_FileWrite;
icculus@5582
   456
    rwops->close = Android_JNI_FileClose;
icculus@5582
   457
icculus@5582
   458
#elif defined(__WIN32__)
slouken@1895
   459
    rwops = SDL_AllocRW();
slouken@1895
   460
    if (!rwops)
slouken@1895
   461
        return NULL;            /* SDL_SetError already setup by SDL_AllocRW() */
slouken@5062
   462
    if (windows_file_open(rwops, file, mode) < 0) {
slouken@1895
   463
        SDL_FreeRW(rwops);
slouken@1895
   464
        return NULL;
slouken@1895
   465
    }
slouken@5062
   466
    rwops->seek = windows_file_seek;
slouken@5062
   467
    rwops->read = windows_file_read;
slouken@5062
   468
    rwops->write = windows_file_write;
slouken@5062
   469
    rwops->close = windows_file_close;
slouken@1447
   470
slouken@1447
   471
#elif HAVE_STDIO_H
icculus@6375
   472
    {
icculus@6375
   473
    	#ifdef __APPLE__
icculus@6375
   474
    	FILE *fp = SDL_OpenFPFromBundleOrFallback(file, mode);
icculus@6375
   475
        #else
icculus@6375
   476
    	FILE *fp = fopen(file, mode);
icculus@6375
   477
    	#endif
icculus@6375
   478
    	if (fp == NULL) {
icculus@6375
   479
            SDL_SetError("Couldn't open %s", file);
icculus@6375
   480
        } else {
icculus@6375
   481
            rwops = SDL_RWFromFP(fp, 1);
icculus@6375
   482
        }
slouken@1895
   483
    }
slouken@0
   484
#else
slouken@1895
   485
    SDL_SetError("SDL not compiled with stdio support");
slouken@1446
   486
#endif /* !HAVE_STDIO_H */
slouken@1447
   487
slouken@1895
   488
    return (rwops);
slouken@0
   489
}
slouken@0
   490
slouken@1330
   491
#ifdef HAVE_STDIO_H
slouken@1895
   492
SDL_RWops *
slouken@2161
   493
SDL_RWFromFP(FILE * fp, SDL_bool autoclose)
slouken@0
   494
{
slouken@1895
   495
    SDL_RWops *rwops = NULL;
slouken@0
   496
slouken@2735
   497
#if 0
slouken@2735
   498
/*#ifdef __NDS__*/
slouken@2735
   499
    /* set it up so we can use stdio file function */
slouken@2735
   500
    fatInitDefault();
slouken@2735
   501
    printf("called fatInitDefault()");
slouken@2735
   502
#endif /* __NDS__ */
slouken@2735
   503
slouken@1895
   504
    rwops = SDL_AllocRW();
slouken@1895
   505
    if (rwops != NULL) {
slouken@1895
   506
        rwops->seek = stdio_seek;
slouken@1895
   507
        rwops->read = stdio_read;
slouken@1895
   508
        rwops->write = stdio_write;
slouken@1895
   509
        rwops->close = stdio_close;
slouken@1895
   510
        rwops->hidden.stdio.fp = fp;
slouken@1895
   511
        rwops->hidden.stdio.autoclose = autoclose;
slouken@1895
   512
    }
slouken@1895
   513
    return (rwops);
slouken@0
   514
}
slouken@3564
   515
#else
slouken@3564
   516
SDL_RWops *
slouken@3564
   517
SDL_RWFromFP(void * fp, SDL_bool autoclose)
slouken@3564
   518
{
slouken@3564
   519
    SDL_SetError("SDL not compiled with stdio support");
slouken@3564
   520
    return NULL;
slouken@3564
   521
}
slouken@1330
   522
#endif /* HAVE_STDIO_H */
slouken@0
   523
slouken@1895
   524
SDL_RWops *
slouken@1895
   525
SDL_RWFromMem(void *mem, int size)
slouken@0
   526
{
slouken@1895
   527
    SDL_RWops *rwops;
slouken@0
   528
slouken@1895
   529
    rwops = SDL_AllocRW();
slouken@1895
   530
    if (rwops != NULL) {
slouken@1895
   531
        rwops->seek = mem_seek;
slouken@1895
   532
        rwops->read = mem_read;
slouken@1895
   533
        rwops->write = mem_write;
slouken@1895
   534
        rwops->close = mem_close;
slouken@1895
   535
        rwops->hidden.mem.base = (Uint8 *) mem;
slouken@1895
   536
        rwops->hidden.mem.here = rwops->hidden.mem.base;
slouken@1895
   537
        rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
slouken@1895
   538
    }
slouken@1895
   539
    return (rwops);
slouken@0
   540
}
slouken@0
   541
slouken@1895
   542
SDL_RWops *
slouken@1895
   543
SDL_RWFromConstMem(const void *mem, int size)
slouken@764
   544
{
slouken@1895
   545
    SDL_RWops *rwops;
slouken@764
   546
slouken@1895
   547
    rwops = SDL_AllocRW();
slouken@1895
   548
    if (rwops != NULL) {
slouken@1895
   549
        rwops->seek = mem_seek;
slouken@1895
   550
        rwops->read = mem_read;
slouken@1895
   551
        rwops->write = mem_writeconst;
slouken@1895
   552
        rwops->close = mem_close;
slouken@1895
   553
        rwops->hidden.mem.base = (Uint8 *) mem;
slouken@1895
   554
        rwops->hidden.mem.here = rwops->hidden.mem.base;
slouken@1895
   555
        rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
slouken@1895
   556
    }
slouken@1895
   557
    return (rwops);
slouken@764
   558
}
slouken@764
   559
slouken@1895
   560
SDL_RWops *
slouken@1895
   561
SDL_AllocRW(void)
slouken@0
   562
{
slouken@1895
   563
    SDL_RWops *area;
slouken@0
   564
slouken@1895
   565
    area = (SDL_RWops *) SDL_malloc(sizeof *area);
slouken@1895
   566
    if (area == NULL) {
slouken@1895
   567
        SDL_OutOfMemory();
slouken@1895
   568
    }
slouken@1895
   569
    return (area);
slouken@0
   570
}
slouken@0
   571
slouken@1895
   572
void
slouken@1895
   573
SDL_FreeRW(SDL_RWops * area)
slouken@0
   574
{
slouken@1895
   575
    SDL_free(area);
slouken@0
   576
}
slouken@1354
   577
slouken@1354
   578
/* Functions for dynamically reading and writing endian-specific values */
slouken@1354
   579
slouken@1895
   580
Uint16
slouken@1895
   581
SDL_ReadLE16(SDL_RWops * src)
slouken@1354
   582
{
slouken@1895
   583
    Uint16 value;
slouken@1354
   584
slouken@1895
   585
    SDL_RWread(src, &value, (sizeof value), 1);
slouken@1895
   586
    return (SDL_SwapLE16(value));
slouken@1354
   587
}
slouken@1354
   588
slouken@1895
   589
Uint16
slouken@1895
   590
SDL_ReadBE16(SDL_RWops * src)
slouken@1354
   591
{
slouken@1895
   592
    Uint16 value;
slouken@1895
   593
slouken@1895
   594
    SDL_RWread(src, &value, (sizeof value), 1);
slouken@1895
   595
    return (SDL_SwapBE16(value));
slouken@1354
   596
}
slouken@1895
   597
slouken@1895
   598
Uint32
slouken@1895
   599
SDL_ReadLE32(SDL_RWops * src)
slouken@1354
   600
{
slouken@1895
   601
    Uint32 value;
slouken@1895
   602
slouken@1895
   603
    SDL_RWread(src, &value, (sizeof value), 1);
slouken@1895
   604
    return (SDL_SwapLE32(value));
slouken@1354
   605
}
slouken@1895
   606
slouken@1895
   607
Uint32
slouken@1895
   608
SDL_ReadBE32(SDL_RWops * src)
slouken@1354
   609
{
slouken@1895
   610
    Uint32 value;
slouken@1895
   611
slouken@1895
   612
    SDL_RWread(src, &value, (sizeof value), 1);
slouken@1895
   613
    return (SDL_SwapBE32(value));
slouken@1354
   614
}
slouken@1895
   615
slouken@1895
   616
Uint64
slouken@1895
   617
SDL_ReadLE64(SDL_RWops * src)
slouken@1354
   618
{
slouken@1895
   619
    Uint64 value;
slouken@1895
   620
slouken@1895
   621
    SDL_RWread(src, &value, (sizeof value), 1);
slouken@1895
   622
    return (SDL_SwapLE64(value));
slouken@1354
   623
}
slouken@1895
   624
slouken@1895
   625
Uint64
slouken@1895
   626
SDL_ReadBE64(SDL_RWops * src)
slouken@1354
   627
{
slouken@1895
   628
    Uint64 value;
slouken@1895
   629
slouken@1895
   630
    SDL_RWread(src, &value, (sizeof value), 1);
slouken@1895
   631
    return (SDL_SwapBE64(value));
slouken@1354
   632
}
slouken@1895
   633
slouken@3253
   634
size_t
slouken@1895
   635
SDL_WriteLE16(SDL_RWops * dst, Uint16 value)
slouken@1354
   636
{
slouken@1895
   637
    value = SDL_SwapLE16(value);
slouken@1895
   638
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1354
   639
}
slouken@1895
   640
slouken@3253
   641
size_t
slouken@1895
   642
SDL_WriteBE16(SDL_RWops * dst, Uint16 value)
slouken@1895
   643
{
slouken@1895
   644
    value = SDL_SwapBE16(value);
slouken@1895
   645
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1895
   646
}
slouken@1895
   647
slouken@3253
   648
size_t
slouken@1895
   649
SDL_WriteLE32(SDL_RWops * dst, Uint32 value)
slouken@1895
   650
{
slouken@1895
   651
    value = SDL_SwapLE32(value);
slouken@1895
   652
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1895
   653
}
slouken@1895
   654
slouken@3253
   655
size_t
slouken@1895
   656
SDL_WriteBE32(SDL_RWops * dst, Uint32 value)
slouken@1895
   657
{
slouken@1895
   658
    value = SDL_SwapBE32(value);
slouken@1895
   659
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1895
   660
}
slouken@1895
   661
slouken@3253
   662
size_t
slouken@1895
   663
SDL_WriteLE64(SDL_RWops * dst, Uint64 value)
slouken@1895
   664
{
slouken@1895
   665
    value = SDL_SwapLE64(value);
slouken@1895
   666
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1895
   667
}
slouken@1895
   668
slouken@3253
   669
size_t
slouken@1895
   670
SDL_WriteBE64(SDL_RWops * dst, Uint64 value)
slouken@1895
   671
{
slouken@1895
   672
    value = SDL_SwapBE64(value);
slouken@1895
   673
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1895
   674
}
slouken@1895
   675
slouken@1895
   676
/* vi: set ts=4 sw=4 expandtab: */