src/file/SDL_rwops.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 04 Jan 2004 16:49:27 +0000
changeset 769 b8d311d90021
parent 764 974c0fb74bf8
child 1078 e2ef6b7001fd
permissions -rw-r--r--
Updated copyright information for 2004 (Happy New Year!)
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2004 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 #ifdef SAVE_RCSID
    24 static char rcsid =
    25  "@(#) $Id$";
    26 #endif
    27 
    28 /* This file provides a general interface for SDL to read and write
    29    data sources.  It can easily be extended to files, memory, etc.
    30 */
    31 
    32 #include <stdlib.h>
    33 #include <stdio.h>
    34 #include <string.h>
    35 
    36 #include "SDL_error.h"
    37 #include "SDL_rwops.h"
    38 
    39 /* Functions to read/write stdio file pointers */
    40 
    41 static int stdio_seek(SDL_RWops *context, int offset, int whence)
    42 {
    43 	if ( fseek(context->hidden.stdio.fp, offset, whence) == 0 ) {
    44 		return(ftell(context->hidden.stdio.fp));
    45 	} else {
    46 		SDL_Error(SDL_EFSEEK);
    47 		return(-1);
    48 	}
    49 }
    50 static int stdio_read(SDL_RWops *context, void *ptr, int size, int maxnum)
    51 {
    52 	size_t nread;
    53 
    54 	nread = fread(ptr, size, maxnum, context->hidden.stdio.fp); 
    55 	if ( nread == 0 && ferror(context->hidden.stdio.fp) ) {
    56 		SDL_Error(SDL_EFREAD);
    57 	}
    58 	return(nread);
    59 }
    60 static int stdio_write(SDL_RWops *context, const void *ptr, int size, int num)
    61 {
    62 	size_t nwrote;
    63 
    64 	nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
    65 	if ( nwrote == 0 && ferror(context->hidden.stdio.fp) ) {
    66 		SDL_Error(SDL_EFWRITE);
    67 	}
    68 	return(nwrote);
    69 }
    70 static int stdio_close(SDL_RWops *context)
    71 {
    72 	if ( context ) {
    73 		if ( context->hidden.stdio.autoclose ) {
    74 			/* WARNING:  Check the return value here! */
    75 			fclose(context->hidden.stdio.fp);
    76 		}
    77 		free(context);
    78 	}
    79 	return(0);
    80 }
    81 
    82 /* Functions to read/write memory pointers */
    83 
    84 static int mem_seek(SDL_RWops *context, int offset, int whence)
    85 {
    86 	Uint8 *newpos;
    87 
    88 	switch (whence) {
    89 		case SEEK_SET:
    90 			newpos = context->hidden.mem.base+offset;
    91 			break;
    92 		case SEEK_CUR:
    93 			newpos = context->hidden.mem.here+offset;
    94 			break;
    95 		case SEEK_END:
    96 			newpos = context->hidden.mem.stop+offset;
    97 			break;
    98 		default:
    99 			SDL_SetError("Unknown value for 'whence'");
   100 			return(-1);
   101 	}
   102 	if ( newpos < context->hidden.mem.base ) {
   103 		newpos = context->hidden.mem.base;
   104 	}
   105 	if ( newpos > context->hidden.mem.stop ) {
   106 		newpos = context->hidden.mem.stop;
   107 	}
   108 	context->hidden.mem.here = newpos;
   109 	return(context->hidden.mem.here-context->hidden.mem.base);
   110 }
   111 static int mem_read(SDL_RWops *context, void *ptr, int size, int maxnum)
   112 {
   113 	int num;
   114 
   115 	num = maxnum;
   116 	if ( (context->hidden.mem.here + (num*size)) > context->hidden.mem.stop ) {
   117 		num = (context->hidden.mem.stop-context->hidden.mem.here)/size;
   118 	}
   119 	memcpy(ptr, context->hidden.mem.here, num*size);
   120 	context->hidden.mem.here += num*size;
   121 	return(num);
   122 }
   123 static int mem_write(SDL_RWops *context, const void *ptr, int size, int num)
   124 {
   125 	if ( (context->hidden.mem.here + (num*size)) > context->hidden.mem.stop ) {
   126 		num = (context->hidden.mem.stop-context->hidden.mem.here)/size;
   127 	}
   128 	memcpy(context->hidden.mem.here, ptr, num*size);
   129 	context->hidden.mem.here += num*size;
   130 	return(num);
   131 }
   132 static int mem_writeconst(SDL_RWops *context, const void *ptr, int size, int num)
   133 {
   134 	SDL_SetError("Can't write to read-only memory");
   135 	return(-1);
   136 }
   137 static int mem_close(SDL_RWops *context)
   138 {
   139 	if ( context ) {
   140 		free(context);
   141 	}
   142 	return(0);
   143 }
   144 
   145 /* Functions to create SDL_RWops structures from various data sources */
   146 #ifdef WIN32
   147 /* Aggh.  You can't (apparently) open a file in an application and
   148    read from it in a DLL.
   149 */
   150 static int in_sdl = 0;
   151 #endif
   152 
   153 #ifdef macintosh
   154 /*
   155  * translate unix-style slash-separated filename to mac-style colon-separated
   156  * name; return malloced string
   157  */
   158 static char *unix_to_mac(const char *file)
   159 {
   160 	int flen = strlen(file);
   161 	char *path = malloc(flen + 2);
   162 	const char *src = file;
   163 	char *dst = path;
   164 	if(*src == '/') {
   165 		/* really depends on filesystem layout, hope for the best */
   166 		src++;
   167 	} else {
   168 		/* Check if this is a MacOS path to begin with */
   169 		if(*src != ':')
   170 			*dst++ = ':';   /* relative paths begin with ':' */
   171 	}
   172 	while(src < file + flen) {
   173 		const char *end = strchr(src, '/');
   174 		int len;
   175 		if(!end)
   176 			end = file + flen; /* last component */
   177 		len = end - src;
   178 		if(len == 0 || (len == 1 && src[0] == '.')) {
   179 			/* remove repeated slashes and . */
   180 		} else {
   181 			if(len == 2 && src[0] == '.' && src[1] == '.') {
   182 				/* replace .. with the empty string */
   183 			} else {
   184 				memcpy(dst, src, len);
   185 				dst += len;
   186 			}
   187 			if(end < file + flen)
   188 				*dst++ = ':';
   189 		}
   190 		src = end + 1;
   191 	}
   192 	*dst++ = '\0';
   193 	return path;
   194 }
   195 #endif /* macintosh */
   196 
   197 SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
   198 {
   199 	FILE *fp;
   200 	SDL_RWops *rwops;
   201 
   202 	rwops = NULL;
   203 
   204 #ifdef macintosh
   205 	{
   206 		char *mpath = unix_to_mac(file);
   207 		fp = fopen(mpath, mode);
   208 		free(mpath);
   209 	}
   210 #else
   211 	fp = fopen(file, mode);
   212 #endif
   213 	if ( fp == NULL ) {
   214 		SDL_SetError("Couldn't open %s", file);
   215 	} else {
   216 #ifdef WIN32
   217 		in_sdl = 1;
   218 		rwops = SDL_RWFromFP(fp, 1);
   219 		in_sdl = 0;
   220 #else
   221 		rwops = SDL_RWFromFP(fp, 1);
   222 #endif
   223 	}
   224 	return(rwops);
   225 }
   226 
   227 SDL_RWops *SDL_RWFromFP(FILE *fp, int autoclose)
   228 {
   229 	SDL_RWops *rwops;
   230 
   231 #ifdef WIN32
   232 	if ( ! in_sdl ) {
   233 		SDL_SetError("You can't pass a FILE pointer to a DLL (?)");
   234 		/*return(NULL);*/
   235 	}
   236 #endif
   237 	rwops = SDL_AllocRW();
   238 	if ( rwops != NULL ) {
   239 		rwops->seek = stdio_seek;
   240 		rwops->read = stdio_read;
   241 		rwops->write = stdio_write;
   242 		rwops->close = stdio_close;
   243 		rwops->hidden.stdio.fp = fp;
   244 		rwops->hidden.stdio.autoclose = autoclose;
   245 	}
   246 	return(rwops);
   247 }
   248 
   249 SDL_RWops *SDL_RWFromMem(void *mem, int size)
   250 {
   251 	SDL_RWops *rwops;
   252 
   253 	rwops = SDL_AllocRW();
   254 	if ( rwops != NULL ) {
   255 		rwops->seek = mem_seek;
   256 		rwops->read = mem_read;
   257 		rwops->write = mem_write;
   258 		rwops->close = mem_close;
   259 		rwops->hidden.mem.base = (Uint8 *)mem;
   260 		rwops->hidden.mem.here = rwops->hidden.mem.base;
   261 		rwops->hidden.mem.stop = rwops->hidden.mem.base+size;
   262 	}
   263 	return(rwops);
   264 }
   265 
   266 SDL_RWops *SDL_RWFromConstMem(const void *mem, int size)
   267 {
   268 	SDL_RWops *rwops;
   269 
   270 	rwops = SDL_AllocRW();
   271 	if ( rwops != NULL ) {
   272 		rwops->seek = mem_seek;
   273 		rwops->read = mem_read;
   274 		rwops->write = mem_writeconst;
   275 		rwops->close = mem_close;
   276 		rwops->hidden.mem.base = (Uint8 *)mem;
   277 		rwops->hidden.mem.here = rwops->hidden.mem.base;
   278 		rwops->hidden.mem.stop = rwops->hidden.mem.base+size;
   279 	}
   280 	return(rwops);
   281 }
   282 
   283 SDL_RWops *SDL_AllocRW(void)
   284 {
   285 	SDL_RWops *area;
   286 
   287 	area = (SDL_RWops *)malloc(sizeof *area);
   288 	if ( area == NULL ) {
   289 		SDL_OutOfMemory();
   290 	}
   291 	return(area);
   292 }
   293 
   294 void SDL_FreeRW(SDL_RWops *area)
   295 {
   296 	free(area);
   297 }