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