src/file/SDL_rwops.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 27 Aug 2008 15:10:03 +0000
changeset 2735 204be4fc2726
parent 2185 2032348afed1
child 2859 99210400e8b9
permissions -rw-r--r--
Final merge of Google Summer of Code 2008 work...

Port SDL 1.3 to the Nintendo DS
by Darren Alton, mentored by Sam Lantinga
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* This file provides a general interface for SDL to read and write
slouken@0
    25
   data sources.  It can easily be extended to files, memory, etc.
slouken@0
    26
*/
slouken@0
    27
slouken@1354
    28
#include "SDL_endian.h"
slouken@0
    29
#include "SDL_rwops.h"
slouken@1330
    30
slouken@2735
    31
#ifdef __NDS__
slouken@2735
    32
/* include libfat headers for fatInitDefault(). */
slouken@2735
    33
#include <fat.h>
slouken@2735
    34
#endif /* __NDS__ */
slouken@1354
    35
slouken@2159
    36
#ifdef __WIN32__
slouken@0
    37
slouken@1447
    38
/* Functions to read/write Win32 API file pointers */
slouken@1465
    39
/* Will not use it on WinCE because stdio is buffered, it means
slouken@1465
    40
   faster, and all stdio functions anyway are embedded in coredll.dll - 
slouken@1465
    41
   the main wince dll*/
slouken@0
    42
slouken@1446
    43
#define WINDOWS_LEAN_AND_MEAN
slouken@1446
    44
#include <windows.h>
slouken@1446
    45
slouken@1465
    46
#ifndef INVALID_SET_FILE_POINTER
slouken@1465
    47
#define INVALID_SET_FILE_POINTER 0xFFFFFFFF
slouken@1465
    48
#endif
slouken@1465
    49
slouken@2159
    50
#define READAHEAD_BUFFER_SIZE	1024
slouken@2159
    51
slouken@1895
    52
static int SDLCALL
slouken@1895
    53
