changeset 2165:c2ffcc627701

Merged r3201:3204 from branches/SDL-1.2: win32 rwops tweaks.
author Ryan C. Gordon <icculus@icculus.org>
date Tue, 10 Jul 2007 19:06:02 +0000
parents d083a2e4433d
children 711bea885c1e
files src/file/SDL_rwops.c
diffstat 1 files changed, 16 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/file/SDL_rwops.c	Tue Jul 10 16:05:50 2007 +0000
+++ b/src/file/SDL_rwops.c	Tue Jul 10 19:06:02 2007 +0000
@@ -59,6 +59,11 @@
     if (!context)
         return -1;              /* failed (invalid call) */
 
+    context->hidden.win32io.h = INVALID_HANDLE_VALUE; /* mark this as unusable */
+    context->hidden.win32io.buffer.data = NULL;
+    context->hidden.win32io.buffer.size = 0;
+    context->hidden.win32io.buffer.left = 0;
+
     /* "r" = reading, file must exist */
     /* "w" = writing, truncate existing, file may not exist */
     /* "r+"= reading or writing, file must exist            */
@@ -77,6 +82,12 @@
     if (!r_right && !w_right)   /* inconsistent mode */
         return -1;              /* failed (invalid call) */
 
+    context->hidden.win32io.buffer.data = (char *)SDL_malloc(READAHEAD_BUFFER_SIZE);
+    if (!context->hidden.win32io.buffer.data) {
+        SDL_OutOfMemory();
+        return -1;
+    }
+
 #ifdef _WIN32_WCE
     {
         size_t size = SDL_strlen(filename) + 1;
@@ -84,8 +95,10 @@
 
         if (MultiByteToWideChar(CP_UTF8, 0, filename, -1, filenameW, size) ==
             0) {
+            SDL_stack_free(filenameW);
+            SDL_free(context->hidden.win32io.buffer.data);
+            context->hidden.win32io.buffer.data = NULL;
             SDL_SetError("Unable to convert filename to Unicode");
-            SDL_stack_free(filenameW);
             return -1;
         }
         h = CreateFile(filenameW, (w_right | r_right),
@@ -109,22 +122,14 @@
 #endif /* _WIN32_WCE */
 
     if (h == INVALID_HANDLE_VALUE) {
+        SDL_free(context->hidden.win32io.buffer.data);
+        context->hidden.win32io.buffer.data = NULL;
         SDL_SetError("Couldn't open %s", filename);
         return -2;              /* failed (CreateFile) */
     }
     context->hidden.win32io.h = h;
     context->hidden.win32io.append = a_mode ? SDL_TRUE : SDL_FALSE;
 
-    context->hidden.win32io.buffer.data =
-        (char *) SDL_malloc(READAHEAD_BUFFER_SIZE);
-    if (!context->hidden.win32io.buffer.data) {
-        SDL_OutOfMemory();
-        CloseHandle(context->hidden.win32io.h);
-        return -1;
-    }
-    context->hidden.win32io.buffer.size = 0;
-    context->hidden.win32io.buffer.left = 0;
-
     return 0;                   /* ok */
 }
 static long SDLCALL