src/file/SDL_rwops.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 10 Jul 2007 16:05:50 +0000
changeset 2164 d083a2e4433d
parent 2161 e635db5b45ef
child 2165 c2ffcc627701
permissions -rw-r--r--
Merged memory leak fix from SDL 1.2
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@2159
    32
#ifdef __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@2159
    46
#define READAHEAD_BUFFER_SIZE	1024
slouken@2159
    47
slouken@1895
    48
static int SDLCALL
slouken@1895
    49
win32_file_open(SDL_RWops * context, const char *filename, const char *mode)
slouken@1465
    50
{
slouken@1465
    51
#ifndef _WIN32_WCE
slouken@1895
    52
    UINT old_error_mode;
slouken@1465
    53
#endif
slouken@1895
    54
    HANDLE h;
slouken@1895
    55
    DWORD r_right, w_right;
slouken@1895
    56
    DWORD must_exist, truncate;
slouken@1895
    57
    int a_mode;
slouken@1446
    58
slouken@1895
    59
    if (!context)
slouken@2164
    60
        return -1;              /* failed (invalid call) */
slouken@2159
    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@2161
   116
    context->hidden.win32io.append = a_mode ? SDL_TRUE : SDL_FALSE;
slouken@1895
   117
slouken@2164
   118
    context->hidden.win32io.buffer.data =
slouken@2164
   119
        (char *) SDL_malloc(READAHEAD_BUFFER_SIZE);
slouken@2164
   120
    if (!context->hidden.win32io.buffer.data) {
slouken@2164
   121
        SDL_OutOfMemory();
slouken@2164
   122
        CloseHandle(context->hidden.win32io.h);
slouken@2164
   123
        return -1;
slouken@2164
   124
    }
slouken@2164
   125
    context->hidden.win32io.buffer.size = 0;
slouken@2164
   126
    context->hidden.win32io.buffer.left = 0;
slouken@2164
   127
slouken@1895
   128
    return 0;                   /* ok */
slouken@1446
   129
}
slouken@2160
   130
static long SDLCALL
slouken@2160
   131
win32_file_seek(SDL_RWops * context, long offset, int whence)
slouken@1465
   132
{
slouken@1895
   133
    DWORD win32whence;
slouken@2160
   134
    long file_pos;
slouken@1446
   135
slouken@1895
   136
    if (!context || context->hidden.win32io.h == INVALID_HANDLE_VALUE) {
slouken@1895
   137
        SDL_SetError("win32_file_seek: invalid context/file not opened");
slouken@1895
   138
        return -1;
slouken@1895
   139
    }
slouken@1895
   140
slouken@2159
   141
    /* FIXME: We may be able to satisfy the seek within buffered data */
slouken@2159
   142
    if (whence == RW_SEEK_CUR && context->hidden.win32io.buffer.left) {
slouken@2159
   143
        offset -= context->hidden.win32io.buffer.left;
slouken@2159
   144
    }
slouken@2159
   145
    context->hidden.win32io.buffer.left = 0;
slouken@2159
   146
slouken@1895
   147
    switch (whence) {
slouken@1895
   148
    case RW_SEEK_SET:
slouken@1895
   149
        win32whence = FILE_BEGIN;
slouken@1895
   150
        break;
slouken@1895
   151
    case RW_SEEK_CUR:
slouken@1895
   152
        win32whence = FILE_CURRENT;
slouken@1895
   153
        break;
slouken@1895
   154
    case RW_SEEK_END:
slouken@1895
   155
        win32whence = FILE_END;
slouken@1895
   156
        break;
slouken@1895
   157
    default:
slouken@1895
   158
        SDL_SetError("win32_file_seek: Unknown value for 'whence'");
slouken@1895
   159
        return -1;
slouken@1895
   160
    }
slouken@1895
   161
slouken@1895
   162
    file_pos =
slouken@1895
   163
        SetFilePointer(context->hidden.win32io.h, offset, NULL, win32whence);
slouken@1895
   164
slouken@1895
   165
    if (file_pos != INVALID_SET_FILE_POINTER)
slouken@1895
   166
        return file_pos;        /* success */
slouken@1895
   167
slouken@1895
   168
    SDL_Error(SDL_EFSEEK);
slouken@1895
   169
    return -1;                  /* error */
slouken@1446
   170
}
slouken@2160
   171
