src/file/SDL_rwops.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 16 Sep 2002 07:09:04 +0000
changeset 496 864a66f028d8
parent 297 f6ffac90895c
child 543 522e5202014d
permissions -rw-r--r--
Don't be fooled by unclutter - the mouse isn't really leaving our window
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  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_close(SDL_RWops *context)
   133 {
   134 	if ( context ) {
   135 		free(context);
   136 	}
   137 	return(0);
   138 }
   139 
   140 /* Functions to create SDL_RWops structures from various data sources */
   141 #ifdef WIN32
   142 /* Aggh.  You can't (apparently) open a file in an application and
   143    read from it in a DLL.
   144 */
   145 static int in_sdl = 0;
   146 #endif
   147 
   148 #ifdef macintosh
   149 /*
   150  * translate unix-style slash-separated filename to mac-style colon-separated
   151  * name; return malloced string
   152  */
   153 static char *unix_to_mac(const char *file)
   154 {
   155 	int flen = strlen(file);
   156 	char *path = malloc(flen + 2);
   157 	const char *src = file;
   158 	char *dst = path;
   159 	if(*src == '/') {
   160 		/* really depends on filesystem layout, hope for the best */
   161 		src++;
   162 	} else {
   163 		/* Check if this is a MacOS path to begin with */
   164 		if(*src != ':')
   165 			*dst++ = ':';   /* relative paths begin with ':' */
   166 	}
   167 	while(src < file + flen) {
   168 		const char *end = strchr(src, '/');
   169 		int len;
   170 		if(!end)
   171 			end = file + flen; /* last component */
   172 		len = end - src;
   173 		if(len == 0 || (len == 1 && src[0] == '.')) {
   174 			/* remove repeated slashes and . */
   175 		} else {
   176 			if(len == 2 && src[0] == '.' && src[1] == '.') {
   177 				/* replace .. with the empty string */
   178 			} else {
   179 				memcpy(dst, src, len);
   180 				dst += len;
   181 			}
   182 			if(end < file + flen)
   183 				*dst++ = ':';
   184 		}
   185 		src = end + 1;
   186 	}
   187 	*dst++ = '\0';
   188 	return path;
   189 }
   190 #endif /* macintosh */
   191 
   192 SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
   193 {
   194 	FILE *fp;
   195 	SDL_RWops *rwops;
   196 
   197 	rwops = NULL;
   198 
   199 #ifdef macintosh
   200 	{
   201 		char *mpath = unix_to_mac(file);
   202 		fp = fopen(mpath, mode);
   203 		free(mpath);
   204 	}
   205 #else
   206 	fp = fopen(file, mode);
   207 #endif
   208 	if ( fp == NULL ) {
   209 		SDL_SetError("Couldn't open %s", file);
   210 	} else {
   211 #ifdef WIN32
   212 		in_sdl = 1;
   213 		rwops = SDL_RWFromFP(fp, 1);
   214 		in_sdl = 0;
   215 #else
   216 		rwops = SDL_RWFromFP(fp, 1);
   217 #endif
   218 	}
   219 	return(rwops);
   220 }
   221 
   222 SDL_RWops *SDL_RWFromFP(FILE *fp, int autoclose)
   223 {
   224 	SDL_RWops *rwops;
   225 
   226 #ifdef WIN32
   227 	if ( ! in_sdl ) {
   228 		SDL_SetError("You can't pass a FILE pointer to a DLL (??)");
   229 		/*return(NULL);*/
   230 	}
   231 #endif
   232 	rwops = SDL_AllocRW();
   233 	if ( rwops != NULL ) {
   234 		rwops->seek = stdio_seek;
   235 		rwops->read = stdio_read;
   236 		rwops->write = stdio_write;
   237 		rwops->close = stdio_close;
   238 		rwops->hidden.stdio.fp = fp;
   239 		rwops->hidden.stdio.autoclose = autoclose;
   240 	}
   241 	return(rwops);
   242 }
   243 
   244 SDL_RWops *SDL_RWFromMem(void *mem, int size)
   245 {
   246 	SDL_RWops *rwops;
   247 
   248 	rwops = SDL_AllocRW();
   249 	if ( rwops != NULL ) {
   250 		rwops->seek = mem_seek;
   251 		rwops->read = mem_read;
   252 		rwops->write = mem_write;
   253 		rwops->close = mem_close;
   254 		rwops->hidden.mem.base = (Uint8 *)mem;
   255 		rwops->hidden.mem.here = rwops->hidden.mem.base;
   256 		rwops->hidden.mem.stop = rwops->hidden.mem.base+size;
   257 	}
   258 	return(rwops);
   259 }
   260 
   261 SDL_RWops *SDL_AllocRW(void)
   262 {
   263 	SDL_RWops *area;
   264 
   265 	area = (SDL_RWops *)malloc(sizeof *area);
   266 	if ( area == NULL ) {
   267 		SDL_OutOfMemory();
   268 	}
   269 	return(area);
   270 }
   271 
   272 void SDL_FreeRW(SDL_RWops *area)
   273 {
   274 	free(area);
   275 }