src/file/SDL_rwops.c
changeset 2165 c2ffcc627701
parent 2164 d083a2e4433d
child 2173 26233707bc94
equal deleted inserted replaced
2164:d083a2e4433d 2165:c2ffcc627701
    57     int a_mode;
    57     int a_mode;
    58 
    58 
    59     if (!context)
    59     if (!context)
    60         return -1;              /* failed (invalid call) */
    60         return -1;              /* failed (invalid call) */
    61 
    61 
       
    62     context->hidden.win32io.h = INVALID_HANDLE_VALUE; /* mark this as unusable */
       
    63     context->hidden.win32io.buffer.data = NULL;
       
    64     context->hidden.win32io.buffer.size = 0;
       
    65     context->hidden.win32io.buffer.left = 0;
       
    66 
    62     /* "r" = reading, file must exist */
    67     /* "r" = reading, file must exist */
    63     /* "w" = writing, truncate existing, file may not exist */
    68     /* "w" = writing, truncate existing, file may not exist */
    64     /* "r+"= reading or writing, file must exist            */
    69     /* "r+"= reading or writing, file must exist            */
    65     /* "a" = writing, append file may not exist             */
    70     /* "a" = writing, append file may not exist             */
    66     /* "a+"= append + read, file may not exist              */
    71     /* "a+"= append + read, file may not exist              */
    75                || truncate) ? GENERIC_WRITE : 0;
    80                || truncate) ? GENERIC_WRITE : 0;
    76 
    81 
    77     if (!r_right && !w_right)   /* inconsistent mode */
    82     if (!r_right && !w_right)   /* inconsistent mode */
    78         return -1;              /* failed (invalid call) */
    83         return -1;              /* failed (invalid call) */
    79 
    84 
       
    85     context->hidden.win32io.buffer.data = (char *)SDL_malloc(READAHEAD_BUFFER_SIZE);
       
    86     if (!context->hidden.win32io.buffer.data) {
       
    87         SDL_OutOfMemory();
       
    88         return -1;
       
    89     }
       
    90 
    80 #ifdef _WIN32_WCE
    91 #ifdef _WIN32_WCE
    81     {
    92     {
    82         size_t size = SDL_strlen(filename) + 1;
    93         size_t size = SDL_strlen(filename) + 1;
    83         wchar_t *filenameW = SDL_stack_alloc(wchar_t, size);
    94         wchar_t *filenameW = SDL_stack_alloc(wchar_t, size);
    84 
    95 
    85         if (MultiByteToWideChar(CP_UTF8, 0, filename, -1, filenameW, size) ==
    96         if (MultiByteToWideChar(CP_UTF8, 0, filename, -1, filenameW, size) ==
    86             0) {
    97             0) {
       
    98             SDL_stack_free(filenameW);
       
    99             SDL_free(context->hidden.win32io.buffer.data);
       
   100             context->hidden.win32io.buffer.data = NULL;
    87             SDL_SetError("Unable to convert filename to Unicode");
   101             SDL_SetError("Unable to convert filename to Unicode");
    88             SDL_stack_free(filenameW);
       
    89             return -1;
   102             return -1;
    90         }
   103         }
    91         h = CreateFile(filenameW, (w_right | r_right),
   104         h = CreateFile(filenameW, (w_right | r_right),
    92                        (w_right) ? 0 : FILE_SHARE_READ, NULL,
   105                        (w_right) ? 0 : FILE_SHARE_READ, NULL,
    93                        (must_exist | truncate | a_mode),
   106                        (must_exist | truncate | a_mode),
   107     /* restore old behaviour */
   120     /* restore old behaviour */
   108     SetErrorMode(old_error_mode);
   121     SetErrorMode(old_error_mode);
   109 #endif /* _WIN32_WCE */
   122 #endif /* _WIN32_WCE */
   110 
   123 
   111     if (h == INVALID_HANDLE_VALUE) {
   124     if (h == INVALID_HANDLE_VALUE) {
       
   125         SDL_free(context->hidden.win32io.buffer.data);
       
   126         context->hidden.win32io.buffer.data = NULL;
   112         SDL_SetError("Couldn't open %s", filename);
   127         SDL_SetError("Couldn't open %s", filename);
   113         return -2;              /* failed (CreateFile) */
   128         return -2;              /* failed (CreateFile) */
   114     }
   129     }
   115     context->hidden.win32io.h = h;
   130     context->hidden.win32io.h = h;
   116     context->hidden.win32io.append = a_mode ? SDL_TRUE : SDL_FALSE;
   131     context->hidden.win32io.append = a_mode ? SDL_TRUE : SDL_FALSE;
   117 
       
   118     context->hidden.win32io.buffer.data =
       
   119         (char *) SDL_malloc(READAHEAD_BUFFER_SIZE);
       
   120     if (!context->hidden.win32io.buffer.data) {
       
   121         SDL_OutOfMemory();
       
   122         CloseHandle(context->hidden.win32io.h);
       
   123         return -1;
       
   124     }
       
   125     context->hidden.win32io.buffer.size = 0;
       
   126     context->hidden.win32io.buffer.left = 0;
       
   127 
   132 
   128     return 0;                   /* ok */
   133     return 0;                   /* ok */
   129 }
   134 }
   130 static long SDLCALL
   135 static long SDLCALL
   131 win32_file_seek(SDL_RWops * context, long offset, int whence)
   136 win32_file_seek(SDL_RWops * context, long offset, int whence)