Merged r3201:3204 from branches/SDL-1.2: win32 rwops tweaks.
authorRyan C. Gordon <icculus@icculus.org>
Tue, 10 Jul 2007 19:06:02 +0000
changeset 2165c2ffcc627701
parent 2164 d083a2e4433d
child 2166 711bea885c1e
Merged r3201:3204 from branches/SDL-1.2: win32 rwops tweaks.
src/file/SDL_rwops.c
     1.1 --- a/src/file/SDL_rwops.c	Tue Jul 10 16:05:50 2007 +0000
     1.2 +++ b/src/file/SDL_rwops.c	Tue Jul 10 19:06:02 2007 +0000
     1.3 @@ -59,6 +59,11 @@
     1.4      if (!context)
     1.5          return -1;              /* failed (invalid call) */
     1.6  
     1.7 +    context->hidden.win32io.h = INVALID_HANDLE_VALUE; /* mark this as unusable */
     1.8 +    context->hidden.win32io.buffer.data = NULL;
     1.9 +    context->hidden.win32io.buffer.size = 0;
    1.10 +    context->hidden.win32io.buffer.left = 0;
    1.11 +
    1.12      /* "r" = reading, file must exist */
    1.13      /* "w" = writing, truncate existing, file may not exist */
    1.14      /* "r+"= reading or writing, file must exist            */
    1.15 @@ -77,6 +82,12 @@
    1.16      if (!r_right && !w_right)   /* inconsistent mode */
    1.17          return -1;              /* failed (invalid call) */
    1.18  
    1.19 +    context->hidden.win32io.buffer.data = (char *)SDL_malloc(READAHEAD_BUFFER_SIZE);
    1.20 +    if (!context->hidden.win32io.buffer.data) {
    1.21 +        SDL_OutOfMemory();
    1.22 +        return -1;
    1.23 +    }
    1.24 +
    1.25  #ifdef _WIN32_WCE
    1.26      {
    1.27          size_t size = SDL_strlen(filename) + 1;
    1.28 @@ -84,8 +95,10 @@
    1.29  
    1.30          if (MultiByteToWideChar(CP_UTF8, 0, filename, -1, filenameW, size) ==
    1.31              0) {
    1.32 +            SDL_stack_free(filenameW);
    1.33 +            SDL_free(context->hidden.win32io.buffer.data);
    1.34 +            context->hidden.win32io.buffer.data = NULL;
    1.35              SDL_SetError("Unable to convert filename to Unicode");
    1.36 -            SDL_stack_free(filenameW);
    1.37              return -1;
    1.38          }
    1.39          h = CreateFile(filenameW, (w_right | r_right),
    1.40 @@ -109,22 +122,14 @@
    1.41  #endif /* _WIN32_WCE */
    1.42  
    1.43      if (h == INVALID_HANDLE_VALUE) {
    1.44 +        SDL_free(context->hidden.win32io.buffer.data);
    1.45 +        context->hidden.win32io.buffer.data = NULL;
    1.46          SDL_SetError("Couldn't open %s", filename);
    1.47          return -2;              /* failed (CreateFile) */
    1.48      }
    1.49      context->hidden.win32io.h = h;
    1.50      context->hidden.win32io.append = a_mode ? SDL_TRUE : SDL_FALSE;
    1.51  
    1.52 -    context->hidden.win32io.buffer.data =
    1.53 -        (char *) SDL_malloc(READAHEAD_BUFFER_SIZE);
    1.54 -    if (!context->hidden.win32io.buffer.data) {
    1.55 -        SDL_OutOfMemory();
    1.56 -        CloseHandle(context->hidden.win32io.h);
    1.57 -        return -1;
    1.58 -    }
    1.59 -    context->hidden.win32io.buffer.size = 0;
    1.60 -    context->hidden.win32io.buffer.left = 0;
    1.61 -
    1.62      return 0;                   /* ok */
    1.63  }
    1.64  static long SDLCALL