src/file/SDL_rwops.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 24 Jan 2011 21:20:30 -0800
changeset 5090 327f181542f1
parent 5086 c2539ff054c8
child 5262 b530ef003506
permissions -rw-r--r--
Include windows.h in a single point in the source, so we can be consistent about the definition of UNICODE and have core utility functions for Windows that all modules can share.

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