static size_t SDLCALL
slouken@2160
   172
win32_file_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
slouken@1465
   173
{
slouken@2160
   174
    size_t total_need;
slouken@2160
   175
    size_t total_read = 0;
slouken@2160
   176
    size_t read_ahead;
slouken@2159
   177
    DWORD byte_read;
slouken@1895
   178
slouken@2159
   179
    total_need = size * maxnum;
slouken@1895
   180
slouken@1895
   181
    if (!context || context->hidden.win32io.h == INVALID_HANDLE_VALUE
slouken@2160
   182
        || !total_need)
slouken@1895
   183
        return 0;
slouken@1895
   184
slouken@2159
   185
    if (context->hidden.win32io.buffer.left > 0) {
slouken@2159
   186
        void *data = (char *) context->hidden.win32io.buffer.data +
slouken@2159
   187
            context->hidden.win32io.buffer.size -
slouken@2159
   188
            context->hidden.win32io.buffer.left;
slouken@2159
   189
        read_ahead = SDL_min(total_need, context->hidden.win32io.buffer.left);
slouken@2159
   190
        SDL_memcpy(ptr, data, read_ahead);
slouken@2159
   191
        context->hidden.win32io.buffer.left -= read_ahead;
slouken@2159
   192
slouken@2159
   193
        if (read_ahead == total_need) {
slouken@2159
   194
            return maxnum;
slouken@2159
   195
        }
slouken@2159
   196
        ptr = (char *) ptr + read_ahead;
slouken@2159
   197
        total_need -= read_ahead;
slouken@2159
   198
        total_read += read_ahead;
slouken@1895
   199
    }
slouken@2159
   200
slouken@2159
   201
    if (total_need < READAHEAD_BUFFER_SIZE) {
slouken@2159
   202
        if (!ReadFile
slouken@2159
   203
            (context->hidden.win32io.h, context->hidden.win32io.buffer.data,
slouken@2159
   204
             READAHEAD_BUFFER_SIZE, &byte_read, NULL)) {
slouken@2159
   205
            SDL_Error(SDL_EFREAD);
slouken@2159
   206
            return 0;
slouken@2159
   207
        }
slouken@2159
   208
        read_ahead = SDL_min(total_need, (int) byte_read);
slouken@2159
   209
        SDL_memcpy(ptr, context->hidden.win32io.buffer.data, read_ahead);
slouken@2159
   210
        context->hidden.win32io.buffer.size = byte_read;
slouken@2159
   211
        context->hidden.win32io.buffer.left = byte_read - read_ahead;
slouken@2159
   212
        total_read += read_ahead;
slouken@2159
   213
    } else {
slouken@2159
   214
        if (!ReadFile
slouken@2159
   215
            (context->hidden.win32io.h, ptr, total_need, &byte_read, NULL)) {
slouken@2159
   216
            SDL_Error(SDL_EFREAD);
slouken@2159
   217
            return 0;
slouken@2159
   218
        }
slouken@2159
   219
        total_read += byte_read;
slouken@2159
   220
    }
slouken@2159
   221
    return (total_read / size);
slouken@1446
   222
}
slouken@2160
   223
static size_t SDLCALL
slouken@2160
   224
win32_file_write(SDL_RWops * context, const void *ptr, size_t size,
slouken@2160
   225
                 size_t num)
