src/file/SDL_rwops.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 27 Feb 2006 03:48:48 +0000
changeset 1446 47bf1767c4ca
parent 1402 d910939febfa
child 1447 515df0086eb7
permissions -rw-r--r--
Date: Mon, 27 Feb 2006 02:17:29 +0100
From: "William Petiot [exoide]"
Subject: Re: [SDL] cvs version: questions regarding HAVE_STDIO_H in standard w

I managed to get a first implementation of it, which I tested for "simple" use
with tests sample apps, they work ok. I think this needs more tests.

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