src/file/SDL_rwops.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 16 Feb 2006 10:11:48 +0000
changeset 1361 19418e4422cb
parent 1358 c71e05b4dc2e
child 1402 d910939febfa
permissions -rw-r--r--
New configure-based build system. Still work in progress, but much improved
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@0
    22
slouken@0
    23
/* This file provides a general interface for SDL to read and write
slouken@0
    24
   data sources.  It can easily be extended to files, memory, etc.
slouken@0
    25
*/
slouken@0
    26
slouken@1354
    27
#include "SDL_endian.h"
slouken@0
    28
#include "SDL_rwops.h"
slouken@1330
    29
slouken@1354
    30
slouken@1330
    31
#ifdef HAVE_STDIO_H
slouken@0
    32
slouken@0
    33
/* Functions to read/write stdio file pointers */
slouken@0
    34
slouken@0
    35
static int stdio_seek(SDL_RWops *context, int offset, int whence)
slouken@0
    36
{
slouken@0
    37
	if ( fseek(context->hidden.stdio.fp, offset, whence) == 0 ) {
slouken@0
    38
		return(ftell(context->hidden.stdio.fp));
slouken@0
    39
	} else {
slouken@0
    40
		SDL_Error(SDL_EFSEEK);
slouken@0
    41
		return(-1);
slouken@0
    42
	}
slouken@0
    43
}
slouken@0
    44
static int stdio_read(SDL_RWops *context, void *ptr, int size, int maxnum)
slouken@0
    45
{
slouken@0
    46
	size_t nread;
slouken@0
    47
slouken@0
    48
	nread = fread(ptr, size, maxnum, context->hidden.stdio.fp); 
slouken@0
    49
	if ( nread == 0 && ferror(context->hidden.stdio.fp) ) {
slouken@0
    50
		SDL_Error(SDL_EFREAD);
slouken@0
    51
	}
slouken@0
    52
	return(nread);
slouken@0
    53
}
slouken@0
    54
static int stdio_write(SDL_RWops *context, const void *ptr, int size, int num)
slouken@0
    55
{
slouken@0
    56
	size_t nwrote;
slouken@0
    57
slouken@0
    58
	nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
slouken@0
    59
	if ( nwrote == 0 && ferror(context->hidden.stdio.fp) ) {
slouken@0
    60
		SDL_Error(SDL_EFWRITE);
slouken@0
    61
	}
slouken@0
    62
	return(nwrote);
slouken@0
    63
}
slouken@0
    64
static int stdio_close(SDL_RWops *context)
slouken@0
    65
{
slouken@0
    66
	if ( context ) {
slouken@0
    67
		if ( context->hidden.stdio.autoclose ) {
slouken@0
    68
			/* WARNING:  Check the return value here! */
slouken@0
    69
			fclose(context->hidden.stdio.fp);
slouken@0
    70
		}
icculus@1202
    71
		SDL_FreeRW(context);
slouken@0
    72
	}
slouken@0
    73
	return(0);
slouken@0
    74
}
slouken@0
    75
slouken@1330
    76
#endif /* HAVE_STDIO_H */
slouken@1330
    77
slouken@0
    78
/* Functions to read/write memory pointers */
slouken@0
    79
slouken@0
    80
static int mem_seek(SDL_RWops *context, int offset, int whence)
slouken@0
    81
{
slouken@0
    82
	Uint8 *newpos;
slouken@0
    83
slouken@0
    84
	switch (whence) {
slouken@1330
    85
		case RW_SEEK_SET:
slouken@0
    86
			newpos = context->hidden.mem.base+offset;
slouken@0
    87
			break;
slouken@1330
    88
		case RW_SEEK_CUR:
slouken@0
    89
			newpos = context->hidden.mem.here+offset;
slouken@0
    90
			break;
slouken@1330
    91
		case RW_SEEK_END:
slouken@0
    92
			newpos = context->hidden.mem.stop+offset;
slouken@0
    93
			break;
slouken@0
    94
		default:
slouken@0
    95
			SDL_SetError("Unknown value for 'whence'");
slouken@0
    96
			return(-1);
slouken@0
    97
	}
slouken@0
    98
	if ( newpos < context->hidden.mem.base ) {
slouken@0
    99
		newpos = context->hidden.mem.base;
slouken@0
   100
	}
slouken@0
   101
	if ( newpos > context->hidden.mem.stop ) {
slouken@0
   102
		newpos = context->hidden.mem.stop;
slouken@0
   103
	}
slouken@0
   104
	context->hidden.mem.here = newpos;
slouken@0
   105
	return(context->hidden.mem.here-context->hidden.mem.base);
slouken@0
   106
}
slouken@0
   107
