changeset 1447:515df0086eb7

Prefer the raw Win32 API over stdio for file RWops
author Sam Lantinga <slouken@libsdl.org>
date Mon, 27 Feb 2006 04:16:44 +0000
parents 47bf1767c4ca
children 9a845c7b8b35
files include/SDL_rwops.h src/file/SDL_rwops.c
diffstat 2 files changed, 70 insertions(+), 92 deletions(-) [+]
line wrap: on
line diff
--- a/include/SDL_rwops.h	Mon Feb 27 03:48:48 2006 +0000
+++ b/include/SDL_rwops.h	Mon Feb 27 04:16:44 2006 +0000
@@ -62,20 +62,18 @@
 
 	Uint32 type;
 	union {
-
+#ifdef __WIN32__
+		struct {
+		int    autoclose;
+		int    append;
+		void*  h;
+		} win32io;
+#endif
 #ifdef HAVE_STDIO_H 
 	    struct {
 		int autoclose;
 	 	FILE *fp;
 	    } stdio;
-#else
-#ifdef __WIN32__
-		struct {
-			void*  h;
-			int    autoclose;
-			int    append;
-		} win32io;
-#endif
 #endif
 	    struct {
 		Uint8 *base;
--- a/src/file/SDL_rwops.c	Mon Feb 27 03:48:48 2006 +0000
+++ b/src/file/SDL_rwops.c	Mon Feb 27 04:16:44 2006 +0000
@@ -29,53 +29,10 @@
 #include "SDL_rwops.h"
 
 
-#ifdef HAVE_STDIO_H
-
-/* Functions to read/write stdio file pointers */
-
-static int stdio_seek(SDL_RWops *context, int offset, int whence)
-{
-	if ( fseek(context->hidden.stdio.fp, offset, whence) == 0 ) {
-		return(ftell(context->hidden.stdio.fp));
-	} else {
-		SDL_Error(SDL_EFSEEK);
-		return(-1);
-	}
-}
-static int stdio_read(SDL_RWops *context, void *ptr, int size, int maxnum)
-{
-	size_t nread;
+#ifdef __WIN32__
 
-	nread = fread(ptr, size, maxnum, context->hidden.stdio.fp); 
-	if ( nread == 0 && ferror(context->hidden.stdio.fp) ) {
-		SDL_Error(SDL_EFREAD);
-	}
-	return(nread);
-}
-static int stdio_write(SDL_RWops *context, const void *ptr, int size, int num)
-{
-	size_t nwrote;
+/* Functions to read/write Win32 API file pointers */
 
-	nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
-	if ( nwrote == 0 && ferror(context->hidden.stdio.fp) ) {
-		SDL_Error(SDL_EFWRITE);
-	}
-	return(nwrote);
-}
-static int stdio_close(SDL_RWops *context)
-{
-	if ( context ) {
-		if ( context->hidden.stdio.autoclose ) {
-			/* WARNING:  Check the return value here! */
-			fclose(context->hidden.stdio.fp);
-		}
-		SDL_FreeRW(context);
-	}
-	return(0);
-}
-#else /* HAVE_STDIO_H */
-
-#ifdef __WIN32__
 #define WINDOWS_LEAN_AND_MEAN
 #include <windows.h>
 
@@ -126,7 +83,6 @@
 	
 	return 0; /* ok */
 }
-
 static int win32_file_seek(SDL_RWops *context, int offset, int whence) {
 	DWORD win32whence;
 	int   file_pos;
@@ -156,7 +112,6 @@
 	SDL_Error(SDL_EFSEEK);
 	return -1; /* error */
 }
-
 static int win32_file_read(SDL_RWops *context, void *ptr, int size, int maxnum) {
 	
 	int		total_bytes; 
@@ -174,7 +129,6 @@
 	nread = byte_read/size;
 	return nread;
 }
-
 static int win32_file_write(SDL_RWops *context, const void *ptr, int size, int num) {
 	
 	int		total_bytes; 
@@ -201,7 +155,6 @@
 	nwritten = byte_written/size;
 	return nwritten;
 }
-
 static int win32_file_close(SDL_RWops *context) {
 	
 	if ( context ) {								
@@ -213,10 +166,52 @@
 	}
 	return(0);
 }
+#endif /* __WIN32__ */
 
+#ifdef HAVE_STDIO_H
+
+/* Functions to read/write stdio file pointers */
+
+static int stdio_seek(SDL_RWops *context, int offset, int whence)
+{
+	if ( fseek(context->hidden.stdio.fp, offset, whence) == 0 ) {
+		return(ftell(context->hidden.stdio.fp));
+	} else {
+		SDL_Error(SDL_EFSEEK);
+		return(-1);
+	}
+}
+static int stdio_read(SDL_RWops *context, void *ptr, int size, int maxnum)
+{
+	size_t nread;
 
+	nread = fread(ptr, size, maxnum, context->hidden.stdio.fp); 
+	if ( nread == 0 && ferror(context->hidden.stdio.fp) ) {
+		SDL_Error(SDL_EFREAD);
+	}
+	return(nread);
+}
+static int stdio_write(SDL_RWops *context, const void *ptr, int size, int num)
+{
+	size_t nwrote;
 
-#endif /* __WIN32__ */
+	nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
+	if ( nwrote == 0 && ferror(context->hidden.stdio.fp) ) {
+		SDL_Error(SDL_EFWRITE);
+	}
+	return(nwrote);
+}
+static int stdio_close(SDL_RWops *context)
+{
+	if ( context ) {
+		if ( context->hidden.stdio.autoclose ) {
+			/* WARNING:  Check the return value here! */
+			fclose(context->hidden.stdio.fp);
+		}
+		SDL_FreeRW(context);
+	}
+	return(0);
+}
 #endif /* !HAVE_STDIO_H */
 
 /* Functions to read/write memory pointers */
@@ -290,13 +285,8 @@
 	return(0);
 }
 