slouken@1465
   226
{
slouken@1446
   227
slouken@2160
   228
    size_t total_bytes;
slouken@1895
   229
    DWORD byte_written, nwritten;
slouken@1446
   230
slouken@1895
   231
    total_bytes = size * num;
slouken@1895
   232
slouken@1895
   233
    if (!context || context->hidden.win32io.h == INVALID_HANDLE_VALUE
slouken@1895
   234
        || total_bytes <= 0 || !size)
slouken@1895
   235
        return 0;
slouken@1895
   236
slouken@2159
   237
    if (context->hidden.win32io.buffer.left) {
slouken@2159
   238
        SetFilePointer(context->hidden.win32io.h,
slouken@2159
   239
                       -context->hidden.win32io.buffer.left, NULL,
slouken@2159
   240
                       FILE_CURRENT);
slouken@2159
   241
        context->hidden.win32io.buffer.left = 0;
slouken@2159
   242
    }
slouken@2159
   243
slouken@1895
   244
    /* if in append mode, we must go to the EOF before write */
slouken@1895
   245
    if (context->hidden.win32io.append) {
slouken@2159
   246
        if (SetFilePointer(context->hidden.win32io.h, 0L, NULL, FILE_END) ==
slouken@2159
   247
            INVALID_SET_FILE_POINTER) {
slouken@1895
   248
            SDL_Error(SDL_EFWRITE);
slouken@1895
   249
            return 0;
slouken@1895
   250
        }
slouken@1895
   251
    }
slouken@1895
   252
slouken@1895
   253
    if (!WriteFile
slouken@1895
   254
        (context->hidden.win32io.h, ptr, total_bytes, &byte_written, NULL)) {
slouken@1895
   255
        SDL_Error(SDL_EFWRITE);
slouken@1895
   256
        return 0;
slouken@1895
   257
    }
slouken@1895
   258
slouken@1895
   259
    nwritten = byte_written / size;
slouken@1895
   260
    return nwritten;
slouken@1446
   261
}
slouken@1895
   262
static int SDLCALL
slouken@1895
   263
win32_file_close(SDL_RWops * context)
slouken@1465
   264
{
slouken@1895
   265
slouken@1895
   266
    if (context) {
slouken@1895
   267
        if (context->hidden.win32io.h != INVALID_HANDLE_VALUE) {
slouken@1895
   268
            CloseHandle(context->hidden.win32io.h);
slouken@1895
   269
            context->hidden.win32io.h = INVALID_HANDLE_VALUE;   /* to be sure */
slouken@1895
   270
        }
slouken@2159
   271
        if (context->hidden.win32io.buffer.data) {
slouken@2159
   272
            SDL_free(context->hidden.win32io.buffer.data);
slouken@2159
   273
            context->hidden.win32io.buffer.data = NULL;
slouken@2159
   274
        }
slouken@1895
   275
        SDL_FreeRW(context);
slouken@1895
   276
    }
slouken@1895
   277
    return (0);
slouken@1446
   278
}
slouken@1447
   279
#endif /* __WIN32__ */
slouken@1446
   280
slouken@1447
   281
#ifdef HAVE_STDIO_H
slouken@1446
   282
slouken@1447
   283
/* Functions to read/write stdio file pointers */
slouken@1446
   284
slouken@2160
   285
static long SDLCALL
slouken@2160
   286
stdio_seek(SDL_RWops * context, long offset, int whence)
slouken@1447
   287
{
slouken@1895
   288
    if (fseek(context->hidden.stdio.fp, offset, whence) == 0) {
slouken@1895
   289
        return (ftell(context->hidden.stdio.fp));
slouken@1895
   290
    } else {
slouken@1895
   291
        SDL_Error(SDL_EFSEEK);
slouken@1895
   292
        return (-1);
slouken@1895
   293
    }
slouken@1447
   294
}
slouken@2160
   295
static size_t SDLCALL
slouken@2160
   296
stdio_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
slouken@1447
   297
{
slouken@1895
   298
    size_t nread;
slouken@1447
   299
slouken@1895
   300
    nread = fread(ptr, size, maxnum, context->hidden.stdio.fp);
slouken@1895
   301
    if (nread == 0 && ferror(context->hidden.stdio.fp)) {
slouken@1895
   302
        SDL_Error(SDL_EFREAD);
slouken@1895
   303
    }
slouken@1895
   304
    return (nread);
slouken@1447
   305
}
slouken@2160
   306
