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