1.1 --- a/include/SDL_rwops.h Mon Feb 27 01:32:12 2006 +0000
1.2 +++ b/include/SDL_rwops.h Mon Feb 27 03:48:48 2006 +0000
1.3 @@ -62,11 +62,20 @@
1.4
1.5 Uint32 type;
1.6 union {
1.7 -#ifdef HAVE_STDIO_H
1.8 +
1.9 +#ifdef HAVE_STDIO_H
1.10 struct {
1.11 int autoclose;
1.12 FILE *fp;
1.13 } stdio;
1.14 +#else
1.15 +#ifdef __WIN32__
1.16 + struct {
1.17 + void* h;
1.18 + int autoclose;
1.19 + int append;
1.20 + } win32io;
1.21 +#endif
1.22 #endif
1.23 struct {
1.24 Uint8 *base;
2.1 --- a/src/file/SDL_rwops.c Mon Feb 27 01:32:12 2006 +0000
2.2 +++ b/src/file/SDL_rwops.c Mon Feb 27 03:48:48 2006 +0000
2.3 @@ -73,8 +73,151 @@
2.4 }
2.5 return(0);
2.6 }
2.7 +#else /* HAVE_STDIO_H */
2.8
2.9 -#endif /* HAVE_STDIO_H */
2.10 +#ifdef __WIN32__
2.11 +#define WINDOWS_LEAN_AND_MEAN
2.12 +#include <windows.h>
2.13 +
2.14 +static int win32_file_open(SDL_RWops *context, const char *filename, const char *mode) {
2.15 +
2.16 + UINT old_error_mode;
2.17 + HANDLE h;
2.18 + DWORD r_right, w_right;
2.19 + DWORD must_exist, truncate;
2.20 + int a_mode;
2.21 +
2.22 + if (!context || !filename || !mode)
2.23 + return -1;
2.24 +
2.25 + context->hidden.win32io.h = INVALID_HANDLE_VALUE; /* mark this as unusable */
2.26 +
2.27 + /* "r" = reading, file must exist */
2.28 + /* "w" = writing, truncate existing, file may not exist */
2.29 + /* "r+"= reading or writing, file must exist */
2.30 + /* "a" = writing, append file may not exist */
2.31 + /* "a+"= append + read, file may not exist */
2.32 + /* "w+" = read, write, truncate. file may not exist */
2.33 +
2.34 + must_exist = ( SDL_strchr(mode,'r') != NULL ) ? OPEN_EXISTING : 0;
2.35 + truncate = ( SDL_strchr(mode,'w') != NULL ) ? CREATE_ALWAYS : 0;
2.36 + r_right = ( SDL_strchr(mode,'+') != NULL || must_exist ) ? GENERIC_READ : 0;
2.37 + a_mode = ( SDL_strchr(mode,'a') != NULL );
2.38 + w_right = ( a_mode || SDL_strchr(mode,'w') || truncate ) ? GENERIC_WRITE : 0;
2.39 +
2.40 + if (!r_right && !w_right) /* inconsistent mode */
2.41 + return -1; /* failed (invalid call)*/
2.42 +
2.43 + /* Do not open a dialog box if failure */
2.44 + old_error_mode = SetErrorMode(SEM_NOOPENFILEERRORBOX|SEM_FAILCRITICALERRORS);
2.45 +
2.46 + h = CreateFile(filename, (w_right|r_right), (w_right)? 0 : FILE_SHARE_READ,
2.47 + NULL, (must_exist|truncate), FILE_ATTRIBUTE_NORMAL,NULL);
2.48 +
2.49 + /* restore old behaviour */
2.50 + SetErrorMode(old_error_mode);
2.51 +
2.52 + if (h==INVALID_HANDLE_VALUE) {
2.53 + SDL_SetError("Couldn't open %s",filename);
2.54 + return -2; /* failed (CreateFile) */
2.55 + }
2.56 + context->hidden.win32io.h = h;
2.57 + context->hidden.win32io.append = a_mode;
2.58 +
2.59 + return 0; /* ok */
2.60 +}
2.61 +
2.62 +static int win32_file_seek(SDL_RWops *context, int offset, int whence) {
2.63 + DWORD win32whence;
2.64 + int file_pos;
2.65 +
2.66 + if (!context || context->hidden.win32io.h == INVALID_HANDLE_VALUE) {
2.67 + SDL_SetError("win32_file_seek: invalid context/file not opened");
2.68 + return -1;
2.69 + }
2.70 +
2.71 + switch (whence) {
2.72 + case RW_SEEK_SET:
2.73 + win32whence = FILE_BEGIN; break;
2.74 + case RW_SEEK_CUR:
2.75 + win32whence = FILE_CURRENT; break;
2.76 + case RW_SEEK_END:
2.77 + win32whence = FILE_END; break;
2.78 + default:
2.79 + SDL_SetError("win32_file_seek: Unknown value for 'whence'");
2.80 + return -1;
2.81 + }
2.82 +
2.83 + file_pos = SetFilePointer(context->hidden.win32io.h,offset,NULL,win32whence);
2.84 +
2.85 + if ( file_pos != INVALID_SET_FILE_POINTER )
2.86 + return file_pos; /* success */
2.87 +
2.88 + SDL_Error(SDL_EFSEEK);
2.89 + return -1; /* error */
2.90 +}
2.91 +
2.92 +static int win32_file_read(SDL_RWops *context, void *ptr, int size, int maxnum) {
2.93 +
2.94 + int total_bytes;
2.95 + DWORD byte_read,nread;
2.96 +
2.97 + total_bytes = size*maxnum;
2.98 +
2.99 + if (!context || context->hidden.win32io.h == INVALID_HANDLE_VALUE || total_bytes<=0 || !size)
2.100 + return 0;
2.101 +
2.102 + if (!ReadFile(context->hidden.win32io.h,ptr,total_bytes,&byte_read,NULL)) {
2.103 + SDL_Error(SDL_EFREAD);
2.104 + return 0;
2.105 + }
2.106 + nread = byte_read/size;
2.107 + return nread;
2.108 +}
2.109 +
2.110 +static int win32_file_write(SDL_RWops *context, const void *ptr, int size, int num) {
2.111 +
2.112 + int total_bytes;
2.113 + DWORD byte_written,nwritten;
2.114 +
2.115 + total_bytes = size*num;
2.116 +
2.117 + if (!context || context->hidden.win32io.h==INVALID_HANDLE_VALUE || total_bytes<=0 || !size)
2.118 + return 0;
2.119 +
2.120 + /* if in append mode, we must go to the EOF before write */
2.121 + if (context->hidden.win32io.append) {
2.122 + if ( SetFilePointer(context->hidden.win32io.h,0L,NULL,FILE_END) == INVALID_SET_FILE_POINTER ) {
2.123 + SDL_Error(SDL_EFWRITE);
2.124 + return 0;
2.125 + }
2.126 + }
2.127 +
2.128 + if (!WriteFile(context->hidden.win32io.h,ptr,total_bytes,&byte_written,NULL)) {
2.129 + SDL_Error(SDL_EFWRITE);
2.130 + return 0;
2.131 + }
2.132 +
2.133 + nwritten = byte_written/size;
2.134 + return nwritten;
2.135 +}
2.136 +
2.137 +static int win32_file_close(SDL_RWops *context) {
2.138 +
2.139 + if ( context ) {
2.140 + if (context->hidden.win32io.h != INVALID_HANDLE_VALUE) {
2.141 + CloseHandle(context->hidden.win32io.h);
2.142 + context->hidden.win32io.h = INVALID_HANDLE_VALUE; /* to be sure */
2.143 + }
2.144 + SDL_FreeRW(context);
2.145 + }
2.146 + return(0);
2.147 +}
2.148 +
2.149 +
2.150 +
2.151 +#endif /* __WIN32__ */
2.152 +#endif /* !HAVE_STDIO_H */
2.153
2.154 /* Functions to read/write memory pointers */
2.155
2.156 @@ -225,7 +368,20 @@
2.157 rwops = SDL_RWFromFP(fp, 1);
2.158 #endif
2.159 }
2.160 -#endif /* HAVE_STDIO_H */
2.161 +#else /* HAVE_STDIO_H */
2.162 +#ifdef __WIN32__
2.163 + rwops = SDL_AllocRW();
2.164 + rwops->hidden.win32io.h = INVALID_HANDLE_VALUE;
2.165 + if (win32_file_open(rwops,file,mode)) {
2.166 + SDL_FreeRW(rwops);
2.167 + return NULL;
2.168 + }
2.169 + rwops->seek = win32_file_seek;
2.170 + rwops->read = win32_file_read;
2.171 + rwops->write = win32_file_write;
2.172 + rwops->close = win32_file_close;
2.173 +#endif /* __WIN32__ */
2.174 +#endif /* !HAVE_STDIO_H */
2.175 return(rwops);
2.176 }
2.177