static size_t SDLCALL
slouken@2160
   307
stdio_write(SDL_RWops * context, const void *ptr, size_t size, size_t num)
slouken@1447
   308
{
slouken@1895
   309
    size_t nwrote;
slouken@1447
   310
slouken@1895
   311
    nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
slouken@1895
   312
    if (nwrote == 0 && ferror(context->hidden.stdio.fp)) {
slouken@1895
   313
        SDL_Error(SDL_EFWRITE);
slouken@1895
   314
    }
slouken@1895
   315
    return (nwrote);
slouken@1447
   316
}
slouken@1895
   317
static int SDLCALL
slouken@1895
   318
stdio_close(SDL_RWops * context)
slouken@1447
   319
{
slouken@2160
   320
    int status = 0;
slouken@1895
   321
    if (context) {
slouken@1895
   322
        if (context->hidden.stdio.autoclose) {
slouken@1895
   323
            /* WARNING:  Check the return value here! */
slouken@2160
   324
            if (fclose(context->hidden.stdio.fp) != 0) {
slouken@2160
   325
                SDL_Error(SDL_EFWRITE);
slouken@2160
   326
                status = -1;
slouken@2160
   327
            }
slouken@1895
   328
        }
slouken@1895
   329
        SDL_FreeRW(context);
slouken@1895
   330
    }
slouken@2160
   331
    return status;
slouken@1447
   332
}
slouken@1446
   333
#endif /* !HAVE_STDIO_H */
slouken@1330
   334
slouken@0
   335
/* Functions to read/write memory pointers */
slouken@0
   336
slouken@2160
   337
static long SDLCALL
slouken@2160
   338
mem_seek(SDL_RWops * context, long offset, int whence)
slouken@0
   339
{
slouken@1895
   340
    Uint8 *newpos;
slouken@0
   341
slouken@1895
   342
    switch (whence) {
slouken@1895
   343
    case RW_SEEK_SET:
slouken@1895
   344
        newpos = context->hidden.mem.base + offset;
slouken@1895
   345
        break;
slouken@1895
   346
    case RW_SEEK_CUR:
slouken@1895
   347
        newpos = context->hidden.mem.here + offset;
slouken@1895
   348
        break;
slouken@1895
   349
    case RW_SEEK_END:
slouken@1895
   350
        newpos = context->hidden.mem.stop + offset;
slouken@1895
   351
        break;
slouken@1895
   352
    default:
slouken@1895
   353
        SDL_SetError("Unknown value for 'whence'");
slouken@1895
   354
        return (-1);
slouken@1895
   355
    }
slouken@1895
   356
    if (newpos < context->hidden.mem.base) {
slouken@1895
   357
        newpos = context->hidden.mem.base;
slouken@1895
   358
    }
slouken@1895
   359
    if (newpos > context->hidden.mem.stop) {
slouken@1895
   360
        newpos = context->hidden.mem.stop;
slouken@1895
   361
    }
slouken@1895
   362
    context->hidden.mem.here = newpos;
slouken@1895
   363
    return (context->hidden.mem.here - context->hidden.mem.base);
slouken@0
   364
}
slouken@2160
   365
static size_t SDLCALL
slouken@2160
   366
mem_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
slouken@0
   367
{
slouken@1895
   368
    size_t total_bytes;
slouken@1895
   369
    size_t mem_available;
slouken@0
   370
slouken@1895
   371
    total_bytes = (maxnum * size);
slouken@1895
   372
    if ((maxnum <= 0) || (size <= 0)
slouken@1895
   373
        || ((total_bytes / maxnum) != (size_t) size)) {
slouken@1895
   374
        return 0;
slouken@1895
   375
    }
icculus@1078
   376
slouken@1895
   377
    mem_available = (context->hidden.mem.stop - context->hidden.mem.here);
slouken@1895
   378
    if (total_bytes > mem_available) {
slouken@1895
   379
        total_bytes = mem_available;
slouken@1895
   380
    }
icculus@1078
   381
slouken@1895
   382
    SDL_memcpy(ptr, context->hidden.mem.here, total_bytes);
slouken@1895
   383
    context->hidden.mem.here += total_bytes;
icculus@1078
   384
slouken@1895
   385
    return (total_bytes / size);
slouken@0
   386
}
slouken@2160
   387
