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