src/file/SDL_rwops.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 16 Feb 2006 10:11:48 +0000
changeset 1361 19418e4422cb
parent 1358 c71e05b4dc2e
child 1402 d910939febfa
permissions -rw-r--r--
New configure-based build system. Still work in progress, but much improved
     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 "SDL_endian.h"
    28 #include "SDL_rwops.h"
    29 
    30 
    31 #ifdef HAVE_STDIO_H
    32 
    33 /* Functions to read/write stdio file pointers */
    34 
    35 static int stdio_seek(SDL_RWops *context, int offset, int whence)
    36 {
    37 	if ( fseek(context->hidden.stdio.fp, offset, whence) == 0 ) {
    38 		return(ftell(context->hidden.stdio.fp));
    39 	} else {
    40 		SDL_Error(SDL_EFSEEK);
    41 		return(-1);
    42 	}
    43 }
    44 static int stdio_read(SDL_RWops *context, void *ptr, int size, int maxnum)
    45 {
    46 	size_t nread;
    47 
    48 	nread = fread(ptr, size, maxnum, context->hidden.stdio.fp); 
    49 	if ( nread == 0 && ferror(context->hidden.stdio.fp) ) {
    50 		SDL_Error(SDL_EFREAD);
    51 	}
    52 	return(nread);
    53 }
    54 static int stdio_write(SDL_RWops *context, const void *ptr, int size, int num)
    55 {
    56 	size_t nwrote;
    57 
    58 	nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
    59 	if ( nwrote == 0 && ferror(context->hidden.stdio.fp) ) {
    60 		SDL_Error(SDL_EFWRITE);
    61 	}
    62 	return(nwrote);
    63 }
    64 static int stdio_close(SDL_RWops *context)
    65 {
    66 	if ( context ) {
    67 		if ( context->hidden.stdio.autoclose ) {
    68 			/* WARNING:  Check the return value here! */
    69 			fclose(context->hidden.stdio.fp);
    70 		}
    71 		SDL_FreeRW(context);
    72 	}
    73 	return(0);
    74 }
    75 
    76 #endif /* HAVE_STDIO_H */
    77 
    78 /* Functions to read/write memory pointers */
    79 
    80 static int mem_seek(SDL_RWops *context, int offset, int whence)
    81 {
    82 	Uint8 *newpos;
    83 
    84 	switch (whence) {
    85 		case RW_SEEK_SET:
    86 			newpos = context->hidden.mem.base+offset;
    87 			break;
    88 		case RW_SEEK_CUR:
    89 			newpos = context->hidden.mem.here+offset;
    90 			break;
    91 		case RW_SEEK_END:
    92 			newpos = context->hidden.mem.stop+offset;
    93 			break;
    94 		default:
    95 			SDL_SetError("Unknown value for 'whence'");
    96 			return(-1);
    97 	}
    98 	if ( newpos < context->hidden.mem.base ) {
    99 		newpos = context->hidden.mem.base;
   100 	}
   101 	if ( newpos > context->hidden.mem.stop ) {
   102 		newpos = context->hidden.mem.stop;
   103 	}
   104 	context->hidden.mem.here = newpos;
   105 	return(context->hidden.mem.here-context->hidden.mem.base);
   106 }
   107 static int mem_read(SDL_RWops *context, void *ptr, int size, int maxnum)
   108 {
   109 	int total_bytes;
   110 	int mem_available;
   111 
   112 	total_bytes = (maxnum * size);
   113 	if ( (maxnum <= 0) || (size <= 0) || ((total_bytes / maxnum) != size) ) {
   114 		return 0;
   115 	}
   116 
   117 	mem_available = (context->hidden.mem.stop - context->hidden.mem.here);
   118 	if (total_bytes > mem_available) {
   119 		total_bytes = mem_available;
   120 	}
   121 
   122 	SDL_memcpy(ptr, context->hidden.mem.here, total_bytes);
   123 	context->hidden.mem.here += total_bytes;
   124 
   125 	return (total_bytes / size);
   126 }
   127 static int mem_write(SDL_RWops *context, const void *ptr, int size, int num)
   128 {
   129 	if ( (context->hidden.mem.here + (num*size)) > context->hidden.mem.stop ) {
   130 		num = (context->hidden.mem.stop-context->hidden.mem.here)/size;
   131 	}
   132 	SDL_memcpy(context->hidden.mem.here, ptr, num*size);
   133 	context->hidden.mem.here += num*size;
   134 	return(num);
   135 }
   136 static int mem_writeconst(SDL_RWops *context, const void *ptr, int size, int num)
   137 {
   138 	SDL_SetError("Can't write to read-only memory");
   139 	return(-1);
   140 }
   141 static int mem_close(SDL_RWops *context)
   142 {
   143 	if ( context ) {
   144 		SDL_FreeRW(context);
   145 	}
   146 	return(0);
   147 }
   148 
   149 /* Functions to create SDL_RWops structures from various data sources */
   150 #ifdef WIN32
   151 /* Aggh.  You can't (apparently) open a file in an application and
   152    read from it in a DLL.
   153 */
   154 static int in_sdl = 0;
   155 #endif
   156 
   157 #ifdef macintosh
   158 /*
   159  * translate unix-style slash-separated filename to mac-style colon-separated
   160  * name; return malloced string
   161  */
   162 static char *unix_to_mac(const char *file)
   163 {
   164 	int flen = SDL_strlen(file);
   165 	char *path = SDL_malloc(flen + 2);
   166 	const char *src = file;
   167 	char *dst = path;
   168 	if(*src == '/') {
   169 		/* really depends on filesystem layout, hope for the best */
   170 		src++;
   171 	} else {
   172 		/* Check if this is a MacOS path to begin with */
   173 		if(*src != ':')
   174 			*dst++ = ':';   /* relative paths begin with ':' */
   175 	}
   176 	while(src < file + flen) {
   177 		const char *end = SDL_strchr(src, '/');
   178 		int len;
   179 		if(!end)
   180 			end = file + flen; /* last component */
   181 		len = end - src;
   182 		if(len == 0 || (len == 1 && src[0] == '.')) {
   183 			/* remove repeated slashes and . */
   184 		} else {
   185 			if(len == 2 && src[0] == '.' && src[1] == '.') {
   186 				/* replace .. with the empty string */
   187 			} else {
   188 				SDL_memcpy(dst, src, len);
   189 				dst += len;
   190 			}
   191 			if(end < file + flen)
   192 				*dst++ = ':';
   193 		}
   194 		src = end + 1;
   195 	}
   196 	*dst++ = '\0';
   197 	return path;
   198 }
   199 #endif /* macintosh */
   200 
   201 SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
   202 {
   203 	SDL_RWops *rwops = NULL;
   204 #ifdef HAVE_STDIO_H
   205 	FILE *fp;
   206 
   207 #ifdef macintosh
   208 	{
   209 		char *mpath = unix_to_mac(file);
   210 		fp = fopen(mpath, mode);
   211 		SDL_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 #endif /* HAVE_STDIO_H */
   228 	return(rwops);
   229 }
   230 
   231 #ifdef HAVE_STDIO_H
   232 SDL_RWops *SDL_RWFromFP(FILE *fp, int autoclose)
   233 {
   234 	SDL_RWops *rwops = NULL;
   235 
   236 #ifdef WIN32
   237 	if ( ! in_sdl ) {
   238 		/* It's when SDL and the app are compiled with different C runtimes */
   239 		SDL_SetError("You can't pass a FILE pointer to a DLL (?)");
   240 		/*return(NULL);*/
   241 	}
   242 #endif
   243 	rwops = SDL_AllocRW();
   244 	if ( rwops != NULL ) {
   245 		rwops->seek = stdio_seek;
   246 		rwops->read = stdio_read;
   247 		rwops->write = stdio_write;
   248 		rwops->close = stdio_close;
   249 		rwops->hidden.stdio.fp = fp;
   250 		rwops->hidden.stdio.autoclose = autoclose;
   251 	}
   252 	return(rwops);
   253 }
   254 #endif /* HAVE_STDIO_H */
   255 
   256 SDL_RWops *SDL_RWFromMem(void *mem, int size)
   257 {
   258 	SDL_RWops *rwops;
   259 
   260 	rwops = SDL_AllocRW();
   261 	if ( rwops != NULL ) {
   262 		rwops->seek = mem_seek;
   263 		rwops->read = mem_read;
   264 		rwops->write = mem_write;
   265 		rwops->close = mem_close;
   266 		rwops->hidden.mem.base = (Uint8 *)mem;
   267 		rwops->hidden.mem.here = rwops->hidden.mem.base;
   268 		rwops->hidden.mem.stop = rwops->hidden.mem.base+size;
   269 	}
   270 	return(rwops);
   271 }
   272 
   273 SDL_RWops *SDL_RWFromConstMem(const void *mem, int size)
   274 {
   275 	SDL_RWops *rwops;
   276 
   277 	rwops = SDL_AllocRW();
   278 	if ( rwops != NULL ) {
   279 		rwops->seek = mem_seek;
   280 		rwops->read = mem_read;
   281 		rwops->write = mem_writeconst;
   282 		rwops->close = mem_close;
   283 		rwops->hidden.mem.base = (Uint8 *)mem;
   284 		rwops->hidden.mem.here = rwops->hidden.mem.base;
   285 		rwops->hidden.mem.stop = rwops->hidden.mem.base+size;
   286 	}
   287 	return(rwops);
   288 }
   289 
   290 SDL_RWops *SDL_AllocRW(void)
   291 {
   292 	SDL_RWops *area;
   293 
   294 	area = (SDL_RWops *)SDL_malloc(sizeof *area);
   295 	if ( area == NULL ) {
   296 		SDL_OutOfMemory();
   297 	}
   298 	return(area);
   299 }
   300 
   301 void SDL_FreeRW(SDL_RWops *area)
   302 {
   303 	SDL_free(area);
   304 }
   305 
   306 /* Functions for dynamically reading and writing endian-specific values */
   307 
   308 Uint16 SDL_ReadLE16 (SDL_RWops *src)
   309 {
   310 	Uint16 value;
   311 
   312 	SDL_RWread(src, &value, (sizeof value), 1);
   313 	return(SDL_SwapLE16(value));
   314 }
   315 Uint16 SDL_ReadBE16 (SDL_RWops *src)
   316 {
   317 	Uint16 value;
   318 
   319 	SDL_RWread(src, &value, (sizeof value), 1);
   320 	return(SDL_SwapBE16(value));
   321 }
   322 Uint32 SDL_ReadLE32 (SDL_RWops *src)
   323 {
   324 	Uint32 value;
   325 
   326 	SDL_RWread(src, &value, (sizeof value), 1);
   327 	return(SDL_SwapLE32(value));
   328 }
   329 Uint32 SDL_ReadBE32 (SDL_RWops *src)
   330 {
   331 	Uint32 value;
   332 
   333 	SDL_RWread(src, &value, (sizeof value), 1);
   334 	return(SDL_SwapBE32(value));
   335 }
   336 Uint64 SDL_ReadLE64 (SDL_RWops *src)
   337 {
   338 	Uint64 value;
   339 
   340 	SDL_RWread(src, &value, (sizeof value), 1);
   341 	return(SDL_SwapLE64(value));
   342 }
   343 Uint64 SDL_ReadBE64 (SDL_RWops *src)
   344 {
   345 	Uint64 value;
   346 
   347 	SDL_RWread(src, &value, (sizeof value), 1);
   348 	return(SDL_SwapBE64(value));
   349 }
   350 
   351 int SDL_WriteLE16 (SDL_RWops *dst, Uint16 value)
   352 {
   353 	value = SDL_SwapLE16(value);
   354 	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
   355 }
   356 int SDL_WriteBE16 (SDL_RWops *dst, Uint16 value)
   357 {
   358 	value = SDL_SwapBE16(value);
   359 	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
   360 }
   361 int SDL_WriteLE32 (SDL_RWops *dst, Uint32 value)
   362 {
   363 	value = SDL_SwapLE32(value);
   364 	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
   365 }
   366 int SDL_WriteBE32 (SDL_RWops *dst, Uint32 value)
   367 {
   368 	value = SDL_SwapBE32(value);
   369 	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
   370 }
   371 int SDL_WriteLE64 (SDL_RWops *dst, Uint64 value)
   372 {
   373 	value = SDL_SwapLE64(value);
   374 	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
   375 }
   376 int SDL_WriteBE64 (SDL_RWops *dst, Uint64 value)
   377 {
   378 	value = SDL_SwapBE64(value);
   379 	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
   380 }