static int mem_read(SDL_RWops *context, void *ptr, int size, int maxnum)
slouken@0
   108
{
icculus@1078
   109
	int total_bytes;
icculus@1078
   110
	int mem_available;
slouken@0
   111
icculus@1078
   112
	total_bytes = (maxnum * size);
icculus@1078
   113
	if ( (maxnum <= 0) || (size <= 0) || ((total_bytes / maxnum) != size) ) {
icculus@1078
   114
		return 0;
slouken@0
   115
	}
icculus@1078
   116
icculus@1078
   117
	mem_available = (context->hidden.mem.stop - context->hidden.mem.here);
icculus@1078
   118
	if (total_bytes > mem_available) {
icculus@1078
   119
		total_bytes = mem_available;
icculus@1078
   120
	}
icculus@1078
   121
slouken@1336
   122
	SDL_memcpy(ptr, context->hidden.mem.here, total_bytes);
icculus@1078
   123
	context->hidden.mem.here += total_bytes;
icculus@1078
   124
icculus@1078
   125
	return (total_bytes / size);
slouken@0
   126
}
slouken@0
   127
static int mem_write(SDL_RWops *context, const void *ptr, int size, int num)
slouken@0
   128
{
slouken@0
   129
	if ( (context->hidden.mem.here + (num*size)) > context->hidden.mem.stop ) {
slouken@0
   130
		num = (context->hidden.mem.stop-context->hidden.mem.here)/size;
slouken@0
   131
	}
slouken@1336
   132
	SDL_memcpy(context->hidden.mem.here, ptr, num*size);
slouken@0
   133
	context->hidden.mem.here += num*size;
slouken@0
   134
	return(num);
slouken@0
   135
}
slouken@764
   136
static int mem_writeconst(SDL_RWops *context, const void *ptr, int size, int num)
slouken@764
   137
{
slouken@764
   138
	SDL_SetError("Can't write to read-only memory");
slouken@764
   139
	return(-1);
slouken@764
   140
}
slouken@0
   141
static int mem_close(SDL_RWops *context)
slouken@0
   142
{
slouken@0
   143
	if ( context ) {
icculus@1202
   144
		SDL_FreeRW(context);
slouken@0
   145
	}
slouken@0
   146
	return(0);
slouken@0
   147
}
slouken@0
   148
slouken@0
   149
/* Functions to create SDL_RWops structures from various data sources */
slouken@0
   150
#ifdef WIN32
slouken@0
   151
/* Aggh.  You can't (apparently) open a file in an application and
slouken@0
   152
   read from it in a DLL.
slouken@0
   153
*/
slouken@0
   154
static int in_sdl = 0;
slouken@0
   155
#endif
slouken@0
   156
slouken@0
   157
#ifdef macintosh
slouken@0
   158
/*
slouken@0
   159
 * translate unix-style slash-separated filename to mac-style colon-separated
slouken@0
   160
 * name; return malloced string
slouken@0
   161
 */
slouken@0
   162
static char *unix_to_mac(const char *file)
slouken@0
   163
{
slouken@1336
   164
	int flen = SDL_strlen(file);
slouken@1336
   165
	char *path = SDL_malloc(flen + 2);
slouken@0
   166
	const char *src = file;
slouken@0
   167
	char *dst = path;
slouken@0
   168
	if(*src == '/') {
slouken@0
   169
		/* really depends on filesystem layout, hope for the best */
slouken@0
   170
		src++;
slouken@0
   171
	} else {
slouken@0
   172
		/* Check if this is a MacOS path to begin with */
slouken@0
   173
		if(*src != ':')
slouken@0
   174
			*dst++ = ':';   /* relative paths begin with ':' */
slouken@0
   175
	}
slouken@0
   176
	while(src < file + flen) {
slouken@1336
   177
		const char *end = SDL_strchr(src, '/');
slouken@0
   178
		int len;
slouken@0
   179
		if(!end)
slouken@0
   180
			end = file + flen; /* last component */
slouken@0
   181
		len = end - src;
slouken@0
   182
		if(len == 0 || (len == 1 && src[0] == '.')) {
slouken@0
   183
			/* remove repeated slashes and . */
slouken@0
   184
		} else {
slouken@0
   185
			if(len == 2 && src[0] == '.' && src[1] == '.') {
slouken@0
   186
				/* replace .. with the empty string */
slouken@0
   187
			} else {
slouken@1336
   188
				SDL_memcpy(dst, src, len);
slouken@0
   189
				dst += len;
slouken@0
   190
			}
slouken@0
   191
			if(end < file + flen)
slouken@0
   192
				*dst++ = ':';
slouken@0
   193
		}
slouken@0
   194
		src = end + 1;
slouken@0
   195
	}
slouken@0
   196
	*dst++ = '\0';
slouken@0
   197
	return path;
slouken@0
   198
}
slouken@0
   199
