src/file/SDL_rwops.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 28 May 2006 13:04:16 +0000
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1659 14717b52abc0
child 1668 4da1ee79c9af
permissions -rw-r--r--
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.

WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.

The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce

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