src/file/SDL_rwops.c
changeset 1447 515df0086eb7
parent 1446 47bf1767c4ca
child 1453 2eb75f8a32ca
     1.1 --- a/src/file/SDL_rwops.c	Mon Feb 27 03:48:48 2006 +0000
     1.2 +++ b/src/file/SDL_rwops.c	Mon Feb 27 04:16:44 2006 +0000
     1.3 @@ -29,53 +29,10 @@
     1.4  #include "SDL_rwops.h"
     1.5  
     1.6  
     1.7 -#ifdef HAVE_STDIO_H
     1.8 -
     1.9 -/* Functions to read/write stdio file pointers */
    1.10 -
    1.11 -static int stdio_seek(SDL_RWops *context, int offset, int whence)
    1.12 -{
    1.13 -	if ( fseek(context->hidden.stdio.fp, offset, whence) == 0 ) {
    1.14 -		return(ftell(context->hidden.stdio.fp));
    1.15 -	} else {
    1.16 -		SDL_Error(SDL_EFSEEK);
    1.17 -		return(-1);
    1.18 -	}
    1.19 -}
    1.20 -static int stdio_read(SDL_RWops *context, void *ptr, int size, int maxnum)
    1.21 -{
    1.22 -	size_t nread;
    1.23 +#ifdef __WIN32__
    1.24  
    1.25 -	nread = fread(ptr, size, maxnum, context->hidden.stdio.fp); 
    1.26 -	if ( nread == 0 && ferror(context->hidden.stdio.fp) ) {
    1.27 -		SDL_Error(SDL_EFREAD);
    1.28 -	}
    1.29 -	return(nread);
    1.30 -}
    1.31 -static int stdio_write(SDL_RWops *context, const void *ptr, int size, int num)
    1.32 -{
    1.33 -	size_t nwrote;
    1.34 +/* Functions to read/write Win32 API file pointers */
    1.35  
    1.36 -	nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
    1.37 -	if ( nwrote == 0 && ferror(context->hidden.stdio.fp) ) {
    1.38 -		SDL_Error(SDL_EFWRITE);
    1.39 -	}
    1.40 -	return(nwrote);
    1.41 -}
    1.42 -static int stdio_close(SDL_RWops *context)
    1.43 -{
    1.44 -	if ( context ) {
    1.45 -		if ( context->hidden.stdio.autoclose ) {
    1.46 -			/* WARNING:  Check the return value here! */
    1.47 -			fclose(context->hidden.stdio.fp);
    1.48 -		}
    1.49 -		SDL_FreeRW(context);
    1.50 -	}
    1.51 -	return(0);
    1.52 -}
    1.53 -#else /* HAVE_STDIO_H */
    1.54 -
    1.55 -#ifdef __WIN32__
    1.56  #define WINDOWS_LEAN_AND_MEAN
    1.57  #include <windows.h>
    1.58  
    1.59 @@ -126,7 +83,6 @@
    1.60  	
    1.61  	return 0; /* ok */
    1.62  }
    1.63 -
    1.64  static int win32_file_seek(SDL_RWops *context, int offset, int whence) {
    1.65  	DWORD win32whence;
    1.66  	int   file_pos;
    1.67 @@ -156,7 +112,6 @@
    1.68  	SDL_Error(SDL_EFSEEK);
    1.69  	return -1; /* error */
    1.70  }
    1.71 -
    1.72  static int win32_file_read(SDL_RWops *context, void *ptr, int size, int maxnum) {
    1.73  	
    1.74  	int		total_bytes; 
    1.75 @@ -174,7 +129,6 @@
    1.76  	nread = byte_read/size;
    1.77  	return nread;
    1.78  }
    1.79 -
    1.80  static int win32_file_write(SDL_RWops *context, const void *ptr, int size, int num) {
    1.81  	
    1.82  	int		total_bytes; 
    1.83 @@ -201,7 +155,6 @@
    1.84  	nwritten = byte_written/size;
    1.85  	return nwritten;
    1.86  }
    1.87 -
    1.88  static int win32_file_close(SDL_RWops *context) {
    1.89  	
    1.90  	if ( context ) {								
    1.91 @@ -213,10 +166,52 @@
    1.92  	}
    1.93  	return(0);
    1.94  }
    1.95 +#endif /* __WIN32__ */
    1.96  
    1.97 +#ifdef HAVE_STDIO_H
    1.98 +
    1.99 +/* Functions to read/write stdio file pointers */
   1.100 +
   1.101 +static int stdio_seek(SDL_RWops *context, int offset, int whence)
   1.102 +{
   1.103 +	if ( fseek(context->hidden.stdio.fp, offset, whence) == 0 ) {
   1.104 +		return(ftell(context->hidden.stdio.fp));
   1.105 +	} else {
   1.106 +		SDL_Error(SDL_EFSEEK);
   1.107 +		return(-1);
   1.108 +	}
   1.109 +}
   1.110 +static int stdio_read(SDL_RWops *context, void *ptr, int size, int maxnum)
   1.111 +{
   1.112 +	size_t nread;
   1.113  
   1.114 +	nread = fread(ptr, size, maxnum, context->hidden.stdio.fp); 
   1.115 +	if ( nread == 0 && ferror(context->hidden.stdio.fp) ) {
   1.116 +		SDL_Error(SDL_EFREAD);
   1.117 +	}
   1.118 +	return(nread);
   1.119 +}
   1.120 +static int stdio_write(SDL_RWops *context, const void *ptr, int size, int num)
   1.121 +{
   1.122 +	size_t nwrote;
   1.123  
   1.124 -#endif /* __WIN32__ */
   1.125 +	nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
   1.126 +	if ( nwrote == 0 && ferror(context->hidden.stdio.fp) ) {
   1.127 +		SDL_Error(SDL_EFWRITE);
   1.128 +	}
   1.129 +	return(nwrote);
   1.130 +}
   1.131 +static int stdio_close(SDL_RWops *context)
   1.132 +{
   1.133 +	if ( context ) {
   1.134 +		if ( context->hidden.stdio.autoclose ) {
   1.135 +			/* WARNING:  Check the return value here! */
   1.136 +			fclose(context->hidden.stdio.fp);
   1.137 +		}
   1.138 +		SDL_FreeRW(context);
   1.139 +	}
   1.140 +	return(0);
   1.141 +}
   1.142  #endif /* !HAVE_STDIO_H */
   1.143  
   1.144  /* Functions to read/write memory pointers */
   1.145 @@ -290,13 +285,8 @@
   1.146  	return(0);
   1.147  }
   1.148  
   1.149 +
   1.150  /* Functions to create SDL_RWops structures from various data sources */
   1.151 -#ifdef __WIN32__
   1.152 -/* Aggh.  You can't (apparently) open a file in an application and
   1.153 -   read from it in a DLL.
   1.154 -*/
   1.155 -static int in_sdl = 0;
   1.156 -#endif
   1.157  
   1.158  #ifdef __MACOS__
   1.159  /*
   1.160 @@ -345,7 +335,20 @@
   1.161  SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
   1.162  {
   1.163  	SDL_RWops *rwops = NULL;
   1.164 -#ifdef HAVE_STDIO_H
   1.165 +
   1.166 +#ifdef __WIN32__
   1.167 +	rwops = SDL_AllocRW();
   1.168 +	rwops->hidden.win32io.h = INVALID_HANDLE_VALUE;
   1.169 +	if (win32_file_open(rwops,file,mode)) {
   1.170 +		SDL_FreeRW(rwops);
   1.171 +		return NULL;
   1.172 +	}	
   1.173 +	rwops->seek  = win32_file_seek;
   1.174 +	rwops->read  = win32_file_read;
   1.175 +	rwops->write = win32_file_write;
   1.176 +	rwops->close = win32_file_close;
   1.177 +
   1.178 +#elif HAVE_STDIO_H
   1.179  	FILE *fp;
   1.180  
   1.181  #ifdef __MACOS__
   1.182 @@ -360,28 +363,12 @@
   1.183  	if ( fp == NULL ) {
   1.184  		SDL_SetError("Couldn't open %s", file);
   1.185  	} else {
   1.186 -#ifdef __WIN32__
   1.187 -		in_sdl = 1;
   1.188  		rwops = SDL_RWFromFP(fp, 1);
   1.189 -		in_sdl = 0;
   1.190 -#else
   1.191 -		rwops = SDL_RWFromFP(fp, 1);
   1.192 -#endif
   1.193  	}
   1.194 -#else  /* HAVE_STDIO_H */
   1.195 -#ifdef __WIN32__
   1.196 -	rwops = SDL_AllocRW();
   1.197 -	rwops->hidden.win32io.h = INVALID_HANDLE_VALUE;
   1.198 -	if (win32_file_open(rwops,file,mode)) {
   1.199 -		SDL_FreeRW(rwops);
   1.200 -		return NULL;
   1.201 -	}	
   1.202 -	rwops->seek  = win32_file_seek;
   1.203 -	rwops->read  = win32_file_read;
   1.204 -	rwops->write = win32_file_write;
   1.205 -	rwops->close = win32_file_close;
   1.206 -#endif /* __WIN32__ */
   1.207 +#else
   1.208 +	SDL_SetError("SDL not compiled with stdio support");
   1.209  #endif /* !HAVE_STDIO_H */
   1.210 +
   1.211  	return(rwops);
   1.212  }
   1.213  
   1.214 @@ -390,13 +377,6 @@
   1.215  {
   1.216  	SDL_RWops *rwops = NULL;
   1.217  
   1.218 -#ifdef __WIN32__
   1.219 -	if ( ! in_sdl ) {
   1.220 -		/* It's when SDL and the app are compiled with different C runtimes */
   1.221 -		SDL_SetError("You can't pass a FILE pointer to a DLL (?)");
   1.222 -		/*return(NULL);*/
   1.223 -	}
   1.224 -#endif
   1.225  	rwops = SDL_AllocRW();
   1.226  	if ( rwops != NULL ) {
   1.227  		rwops->seek = stdio_seek;