#endif /* macintosh */
slouken@0
   200
slouken@0
   201
SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
slouken@0
   202
{
slouken@1330
   203
	SDL_RWops *rwops = NULL;
slouken@1330
   204
#ifdef HAVE_STDIO_H
slouken@0
   205
	FILE *fp;
slouken@0
   206
slouken@0
   207
#ifdef macintosh
slouken@0
   208
	{
slouken@0
   209
		char *mpath = unix_to_mac(file);
slouken@0
   210
		fp = fopen(mpath, mode);
slouken@1336
   211
		SDL_free(mpath);
slouken@0
   212
	}
slouken@0
   213
#else
slouken@0
   214
	fp = fopen(file, mode);
slouken@0
   215
#endif
slouken@0
   216
	if ( fp == NULL ) {
slouken@0
   217
		SDL_SetError("Couldn't open %s", file);
slouken@0
   218
	} else {
slouken@0
   219
#ifdef WIN32
slouken@0
   220
		in_sdl = 1;
slouken@0
   221
		rwops = SDL_RWFromFP(fp, 1);
slouken@0
   222
		in_sdl = 0;
slouken@0
   223
#else
slouken@0
   224
		rwops = SDL_RWFromFP(fp, 1);
slouken@0
   225
#endif
slouken@0
   226
	}
slouken@1330
   227
#endif /* HAVE_STDIO_H */
slouken@0
   228
	return(rwops);
slouken@0
   229
}
slouken@0
   230
slouken@1330
   231
#ifdef HAVE_STDIO_H
slouken@0
   232
SDL_RWops *SDL_RWFromFP(FILE *fp, int autoclose)
slouken@0
   233
{
slouken@1330
   234
	SDL_RWops *rwops = NULL;
slouken@0
   235
slouken@0
   236
#ifdef WIN32
slouken@0
   237
	if ( ! in_sdl ) {
slouken@1269
   238
		/* It's when SDL and the app are compiled with different C runtimes */
slouken@543
   239
		SDL_SetError("You can't pass a FILE pointer to a DLL (?)");
slouken@0
   240
		/*return(NULL);*/
slouken@0
   241
	}
slouken@0
   242
#endif
slouken@0
   243
	rwops = SDL_AllocRW();
slouken@0
   244
	if ( rwops != NULL ) {
slouken@0
   245
		rwops->seek = stdio_seek;
slouken@0
   246
		rwops->read = stdio_read;
slouken@0
   247
		rwops->write = stdio_write;
slouken@0
   248
		rwops->close = stdio_close;
slouken@0
   249
		rwops->hidden.stdio.fp = fp;
slouken@0
   250
		rwops->hidden.stdio.autoclose = autoclose;
slouken@0
   251
	}
slouken@0
   252
	return(rwops);
slouken@0
   253
}
slouken@1330
   254
#endif /* HAVE_STDIO_H */
slouken@0
   255
slouken@0
   256
SDL_RWops *SDL_RWFromMem(void *mem, int size)
slouken@0
   257
{
slouken@0
   258
	SDL_RWops *rwops;
slouken@0
   259
slouken@0
   260
	rwops = SDL_AllocRW();
slouken@0
   261
	if ( rwops != NULL ) {
slouken@0
   262
		rwops->seek = mem_seek;
slouken@0
   263
		rwops->read = mem_read;
slouken@0
   264
		rwops->write = mem_write;
slouken@0
   265
		rwops->close = mem_close;
slouken@0
   266
		rwops->hidden.mem.base = (Uint8 *)mem;
slouken@0
   267
		rwops->hidden.mem.here = rwops->hidden.mem.base;
slouken@0
   268
		rwops->hidden.mem.stop = rwops->hidden.mem.base+size;
slouken@0
   269
	}
slouken@0
   270
	return(rwops);
slouken@0
   271
}
slouken@0
   272
slouken@764
   273
SDL_RWops *SDL_RWFromConstMem(const void *mem, int size)
slouken@764
   274
{
slouken@764
   275
	SDL_RWops *rwops;
slouken@764
   276
slouken@764
   277
	rwops = SDL_AllocRW();
slouken@764
   278
	if ( rwops != NULL ) {
slouken@764
   279
		rwops->seek = mem_seek;
slouken@764
   280
		rwops->read = mem_read;
slouken@764
   281
		rwops->write = mem_writeconst;
slouken@764
   282
		rwops->close = mem_close;
slouken@764
   283
		rwops->hidden.mem.base = (Uint8 *)mem;
slouken@764
   284
		rwops->hidden.mem.here = rwops->hidden.mem.base;
slouken@764
   285
		rwops->hidden.mem.stop = rwops->hidden.mem.base+size;
slouken@764
   286
	}
slouken@764
   287
	return(rwops);
slouken@764
   288
}
slouken@764
   289