static size_t SDLCALL
slouken@2160
   388
mem_write(SDL_RWops * context, const void *ptr, size_t size, size_t num)
slouken@0
   389
{
slouken@1895
   390
    if ((context->hidden.mem.here + (num * size)) > context->hidden.mem.stop) {
slouken@1895
   391
        num = (context->hidden.mem.stop - context->hidden.mem.here) / size;
slouken@1895
   392
    }
slouken@1895
   393
    SDL_memcpy(context->hidden.mem.here, ptr, num * size);
slouken@1895
   394
    context->hidden.mem.here += num * size;
slouken@1895
   395
    return (num);
slouken@0
   396
}
slouken@2160
   397
static size_t SDLCALL
slouken@2160
   398
mem_writeconst(SDL_RWops * context, const void *ptr, size_t size, size_t num)
slouken@764
   399
{
slouken@1895
   400
    SDL_SetError("Can't write to read-only memory");
slouken@1895
   401
    return (-1);
slouken@764
   402
}
slouken@1895
   403
static int SDLCALL
slouken@1895
   404
mem_close(SDL_RWops * context)
slouken@0
   405
{
slouken@1895
   406
    if (context) {
slouken@1895
   407
        SDL_FreeRW(context);
slouken@1895
   408
    }
slouken@1895
   409
    return (0);
slouken@0
   410
}
slouken@0
   411
slouken@1447
   412
slouken@0
   413
/* Functions to create SDL_RWops structures from various data sources */
slouken@0
   414
slouken@1895
   415
SDL_RWops *
slouken@1895
   416
SDL_RWFromFile(const char *file, const char *mode)
slouken@0
   417
{
slouken@1895
   418
    SDL_RWops *rwops = NULL;
slouken@1465
   419
#ifdef HAVE_STDIO_H
slouken@1895
   420
    FILE *fp = NULL;
slouken@1465
   421
#endif
slouken@1895
   422
    if (!file || !*file || !mode || !*mode) {
slouken@1895
   423
        SDL_SetError("SDL_RWFromFile(): No file or no mode specified");
slouken@1895
   424
        return NULL;
slouken@1895
   425
    }
slouken@1465
   426
#if defined(__WIN32__)
slouken@1895
   427
    rwops = SDL_AllocRW();
slouken@1895
   428
    if (!rwops)
slouken@1895
   429
        return NULL;            /* SDL_SetError already setup by SDL_AllocRW() */
slouken@2164
   430
    if (win32_file_open(rwops, file, mode) < 0) {
slouken@1895
   431
        SDL_FreeRW(rwops);
slouken@1895
   432
        return NULL;
slouken@1895
   433
    }
slouken@1895
   434
    rwops->seek = win32_file_seek;
slouken@1895
   435
    rwops->read = win32_file_read;
slouken@1895
   436
    rwops->write = win32_file_write;
slouken@1895
   437
    rwops->close = win32_file_close;
slouken@1447
   438
slouken@1447
   439
#elif HAVE_STDIO_H
slouken@0
   440
slouken@1895
   441
    fp = fopen(file, mode);
slouken@1895
   442
    if (fp == NULL) {
slouken@1895
   443
        SDL_SetError("Couldn't open %s", file);
slouken@1895
   444
    } else {
slouken@1895
   445
        rwops = SDL_RWFromFP(fp, 1);
slouken@1895
   446
    }
slouken@0
   447
#else
slouken@1895
   448
    SDL_SetError("SDL not compiled with stdio support");
slouken@1446
   449
#endif /* !HAVE_STDIO_H */
slouken@1447
   450
slouken@1895
   451
    return (rwops);
slouken@0
   452
}
slouken@0
   453
