comparison src/file/SDL_rwops.c @ 1465:8dfa9a6d69a5

Updated WinCE support by Dmitry (with some tweaks) Converted the disk audio driver to SDL_RWops for portability
author Sam Lantinga <slouken@libsdl.org>
date Sat, 04 Mar 2006 08:24:35 +0000
parents 84de7511f79f
children 14717b52abc0
comparison
equal deleted inserted replaced
1464:af30090c0330 1465:8dfa9a6d69a5
27 27
28 #include "SDL_endian.h" 28 #include "SDL_endian.h"
29 #include "SDL_rwops.h" 29 #include "SDL_rwops.h"
30 30
31 31
32 #ifdef __WIN32__ 32 #if defined(__WIN32__)
33 33
34 /* Functions to read/write Win32 API file pointers */ 34 /* Functions to read/write Win32 API file pointers */
35 /* Will not use it on WinCE because stdio is buffered, it means
36 faster, and all stdio functions anyway are embedded in coredll.dll -
37 the main wince dll*/
35 38
36 #define WINDOWS_LEAN_AND_MEAN 39 #define WINDOWS_LEAN_AND_MEAN
37 #include <windows.h> 40 #include <windows.h>
38 41
39 static int win32_file_open(SDL_RWops *context, const char *filename, const char *mode) { 42 #ifndef INVALID_SET_FILE_POINTER
40 43 #define INVALID_SET_FILE_POINTER 0xFFFFFFFF
44 #endif
45
46 static int win32_file_open(SDL_RWops *context, const char *filename, const char *mode)
47 {
48 #ifndef _WIN32_WCE
41 UINT old_error_mode; 49 UINT old_error_mode;
50 #endif
42 HANDLE h; 51 HANDLE h;
43 DWORD r_right, w_right; 52 DWORD r_right, w_right;
44 DWORD must_exist, truncate; 53 DWORD must_exist, truncate;
45 int a_mode; 54 int a_mode;
46 55
62 a_mode = ( SDL_strchr(mode,'a') != NULL ) ? OPEN_ALWAYS : 0; 71 a_mode = ( SDL_strchr(mode,'a') != NULL ) ? OPEN_ALWAYS : 0;
63 w_right = ( a_mode || SDL_strchr(mode,'+') || truncate ) ? GENERIC_WRITE : 0; 72 w_right = ( a_mode || SDL_strchr(mode,'+') || truncate ) ? GENERIC_WRITE : 0;
64 73
65 if (!r_right && !w_right) /* inconsistent mode */ 74 if (!r_right && !w_right) /* inconsistent mode */
66 return -1; /* failed (invalid call)*/ 75 return -1; /* failed (invalid call)*/
67 76
77 #ifdef _WIN32_WCE
78 {
79 size_t size = SDL_strlen(filename)+1;
80 wchar_t *filenameW = SDL_stack_alloc(wchar_t, size);
81
82 if ( MultiByteToWideChar(CP_UTF8, 0, filename, -1, filenameW, size) == 0 ) {
83 SDL_SetError("Unable to convert filename to Unicode");
84 SDL_stack_free(filenameW);
85 return -1;
86 }
87 h = CreateFile(filenameW, (w_right|r_right), (w_right)? 0 : FILE_SHARE_READ,
88 NULL, (must_exist|truncate|a_mode), FILE_ATTRIBUTE_NORMAL,NULL);
89 SDL_stack_free(filenameW);
90 }
91 #else
68 /* Do not open a dialog box if failure */ 92 /* Do not open a dialog box if failure */
69 old_error_mode = SetErrorMode(SEM_NOOPENFILEERRORBOX|SEM_FAILCRITICALERRORS); 93 old_error_mode = SetErrorMode(SEM_NOOPENFILEERRORBOX|SEM_FAILCRITICALERRORS);
70 94
71 h = CreateFile(filename, (w_right|r_right), (w_right)? 0 : FILE_SHARE_READ, 95 h = CreateFile(filename, (w_right|r_right), (w_right)? 0 : FILE_SHARE_READ,
72 NULL, (must_exist|truncate|a_mode), FILE_ATTRIBUTE_NORMAL,NULL); 96 NULL, (must_exist|truncate|a_mode), FILE_ATTRIBUTE_NORMAL,NULL);
73 97
74 /* restore old behaviour */ 98 /* restore old behaviour */
75 SetErrorMode(old_error_mode); 99 SetErrorMode(old_error_mode);
100 #endif /* _WIN32_WCE */
76 101
77 if (h==INVALID_HANDLE_VALUE) { 102 if (h==INVALID_HANDLE_VALUE) {
78 SDL_SetError("Couldn't open %s",filename); 103 SDL_SetError("Couldn't open %s",filename);
79 return -2; /* failed (CreateFile) */ 104 return -2; /* failed (CreateFile) */
80 } 105 }
81 context->hidden.win32io.h = h; 106 context->hidden.win32io.h = h;
82 context->hidden.win32io.append = a_mode; 107 context->hidden.win32io.append = a_mode;
83 108
84 return 0; /* ok */ 109 return 0; /* ok */
85 } 110 }
86 static int win32_file_seek(SDL_RWops *context, int offset, int whence) { 111 static int win32_file_seek(SDL_RWops *context, int offset, int whence)
112 {
87 DWORD win32whence; 113 DWORD win32whence;
88 int file_pos; 114 int file_pos;
89 115
90 if (!context || context->hidden.win32io.h == INVALID_HANDLE_VALUE) { 116 if (!context || context->hidden.win32io.h == INVALID_HANDLE_VALUE) {
91 SDL_SetError("win32_file_seek: invalid context/file not opened"); 117 SDL_SetError("win32_file_seek: invalid context/file not opened");
110 return file_pos; /* success */ 136 return file_pos; /* success */
111 137
112 SDL_Error(SDL_EFSEEK); 138 SDL_Error(SDL_EFSEEK);
113 return -1; /* error */ 139 return -1; /* error */
114 } 140 }
115 static int win32_file_read(SDL_RWops *context, void *ptr, int size, int maxnum) { 141 static int win32_file_read(SDL_RWops *context, void *ptr, int size, int maxnum)
142 {
116 143
117 int total_bytes; 144 int total_bytes;
118 DWORD byte_read,nread; 145 DWORD byte_read,nread;
119 146
120 total_bytes = size*maxnum; 147 total_bytes = size*maxnum;
127 return 0; 154 return 0;
128 } 155 }
129 nread = byte_read/size; 156 nread = byte_read/size;
130 return nread; 157 return nread;
131 } 158 }
132 static int win32_file_write(SDL_RWops *context, const void *ptr, int size, int num) { 159 static int win32_file_write(SDL_RWops *context, const void *ptr, int size, int num)
160 {
133 161
134 int total_bytes; 162 int total_bytes;
135 DWORD byte_written,nwritten; 163 DWORD byte_written,nwritten;
136 164
137 total_bytes = size*num; 165 total_bytes = size*num;
153 } 181 }
154 182
155 nwritten = byte_written/size; 183 nwritten = byte_written/size;
156 return nwritten; 184 return nwritten;
157 } 185 }
158 static int win32_file_close(SDL_RWops *context) { 186 static int win32_file_close(SDL_RWops *context)
187 {
159 188
160 if ( context ) { 189 if ( context ) {
161 if (context->hidden.win32io.h != INVALID_HANDLE_VALUE) { 190 if (context->hidden.win32io.h != INVALID_HANDLE_VALUE) {
162 CloseHandle(context->hidden.win32io.h); 191 CloseHandle(context->hidden.win32io.h);
163 context->hidden.win32io.h = INVALID_HANDLE_VALUE; /* to be sure */ 192 context->hidden.win32io.h = INVALID_HANDLE_VALUE; /* to be sure */
247 { 276 {
248 size_t total_bytes; 277 size_t total_bytes;
249 size_t mem_available; 278 size_t mem_available;
250 279
251 total_bytes = (maxnum * size); 280 total_bytes = (maxnum * size);
252 if ( (maxnum <= 0) || (size <= 0) || ((total_bytes / maxnum) != size) ) { 281 if ( (maxnum <= 0) || (size <= 0) || ((total_bytes / maxnum) != (size_t) size) ) {
253 return 0; 282 return 0;
254 } 283 }
255 284
256 mem_available = (context->hidden.mem.stop - context->hidden.mem.here); 285 mem_available = (context->hidden.mem.stop - context->hidden.mem.here);
257 if (total_bytes > mem_available) { 286 if (total_bytes > mem_available) {
333 #endif /* __MACOS__ */ 362 #endif /* __MACOS__ */
334 363
335 SDL_RWops *SDL_RWFromFile(const char *file, const char *mode) 364 SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
336 { 365 {
337 SDL_RWops *rwops = NULL; 366 SDL_RWops *rwops = NULL;
338 367 #ifdef HAVE_STDIO_H
368 FILE *fp = NULL;
369 #endif
339 if ( !file || !*file || !mode || !*mode ) { 370 if ( !file || !*file || !mode || !*mode ) {
340 SDL_SetError("SDL_RWFromFile(): No file or no mode specified"); 371 SDL_SetError("SDL_RWFromFile(): No file or no mode specified");
341 return NULL; 372 return NULL;
342 } 373 }
343 374
344 #ifdef __WIN32__ 375 #if defined(__WIN32__)
345 rwops = SDL_AllocRW(); 376 rwops = SDL_AllocRW();
346 if (!rwops) 377 if (!rwops)
347 return NULL; /* SDL_SetError already setup by SDL_AllocRW() */ 378 return NULL; /* SDL_SetError already setup by SDL_AllocRW() */
348 rwops->hidden.win32io.h = INVALID_HANDLE_VALUE; 379 rwops->hidden.win32io.h = INVALID_HANDLE_VALUE;
349 if (win32_file_open(rwops,file,mode)) { 380 if (win32_file_open(rwops,file,mode)) {
354 rwops->read = win32_file_read; 385 rwops->read = win32_file_read;
355 rwops->write = win32_file_write; 386 rwops->write = win32_file_write;
356 rwops->close = win32_file_close; 387 rwops->close = win32_file_close;
357 388
358 #elif HAVE_STDIO_H 389 #elif HAVE_STDIO_H
359 FILE *fp;
360 390
361 #ifdef __MACOS__ 391 #ifdef __MACOS__
362 { 392 {
363 char *mpath = unix_to_mac(file); 393 char *mpath = unix_to_mac(file);
364 fp = fopen(mpath, mode); 394 fp = fopen(mpath, mode);