slouken@0
   290
SDL_RWops *SDL_AllocRW(void)
slouken@0
   291
{
slouken@0
   292
	SDL_RWops *area;
slouken@0
   293
slouken@1336
   294
	area = (SDL_RWops *)SDL_malloc(sizeof *area);
slouken@0
   295
	if ( area == NULL ) {
slouken@0
   296
		SDL_OutOfMemory();
slouken@0
   297
	}
slouken@0
   298
	return(area);
slouken@0
   299
}
slouken@0
   300
slouken@0
   301
void SDL_FreeRW(SDL_RWops *area)
slouken@0
   302
{
slouken@1336
   303
	SDL_free(area);
slouken@0
   304
}
slouken@1354
   305
slouken@1354
   306
/* Functions for dynamically reading and writing endian-specific values */
slouken@1354
   307
slouken@1354
   308
Uint16 SDL_ReadLE16 (SDL_RWops *src)
slouken@1354
   309
{
slouken@1354
   310
	Uint16 value;
slouken@1354
   311
slouken@1354
   312
	SDL_RWread(src, &value, (sizeof value), 1);
slouken@1354
   313
	return(SDL_SwapLE16(value));
slouken@1354
   314
}
slouken@1354
   315
Uint16 SDL_ReadBE16 (SDL_RWops *src)
slouken@1354
   316
{
slouken@1354
   317
	Uint16 value;
slouken@1354
   318
slouken@1354
   319
	SDL_RWread(src, &value, (sizeof value), 1);
slouken@1354
   320
	return(SDL_SwapBE16(value));
slouken@1354
   321
}
slouken@1354
   322
Uint32 SDL_ReadLE32 (SDL_RWops *src)
slouken@1354
   323
{
slouken@1354
   324
	Uint32 value;
slouken@1354
   325
slouken@1354
   326
	SDL_RWread(src, &value, (sizeof value), 1);
slouken@1354
   327
	return(SDL_SwapLE32(value));
slouken@1354
   328
}
slouken@1354
   329
Uint32 SDL_ReadBE32 (SDL_RWops *src)
slouken@1354
   330
{
slouken@1354
   331
	Uint32 value;
slouken@1354
   332
slouken@1354
   333
	SDL_RWread(src, &value, (sizeof value), 1);
slouken@1354
   334
	return(SDL_SwapBE32(value));
slouken@1354
   335
}
slouken@1354
   336
Uint64 SDL_ReadLE64 (SDL_RWops *src)
slouken@1354
   337
{
slouken@1354
   338
	Uint64 value;
slouken@1354
   339
slouken@1354
   340
	SDL_RWread(src, &value, (sizeof value), 1);
slouken@1354
   341
	return(SDL_SwapLE64(value));
slouken@1354
   342
}
slouken@1354
   343
Uint64 SDL_ReadBE64 (SDL_RWops *src)
slouken@1354
   344
{
slouken@1354
   345
	Uint64 value;
slouken@1354
   346
slouken@1354
   347
	SDL_RWread(src, &value, (sizeof value), 1);
slouken@1354
   348
	return(SDL_SwapBE64(value));
slouken@1354
   349
}
slouken@1354
   350
slouken@1354
   351
int SDL_WriteLE16 (SDL_RWops *dst, Uint16 value)
slouken@1354
   352
{
slouken@1354
   353
	value = SDL_SwapLE16(value);
slouken@1354
   354
	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1354
   355
}
slouken@1354
   356
int SDL_WriteBE16 (SDL_RWops *dst, Uint16 value)
slouken@1354
   357
{
slouken@1354
   358
	value = SDL_SwapBE16(value);
slouken@1354
   359
	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1354
   360
}
slouken@1354
   361
int SDL_WriteLE32 (SDL_RWops *dst, Uint32 value)
slouken@1354
   362
{
slouken@1354
   363
	value = SDL_SwapLE32(value);
slouken@1354
   364
	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1354
   365
}
slouken@1354
   366
int SDL_WriteBE32 (SDL_RWops *dst, Uint32 value)
slouken@1354
   367
{
slouken@1354
   368
	value = SDL_SwapBE32(value);
slouken@1354
   369
	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1354
   370
}
slouken@1354
   371
int SDL_WriteLE64 (SDL_RWops *dst, Uint64 value)
slouken@1354
   372
{
slouken@1354
   373
	value = SDL_SwapLE64(value);
slouken@1354
   374
	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1354
   375
}
slouken@1354
   376
int SDL_WriteBE64 (SDL_RWops *dst, Uint64 value)
slouken@1354
   377
{
slouken@1354
   378
	value = SDL_SwapBE64(value);
slouken@1354
   379
	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1354
   380
}