slouken@1330
   454
#ifdef HAVE_STDIO_H
slouken@1895
   455
SDL_RWops *
slouken@2161
   456
SDL_RWFromFP(FILE * fp, SDL_bool autoclose)
slouken@0
   457
{
slouken@1895
   458
    SDL_RWops *rwops = NULL;
slouken@0
   459
slouken@1895
   460
    rwops = SDL_AllocRW();
slouken@1895
   461
    if (rwops != NULL) {
slouken@1895
   462
        rwops->seek = stdio_seek;
slouken@1895
   463
        rwops->read = stdio_read;
slouken@1895
   464
        rwops->write = stdio_write;
slouken@1895
   465
        rwops->close = stdio_close;
slouken@1895
   466
        rwops->hidden.stdio.fp = fp;
slouken@1895
   467
        rwops->hidden.stdio.autoclose = autoclose;
slouken@1895
   468
    }
slouken@1895
   469
    return (rwops);
slouken@0
   470
}
slouken@1330
   471
#endif /* HAVE_STDIO_H */
slouken@0
   472
slouken@1895
   473
SDL_RWops *
slouken@1895
   474
SDL_RWFromMem(void *mem, int size)
slouken@0
   475
{
slouken@1895
   476
    SDL_RWops *rwops;
slouken@0
   477
slouken@1895
   478
    rwops = SDL_AllocRW();
slouken@1895
   479
    if (rwops != NULL) {
slouken@1895
   480
        rwops->seek = mem_seek;
slouken@1895
   481
        rwops->read = mem_read;
slouken@1895
   482
        rwops->write = mem_write;
slouken@1895
   483
        rwops->close = mem_close;
slouken@1895
   484
        rwops->hidden.mem.base = (Uint8 *) mem;
slouken@1895
   485
        rwops->hidden.mem.here = rwops->hidden.mem.base;
slouken@1895
   486
        rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
slouken@1895
   487
    }
slouken@1895
   488
    return (rwops);
slouken@0
   489
}
slouken@0
   490
slouken@1895
   491
SDL_RWops *
slouken@1895
   492
SDL_RWFromConstMem(const void *mem, int size)
slouken@764
   493
{
slouken@1895
   494
    SDL_RWops *rwops;
slouken@764
   495
slouken@1895
   496
    rwops = SDL_AllocRW();
slouken@1895
   497
    if (rwops != NULL) {
slouken@1895
   498
        rwops->seek = mem_seek;
slouken@1895
   499
        rwops->read = mem_read;
slouken@1895
   500
        rwops->write = mem_writeconst;
slouken@1895
   501
        rwops->close = mem_close;
slouken@1895
   502
        rwops->hidden.mem.base = (Uint8 *) mem;
slouken@1895
   503
        rwops->hidden.mem.here = rwops->hidden.mem.base;
slouken@1895
   504
        rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
slouken@1895
   505
    }
slouken@1895
   506
    return (rwops);
slouken@764
   507
}
slouken@764
   508
slouken@1895
   509
SDL_RWops *
slouken@1895
   510
SDL_AllocRW(void)
slouken@0
   511
{
slouken@1895
   512
    SDL_RWops *area;
slouken@0
   513
slouken@1895
   514
    area = (SDL_RWops *) SDL_malloc(sizeof *area);
slouken@1895
   515
    if (area == NULL) {
slouken@1895
   516
        SDL_OutOfMemory();
slouken@1895
   517
    }
slouken@1895
   518
    return (area);
slouken@0
   519
}
slouken@0
   520
slouken@1895
   521
void
slouken@1895
   522
SDL_FreeRW(SDL_RWops * area)
slouken@0
   523
{
slouken@1895
   524
    SDL_free(area);
slouken@0
   525
}
slouken@1354
   526
slouken@1354
   527
/* Functions for dynamically reading and writing endian-specific values */
slouken@1354
   528