win32_file_open(SDL_RWops * context, const char *filename, const char *mode)
slouken@1465
    54
{
slouken@1465
    55
#ifndef _WIN32_WCE
slouken@1895
    56
    UINT old_error_mode;
slouken@1465
    57
#endif
slouken@1895
    58
    HANDLE h;
slouken@1895
    59
    DWORD r_right, w_right;
slouken@1895
    60
    DWORD must_exist, truncate;
slouken@1895
    61
    int a_mode;
slouken@1446
    62
slouken@1895
    63
    if (!context)
slouken@2164
    64
        return -1;              /* failed (invalid call) */
slouken@2159
    65
slouken@2173
    66
    context->hidden.win32io.h = INVALID_HANDLE_VALUE;   /* mark this as unusable */
icculus@2165
    67
    context->hidden.win32io.buffer.data = NULL;
icculus@2165
    68
    context->hidden.win32io.buffer.size = 0;
icculus@2165
    69
    context->hidden.win32io.buffer.left = 0;
icculus@2165
    70
slouken@1895
    71
    /* "r" = reading, file must exist */
slouken@1895
    72
    /* "w" = writing, truncate existing, file may not exist */
slouken@1895
    73
    /* "r+"= reading or writing, file must exist            */
slouken@1895
    74
    /* "a" = writing, append file may not exist             */
slouken@1895
    75
    /* "a+"= append + read, file may not exist              */
slouken@1895
    76
    /* "w+" = read, write, truncate. file may not exist    */
slouken@1895
    77
slouken@1895
    78
    must_exist = (SDL_strchr(mode, 'r') != NULL) ? OPEN_EXISTING : 0;
slouken@1895
    79
    truncate = (SDL_strchr(mode, 'w') != NULL) ? CREATE_ALWAYS : 0;
slouken@1895
    80
    r_right = (SDL_strchr(mode, '+') != NULL
slouken@1895
    81
               || must_exist) ? GENERIC_READ : 0;
slouken@1895
    82
    a_mode = (SDL_strchr(mode, 'a') != NULL) ? OPEN_ALWAYS : 0;
slouken@1895
    83
    w_right = (a_mode || SDL_strchr(mode, '+')
slouken@1895
    84
               || truncate) ? GENERIC_WRITE : 0;
slouken@1895
    85
slouken@1895
    86
    if (!r_right && !w_right)   /* inconsistent mode */
slouken@1895
    87
        return -1;              /* failed (invalid call) */
slouken@1465
    88
slouken@2173
    89
    context->hidden.win32io.buffer.data =
slouken@2173
    90
        (char *) SDL_malloc(READAHEAD_BUFFER_SIZE);
icculus@2165
    91
    if (!context->hidden.win32io.buffer.data) {
icculus@2165
    92
        SDL_OutOfMemory();
icculus@2165
    93
        return -1;
icculus@2165
    94
    }
slouken@1465
    95
#ifdef _WIN32_WCE
slouken@1895
    96
    {
slouken@1895
    97
        size_t size = SDL_strlen(filename) + 1;
slouken@1895
    98
        wchar_t *filenameW = SDL_stack_alloc(wchar_t, size);
slouken@1465
    99
slouken@1895
   100
        if (MultiByteToWideChar(CP_UTF8, 0, filename, -1, filenameW, size) ==
slouken@1895
   101
            0) {
icculus@2165
   102
            SDL_stack_free(filenameW);
icculus@2165
   103
            SDL_free(context->hidden.win32io.buffer.data);
icculus@2165
   104
            context->hidden.win32io.buffer.data = NULL;
slouken@1895
   105
            SDL_SetError("Unable to convert filename to Unicode");
slouken@1895
   106
            return -1;
slouken@1895
   107
        }
slouken@1895
   108
        h = CreateFile(filenameW, (w_right | r_right),
slouken@1895
   109
                       (w_right) ? 0 : FILE_SHARE_READ, NULL,
slouken@1895
   110
                       (must_exist | truncate | a_mode),
slouken@1895
   111
                       FILE_ATTRIBUTE_NORMAL, NULL);
slouken@1895
   112
        SDL_stack_free(filenameW);
slouken@1895
   113
    }
slouken@1465
   114
#else
slouken@1895
   115
    /* Do not open a dialog box if failure */
slouken@1895
   116
    old_error_mode =
slouken@1895
   117
        SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
slouken@1465
   118
slouken@1895
   119
    h = CreateFile(filename, (w_right | r_right),
slouken@1895
   120
                   (w_right) ? 0 : FILE_SHARE_READ, NULL,
slouken@1895
   121
                   (must_exist | truncate | a_mode), FILE_ATTRIBUTE_NORMAL,
slouken@1895
   122
                   NULL);
slouken@1465
   123
slouken@1895
   124
    /* restore old behaviour */
slouken@1895
   125
    SetErrorMode(old_error_mode);
slouken@1465
   126
#endif /* _WIN32_WCE */
slouken@1446
   127
slouken@1895
   128
    if (h == INVALID_HANDLE_VALUE) {
icculus@2165
   129
        SDL_free(context->hidden.win32io.buffer.data);
icculus@2165
   130
        context->hidden.win32io.buffer.data = NULL;
slouken@1895
   131
        SDL_SetError("Couldn't open %s", filename);
slouken@1895
   132
        return -2;              /* failed (CreateFile) */
slouken@1895
   133
    }
slouken@1895
   134
    context->hidden.win32io.h = h;
slouken@2161
   135
    context->hidden.win32io.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@2160
   141
win32_file_seek(SDL_RWops * context, long offset, int whence)
slouken@1465
   142
{
slouken@1895
   143
    DWORD win32whence;
slouken@2160
   144
    long file_pos;
slouken@1446
   145
slouken@1895
   146
    if (!context || context->hidden.win32io.h == INVALID_HANDLE_VALUE) {
slouken@1895
   147
        SDL_SetError("win32_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@2159
   152
    if (whence == RW_SEEK_CUR && context->hidden.win32io.buffer.left) {
slouken@2159
   153
        offset -= context->hidden.win32io.buffer.left;
slouken@2159
   154
    }
slouken@2159
   155
    context->hidden.win32io.buffer.left = 0;
slouken@2159
   156
slouken@1895
   157
    switch (whence) {
slouken@1895
   158
    case RW_SEEK_SET:
slouken@1895
   159
        win32whence = FILE_BEGIN;
slouken@1895
   160
        break;
slouken@1895
   161
    case RW_SEEK_CUR:
slouken@1895
   162
        win32whence = FILE_CURRENT;
slouken@1895
   163
        break;
slouken@1895
   164
    case RW_SEEK_END:
slouken@1895
   165
        win32whence = FILE_END;
slouken@1895
   166
        break;
slouken@1895
   167
    default:
slouken@1895
   168
        SDL_SetError("win32_file_seek: Unknown value for 'whence'");
slouken@1895
   169
        return -1;
slouken@1895
   170
    }
slouken@1895
   171
slouken@1895
   172
    file_pos =
slouken@1895
   173
        SetFilePointer(context->hidden.win32io.h, offset, NULL, win32whence);
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@2160
   183
win32_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@1895
   192
    if (!context || context->hidden.win32io.h == INVALID_HANDLE_VALUE
slouken@2160
   193
        || !total_need)
slouken@1895
   194
        return 0;
slouken@1895
   195
slouken@2159
   196
    if (context->hidden.win32io.buffer.left > 0) {
slouken@2159
   197
        void *data = (char *) context->hidden.win32io.buffer.data +
slouken@2159
   198
            context->hidden.win32io.buffer.size -
slouken@2159
   199
            context->hidden.win32io.buffer.left;
bob@2185
   200
        read_ahead =
bob@2185
   201
            SDL_min(total_need, (size_t) context->hidden.win32io.buffer.left);
slouken@2159
   202
        SDL_memcpy(ptr, data, read_ahead);
slouken@2159
   203
        context->hidden.win32io.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@2159
   215
            (context->hidden.win32io.h, context->hidden.win32io.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@2159
   221
        SDL_memcpy(ptr, context->hidden.win32io.buffer.data, read_ahead);
slouken@2159
   222
        context->hidden.win32io.buffer.size = byte_read;
slouken@2159
   223
        context->hidden.win32io.buffer.left = byte_read - read_ahead;
slouken@2159
   224
        total_read += read_ahead;
slouken@2159
   225
    } else {
slouken@2159
   226
        if (!ReadFile
slouken@2159
   227
            (context->hidden.win32io.h, ptr, 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@2160
   237
win32_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@1895
   242
    DWORD byte_written, nwritten;
slouken@1446
   243
slouken@1895
   244
    total_bytes = size * num;
slouken@1895
   245
slouken@1895
   246
    if (!context || context->hidden.win32io.h == INVALID_HANDLE_VALUE
slouken@1895
   247
        || total_bytes <= 0 || !size)
slouken@1895
   248
        return 0;
slouken@1895
   249
slouken@2159
   250
    if (context->hidden.win32io.buffer.left) {
slouken@2159
   251
        SetFilePointer(context->hidden.win32io.h,
slouken@2159
   252
                       -context->hidden.win32io.buffer.left, NULL,
slouken@2159
   253
                       FILE_CURRENT);
slouken@2159
   254
        context->hidden.win32io.buffer.left = 0;
slouken@2159
   255
    }
slouken@2159
   256
slouken@1895
   257
    /* if in append mode, we must go to the EOF before write */
slouken@1895
   258
    if (context->hidden.win32io.append) {
slouken@2159
   259
        if (SetFilePointer(context->hidden.win32io.h, 0L, NULL, FILE_END) ==
slouken@2159
   260
            INVALID_SET_FILE_POINTER) {
slouken@1895
   261
            SDL_Error(SDL_EFWRITE);
slouken@1895
   262
            return 0;
slouken@1895
   263
        }
slouken@1895
   264
    }
slouken@1895
   265
slouken@1895
   266
    if (!WriteFile
slouken@1895
   267
        (context->hidden.win32io.h, ptr, total_bytes, &byte_written, NULL)) {
slouken@1895
   268
        SDL_Error(SDL_EFWRITE);
slouken@1895
   269
        return 0;
slouken@1895
   270
    }
slouken@1895
   271
slouken@1895
   272
    nwritten = byte_written / size;
slouken@1895
   273
    return nwritten;
slouken@1446
   274
}
slouken@2735
   275
slouken@1895
   276
static int SDLCALL
slouken@1895
   277
win32_file_close(SDL_RWops * context)
slouken@1465
   278
{
slouken@1895
   279
slouken@1895
   280
    if (context) {
slouken@1895
   281
        if (context->hidden.win32io.h != INVALID_HANDLE_VALUE) {
slouken@1895
   282
            CloseHandle(context->hidden.win32io.h);
slouken@1895
   283
            context->hidden.win32io.h = INVALID_HANDLE_VALUE;   /* to be sure */
slouken@1895
   284
        }
slouken@2159
   285
        if (context->hidden.win32io.buffer.data) {
slouken@2159
   286
            SDL_free(context->hidden.win32io.buffer.data);
slouken@2159
   287
            context->hidden.win32io.buffer.data = NULL;
slouken@2159
   288
        }
slouken@1895
   289
        SDL_FreeRW(context);
slouken@1895
   290
    }
slouken@1895
   291
    return (0);
slouken@1446
   292
}
slouken@1447
   293
#endif /* __WIN32__ */
slouken@1446
   294
slouken@1447
   295
#ifdef HAVE_STDIO_H
slouken@1446
   296
slouken@1447
   297
/* Functions to read/write stdio file pointers */
slouken@1446
   298
slouken@2160
   299
static long SDLCALL
slouken@2160
   300
stdio_seek(SDL_RWops * context, long offset, int whence)
slouken@1447
   301
{
slouken@1895
   302
    if (fseek(context->hidden.stdio.fp, offset, whence) == 0) {
slouken@1895
   303
        return (ftell(context->hidden.stdio.fp));
slouken@1895
   304
    } else {
slouken@1895
   305
        SDL_Error(SDL_EFSEEK);
slouken@1895
   306
        return (-1);
slouken@1895
   307
    }
slouken@1447
   308
}
slouken@2735
   309
slouken@2160
   310
static size_t SDLCALL
slouken@2160
   311
stdio_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
slouken@1447
   312
{
slouken@1895
   313
    size_t nread;
slouken@1447
   314
slouken@1895
   315
    nread = fread(ptr, size, maxnum, context->hidden.stdio.fp);
slouken@1895
   316
    if (nread == 0 && ferror(context->hidden.stdio.fp)) {
slouken@1895
   317
        SDL_Error(SDL_EFREAD);
slouken@1895
   318
    }
slouken@1895
   319
    return (nread);
slouken@1447
   320
}
slouken@2735
   321
slouken@2160
   322
static size_t SDLCALL
slouken@2160
   323
stdio_write(SDL_RWops * context, const void *ptr, size_t size, size_t num)
slouken@1447
   324
{
slouken@1895
   325
    size_t nwrote;
slouken@1447
   326
slouken@1895
   327
    nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
slouken@1895
   328
    if (nwrote == 0 && ferror(context->hidden.stdio.fp)) {
slouken@1895
   329
        SDL_Error(SDL_EFWRITE);
slouken@1895
   330
    }
slouken@1895
   331
    return (nwrote);
slouken@1447
   332
}
slouken@2735
   333
slouken@1895
   334
static int SDLCALL
slouken@1895
   335
stdio_close(SDL_RWops * context)
slouken@1447
   336
{
slouken@2160
   337
    int status = 0;
slouken@1895
   338
    if (context) {
slouken@1895
   339
        if (context->hidden.stdio.autoclose) {
slouken@1895
   340
            /* WARNING:  Check the return value here! */
slouken@2160
   341
            if (fclose(context->hidden.stdio.fp) != 0) {
slouken@2160
   342
                SDL_Error(SDL_EFWRITE);
slouken@2160
   343
                status = -1;
slouken@2160
   344
            }
slouken@1895
   345
        }
slouken@1895
   346
        SDL_FreeRW(context);
slouken@1895
   347
    }
slouken@2160
   348
    return status;
slouken@1447
   349
}
slouken@1446
   350
#endif /* !HAVE_STDIO_H */
slouken@1330
   351
slouken@0
   352
/* Functions to read/write memory pointers */
slouken@0
   353
slouken@2160
   354
static long SDLCALL
slouken@2160
   355
mem_seek(SDL_RWops * context, long offset, int whence)
slouken@0
   356
{
slouken@1895
   357
    Uint8 *newpos;
slouken@0
   358
slouken@1895
   359
    switch (whence) {
slouken@1895
   360
    case RW_SEEK_SET:
slouken@1895
   361
        newpos = context->hidden.mem.base + offset;
slouken@1895
   362
        break;
slouken@1895
   363
    case RW_SEEK_CUR:
slouken@1895
   364
        newpos = context->hidden.mem.here + offset;
slouken@1895
   365
        break;
slouken@1895
   366
    case RW_SEEK_END:
slouken@1895
   367
        newpos = context->hidden.mem.stop + offset;
slouken@1895
   368
        break;
slouken@1895
   369
    default:
slouken@1895
   370
        SDL_SetError("Unknown value for 'whence'");
slouken@1895
   371
        return (-1);
slouken@1895
   372
    }
slouken@1895
   373
    if (newpos < context->hidden.mem.base) {
slouken@1895
   374
        newpos = context->hidden.mem.base;
slouken@1895
   375
    }
slouken@1895
   376
    if (newpos > context->hidden.mem.stop) {
slouken@1895
   377
        newpos = context->hidden.mem.stop;
slouken@1895
   378
    }
slouken@1895
   379
    context->hidden.mem.here = newpos;
slouken@1895
   380
    return (context->hidden.mem.here - context->hidden.mem.base);
slouken@0
   381
}
slouken@2735
   382
slouken@2160
   383
static size_t SDLCALL
slouken@2160
   384
mem_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
slouken@0
   385
{
slouken@1895
   386
    size_t total_bytes;
slouken@1895
   387
    size_t mem_available;
slouken@0
   388
slouken@1895
   389
    total_bytes = (maxnum * size);
slouken@1895
   390
    if ((maxnum <= 0) || (size <= 0)
slouken@1895
   391
        || ((total_bytes / maxnum) != (size_t) size)) {
slouken@1895
   392
        return 0;
slouken@1895
   393
    }
icculus@1078
   394
slouken@1895
   395
    mem_available = (context->hidden.mem.stop - context->hidden.mem.here);
slouken@1895
   396
    if (total_bytes > mem_available) {
slouken@1895
   397
        total_bytes = mem_available;
slouken@1895
   398
    }
icculus@1078
   399
slouken@1895
   400
    SDL_memcpy(ptr, context->hidden.mem.here, total_bytes);
slouken@1895
   401
    context->hidden.mem.here += total_bytes;
icculus@1078
   402
slouken@1895
   403
    return (total_bytes / size);
slouken@0
   404
}
slouken@2735
   405
slouken@2160
   406
static size_t SDLCALL
slouken@2160
   407
mem_write(SDL_RWops * context, const void *ptr, size_t size, size_t num)
slouken@0
   408
{
slouken@1895
   409
    if ((context->hidden.mem.here + (num * size)) > context->hidden.mem.stop) {
slouken@1895
   410
        num = (context->hidden.mem.stop - context->hidden.mem.here) / size;
slouken@1895
   411
    }
slouken@1895
   412
    SDL_memcpy(context->hidden.mem.here, ptr, num * size);
slouken@1895
   413
    context->hidden.mem.here += num * size;
slouken@1895
   414
    return (num);
slouken@0
   415
}
slouken@2735
   416
slouken@2160
   417
static size_t SDLCALL
slouken@2160
   418
mem_writeconst(SDL_RWops * context, const void *ptr, size_t size, size_t num)
slouken@764
   419
{
slouken@1895
   420
    SDL_SetError("Can't write to read-only memory");
slouken@1895
   421
    return (-1);
slouken@764
   422
}
slouken@2735
   423
slouken@1895
   424
static int SDLCALL
slouken@1895
   425
mem_close(SDL_RWops * context)
slouken@0
   426
{
slouken@1895
   427
    if (context) {
slouken@1895
   428
        SDL_FreeRW(context);
slouken@1895
   429
    }
slouken@1895
   430
    return (0);
slouken@0
   431
}
slouken@0
   432
slouken@1447
   433
slouken@0
   434
/* Functions to create SDL_RWops structures from various data sources */
slouken@0
   435
slouken@1895
   436
SDL_RWops *
slouken@1895
   437
SDL_RWFromFile(const char *file, const char *mode)
slouken@0
   438
{
slouken@1895
   439
    SDL_RWops *rwops = NULL;
slouken@1465
   440
#ifdef HAVE_STDIO_H
slouken@1895
   441
    FILE *fp = NULL;
slouken@1465
   442
#endif
slouken@1895
   443
    if (!file || !*file || !mode || !*mode) {
slouken@1895
   444
        SDL_SetError("SDL_RWFromFile(): No file or no mode specified");
slouken@1895
   445
        return NULL;
slouken@1895
   446
    }
slouken@1465
   447
#if defined(__WIN32__)
slouken@1895
   448
    rwops = SDL_AllocRW();
slouken@1895
   449
    if (!rwops)
slouken@1895
   450
        return NULL;            /* SDL_SetError already setup by SDL_AllocRW() */
slouken@2164
   451
    if (win32_file_open(rwops, file, mode) < 0) {
slouken@1895
   452
        SDL_FreeRW(rwops);
slouken@1895
   453
        return NULL;
slouken@1895
   454
    }
slouken@1895
   455
    rwops->seek = win32_file_seek;
slouken@1895
   456
    rwops->read = win32_file_read;
slouken@1895
   457
    rwops->write = win32_file_write;
slouken@1895
   458
    rwops->close = win32_file_close;
slouken@1447
   459
slouken@1447
   460
#elif HAVE_STDIO_H
slouken@0
   461
slouken@1895
   462
    fp = fopen(file, mode);
slouken@1895
   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@1330
   499
#endif /* HAVE_STDIO_H */
slouken@0
   500
slouken@1895
   501
SDL_RWops *
slouken@1895
   502
SDL_RWFromMem(void *mem, int size)
slouken@0
   503
{
slouken@1895
   504
    SDL_RWops *rwops;
slouken@0
   505
slouken@1895
   506
    rwops = SDL_AllocRW();
slouken@1895
   507
    if (rwops != NULL) {
slouken@1895
   508
        rwops->seek = mem_seek;
slouken@1895
   509
        rwops->read = mem_read;
slouken@1895
   510
        rwops->write = mem_write;
slouken@1895
   511
        rwops->close = mem_close;
slouken@1895
   512
        rwops->hidden.mem.base = (Uint8 *) mem;
slouken@1895
   513
        rwops->hidden.mem.here = rwops->hidden.mem.base;
slouken@1895
   514
        rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
slouken@1895
   515
    }
slouken@1895
   516
    return (rwops);
slouken@0
   517
}
slouken@0
   518
slouken@1895
   519
SDL_RWops *
slouken@1895
   520
SDL_RWFromConstMem(const void *mem, int size)
slouken@764
   521
{
slouken@1895
   522
    SDL_RWops *rwops;
slouken@764
   523
slouken@1895
   524
    rwops = SDL_AllocRW();
slouken@1895
   525
    if (rwops != NULL) {
slouken@1895
   526
        rwops->seek = mem_seek;
slouken@1895
   527
        rwops->read = mem_read;
slouken@1895
   528
        rwops->write = mem_writeconst;
slouken@1895
   529
        rwops->close = mem_close;
slouken@1895
   530
        rwops->hidden.mem.base = (Uint8 *) mem;
slouken@1895
   531
        rwops->hidden.mem.here = rwops->hidden.mem.base;
slouken@1895
   532
        rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
slouken@1895
   533
    }
slouken@1895
   534
    return (rwops);
slouken@764
   535
}
slouken@764
   536
slouken@1895
   537
SDL_RWops *
slouken@1895
   538
SDL_AllocRW(void)
slouken@0
   539
{
slouken@1895
   540
    SDL_RWops *area;
slouken@0
   541
slouken@1895
   542
    area = (SDL_RWops *) SDL_malloc(sizeof *area);
slouken@1895
   543
    if (area == NULL) {
slouken@1895
   544
        SDL_OutOfMemory();
slouken@1895
   545
    }
slouken@1895
   546
    return (area);
slouken@0
   547
}
slouken@0
   548
slouken@1895
   549
void
slouken@1895
   550
SDL_FreeRW(SDL_RWops * area)
slouken@0
   551
{
slouken@1895
   552
    SDL_free(area);
slouken@0
   553
}
slouken@1354
   554
slouken@1354
   555
/* Functions for dynamically reading and writing endian-specific values */
slouken@1354
   556
slouken@1895
   557
Uint16
slouken@1895
   558
SDL_ReadLE16(SDL_RWops * src)
slouken@1354
   559
{
slouken@1895
   560
    Uint16 value;
slouken@1354
   561
slouken@1895
   562
    SDL_RWread(src, &value, (sizeof value), 1);
slouken@1895
   563
    return (SDL_SwapLE16(value));
slouken@1354
   564
}
slouken@1354
   565
slouken@1895
   566
Uint16
slouken@1895
   567
SDL_ReadBE16(SDL_RWops * src)
slouken@1354
   568
{
slouken@1895
   569
    Uint16 value;
slouken@1895
   570
slouken@1895
   571
    SDL_RWread(src, &value, (sizeof value), 1);
slouken@1895
   572
    return (SDL_SwapBE16(value));
slouken@1354
   573
}
slouken@1895
   574
slouken@1895
   575
Uint32
slouken@1895
   576
SDL_ReadLE32(SDL_RWops * src)
slouken@1354
   577
{
slouken@1895
   578
    Uint32 value;
slouken@1895
   579
slouken@1895
   580
    SDL_RWread(src, &value, (sizeof value), 1);
slouken@1895
   581
    return (SDL_SwapLE32(value));
slouken@1354
   582
}
slouken@1895
   583
slouken@1895
   584
Uint32
slouken@1895
   585
SDL_ReadBE32(SDL_RWops * src)
slouken@1354
   586
{
slouken@1895
   587
    Uint32 value;
slouken@1895
   588
slouken@1895
   589
    SDL_RWread(src, &value, (sizeof value), 1);
slouken@1895
   590
    return (SDL_SwapBE32(value));
slouken@1354
   591
}
slouken@1895
   592
slouken@1895
   593
Uint64
slouken@1895
   594
SDL_ReadLE64(SDL_RWops * src)
slouken@1354
   595
{
slouken@1895
   596
    Uint64 value;
slouken@1895
   597
slouken@1895
   598
    SDL_RWread(src, &value, (sizeof value), 1);
slouken@1895
   599
    return (SDL_SwapLE64(value));
slouken@1354
   600
}
slouken@1895
   601
slouken@1895
   602
Uint64
slouken@1895
   603
SDL_ReadBE64(SDL_RWops * src)
slouken@1354
   604
{
slouken@1895
   605
    Uint64 value;
slouken@1895
   606
slouken@1895
   607
    SDL_RWread(src, &value, (sizeof value), 1);
slouken@1895
   608
    return (SDL_SwapBE64(value));
slouken@1354
   609
}
slouken@1895
   610
slouken@1895
   611
int
slouken@1895
   612
SDL_WriteLE16(SDL_RWops * dst, Uint16 value)
slouken@1354
   613
{
slouken@1895
   614
    value = SDL_SwapLE16(value);
slouken@1895
   615
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1354
   616
}
slouken@1895
   617
slouken@1895
   618
int
slouken@1895
   619
SDL_WriteBE16(SDL_RWops * dst, Uint16 value)
slouken@1895
   620
{
slouken@1895
   621
    value = SDL_SwapBE16(value);
slouken@1895
   622
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1895
   623
}
slouken@1895
   624
slouken@1895
   625
int
slouken@1895
   626
SDL_WriteLE32(SDL_RWops * dst, Uint32 value)
slouken@1895
   627
{
slouken@1895
   628
    value = SDL_SwapLE32(value);
slouken@1895
   629
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1895
   630
}
slouken@1895
   631
slouken@1895
   632
int
slouken@1895
   633
SDL_WriteBE32(SDL_RWops * dst, Uint32 value)
slouken@1895
   634
{
slouken@1895
   635
    value = SDL_SwapBE32(value);
slouken@1895
   636
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1895
   637
}
slouken@1895
   638
slouken@1895
   639
int
slouken@1895
   640
SDL_WriteLE64(SDL_RWops * dst, Uint64 value)
slouken@1895
   641
{
slouken@1895
   642
    value = SDL_SwapLE64(value);
slouken@1895
   643
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1895
   644
}
slouken@1895
   645
slouken@1895
   646
int
slouken@1895
   647
SDL_WriteBE64(SDL_RWops * dst, Uint64 value)
slouken@1895
   648
{
slouken@1895
   649
    value = SDL_SwapBE64(value);
slouken@1895
   650
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1895
   651
}
slouken@1895
   652
slouken@1895
   653
/* vi: set ts=4 sw=4 expandtab: */