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