+
 /* Functions to create SDL_RWops structures from various data sources */
-#ifdef __WIN32__
-/* Aggh.  You can't (apparently) open a file in an application and
-   read from it in a DLL.
-*/
-static int in_sdl = 0;
-#endif
 
 #ifdef __MACOS__
 /*
@@ -345,7 +335,20 @@
 SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
 {
 	SDL_RWops *rwops = NULL;
-#ifdef HAVE_STDIO_H
+
+#ifdef __WIN32__
+	rwops = SDL_AllocRW();
+	rwops->hidden.win32io.h = INVALID_HANDLE_VALUE;
+	if (win32_file_open(rwops,file,mode)) {
+		SDL_FreeRW(rwops);
+		return NULL;
+	}	
+	rwops->seek  = win32_file_seek;
+	rwops->read  = win32_file_read;
+	rwops->write = win32_file_write;
+	rwops->close = win32_file_close;
+
+#elif HAVE_STDIO_H
 	FILE *fp;
 
 #ifdef __MACOS__
@@ -360,28 +363,12 @@
 	if ( fp == NULL ) {
 		SDL_SetError("Couldn't open %s", file);
 	} else {
-#ifdef __WIN32__
-		in_sdl = 1;
 		rwops = SDL_RWFromFP(fp, 1);
-		in_sdl = 0;
-#else
-		rwops = SDL_RWFromFP(fp, 1);
-#endif
 	}
-#else  /* HAVE_STDIO_H */
-#ifdef __WIN32__
-	rwops = SDL_AllocRW();
-	rwops->hidden.win32io.h = INVALID_HANDLE_VALUE;
-	if (win32_file_open(rwops,file,mode)) {
-		SDL_FreeRW(rwops);
-		return NULL;
-	}	
-	rwops->seek  = win32_file_seek;
-	rwops->read  = win32_file_read;
-	rwops->write = win32_file_write;
-	rwops->close = win32_file_close;
-#endif /* __WIN32__ */
+#else
+	SDL_SetError("SDL not compiled with stdio support");
 #endif /* !HAVE_STDIO_H */
+
 	return(rwops);
 }
 
@@ -390,13 +377,6 @@
 {
 	SDL_RWops *rwops = NULL;
 
-#ifdef __WIN32__
-	if ( ! in_sdl ) {
-		/* It's when SDL and the app are compiled with different C runtimes */
-		SDL_SetError("You can't pass a FILE pointer to a DLL (?)");
-		/*return(NULL);*/
-	}
-#endif
 	rwops = SDL_AllocRW();
 	if ( rwops != NULL ) {
 		rwops->seek = stdio_seek;