slouken@1895
   529
Uint16
slouken@1895
   530
SDL_ReadLE16(SDL_RWops * src)
slouken@1354
   531
{
slouken@1895
   532
    Uint16 value;
slouken@1354
   533
slouken@1895
   534
    SDL_RWread(src, &value, (sizeof value), 1);
slouken@1895
   535
    return (SDL_SwapLE16(value));
slouken@1354
   536
}
slouken@1354
   537
slouken@1895
   538
Uint16
slouken@1895
   539
SDL_ReadBE16(SDL_RWops * src)
slouken@1354
   540
{
slouken@1895
   541
    Uint16 value;
slouken@1895
   542
slouken@1895
   543
    SDL_RWread(src, &value, (sizeof value), 1);
slouken@1895
   544
    return (SDL_SwapBE16(value));
slouken@1354
   545
}
slouken@1895
   546
slouken@1895
   547
Uint32
slouken@1895
   548
SDL_ReadLE32(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_SwapLE32(value));
slouken@1354
   554
}
slouken@1895
   555
slouken@1895
   556
Uint32
slouken@1895
   557
SDL_ReadBE32(SDL_RWops * src)
slouken@1354
   558
{
slouken@1895
   559
    Uint32 value;
slouken@1895
   560
slouken@1895
   561
    SDL_RWread(src, &value, (sizeof value), 1);
slouken@1895
   562
    return (SDL_SwapBE32(value));
slouken@1354
   563
}
slouken@1895
   564
slouken@1895
   565
Uint64
slouken@1895
   566
SDL_ReadLE64(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_SwapLE64(value));
slouken@1354
   572
}
slouken@1895
   573
slouken@1895
   574
Uint64
slouken@1895
   575
SDL_ReadBE64(SDL_RWops * src)
slouken@1354
   576
{
slouken@1895
   577
    Uint64 value;
slouken@1895
   578
slouken@1895
   579
    SDL_RWread(src, &value, (sizeof value), 1);
slouken@1895
   580
    return (SDL_SwapBE64(value));
slouken@1354
   581
}
slouken@1895
   582
slouken@1895
   583
int
slouken@1895
   584
SDL_WriteLE16(SDL_RWops * dst, Uint16 value)
slouken@1354
   585
{
slouken@1895
   586
    value = SDL_SwapLE16(value);
slouken@1895
   587
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1354
   588
}
slouken@1895
   589
slouken@1895
   590
int
slouken@1895
   591
SDL_WriteBE16(SDL_RWops * dst, Uint16 value)
slouken@1895
   592
{
slouken@1895
   593
    value = SDL_SwapBE16(value);
slouken@1895
   594
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1895
   595
}
slouken@1895
   596
slouken@1895
   597
int
slouken@1895
   598
SDL_WriteLE32(SDL_RWops * dst, Uint32 value)
slouken@1895
   599
{
slouken@1895
   600
    value = SDL_SwapLE32(value);
slouken@1895
   601
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1895
   602
}
slouken@1895
   603
slouken@1895
   604
int
slouken@1895
   605
SDL_WriteBE32(SDL_RWops * dst, Uint32 value)
slouken@1895
   606
{
slouken@1895
   607
    value = SDL_SwapBE32(value);
slouken@1895
   608
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1895
   609
}
slouken@1895
   610
slouken@1895
   611
int
slouken@1895
   612
SDL_WriteLE64(SDL_RWops * dst, Uint64 value)
slouken@1895
   613
{
slouken@1895
   614
    value = SDL_SwapLE64(value);
slouken@1895
   615
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1895
   616
}
slouken@1895
   617
slouken@1895
   618
int
slouken@1895
   619
SDL_WriteBE64(SDL_RWops * dst, Uint64 value)
slouken@1895
   620
{
slouken@1895
   621
    value = SDL_SwapBE64(value);
slouken@1895
   622
    return (SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1895
   623
}
slouken@1895
   624
slouken@1895
   625
/* vi: set ts=4 sw=4 expandtab: */