src/file/SDL_rwops.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 24 Jun 2005 12:48:38 +0000
changeset 1078 e2ef6b7001fd
parent 769 b8d311d90021
child 1202 0748fbb272e7
permissions -rw-r--r--
Patch from Antonio SJ Musumeci:

"
This code with SDL-1.2.8 and CVS:

#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>

int
main(int argc, char** argv)
{
char alphabet[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char buffer_file[27] = {0};
char buffer_mem[27] = {0};
int rv_file;
int rv_mem;
FILE* file_ptr;
SDL_RWops* rwop_file;
SDL_RWops* rwop_mem;

file_ptr = fopen("./blah", "w");
fwrite(alphabet, 1, sizeof(alphabet), file_ptr);
fclose(file_ptr);

rwop_mem = SDL_RWFromMem(alphabet, sizeof(alphabet));
rwop_file = SDL_RWFromFile("./blah", "r");
rv_mem = SDL_RWread(rwop_mem , buffer_mem, 5, 6);
rv_file = SDL_RWread(rwop_file, buffer_file, 5, 6);
printf("From File: %d %s\n"
"From Mem: %d %s\n",
rv_file,
buffer_file,
rv_mem,
buffer_mem);
printf("Seek end of File: %d\n"
"Seek end of Mem: %d\n",
SDL_RWseek(rwop_file, 0, SEEK_END),
SDL_RWseek(rwop_mem , 0, SEEK_END));
SDL_RWclose(rwop_file);
SDL_RWclose(rwop_mem);

return 0;
}


Produces this output:

From File: 5 ABCDEFGHIJKLMNOPQRSTUVWXYZ
From Mem: 5 ABCDEFGHIJKLMNOPQRSTUVWXY
Seek end of File: 26
Seek end of Mem: 26
"

--ryan.
     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 total_bytes;
   114 	int mem_available;
   115 
   116 	total_bytes = (maxnum * size);
   117 	if ( (maxnum <= 0) || (size <= 0) || ((total_bytes / maxnum) != size) ) {
   118 		return 0;
   119 	}
   120 
   121 	mem_available = (context->hidden.mem.stop - context->hidden.mem.here);
   122 	if (total_bytes > mem_available) {
   123 		total_bytes = mem_available;
   124 	}
   125 
   126 	memcpy(ptr, context->hidden.mem.here, total_bytes);
   127 	context->hidden.mem.here += total_bytes;
   128 
   129 	return (total_bytes / size);
   130 }
   131 static int mem_write(SDL_RWops *context, const void *ptr, int size, int num)
   132 {
   133 	if ( (context->hidden.mem.here + (num*size)) > context->hidden.mem.stop ) {
   134 		num = (context->hidden.mem.stop-context->hidden.mem.here)/size;
   135 	}
   136 	memcpy(context->hidden.mem.here, ptr, num*size);
   137 	context->hidden.mem.here += num*size;
   138 	return(num);
   139 }
   140 static int mem_writeconst(SDL_RWops *context, const void *ptr, int size, int num)
   141 {
   142 	SDL_SetError("Can't write to read-only memory");
   143 	return(-1);
   144 }
   145 static int mem_close(SDL_RWops *context)
   146 {
   147 	if ( context ) {
   148 		free(context);
   149 	}
   150 	return(0);
   151 }
   152 
   153 /* Functions to create SDL_RWops structures from various data sources */
   154 #ifdef WIN32
   155 /* Aggh.  You can't (apparently) open a file in an application and
   156    read from it in a DLL.
   157 */
   158 static int in_sdl = 0;
   159 #endif
   160 
   161 #ifdef macintosh
   162 /*
   163  * translate unix-style slash-separated filename to mac-style colon-separated
   164  * name; return malloced string
   165  */
   166 static char *unix_to_mac(const char *file)
   167 {
   168 	int flen = strlen(file);
   169 	char *path = malloc(flen + 2);
   170 	const char *src = file;
   171 	char *dst = path;
   172 	if(*src == '/') {
   173 		/* really depends on filesystem layout, hope for the best */
   174 		src++;
   175 	} else {
   176 		/* Check if this is a MacOS path to begin with */
   177 		if(*src != ':')
   178 			*dst++ = ':';   /* relative paths begin with ':' */
   179 	}
   180 	while(src < file + flen) {
   181 		const char *end = strchr(src, '/');
   182 		int len;
   183 		if(!end)
   184 			end = file + flen; /* last component */
   185 		len = end - src;
   186 		if(len == 0 || (len == 1 && src[0] == '.')) {
   187 			/* remove repeated slashes and . */
   188 		} else {
   189 			if(len == 2 && src[0] == '.' && src[1] == '.') {
   190 				/* replace .. with the empty string */
   191 			} else {
   192 				memcpy(dst, src, len);
   193 				dst += len;
   194 			}
   195 			if(end < file + flen)
   196 				*dst++ = ':';
   197 		}
   198 		src = end + 1;
   199 	}
   200 	*dst++ = '\0';
   201 	return path;
   202 }
   203 #endif /* macintosh */
   204 
   205 SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
   206 {
   207 	FILE *fp;
   208 	SDL_RWops *rwops;
   209 
   210 	rwops = NULL;
   211 
   212 #ifdef macintosh
   213 	{
   214 		char *mpath = unix_to_mac(file);
   215 		fp = fopen(mpath, mode);
   216 		free(mpath);
   217 	}
   218 #else
   219 	fp = fopen(file, mode);
   220 #endif
   221 	if ( fp == NULL ) {
   222 		SDL_SetError("Couldn't open %s", file);
   223 	} else {
   224 #ifdef WIN32
   225 		in_sdl = 1;
   226 		rwops = SDL_RWFromFP(fp, 1);
   227 		in_sdl = 0;
   228 #else
   229 		rwops = SDL_RWFromFP(fp, 1);
   230 #endif
   231 	}
   232 	return(rwops);
   233 }
   234 
   235 SDL_RWops *SDL_RWFromFP(FILE *fp, int autoclose)
   236 {
   237 	SDL_RWops *rwops;
   238 
   239 #ifdef WIN32
   240 	if ( ! in_sdl ) {
   241 		SDL_SetError("You can't pass a FILE pointer to a DLL (?)");
   242 		/*return(NULL);*/
   243 	}
   244 #endif
   245 	rwops = SDL_AllocRW();
   246 	if ( rwops != NULL ) {
   247 		rwops->seek = stdio_seek;
   248 		rwops->read = stdio_read;
   249 		rwops->write = stdio_write;
   250 		rwops->close = stdio_close;
   251 		rwops->hidden.stdio.fp = fp;
   252 		rwops->hidden.stdio.autoclose = autoclose;
   253 	}
   254 	return(rwops);
   255 }
   256 
   257 SDL_RWops *SDL_RWFromMem(void *mem, int size)
   258 {
   259 	SDL_RWops *rwops;
   260 
   261 	rwops = SDL_AllocRW();
   262 	if ( rwops != NULL ) {
   263 		rwops->seek = mem_seek;
   264 		rwops->read = mem_read;
   265 		rwops->write = mem_write;
   266 		rwops->close = mem_close;
   267 		rwops->hidden.mem.base = (Uint8 *)mem;
   268 		rwops->hidden.mem.here = rwops->hidden.mem.base;
   269 		rwops->hidden.mem.stop = rwops->hidden.mem.base+size;
   270 	}
   271 	return(rwops);
   272 }
   273 
   274 SDL_RWops *SDL_RWFromConstMem(const void *mem, int size)
   275 {
   276 	SDL_RWops *rwops;
   277 
   278 	rwops = SDL_AllocRW();
   279 	if ( rwops != NULL ) {
   280 		rwops->seek = mem_seek;
   281 		rwops->read = mem_read;
   282 		rwops->write = mem_writeconst;
   283 		rwops->close = mem_close;
   284 		rwops->hidden.mem.base = (Uint8 *)mem;
   285 		rwops->hidden.mem.here = rwops->hidden.mem.base;
   286 		rwops->hidden.mem.stop = rwops->hidden.mem.base+size;
   287 	}
   288 	return(rwops);
   289 }
   290 
   291 SDL_RWops *SDL_AllocRW(void)
   292 {
   293 	SDL_RWops *area;
   294 
   295 	area = (SDL_RWops *)malloc(sizeof *area);
   296 	if ( area == NULL ) {
   297 		SDL_OutOfMemory();
   298 	}
   299 	return(area);
   300 }
   301 
   302 void SDL_FreeRW(SDL_RWops *area)
   303 {
   304 	free(area);
   305 }