Prefer the raw Win32 API over stdio for file RWops
authorSam Lantinga <slouken@libsdl.org>
Mon, 27 Feb 2006 04:16:44 +0000
changeset 1447515df0086eb7
parent 1446 47bf1767c4ca
child 1448 9a845c7b8b35
Prefer the raw Win32 API over stdio for file RWops
include/SDL_rwops.h
src/file/SDL_rwops.c
     1.1 --- a/include/SDL_rwops.h	Mon Feb 27 03:48:48 2006 +0000
     1.2 +++ b/include/SDL_rwops.h	Mon Feb 27 04:16:44 2006 +0000
     1.3 @@ -62,20 +62,18 @@
     1.4  
     1.5  	Uint32 type;
     1.6  	union {
     1.7 -
     1.8 +#ifdef __WIN32__
     1.9 +		struct {
    1.10 +		int    autoclose;
    1.11 +		int    append;
    1.12 +		void*  h;
    1.13 +		} win32io;
    1.14 +#endif
    1.15  #ifdef HAVE_STDIO_H 
    1.16  	    struct {
    1.17  		int autoclose;
    1.18  	 	FILE *fp;
    1.19  	    } stdio;
    1.20 -#else
    1.21 -#ifdef __WIN32__
    1.22 -		struct {
    1.23 -			void*  h;
    1.24 -			int    autoclose;
    1.25 -			int    append;
    1.26 -		} win32io;
    1.27 -#endif
    1.28  #endif
    1.29  	    struct {
    1.30  		Uint8 *base;
     2.1 --- a/src/file/SDL_rwops.c	Mon Feb 27 03:48:48 2006 +0000
     2.2 +++ b/src/file/SDL_rwops.c	Mon Feb 27 04:16:44 2006 +0000
     2.3 @@ -29,53 +29,10 @@
     2.4  #include "SDL_rwops.h"
     2.5  
     2.6  
     2.7 -#ifdef HAVE_STDIO_H
     2.8 +#ifdef __WIN32__
     2.9  
    2.10 -/* Functions to read/write stdio file pointers */
    2.11 +/* Functions to read/write Win32 API file pointers */
    2.12  
    2.13 -static int stdio_seek(SDL_RWops *context, int offset, int whence)
    2.14 -{
    2.15 -	if ( fseek(context->hidden.stdio.fp, offset, whence) == 0 ) {
    2.16 -		return(ftell(context->hidden.stdio.fp));
    2.17 -	} else {
    2.18 -		SDL_Error(SDL_EFSEEK);
    2.19 -		return(-1);
    2.20 -	}
    2.21 -}
    2.22 -static int stdio_read(SDL_RWops *context, void *ptr, int size, int maxnum)
    2.23 -{
    2.24 -	size_t nread;
    2.25 -
    2.26 -	nread = fread(ptr, size, maxnum, context->hidden.stdio.fp); 
    2.27 -	if ( nread == 0 && ferror(context->hidden.stdio.fp) ) {
    2.28 -		SDL_Error(SDL_EFREAD);
    2.29 -	}
    2.30 -	return(nread);
    2.31 -}
    2.32 -static int stdio_write(SDL_RWops *context, const void *ptr, int size, int num)
    2.33 -{
    2.34 -	size_t nwrote;
    2.35 -
    2.36 -	nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
    2.37 -	if ( nwrote == 0 && ferror(context->hidden.stdio.fp) ) {
    2.38 -		SDL_Error(SDL_EFWRITE);
    2.39 -	}
    2.40 -	return(nwrote);
    2.41 -}
    2.42 -static int stdio_close(SDL_RWops *context)
    2.43 -{
    2.44 -	if ( context ) {
    2.45 -		if ( context->hidden.stdio.autoclose ) {
    2.46 -			/* WARNING:  Check the return value here! */
    2.47 -			fclose(context->hidden.stdio.fp);
    2.48 -		}
    2.49 -		SDL_FreeRW(context);
    2.50 -	}
    2.51 -	return(0);
    2.52 -}
    2.53 -#else /* HAVE_STDIO_H */
    2.54 -
    2.55 -#ifdef __WIN32__
    2.56  #define WINDOWS_LEAN_AND_MEAN
    2.57  #include <windows.h>
    2.58  
    2.59 @@ -126,7 +83,6 @@
    2.60  	
    2.61  	return 0; /* ok */
    2.62  }
    2.63 -
    2.64  static int win32_file_seek(SDL_RWops *context, int offset, int whence) {
    2.65  	DWORD win32whence;
    2.66  	int   file_pos;
    2.67 @@ -156,7 +112,6 @@
    2.68  	SDL_Error(SDL_EFSEEK);
    2.69  	return -1; /* error */
    2.70  }
    2.71 -
    2.72  static int win32_file_read(SDL_RWops *context, void *ptr, int size, int maxnum) {
    2.73  	
    2.74  	int		total_bytes; 
    2.75 @@ -174,7 +129,6 @@
    2.76  	nread = byte_read/size;
    2.77  	return nread;
    2.78  }
    2.79 -
    2.80  static int win32_file_write(SDL_RWops *context, const void *ptr, int size, int num) {
    2.81  	
    2.82  	int		total_bytes; 
    2.83 @@ -201,7 +155,6 @@
    2.84  	nwritten = byte_written/size;
    2.85  	return nwritten;
    2.86  }
    2.87 -
    2.88  static int win32_file_close(SDL_RWops *context) {
    2.89  	
    2.90  	if ( context ) {								
    2.91 @@ -213,10 +166,52 @@
    2.92  	}
    2.93  	return(0);
    2.94  }
    2.95 +#endif /* __WIN32__ */
    2.96  
    2.97 +#ifdef HAVE_STDIO_H
    2.98  
    2.99 +/* Functions to read/write stdio file pointers */
   2.100  
   2.101 -#endif /* __WIN32__ */
   2.102 +static int stdio_seek(SDL_RWops *context, int offset, int whence)
   2.103 +{
   2.104 +	if ( fseek(context->hidden.stdio.fp, offset, whence) == 0 ) {
   2.105 +		return(ftell(context->hidden.stdio.fp));
   2.106 +	} else {
   2.107 +		SDL_Error(SDL_EFSEEK);
   2.108 +		return(-1);
   2.109 +	}
   2.110 +}
   2.111 +static int stdio_read(SDL_RWops *context, void *ptr, int size, int maxnum)
   2.112 +{
   2.113 +	size_t nread;
   2.114 +
   2.115 +	nread = fread(ptr, size, maxnum, context->hidden.stdio.fp); 
   2.116 +	if ( nread == 0 && ferror(context->hidden.stdio.fp) ) {
   2.117 +		SDL_Error(SDL_EFREAD);
   2.118 +	}
   2.119 +	return(nread);
   2.120 +}
   2.121 +static int stdio_write(SDL_RWops *context, const void *ptr, int size, int num)
   2.122 +{
   2.123 +	size_t nwrote;
   2.124 +
   2.125 +	nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
   2.126 +	if ( nwrote == 0 && ferror(context->hidden.stdio.fp) ) {
   2.127 +		SDL_Error(SDL_EFWRITE);
   2.128 +	}
   2.129 +	return(nwrote);
   2.130 +}
   2.131 +static int stdio_close(SDL_RWops *context)
   2.132 +{
   2.133 +	if ( context ) {
   2.134 +		if ( context->hidden.stdio.autoclose ) {
   2.135 +			/* WARNING:  Check the return value here! */
   2.136 +			fclose(context->hidden.stdio.fp);
   2.137 +		}
   2.138 +		SDL_FreeRW(context);
   2.139 +	}
   2.140 +	return(0);
   2.141 +}
   2.142  #endif /* !HAVE_STDIO_H */
   2.143  
   2.144  /* Functions to read/write memory pointers */
   2.145 @@ -290,13 +285,8 @@
   2.146  	return(0);
   2.147  }
   2.148  
   2.149 +
   2.150  /* Functions to create SDL_RWops structures from various data sources */
   2.151 -#ifdef __WIN32__
   2.152 -/* Aggh.  You can't (apparently) open a file in an application and
   2.153 -   read from it in a DLL.
   2.154 -*/
   2.155 -static int in_sdl = 0;
   2.156 -#endif
   2.157  
   2.158  #ifdef __MACOS__
   2.159  /*
   2.160 @@ -345,7 +335,20 @@
   2.161  SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
   2.162  {
   2.163  	SDL_RWops *rwops = NULL;
   2.164 -#ifdef HAVE_STDIO_H
   2.165 +
   2.166 +#ifdef __WIN32__
   2.167 +	rwops = SDL_AllocRW();
   2.168 +	rwops->hidden.win32io.h = INVALID_HANDLE_VALUE;
   2.169 +	if (win32_file_open(rwops,file,mode)) {
   2.170 +		SDL_FreeRW(rwops);
   2.171 +		return NULL;
   2.172 +	}	
   2.173 +	rwops->seek  = win32_file_seek;
   2.174 +	rwops->read  = win32_file_read;
   2.175 +	rwops->write = win32_file_write;
   2.176 +	rwops->close = win32_file_close;
   2.177 +
   2.178 +#elif HAVE_STDIO_H
   2.179  	FILE *fp;
   2.180  
   2.181  #ifdef __MACOS__
   2.182 @@ -360,28 +363,12 @@
   2.183  	if ( fp == NULL ) {
   2.184  		SDL_SetError("Couldn't open %s", file);
   2.185  	} else {
   2.186 -#ifdef __WIN32__
   2.187 -		in_sdl = 1;
   2.188  		rwops = SDL_RWFromFP(fp, 1);
   2.189 -		in_sdl = 0;
   2.190 +	}
   2.191  #else
   2.192 -		rwops = SDL_RWFromFP(fp, 1);
   2.193 -#endif
   2.194 -	}
   2.195 -#else  /* HAVE_STDIO_H */
   2.196 -#ifdef __WIN32__
   2.197 -	rwops = SDL_AllocRW();
   2.198 -	rwops->hidden.win32io.h = INVALID_HANDLE_VALUE;
   2.199 -	if (win32_file_open(rwops,file,mode)) {
   2.200 -		SDL_FreeRW(rwops);
   2.201 -		return NULL;
   2.202 -	}	
   2.203 -	rwops->seek  = win32_file_seek;
   2.204 -	rwops->read  = win32_file_read;
   2.205 -	rwops->write = win32_file_write;
   2.206 -	rwops->close = win32_file_close;
   2.207 -#endif /* __WIN32__ */
   2.208 +	SDL_SetError("SDL not compiled with stdio support");
   2.209  #endif /* !HAVE_STDIO_H */
   2.210 +
   2.211  	return(rwops);
   2.212  }
   2.213  
   2.214 @@ -390,13 +377,6 @@
   2.215  {
   2.216  	SDL_RWops *rwops = NULL;
   2.217  
   2.218 -#ifdef __WIN32__
   2.219 -	if ( ! in_sdl ) {
   2.220 -		/* It's when SDL and the app are compiled with different C runtimes */
   2.221 -		SDL_SetError("You can't pass a FILE pointer to a DLL (?)");
   2.222 -		/*return(NULL);*/
   2.223 -	}
   2.224 -#endif
   2.225  	rwops = SDL_AllocRW();
   2.226  	if ( rwops != NULL ) {
   2.227  		rwops->seek = stdio_seek;