src/file/SDL_rwops.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 01 Feb 2006 06:32:25 +0000
changeset 1312 c9b51268668f
parent 1269 905d5b482f2a
child 1330 450721ad5436
permissions -rw-r--r--
Updated copyright information and removed rcs id lines (problematic in branch merges)
I batch edited these files, so please let me know if I've accidentally removed anybody's
credit here.
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@0
    27
#include <stdlib.h>
slouken@36
    28
#include <stdio.h>
slouken@0
    29
#include <string.h>
slouken@0
    30
slouken@0
    31
#include "SDL_error.h"
slouken@0
    32
#include "SDL_rwops.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@0
    76
slouken@0
    77
/* Functions to read/write memory pointers */
slouken@0
    78
slouken@0
    79
static int mem_seek(SDL_RWops *context, int offset, int whence)
slouken@0
    80
{
slouken@0
    81
	Uint8 *newpos;
slouken@0
    82
slouken@0
    83
	switch (whence) {
slouken@0
    84
		case SEEK_SET:
slouken@0
    85
			newpos = context->hidden.mem.base+offset;
slouken@0
    86
			break;
slouken@0
    87
		case SEEK_CUR:
slouken@0
    88
			newpos = context->hidden.mem.here+offset;
slouken@0
    89
			break;
slouken@0
    90
		case SEEK_END:
slouken@0
    91
			newpos = context->hidden.mem.stop+offset;
slouken@0
    92
			break;
slouken@0
    93
		default:
slouken@0
    94
			SDL_SetError("Unknown value for 'whence'");
slouken@0
    95
			return(-1);
slouken@0
    96
	}
slouken@0
    97
	if ( newpos < context->hidden.mem.base ) {
slouken@0
    98
		newpos = context->hidden.mem.base;
slouken@0
    99
	}
slouken@0
   100
	if ( newpos > context->hidden.mem.stop ) {
slouken@0
   101
		newpos = context->hidden.mem.stop;
slouken@0
   102
	}
slouken@0
   103
	context->hidden.mem.here = newpos;
slouken@0
   104
	return(context->hidden.mem.here-context->hidden.mem.base);
slouken@0
   105
}
slouken@0
   106
static int mem_read(SDL_RWops *context, void *ptr, int size, int maxnum)
slouken@0
   107
{
icculus@1078
   108
	int total_bytes;
icculus@1078
   109
	int mem_available;
slouken@0
   110
icculus@1078
   111
	total_bytes = (maxnum * size);
icculus@1078
   112
	if ( (maxnum <= 0) || (size <= 0) || ((total_bytes / maxnum) != size) ) {
icculus@1078
   113
		return 0;
slouken@0
   114
	}
icculus@1078
   115
icculus@1078
   116
	mem_available = (context->hidden.mem.stop - context->hidden.mem.here);
icculus@1078
   117
	if (total_bytes > mem_available) {
icculus@1078
   118
		total_bytes = mem_available;
icculus@1078
   119
	}
icculus@1078
   120
icculus@1078
   121
	memcpy(ptr, context->hidden.mem.here, total_bytes);
icculus@1078
   122
	context->hidden.mem.here += total_bytes;
icculus@1078
   123
icculus@1078
   124
	return (total_bytes / size);
slouken@0
   125
}
slouken@0
   126
static int mem_write(SDL_RWops *context, const void *ptr, int size, int num)
slouken@0
   127
{
slouken@0
   128
	if ( (context->hidden.mem.here + (num*size)) > context->hidden.mem.stop ) {
slouken@0
   129
		num = (context->hidden.mem.stop-context->hidden.mem.here)/size;
slouken@0
   130
	}
slouken@0
   131
	memcpy(context->hidden.mem.here, ptr, num*size);
slouken@0
   132
	context->hidden.mem.here += num*size;
slouken@0
   133
	return(num);
slouken@0
   134
}
slouken@764
   135
static int mem_writeconst(SDL_RWops *context, const void *ptr, int size, int num)
slouken@764
   136
{
slouken@764
   137
	SDL_SetError("Can't write to read-only memory");
slouken@764
   138
	return(-1);
slouken@764
   139
}
slouken@0
   140
static int mem_close(SDL_RWops *context)
slouken@0
   141
{
slouken@0
   142
	if ( context ) {
icculus@1202
   143
		SDL_FreeRW(context);
slouken@0
   144
	}
slouken@0
   145
	return(0);
slouken@0
   146
}
slouken@0
   147
slouken@0
   148
/* Functions to create SDL_RWops structures from various data sources */
slouken@0
   149
#ifdef WIN32
slouken@0
   150
/* Aggh.  You can't (apparently) open a file in an application and
slouken@0
   151
   read from it in a DLL.
slouken@0
   152
*/
slouken@0
   153
static int in_sdl = 0;
slouken@0
   154
#endif
slouken@0
   155
slouken@0
   156
#ifdef macintosh
slouken@0
   157
/*
slouken@0
   158
 * translate unix-style slash-separated filename to mac-style colon-separated
slouken@0
   159
 * name; return malloced string
slouken@0
   160
 */
slouken@0
   161
