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