src/file/SDL_rwops.c
changeset 0 74212992fb08
child 36 13ee9f4834ea
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/file/SDL_rwops.c	Thu Apr 26 16:45:43 2001 +0000
     1.3 @@ -0,0 +1,274 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Library General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Library General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Library General Public
    1.19 +    License along with this library; if not, write to the Free
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@devolution.com
    1.24 +*/
    1.25 +
    1.26 +#ifdef SAVE_RCSID
    1.27 +static char rcsid =
    1.28 + "@(#) $Id$";
    1.29 +#endif
    1.30 +
    1.31 +/* This file provides a general interface for SDL to read and write
    1.32 +   data sources.  It can easily be extended to files, memory, etc.
    1.33 +*/
    1.34 +
    1.35 +#include <stdlib.h>
    1.36 +#include <string.h>
    1.37 +
    1.38 +#include "SDL_error.h"
    1.39 +#include "SDL_rwops.h"
    1.40 +
    1.41 +/* Functions to read/write stdio file pointers */
    1.42 +
    1.43 +static int stdio_seek(SDL_RWops *context, int offset, int whence)
    1.44 +{
    1.45 +	if ( fseek(context->hidden.stdio.fp, offset, whence) == 0 ) {
    1.46 +		return(ftell(context->hidden.stdio.fp));
    1.47 +	} else {
    1.48 +		SDL_Error(SDL_EFSEEK);
    1.49 +		return(-1);
    1.50 +	}
    1.51 +}
    1.52 +static int stdio_read(SDL_RWops *context, void *ptr, int size, int maxnum)
    1.53 +{
    1.54 +	size_t nread;
    1.55 +
    1.56 +	nread = fread(ptr, size, maxnum, context->hidden.stdio.fp); 
    1.57 +	if ( nread == 0 && ferror(context->hidden.stdio.fp) ) {
    1.58 +		SDL_Error(SDL_EFREAD);
    1.59 +	}
    1.60 +	return(nread);
    1.61 +}
    1.62 +static int stdio_write(SDL_RWops *context, const void *ptr, int size, int num)
    1.63 +{
    1.64 +	size_t nwrote;
    1.65 +
    1.66 +	nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
    1.67 +	if ( nwrote == 0 && ferror(context->hidden.stdio.fp) ) {
    1.68 +		SDL_Error(SDL_EFWRITE);
    1.69 +	}
    1.70 +	return(nwrote);
    1.71 +}
    1.72 +static int stdio_close(SDL_RWops *context)
    1.73 +{
    1.74 +	if ( context ) {
    1.75 +		if ( context->hidden.stdio.autoclose ) {
    1.76 +			/* WARNING:  Check the return value here! */
    1.77 +			fclose(context->hidden.stdio.fp);
    1.78 +		}
    1.79 +		free(context);
    1.80 +	}
    1.81 +	return(0);
    1.82 +}
    1.83 +
    1.84 +/* Functions to read/write memory pointers */
    1.85 +
    1.86 +static int mem_seek(SDL_RWops *context, int offset, int whence)
    1.87 +{
    1.88 +	Uint8 *newpos;
    1.89 +
    1.90 +	switch (whence) {
    1.91 +		case SEEK_SET:
    1.92 +			newpos = context->hidden.mem.base+offset;
    1.93 +			break;
    1.94 +		case SEEK_CUR:
    1.95 +			newpos = context->hidden.mem.here+offset;
    1.96 +			break;
    1.97 +		case SEEK_END:
    1.98 +			newpos = context->hidden.mem.stop+offset;
    1.99 +			break;
   1.100 +		default:
   1.101 +			SDL_SetError("Unknown value for 'whence'");
   1.102 +			return(-1);
   1.103 +	}
   1.104 +	if ( newpos < context->hidden.mem.base ) {
   1.105 +		newpos = context->hidden.mem.base;
   1.106 +	}
   1.107 +	if ( newpos > context->hidden.mem.stop ) {
   1.108 +		newpos = context->hidden.mem.stop;
   1.109 +	}
   1.110 +	context->hidden.mem.here = newpos;
   1.111 +	return(context->hidden.mem.here-context->hidden.mem.base);
   1.112 +}
   1.113 +static int mem_read(SDL_RWops *context, void *ptr, int size, int maxnum)
   1.114 +{
   1.115 +	int num;
   1.116 +
   1.117 +	num = maxnum;
   1.118 +	if ( (context->hidden.mem.here + (num*size)) > context->hidden.mem.stop ) {
   1.119 +		num = (context->hidden.mem.stop-context->hidden.mem.here)/size;
   1.120 +	}
   1.121 +	memcpy(ptr, context->hidden.mem.here, num*size);
   1.122 +	context->hidden.mem.here += num*size;
   1.123 +	return(num);
   1.124 +}
   1.125 +static int mem_write(SDL_RWops *context, const void *ptr, int size, int num)
   1.126 +{
   1.127 +	if ( (context->hidden.mem.here + (num*size)) > context->hidden.mem.stop ) {
   1.128 +		num = (context->hidden.mem.stop-context->hidden.mem.here)/size;
   1.129 +	}
   1.130 +	memcpy(context->hidden.mem.here, ptr, num*size);
   1.131 +	context->hidden.mem.here += num*size;
   1.132 +	return(num);
   1.133 +}
   1.134 +static int mem_close(SDL_RWops *context)
   1.135 +{
   1.136 +	if ( context ) {
   1.137 +		free(context);
   1.138 +	}
   1.139 +	return(0);
   1.140 +}
   1.141 +
   1.142 +/* Functions to create SDL_RWops structures from various data sources */
   1.143 +#ifdef WIN32
   1.144 +/* Aggh.  You can't (apparently) open a file in an application and
   1.145 +   read from it in a DLL.
   1.146 +*/
   1.147 +static int in_sdl = 0;
   1.148 +#endif
   1.149 +
   1.150 +#ifdef macintosh
   1.151 +/*
   1.152 + * translate unix-style slash-separated filename to mac-style colon-separated
   1.153 + * name; return malloced string
   1.154 + */
   1.155 +static char *unix_to_mac(const char *file)
   1.156 +{
   1.157 +	int flen = strlen(file);
   1.158 +	char *path = malloc(flen + 2);
   1.159 +	const char *src = file;
   1.160 +	char *dst = path;
   1.161 +	if(*src == '/') {
   1.162 +		/* really depends on filesystem layout, hope for the best */
   1.163 +		src++;
   1.164 +	} else {
   1.165 +		/* Check if this is a MacOS path to begin with */
   1.166 +		if(*src != ':')
   1.167 +			*dst++ = ':';   /* relative paths begin with ':' */
   1.168 +	}
   1.169 +	while(src < file + flen) {
   1.170 +		const char *end = strchr(src, '/');
   1.171 +		int len;
   1.172 +		if(!end)
   1.173 +			end = file + flen; /* last component */
   1.174 +		len = end - src;
   1.175 +		if(len == 0 || (len == 1 && src[0] == '.')) {
   1.176 +			/* remove repeated slashes and . */
   1.177 +		} else {
   1.178 +			if(len == 2 && src[0] == '.' && src[1] == '.') {
   1.179 +				/* replace .. with the empty string */
   1.180 +			} else {
   1.181 +				memcpy(dst, src, len);
   1.182 +				dst += len;
   1.183 +			}
   1.184 +			if(end < file + flen)
   1.185 +				*dst++ = ':';
   1.186 +		}
   1.187 +		src = end + 1;
   1.188 +	}
   1.189 +	*dst++ = '\0';
   1.190 +	return path;
   1.191 +}
   1.192 +#endif /* macintosh */
   1.193 +
   1.194 +SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
   1.195 +{
   1.196 +	FILE *fp;
   1.197 +	SDL_RWops *rwops;
   1.198 +
   1.199 +	rwops = NULL;
   1.200 +
   1.201 +#ifdef macintosh
   1.202 +	{
   1.203 +		char *mpath = unix_to_mac(file);
   1.204 +		fp = fopen(mpath, mode);
   1.205 +		free(mpath);
   1.206 +	}
   1.207 +#else
   1.208 +	fp = fopen(file, mode);
   1.209 +#endif
   1.210 +	if ( fp == NULL ) {
   1.211 +		SDL_SetError("Couldn't open %s", file);
   1.212 +	} else {
   1.213 +#ifdef WIN32
   1.214 +		in_sdl = 1;
   1.215 +		rwops = SDL_RWFromFP(fp, 1);
   1.216 +		in_sdl = 0;
   1.217 +#else
   1.218 +		rwops = SDL_RWFromFP(fp, 1);
   1.219 +#endif
   1.220 +	}
   1.221 +	return(rwops);
   1.222 +}
   1.223 +
   1.224 +SDL_RWops *SDL_RWFromFP(FILE *fp, int autoclose)
   1.225 +{
   1.226 +	SDL_RWops *rwops;
   1.227 +
   1.228 +#ifdef WIN32
   1.229 +	if ( ! in_sdl ) {
   1.230 +		SDL_SetError("You can't pass a FILE pointer to a DLL (??)");
   1.231 +		/*return(NULL);*/
   1.232 +	}
   1.233 +#endif
   1.234 +	rwops = SDL_AllocRW();
   1.235 +	if ( rwops != NULL ) {
   1.236 +		rwops->seek = stdio_seek;
   1.237 +		rwops->read = stdio_read;
   1.238 +		rwops->write = stdio_write;
   1.239 +		rwops->close = stdio_close;
   1.240 +		rwops->hidden.stdio.fp = fp;
   1.241 +		rwops->hidden.stdio.autoclose = autoclose;
   1.242 +	}
   1.243 +	return(rwops);
   1.244 +}
   1.245 +
   1.246 +SDL_RWops *SDL_RWFromMem(void *mem, int size)
   1.247 +{
   1.248 +	SDL_RWops *rwops;
   1.249 +
   1.250 +	rwops = SDL_AllocRW();
   1.251 +	if ( rwops != NULL ) {
   1.252 +		rwops->seek = mem_seek;
   1.253 +		rwops->read = mem_read;
   1.254 +		rwops->write = mem_write;
   1.255 +		rwops->close = mem_close;
   1.256 +		rwops->hidden.mem.base = (Uint8 *)mem;
   1.257 +		rwops->hidden.mem.here = rwops->hidden.mem.base;
   1.258 +		rwops->hidden.mem.stop = rwops->hidden.mem.base+size;
   1.259 +	}
   1.260 +	return(rwops);
   1.261 +}
   1.262 +
   1.263 +SDL_RWops *SDL_AllocRW(void)
   1.264 +{
   1.265 +	SDL_RWops *area;
   1.266 +
   1.267 +	area = (SDL_RWops *)malloc(sizeof *area);
   1.268 +	if ( area == NULL ) {
   1.269 +		SDL_OutOfMemory();
   1.270 +	}
   1.271 +	return(area);
   1.272 +}
   1.273 +
   1.274 +void SDL_FreeRW(SDL_RWops *area)
   1.275 +{
   1.276 +	free(area);
   1.277 +}