static char *unix_to_mac(const char *file)
slouken@0
   162
{
slouken@0
   163
	int flen = strlen(file);
slouken@0
   164
	char *path = malloc(flen + 2);
slouken@0
   165
	const char *src = file;
slouken@0
   166
	char *dst = path;
slouken@0
   167
	if(*src == '/') {
slouken@0
   168
		/* really depends on filesystem layout, hope for the best */
slouken@0
   169
		src++;
slouken@0
   170
	} else {
slouken@0
   171
		/* Check if this is a MacOS path to begin with */
slouken@0
   172
		if(*src != ':')
slouken@0
   173
			*dst++ = ':';   /* relative paths begin with ':' */
slouken@0
   174
	}
slouken@0
   175
	while(src < file + flen) {
slouken@0
   176
		const char *end = strchr(src, '/');
slouken@0
   177
		int len;
slouken@0
   178
		if(!end)
slouken@0
   179
			end = file + flen; /* last component */
slouken@0
   180
		len = end - src;
slouken@0
   181
		if(len == 0 || (len == 1 && src[0] == '.')) {
slouken@0
   182
			/* remove repeated slashes and . */
slouken@0
   183
		} else {
slouken@0
   184
			if(len == 2 && src[0] == '.' && src[1] == '.') {
slouken@0
   185
				/* replace .. with the empty string */
slouken@0
   186
			} else {
slouken@0
   187
				memcpy(dst, src, len);
slouken@0
   188
				dst += len;
slouken@0
   189
			}
slouken@0
   190
			if(end < file + flen)
slouken@0
   191
				*dst++ = ':';
slouken@0
   192
		}
slouken@0
   193
		src = end + 1;
slouken@0
   194
	}
slouken@0
   195
	*dst++ = '\0';
slouken@0
   196
	return path;
slouken@0
   197
}
slouken@0
   198
#endif /* macintosh */
slouken@0
   199
slouken@0
   200
SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
slouken@0
   201
{
slouken@0
   202
	FILE *fp;
slouken@0
   203
	SDL_RWops *rwops;
slouken@0
   204
slouken@0
   205
	rwops = NULL;
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@0
   211
		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@0
   227
	return(rwops);
slouken@0
   228
}
slouken@0
   229
slouken@0
   230
SDL_RWops *SDL_RWFromFP(FILE *fp, int autoclose)
slouken@0
   231
{
slouken@0
   232
	SDL_RWops *rwops;
slouken@0
   233
slouken@0
   234
#ifdef WIN32
slouken@0
   235
	if ( ! in_sdl ) {
slouken@1269
   236
		/* It's when SDL and the app are compiled with different C runtimes */
slouken@543
   237
		SDL_SetError("You can't pass a FILE pointer to a DLL (?)");
slouken@0
   238
		/*return(NULL);*/
slouken@0
   239
	}
slouken@0
   240
#endif
slouken@0
   241
	rwops = SDL_AllocRW();
slouken@0
   242
	if ( rwops != NULL ) {
slouken@0
   243
		rwops->seek = stdio_seek;
slouken@0
   244
		rwops->read = stdio_read;
slouken@0
   245
		rwops->write = stdio_write;
slouken@0
   246
		rwops->close = stdio_close;
slouken@0
   247
		rwops->hidden.stdio.fp = fp;
slouken@0
   248
		rwops->hidden.stdio.autoclose = autoclose;
slouken@0
   249
	}
slouken@0
   250
	return(rwops);
slouken@0
   251
}
slouken@0
   252
slouken@0
   253
SDL_RWops *SDL_RWFromMem(void *mem, int size)
slouken@0
   254
{
slouken@0
   255
	SDL_RWops *rwops;
slouken@0
   256
slouken@0
   257
	rwops = SDL_AllocRW();
slouken@0
   258
	if ( rwops != NULL ) {
slouken@0
   259
		rwops->seek = mem_seek;
slouken@0
   260
		rwops->read = mem_read;
slouken@0
   261
		rwops->write = mem_write;
slouken@0
   262
		rwops->close = mem_close;
slouken@0
   263
		rwops->hidden.mem.base = (Uint8 *)mem;
slouken@0
   264
		rwops->hidden.mem.here = rwops->hidden.mem.base;
slouken@0
   265
		rwops->hidden.mem.stop = rwops->hidden.mem.base+size;
slouken@0
   266
	}
slouken@0
   267
	return(rwops);
slouken@0
   268
}
slouken@0
   269
slouken@764
   270
SDL_RWops *SDL_RWFromConstMem(const void *mem, int size)
slouken@764
   271
{
slouken@764
   272
	SDL_RWops *rwops;
slouken@764
   273
slouken@764
   274
	rwops = SDL_AllocRW();
slouken@764
   275
	if ( rwops != NULL ) {
slouken@764
   276
		rwops->seek = mem_seek;
slouken@764
   277
		rwops->read = mem_read;
slouken@764
   278
		rwops->write = mem_writeconst;
slouken@764
   279
		rwops->close = mem_close;
slouken@764
   280
		rwops->hidden.mem.base = (Uint8 *)mem;
slouken@764
   281
		rwops->hidden.mem.here = rwops->hidden.mem.base;
slouken@764
   282
		rwops->hidden.mem.stop = rwops->hidden.mem.base+size;
slouken@764
   283
	}
slouken@764
   284
	return(rwops);
slouken@764
   285
}
slouken@764
   286
slouken@0
   287
SDL_RWops *SDL_AllocRW(void)
slouken@0
   288
{
slouken@0
   289
	SDL_RWops *area;
slouken@0
   290
slouken@0
   291
	area = (SDL_RWops *)malloc(sizeof *area);
slouken@0
   292
	if ( area == NULL ) {
slouken@0
   293
		SDL_OutOfMemory();
slouken@0
   294
	}
slouken@0
   295
	return(area);
slouken@0
   296
}
slouken@0
   297
slouken@0
   298
void SDL_FreeRW(SDL_RWops *area)
slouken@0
   299
{
slouken@0
   300
	free(area);
slouken@0
   301
}