src/file/SDL_rwops.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 27 Feb 2006 03:48:48 +0000
changeset 1446 47bf1767c4ca
parent 1402 d910939febfa
child 1447 515df0086eb7
permissions -rw-r--r--
Date: Mon, 27 Feb 2006 02:17:29 +0100
From: "William Petiot [exoide]"
Subject: Re: [SDL] cvs version: questions regarding HAVE_STDIO_H in standard w

I managed to get a first implementation of it, which I tested for "simple" use
with tests sample apps, they work ok. I think this needs more tests.

Here is the diff against CVS/SDL12, attached.
     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 #include "SDL_config.h"
    23 
    24 /* This file provides a general interface for SDL to read and write
    25    data sources.  It can easily be extended to files, memory, etc.
    26 */
    27 
    28 #include "SDL_endian.h"
    29 #include "SDL_rwops.h"
    30 
    31 
    32 #ifdef HAVE_STDIO_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 #else /* HAVE_STDIO_H */
    77 
    78 #ifdef __WIN32__
    79 #define WINDOWS_LEAN_AND_MEAN
    80 #include <windows.h>
    81 
    82 static int win32_file_open(SDL_RWops *context, const char *filename, const char *mode) {
    83 	
    84 	UINT	old_error_mode;
    85 	HANDLE	h;
    86 	DWORD	r_right, w_right;
    87 	DWORD	must_exist, truncate;
    88 	int		a_mode;
    89 
    90 	if (!context || !filename || !mode)
    91 		return -1;
    92 		
    93 	context->hidden.win32io.h = INVALID_HANDLE_VALUE; /* mark this as unusable */
    94 
    95 	/* "r" = reading, file must exist */
    96 	/* "w" = writing, truncate existing, file may not exist */
    97 	/* "r+"= reading or writing, file must exist            */
    98 	/* "a" = writing, append file may not exist             */
    99 	/* "a+"= append + read, file may not exist              */
   100 	/* "w+" = read, write, truncate. file may not exist    */
   101 	
   102 	must_exist = ( SDL_strchr(mode,'r') != NULL ) ? OPEN_EXISTING : 0;
   103 	truncate   = ( SDL_strchr(mode,'w') != NULL ) ? CREATE_ALWAYS : 0;
   104 	r_right    = ( SDL_strchr(mode,'+') != NULL || must_exist ) ? GENERIC_READ : 0;
   105 	a_mode     = ( SDL_strchr(mode,'a') != NULL );
   106 	w_right    = ( a_mode || SDL_strchr(mode,'w') || truncate ) ? GENERIC_WRITE : 0;
   107 
   108 	if (!r_right && !w_right) /* inconsistent mode */
   109 		return -1; /* failed (invalid call)*/
   110 	
   111 	/* Do not open a dialog box if failure */
   112 	old_error_mode = SetErrorMode(SEM_NOOPENFILEERRORBOX|SEM_FAILCRITICALERRORS);	
   113 	
   114 	h = CreateFile(filename, (w_right|r_right), (w_right)? 0 : FILE_SHARE_READ, 
   115 		           NULL, (must_exist|truncate), FILE_ATTRIBUTE_NORMAL,NULL);
   116 	
   117 	/* restore old behaviour */
   118 	SetErrorMode(old_error_mode);
   119 
   120 	if (h==INVALID_HANDLE_VALUE) {
   121 		SDL_SetError("Couldn't open %s",filename);
   122 		return -2; /* failed (CreateFile) */
   123 	}
   124 	context->hidden.win32io.h = h;
   125 	context->hidden.win32io.append = a_mode;
   126 	
   127 	return 0; /* ok */
   128 }
   129 
   130 static int win32_file_seek(SDL_RWops *context, int offset, int whence) {
   131 	DWORD win32whence;
   132 	int   file_pos;
   133 	
   134 	if (!context || context->hidden.win32io.h == INVALID_HANDLE_VALUE) {
   135 		SDL_SetError("win32_file_seek: invalid context/file not opened");
   136 		return -1;
   137 	}
   138 	
   139 	switch (whence) {
   140 		case RW_SEEK_SET:		
   141 			win32whence = FILE_BEGIN; break;
   142 		case RW_SEEK_CUR:
   143 			win32whence = FILE_CURRENT; break;
   144 		case RW_SEEK_END:
   145 			win32whence = FILE_END; break;
   146 		default:
   147 			SDL_SetError("win32_file_seek: Unknown value for 'whence'");			
   148 			return -1;
   149 	}
   150 	
   151 	file_pos = SetFilePointer(context->hidden.win32io.h,offset,NULL,win32whence);
   152 
   153 	if ( file_pos != INVALID_SET_FILE_POINTER )
   154 		return file_pos; /* success */
   155 	
   156 	SDL_Error(SDL_EFSEEK);
   157 	return -1; /* error */
   158 }
   159 
   160 static int win32_file_read(SDL_RWops *context, void *ptr, int size, int maxnum) {
   161 	
   162 	int		total_bytes; 
   163 	DWORD	byte_read,nread;
   164 	
   165 	total_bytes = size*maxnum;
   166 	
   167 	if (!context || context->hidden.win32io.h == INVALID_HANDLE_VALUE || total_bytes<=0 || !size) 	
   168 		return 0;
   169 	
   170 	if (!ReadFile(context->hidden.win32io.h,ptr,total_bytes,&byte_read,NULL)) {
   171 		SDL_Error(SDL_EFREAD);
   172 		return 0;
   173 	}
   174 	nread = byte_read/size;
   175 	return nread;
   176 }
   177 
   178 static int win32_file_write(SDL_RWops *context, const void *ptr, int size, int num) {
   179 	
   180 	int		total_bytes; 
   181 	DWORD	byte_written,nwritten;
   182 	
   183 	total_bytes = size*num;
   184 
   185 	if (!context || context->hidden.win32io.h==INVALID_HANDLE_VALUE || total_bytes<=0 || !size) 	
   186 		return 0;
   187 
   188 	/* if in append mode, we must go to the EOF before write */
   189 	if (context->hidden.win32io.append) {
   190 		if ( SetFilePointer(context->hidden.win32io.h,0L,NULL,FILE_END) == INVALID_SET_FILE_POINTER ) {
   191 			SDL_Error(SDL_EFWRITE);
   192 			return 0;
   193 		}
   194 	}
   195 	
   196 	if (!WriteFile(context->hidden.win32io.h,ptr,total_bytes,&byte_written,NULL)) {
   197 		SDL_Error(SDL_EFWRITE);
   198 		return 0;
   199 	}
   200 	
   201 	nwritten = byte_written/size;
   202 	return nwritten;
   203 }
   204 
   205 static int win32_file_close(SDL_RWops *context) {
   206 	
   207 	if ( context ) {								
   208 		if (context->hidden.win32io.h != INVALID_HANDLE_VALUE) {
   209 			CloseHandle(context->hidden.win32io.h);
   210 			context->hidden.win32io.h = INVALID_HANDLE_VALUE; /* to be sure */
   211 		}
   212 		SDL_FreeRW(context);
   213 	}
   214 	return(0);
   215 }
   216 
   217 
   218 
   219 #endif /* __WIN32__ */
   220 #endif /* !HAVE_STDIO_H */
   221 
   222 /* Functions to read/write memory pointers */
   223 
   224 static int mem_seek(SDL_RWops *context, int offset, int whence)
   225 {
   226 	Uint8 *newpos;
   227 
   228 	switch (whence) {
   229 		case RW_SEEK_SET:
   230 			newpos = context->hidden.mem.base+offset;
   231 			break;
   232 		case RW_SEEK_CUR:
   233 			newpos = context->hidden.mem.here+offset;
   234 			break;
   235 		case RW_SEEK_END:
   236 			newpos = context->hidden.mem.stop+offset;
   237 			break;
   238 		default:
   239 			SDL_SetError("Unknown value for 'whence'");
   240 			return(-1);
   241 	}
   242 	if ( newpos < context->hidden.mem.base ) {
   243 		newpos = context->hidden.mem.base;
   244 	}
   245 	if ( newpos > context->hidden.mem.stop ) {
   246 		newpos = context->hidden.mem.stop;
   247 	}
   248 	context->hidden.mem.here = newpos;
   249 	return(context->hidden.mem.here-context->hidden.mem.base);
   250 }
   251 static int mem_read(SDL_RWops *context, void *ptr, int size, int maxnum)
   252 {
   253 	int total_bytes;
   254 	int mem_available;
   255 
   256 	total_bytes = (maxnum * size);
   257 	if ( (maxnum <= 0) || (size <= 0) || ((total_bytes / maxnum) != size) ) {
   258 		return 0;
   259 	}
   260 
   261 	mem_available = (context->hidden.mem.stop - context->hidden.mem.here);
   262 	if (total_bytes > mem_available) {
   263 		total_bytes = mem_available;
   264 	}
   265 
   266 	SDL_memcpy(ptr, context->hidden.mem.here, total_bytes);
   267 	context->hidden.mem.here += total_bytes;
   268 
   269 	return (total_bytes / size);
   270 }
   271 static int mem_write(SDL_RWops *context, const void *ptr, int size, int num)
   272 {
   273 	if ( (context->hidden.mem.here + (num*size)) > context->hidden.mem.stop ) {
   274 		num = (context->hidden.mem.stop-context->hidden.mem.here)/size;
   275 	}
   276 	SDL_memcpy(context->hidden.mem.here, ptr, num*size);
   277 	context->hidden.mem.here += num*size;
   278 	return(num);
   279 }
   280 static int mem_writeconst(SDL_RWops *context, const void *ptr, int size, int num)
   281 {
   282 	SDL_SetError("Can't write to read-only memory");
   283 	return(-1);
   284 }
   285 static int mem_close(SDL_RWops *context)
   286 {
   287 	if ( context ) {
   288 		SDL_FreeRW(context);
   289 	}
   290 	return(0);
   291 }
   292 
   293 /* Functions to create SDL_RWops structures from various data sources */
   294 #ifdef __WIN32__
   295 /* Aggh.  You can't (apparently) open a file in an application and
   296    read from it in a DLL.
   297 */
   298 static int in_sdl = 0;
   299 #endif
   300 
   301 #ifdef __MACOS__
   302 /*
   303  * translate unix-style slash-separated filename to mac-style colon-separated
   304  * name; return malloced string
   305  */
   306 static char *unix_to_mac(const char *file)
   307 {
   308 	int flen = SDL_strlen(file);
   309 	char *path = SDL_malloc(flen + 2);
   310 	const char *src = file;
   311 	char *dst = path;
   312 	if(*src == '/') {
   313 		/* really depends on filesystem layout, hope for the best */
   314 		src++;
   315 	} else {
   316 		/* Check if this is a MacOS path to begin with */
   317 		if(*src != ':')
   318 			*dst++ = ':';   /* relative paths begin with ':' */
   319 	}
   320 	while(src < file + flen) {
   321 		const char *end = SDL_strchr(src, '/');
   322 		int len;
   323 		if(!end)
   324 			end = file + flen; /* last component */
   325 		len = end - src;
   326 		if(len == 0 || (len == 1 && src[0] == '.')) {
   327 			/* remove repeated slashes and . */
   328 		} else {
   329 			if(len == 2 && src[0] == '.' && src[1] == '.') {
   330 				/* replace .. with the empty string */
   331 			} else {
   332 				SDL_memcpy(dst, src, len);
   333 				dst += len;
   334 			}
   335 			if(end < file + flen)
   336 				*dst++ = ':';
   337 		}
   338 		src = end + 1;
   339 	}
   340 	*dst++ = '\0';
   341 	return path;
   342 }
   343 #endif /* __MACOS__ */
   344 
   345 SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
   346 {
   347 	SDL_RWops *rwops = NULL;
   348 #ifdef HAVE_STDIO_H
   349 	FILE *fp;
   350 
   351 #ifdef __MACOS__
   352 	{
   353 		char *mpath = unix_to_mac(file);
   354 		fp = fopen(mpath, mode);
   355 		SDL_free(mpath);
   356 	}
   357 #else
   358 	fp = fopen(file, mode);
   359 #endif
   360 	if ( fp == NULL ) {
   361 		SDL_SetError("Couldn't open %s", file);
   362 	} else {
   363 #ifdef __WIN32__
   364 		in_sdl = 1;
   365 		rwops = SDL_RWFromFP(fp, 1);
   366 		in_sdl = 0;
   367 #else
   368 		rwops = SDL_RWFromFP(fp, 1);
   369 #endif
   370 	}
   371 #else  /* HAVE_STDIO_H */
   372 #ifdef __WIN32__
   373 	rwops = SDL_AllocRW();
   374 	rwops->hidden.win32io.h = INVALID_HANDLE_VALUE;
   375 	if (win32_file_open(rwops,file,mode)) {
   376 		SDL_FreeRW(rwops);
   377 		return NULL;
   378 	}	
   379 	rwops->seek  = win32_file_seek;
   380 	rwops->read  = win32_file_read;
   381 	rwops->write = win32_file_write;
   382 	rwops->close = win32_file_close;
   383 #endif /* __WIN32__ */
   384 #endif /* !HAVE_STDIO_H */
   385 	return(rwops);
   386 }
   387 
   388 #ifdef HAVE_STDIO_H
   389 SDL_RWops *SDL_RWFromFP(FILE *fp, int autoclose)
   390 {
   391 	SDL_RWops *rwops = NULL;
   392 
   393 #ifdef __WIN32__
   394 	if ( ! in_sdl ) {
   395 		/* It's when SDL and the app are compiled with different C runtimes */
   396 		SDL_SetError("You can't pass a FILE pointer to a DLL (?)");
   397 		/*return(NULL);*/
   398 	}
   399 #endif
   400 	rwops = SDL_AllocRW();
   401 	if ( rwops != NULL ) {
   402 		rwops->seek = stdio_seek;
   403 		rwops->read = stdio_read;
   404 		rwops->write = stdio_write;
   405 		rwops->close = stdio_close;
   406 		rwops->hidden.stdio.fp = fp;
   407 		rwops->hidden.stdio.autoclose = autoclose;
   408 	}
   409 	return(rwops);
   410 }
   411 #endif /* HAVE_STDIO_H */
   412 
   413 SDL_RWops *SDL_RWFromMem(void *mem, int size)
   414 {
   415 	SDL_RWops *rwops;
   416 
   417 	rwops = SDL_AllocRW();
   418 	if ( rwops != NULL ) {
   419 		rwops->seek = mem_seek;
   420 		rwops->read = mem_read;
   421 		rwops->write = mem_write;
   422 		rwops->close = mem_close;
   423 		rwops->hidden.mem.base = (Uint8 *)mem;
   424 		rwops->hidden.mem.here = rwops->hidden.mem.base;
   425 		rwops->hidden.mem.stop = rwops->hidden.mem.base+size;
   426 	}
   427 	return(rwops);
   428 }
   429 
   430 SDL_RWops *SDL_RWFromConstMem(const void *mem, int size)
   431 {
   432 	SDL_RWops *rwops;
   433 
   434 	rwops = SDL_AllocRW();
   435 	if ( rwops != NULL ) {
   436 		rwops->seek = mem_seek;
   437 		rwops->read = mem_read;
   438 		rwops->write = mem_writeconst;
   439 		rwops->close = mem_close;
   440 		rwops->hidden.mem.base = (Uint8 *)mem;
   441 		rwops->hidden.mem.here = rwops->hidden.mem.base;
   442 		rwops->hidden.mem.stop = rwops->hidden.mem.base+size;
   443 	}
   444 	return(rwops);
   445 }
   446 
   447 SDL_RWops *SDL_AllocRW(void)
   448 {
   449 	SDL_RWops *area;
   450 
   451 	area = (SDL_RWops *)SDL_malloc(sizeof *area);
   452 	if ( area == NULL ) {
   453 		SDL_OutOfMemory();
   454 	}
   455 	return(area);
   456 }
   457 
   458 void SDL_FreeRW(SDL_RWops *area)
   459 {
   460 	SDL_free(area);
   461 }
   462 
   463 /* Functions for dynamically reading and writing endian-specific values */
   464 
   465 Uint16 SDL_ReadLE16 (SDL_RWops *src)
   466 {
   467 	Uint16 value;
   468 
   469 	SDL_RWread(src, &value, (sizeof value), 1);
   470 	return(SDL_SwapLE16(value));
   471 }
   472 Uint16 SDL_ReadBE16 (SDL_RWops *src)
   473 {
   474 	Uint16 value;
   475 
   476 	SDL_RWread(src, &value, (sizeof value), 1);
   477 	return(SDL_SwapBE16(value));
   478 }
   479 Uint32 SDL_ReadLE32 (SDL_RWops *src)
   480 {
   481 	Uint32 value;
   482 
   483 	SDL_RWread(src, &value, (sizeof value), 1);
   484 	return(SDL_SwapLE32(value));
   485 }
   486 Uint32 SDL_ReadBE32 (SDL_RWops *src)
   487 {
   488 	Uint32 value;
   489 
   490 	SDL_RWread(src, &value, (sizeof value), 1);
   491 	return(SDL_SwapBE32(value));
   492 }
   493 Uint64 SDL_ReadLE64 (SDL_RWops *src)
   494 {
   495 	Uint64 value;
   496 
   497 	SDL_RWread(src, &value, (sizeof value), 1);
   498 	return(SDL_SwapLE64(value));
   499 }
   500 Uint64 SDL_ReadBE64 (SDL_RWops *src)
   501 {
   502 	Uint64 value;
   503 
   504 	SDL_RWread(src, &value, (sizeof value), 1);
   505 	return(SDL_SwapBE64(value));
   506 }
   507 
   508 int SDL_WriteLE16 (SDL_RWops *dst, Uint16 value)
   509 {
   510 	value = SDL_SwapLE16(value);
   511 	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
   512 }
   513 int SDL_WriteBE16 (SDL_RWops *dst, Uint16 value)
   514 {
   515 	value = SDL_SwapBE16(value);
   516 	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
   517 }
   518 int SDL_WriteLE32 (SDL_RWops *dst, Uint32 value)
   519 {
   520 	value = SDL_SwapLE32(value);
   521 	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
   522 }
   523 int SDL_WriteBE32 (SDL_RWops *dst, Uint32 value)
   524 {
   525 	value = SDL_SwapBE32(value);
   526 	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
   527 }
   528 int SDL_WriteLE64 (SDL_RWops *dst, Uint64 value)
   529 {
   530 	value = SDL_SwapLE64(value);
   531 	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
   532 }
   533 int SDL_WriteBE64 (SDL_RWops *dst, Uint64 value)
   534 {
   535 	value = SDL_SwapBE64(value);
   536 	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
   537 }