comparison src/file/SDL_rwops.c @ 4039:7fd9a811efc7 SDL-1.2

Reverted most of r3200:3201: a malloc() failure would leave a zero-byte file if opening for write.
author Ryan C. Gordon <icculus@icculus.org>
date Tue, 10 Jul 2007 18:56:08 +0000
parents eeb0862b9697
children 8515468091e3
comparison
equal deleted inserted replaced
4038:eeb0862b9697 4039:7fd9a811efc7
55 DWORD must_exist, truncate; 55 DWORD must_exist, truncate;
56 int a_mode; 56 int a_mode;
57 57
58 if (!context) 58 if (!context)
59 return -1; /* failed (invalid call) */ 59 return -1; /* failed (invalid call) */
60
61 context->hidden.win32io.h = INVALID_HANDLE_VALUE; /* mark this as unusable */
62 context->hidden.win32io.buffer.data = NULL;
63 context->hidden.win32io.buffer.size = 0;
64 context->hidden.win32io.buffer.left = 0;
60 65
61 /* "r" = reading, file must exist */ 66 /* "r" = reading, file must exist */
62 /* "w" = writing, truncate existing, file may not exist */ 67 /* "w" = writing, truncate existing, file may not exist */
63 /* "r+"= reading or writing, file must exist */ 68 /* "r+"= reading or writing, file must exist */
64 /* "a" = writing, append file may not exist */ 69 /* "a" = writing, append file may not exist */
72 w_right = ( a_mode || SDL_strchr(mode,'+') || truncate ) ? GENERIC_WRITE : 0; 77 w_right = ( a_mode || SDL_strchr(mode,'+') || truncate ) ? GENERIC_WRITE : 0;
73 78
74 if (!r_right && !w_right) /* inconsistent mode */ 79 if (!r_right && !w_right) /* inconsistent mode */
75 return -1; /* failed (invalid call) */ 80 return -1; /* failed (invalid call) */
76 81
82 context->hidden.win32io.buffer.data = (char *)SDL_malloc(READAHEAD_BUFFER_SIZE);
83 if (!context->hidden.win32io.buffer.data) {
84 SDL_OutOfMemory();
85 return -1;
86 }
87
77 #ifdef _WIN32_WCE 88 #ifdef _WIN32_WCE
78 { 89 {
79 size_t size = SDL_strlen(filename)+1; 90 size_t size = SDL_strlen(filename)+1;
80 wchar_t *filenameW = SDL_stack_alloc(wchar_t, size); 91 wchar_t *filenameW = SDL_stack_alloc(wchar_t, size);
81 92
82 if ( MultiByteToWideChar(CP_UTF8, 0, filename, -1, filenameW, size) == 0 ) { 93 if ( MultiByteToWideChar(CP_UTF8, 0, filename, -1, filenameW, size) == 0 ) {
83 SDL_SetError("Unable to convert filename to Unicode"); 94 SDL_SetError("Unable to convert filename to Unicode");
84 SDL_stack_free(filenameW); 95 SDL_stack_free(filenameW);
96 SDL_free(context->hidden.win32io.buffer.data);
97 context->hidden.win32io.buffer.data = NULL;
85 return -1; 98 return -1;
86 } 99 }
87 h = CreateFile(filenameW, (w_right|r_right), (w_right)? 0 : FILE_SHARE_READ, 100 h = CreateFile(filenameW, (w_right|r_right), (w_right)? 0 : FILE_SHARE_READ,
88 NULL, (must_exist|truncate|a_mode), FILE_ATTRIBUTE_NORMAL,NULL); 101 NULL, (must_exist|truncate|a_mode), FILE_ATTRIBUTE_NORMAL,NULL);
89 SDL_stack_free(filenameW); 102 SDL_stack_free(filenameW);
99 SetErrorMode(old_error_mode); 112 SetErrorMode(old_error_mode);
100 #endif /* _WIN32_WCE */ 113 #endif /* _WIN32_WCE */
101 114
102 if (h==INVALID_HANDLE_VALUE) { 115 if (h==INVALID_HANDLE_VALUE) {
103 SDL_SetError("Couldn't open %s",filename); 116 SDL_SetError("Couldn't open %s",filename);
117 SDL_free(context->hidden.win32io.buffer.data);
118 context->hidden.win32io.buffer.data = NULL;
104 return -2; /* failed (CreateFile) */ 119 return -2; /* failed (CreateFile) */
105 } 120 }
106 context->hidden.win32io.h = h; 121 context->hidden.win32io.h = h;
107 context->hidden.win32io.append = a_mode; 122 context->hidden.win32io.append = a_mode;
108
109 context->hidden.win32io.buffer.data = (char *)SDL_malloc(READAHEAD_BUFFER_SIZE);
110 if (!context->hidden.win32io.buffer.data) {
111 SDL_OutOfMemory();
112 CloseHandle(context->hidden.win32io.h);
113 return -1;
114 }
115 context->hidden.win32io.buffer.size = 0;
116 context->hidden.win32io.buffer.left = 0;
117 123
118 return 0; /* ok */ 124 return 0; /* ok */
119 } 125 }
120 static int SDLCALL win32_file_seek(SDL_RWops *context, int offset, int whence) 126 static int SDLCALL win32_file_seek(SDL_RWops *context, int offset, int whence)
121 { 127 {