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