Add support for (scaled down) high resolution modes.
[SDL.s60v3.git] / src / file / SDL_rwops.c
blobe463bf11c9d5aa58210c77d417196b6cff118c6c
1 /*
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2006 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 Sam Lantinga
20 slouken@libsdl.org
22 #include "SDL_config.h"
24 /* This file provides a general interface for SDL to read and write
25 data sources. It can easily be extended to files, memory, etc.
28 #include "SDL_endian.h"
29 #include "SDL_rwops.h"
32 #if defined(__WIN32__) && !defined(__SYMBIAN32__)
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*/
39 #define WINDOWS_LEAN_AND_MEAN
40 #include <windows.h>
42 #ifndef INVALID_SET_FILE_POINTER
43 #define INVALID_SET_FILE_POINTER 0xFFFFFFFF
44 #endif
46 #define READAHEAD_BUFFER_SIZE 1024
48 static int SDLCALL win32_file_open(SDL_RWops *context, const char *filename, const char *mode)
50 #ifndef _WIN32_WCE
51 UINT old_error_mode;
52 #endif
53 HANDLE h;
54 DWORD r_right, w_right;
55 DWORD must_exist, truncate;
56 int a_mode;
58 if (!context)
59 return -1; /* failed (invalid call) */
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;
66 /* "r" = reading, file must exist */
67 /* "w" = writing, truncate existing, file may not exist */
68 /* "r+"= reading or writing, file must exist */
69 /* "a" = writing, append file may not exist */
70 /* "a+"= append + read, file may not exist */
71 /* "w+" = read, write, truncate. file may not exist */
73 must_exist = ( SDL_strchr(mode,'r') != NULL ) ? OPEN_EXISTING : 0;
74 truncate = ( SDL_strchr(mode,'w') != NULL ) ? CREATE_ALWAYS : 0;
75 r_right = ( SDL_strchr(mode,'+') != NULL || must_exist ) ? GENERIC_READ : 0;
76 a_mode = ( SDL_strchr(mode,'a') != NULL ) ? OPEN_ALWAYS : 0;
77 w_right = ( a_mode || SDL_strchr(mode,'+') || truncate ) ? GENERIC_WRITE : 0;
79 if (!r_right && !w_right) /* inconsistent mode */
80 return -1; /* failed (invalid call) */
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;
88 #ifdef _WIN32_WCE
90 size_t size = SDL_strlen(filename)+1;
91 wchar_t *filenameW = SDL_stack_alloc(wchar_t, size);
93 if ( MultiByteToWideChar(CP_UTF8, 0, filename, -1, filenameW, size) == 0 ) {
94 SDL_stack_free(filenameW);
95 SDL_free(context->hidden.win32io.buffer.data);
96 context->hidden.win32io.buffer.data = NULL;
97 SDL_SetError("Unable to convert filename to Unicode");
98 return -1;
100 h = CreateFile(filenameW, (w_right|r_right), (w_right)? 0 : FILE_SHARE_READ,
101 NULL, (must_exist|truncate|a_mode), FILE_ATTRIBUTE_NORMAL,NULL);
102 SDL_stack_free(filenameW);
104 #else
105 /* Do not open a dialog box if failure */
106 old_error_mode = SetErrorMode(SEM_NOOPENFILEERRORBOX|SEM_FAILCRITICALERRORS);
108 h = CreateFile(filename, (w_right|r_right), (w_right)? 0 : FILE_SHARE_READ,
109 NULL, (must_exist|truncate|a_mode), FILE_ATTRIBUTE_NORMAL,NULL);
111 /* restore old behaviour */
112 SetErrorMode(old_error_mode);
113 #endif /* _WIN32_WCE */
115 if (h==INVALID_HANDLE_VALUE) {
116 SDL_free(context->hidden.win32io.buffer.data);
117 context->hidden.win32io.buffer.data = NULL;
118 SDL_SetError("Couldn't open %s",filename);
119 return -2; /* failed (CreateFile) */
121 context->hidden.win32io.h = h;
122 context->hidden.win32io.append = a_mode;
124 return 0; /* ok */
126 static int SDLCALL win32_file_seek(SDL_RWops *context, int offset, int whence)
128 DWORD win32whence;
129 int file_pos;
131 if (!context || context->hidden.win32io.h == INVALID_HANDLE_VALUE) {
132 SDL_SetError("win32_file_seek: invalid context/file not opened");
133 return -1;
136 /* FIXME: We may be able to satisfy the seek within buffered data */
137 if (whence == RW_SEEK_CUR && context->hidden.win32io.buffer.left) {
138 offset -= context->hidden.win32io.buffer.left;
140 context->hidden.win32io.buffer.left = 0;
142 switch (whence) {
143 case RW_SEEK_SET:
144 win32whence = FILE_BEGIN; break;
145 case RW_SEEK_CUR:
146 win32whence = FILE_CURRENT; break;
147 case RW_SEEK_END:
148 win32whence = FILE_END; break;
149 default:
150 SDL_SetError("win32_file_seek: Unknown value for 'whence'");
151 return -1;
154 file_pos = SetFilePointer(context->hidden.win32io.h,offset,NULL,win32whence);
156 if ( file_pos != INVALID_SET_FILE_POINTER )
157 return file_pos; /* success */
159 SDL_Error(SDL_EFSEEK);
160 return -1; /* error */
162 static int SDLCALL win32_file_read(SDL_RWops *context, void *ptr, int size, int maxnum)
164 int total_need;
165 int total_read = 0;
166 int read_ahead;
167 DWORD byte_read;
169 total_need = size*maxnum;
171 if (!context || context->hidden.win32io.h == INVALID_HANDLE_VALUE || total_need<=0 || !size)
172 return 0;
174 if (context->hidden.win32io.buffer.left > 0) {
175 void *data = (char *)context->hidden.win32io.buffer.data +
176 context->hidden.win32io.buffer.size -
177 context->hidden.win32io.buffer.left;
178 read_ahead = SDL_min(total_need, context->hidden.win32io.buffer.left);
179 SDL_memcpy(ptr, data, read_ahead);
180 context->hidden.win32io.buffer.left -= read_ahead;
182 if (read_ahead == total_need) {
183 return maxnum;
185 ptr = (char *)ptr + read_ahead;
186 total_need -= read_ahead;
187 total_read += read_ahead;
190 if (total_need < READAHEAD_BUFFER_SIZE) {
191 if (!ReadFile(context->hidden.win32io.h,context->hidden.win32io.buffer.data,READAHEAD_BUFFER_SIZE,&byte_read,NULL)) {
192 SDL_Error(SDL_EFREAD);
193 return 0;
195 read_ahead = SDL_min(total_need, (int)byte_read);
196 SDL_memcpy(ptr, context->hidden.win32io.buffer.data, read_ahead);
197 context->hidden.win32io.buffer.size = byte_read;
198 context->hidden.win32io.buffer.left = byte_read-read_ahead;
199 total_read += read_ahead;
200 } else {
201 if (!ReadFile(context->hidden.win32io.h,ptr,total_need,&byte_read,NULL)) {
202 SDL_Error(SDL_EFREAD);
203 return 0;
205 total_read += byte_read;
207 return (total_read/size);
209 static int SDLCALL win32_file_write(SDL_RWops *context, const void *ptr, int size, int num)
212 int total_bytes;
213 DWORD byte_written,nwritten;
215 total_bytes = size*num;
217 if (!context || context->hidden.win32io.h==INVALID_HANDLE_VALUE || total_bytes<=0 || !size)
218 return 0;
220 if (context->hidden.win32io.buffer.left) {
221 SetFilePointer(context->hidden.win32io.h,-context->hidden.win32io.buffer.left,NULL,FILE_CURRENT);
222 context->hidden.win32io.buffer.left = 0;
225 /* if in append mode, we must go to the EOF before write */
226 if (context->hidden.win32io.append) {
227 if ( SetFilePointer(context->hidden.win32io.h,0L,NULL,FILE_END) == INVALID_SET_FILE_POINTER ) {
228 SDL_Error(SDL_EFWRITE);
229 return 0;
233 if (!WriteFile(context->hidden.win32io.h,ptr,total_bytes,&byte_written,NULL)) {
234 SDL_Error(SDL_EFWRITE);
235 return 0;
238 nwritten = byte_written/size;
239 return nwritten;
241 static int SDLCALL win32_file_close(SDL_RWops *context)
244 if ( context ) {
245 if (context->hidden.win32io.h != INVALID_HANDLE_VALUE) {
246 CloseHandle(context->hidden.win32io.h);
247 context->hidden.win32io.h = INVALID_HANDLE_VALUE; /* to be sure */
249 if (context->hidden.win32io.buffer.data) {
250 SDL_free(context->hidden.win32io.buffer.data);
251 context->hidden.win32io.buffer.data = NULL;
253 SDL_FreeRW(context);
255 return(0);
257 #endif /* __WIN32__ */
259 #ifdef HAVE_STDIO_H
261 /* Functions to read/write stdio file pointers */
263 static int SDLCALL stdio_seek(SDL_RWops *context, int offset, int whence)
265 if ( fseek(context->hidden.stdio.fp, offset, whence) == 0 ) {
266 return(ftell(context->hidden.stdio.fp));
267 } else {
268 SDL_Error(SDL_EFSEEK);
269 return(-1);
272 static int SDLCALL stdio_read(SDL_RWops *context, void *ptr, int size, int maxnum)
274 size_t nread;
276 nread = fread(ptr, size, maxnum, context->hidden.stdio.fp);
277 if ( nread == 0 && ferror(context->hidden.stdio.fp) ) {
278 SDL_Error(SDL_EFREAD);
280 return(nread);
282 static int SDLCALL stdio_write(SDL_RWops *context, const void *ptr, int size, int num)
284 size_t nwrote;
286 nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
287 if ( nwrote == 0 && ferror(context->hidden.stdio.fp) ) {
288 SDL_Error(SDL_EFWRITE);
290 return(nwrote);
292 static int SDLCALL stdio_close(SDL_RWops *context)
294 if ( context ) {
295 if ( context->hidden.stdio.autoclose ) {
296 /* WARNING: Check the return value here! */
297 fclose(context->hidden.stdio.fp);
299 SDL_FreeRW(context);
301 return(0);
303 #endif /* !HAVE_STDIO_H */
305 /* Functions to read/write memory pointers */
307 static int SDLCALL mem_seek(SDL_RWops *context, int offset, int whence)
309 Uint8 *newpos;
311 switch (whence) {
312 case RW_SEEK_SET:
313 newpos = context->hidden.mem.base+offset;
314 break;
315 case RW_SEEK_CUR:
316 newpos = context->hidden.mem.here+offset;
317 break;
318 case RW_SEEK_END:
319 newpos = context->hidden.mem.stop+offset;
320 break;
321 default:
322 SDL_SetError("Unknown value for 'whence'");
323 return(-1);
325 if ( newpos < context->hidden.mem.base ) {
326 newpos = context->hidden.mem.base;
328 if ( newpos > context->hidden.mem.stop ) {
329 newpos = context->hidden.mem.stop;
331 context->hidden.mem.here = newpos;
332 return(context->hidden.mem.here-context->hidden.mem.base);
334 static int SDLCALL mem_read(SDL_RWops *context, void *ptr, int size, int maxnum)
336 size_t total_bytes;
337 size_t mem_available;
339 total_bytes = (maxnum * size);
340 if ( (maxnum <= 0) || (size <= 0) || ((total_bytes / maxnum) != (size_t) size) ) {
341 return 0;
344 mem_available = (context->hidden.mem.stop - context->hidden.mem.here);
345 if (total_bytes > mem_available) {
346 total_bytes = mem_available;
349 SDL_memcpy(ptr, context->hidden.mem.here, total_bytes);
350 context->hidden.mem.here += total_bytes;
352 return (total_bytes / size);
354 static int SDLCALL mem_write(SDL_RWops *context, const void *ptr, int size, int num)
356 if ( (context->hidden.mem.here + (num*size)) > context->hidden.mem.stop ) {
357 num = (context->hidden.mem.stop-context->hidden.mem.here)/size;
359 SDL_memcpy(context->hidden.mem.here, ptr, num*size);
360 context->hidden.mem.here += num*size;
361 return(num);
363 static int SDLCALL mem_writeconst(SDL_RWops *context, const void *ptr, int size, int num)
365 SDL_SetError("Can't write to read-only memory");
366 return(-1);
368 static int SDLCALL mem_close(SDL_RWops *context)
370 if ( context ) {
371 SDL_FreeRW(context);
373 return(0);
377 /* Functions to create SDL_RWops structures from various data sources */
379 #ifdef __MACOS__
381 * translate unix-style slash-separated filename to mac-style colon-separated
382 * name; return malloced string
384 static char *unix_to_mac(const char *file)
386 int flen = SDL_strlen(file);
387 char *path = SDL_malloc(flen + 2);
388 const char *src = file;
389 char *dst = path;
390 if(*src == '/') {
391 /* really depends on filesystem layout, hope for the best */
392 src++;
393 } else {
394 /* Check if this is a MacOS path to begin with */
395 if(*src != ':')
396 *dst++ = ':'; /* relative paths begin with ':' */
398 while(src < file + flen) {
399 const char *end = SDL_strchr(src, '/');
400 int len;
401 if(!end)
402 end = file + flen; /* last component */
403 len = end - src;
404 if(len == 0 || (len == 1 && src[0] == '.')) {
405 /* remove repeated slashes and . */
406 } else {
407 if(len == 2 && src[0] == '.' && src[1] == '.') {
408 /* replace .. with the empty string */
409 } else {
410 SDL_memcpy(dst, src, len);
411 dst += len;
413 if(end < file + flen)
414 *dst++ = ':';
416 src = end + 1;
418 *dst++ = '\0';
419 return path;
421 #endif /* __MACOS__ */
423 SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
425 SDL_RWops *rwops = NULL;
426 #ifdef HAVE_STDIO_H
427 FILE *fp = NULL;
428 #endif
429 if ( !file || !*file || !mode || !*mode ) {
430 SDL_SetError("SDL_RWFromFile(): No file or no mode specified");
431 return NULL;
434 #if defined(__WIN32__) && !defined(__SYMBIAN32__)
435 rwops = SDL_AllocRW();
436 if (!rwops)
437 return NULL; /* SDL_SetError already setup by SDL_AllocRW() */
438 if (win32_file_open(rwops,file,mode) < 0) {
439 SDL_FreeRW(rwops);
440 return NULL;
442 rwops->seek = win32_file_seek;
443 rwops->read = win32_file_read;
444 rwops->write = win32_file_write;
445 rwops->close = win32_file_close;
447 #elif HAVE_STDIO_H
449 #ifdef __MACOS__
451 char *mpath = unix_to_mac(file);
452 fp = fopen(mpath, mode);
453 SDL_free(mpath);
455 #else
456 fp = fopen(file, mode);
457 #endif
458 if ( fp == NULL ) {
459 SDL_SetError("Couldn't open %s", file);
460 } else {
461 rwops = SDL_RWFromFP(fp, 1);
463 #else
464 SDL_SetError("SDL not compiled with stdio support");
465 #endif /* !HAVE_STDIO_H */
467 return(rwops);
470 #ifdef HAVE_STDIO_H
471 SDL_RWops *SDL_RWFromFP(FILE *fp, int autoclose)
473 SDL_RWops *rwops = NULL;
475 rwops = SDL_AllocRW();
476 if ( rwops != NULL ) {
477 rwops->seek = stdio_seek;
478 rwops->read = stdio_read;
479 rwops->write = stdio_write;
480 rwops->close = stdio_close;
481 rwops->hidden.stdio.fp = fp;
482 rwops->hidden.stdio.autoclose = autoclose;
484 return(rwops);
486 #endif /* HAVE_STDIO_H */
488 SDL_RWops *SDL_RWFromMem(void *mem, int size)
490 SDL_RWops *rwops;
492 rwops = SDL_AllocRW();
493 if ( rwops != NULL ) {
494 rwops->seek = mem_seek;
495 rwops->read = mem_read;
496 rwops->write = mem_write;
497 rwops->close = mem_close;
498 rwops->hidden.mem.base = (Uint8 *)mem;
499 rwops->hidden.mem.here = rwops->hidden.mem.base;
500 rwops->hidden.mem.stop = rwops->hidden.mem.base+size;
502 return(rwops);
505 SDL_RWops *SDL_RWFromConstMem(const void *mem, int size)
507 SDL_RWops *rwops;
509 rwops = SDL_AllocRW();
510 if ( rwops != NULL ) {
511 rwops->seek = mem_seek;
512 rwops->read = mem_read;
513 rwops->write = mem_writeconst;
514 rwops->close = mem_close;
515 rwops->hidden.mem.base = (Uint8 *)mem;
516 rwops->hidden.mem.here = rwops->hidden.mem.base;
517 rwops->hidden.mem.stop = rwops->hidden.mem.base+size;
519 return(rwops);
522 SDL_RWops *SDL_AllocRW(void)
524 SDL_RWops *area;
526 area = (SDL_RWops *)SDL_malloc(sizeof *area);
527 if ( area == NULL ) {
528 SDL_OutOfMemory();
530 return(area);
533 void SDL_FreeRW(SDL_RWops *area)
535 SDL_free(area);
538 /* Functions for dynamically reading and writing endian-specific values */
540 Uint16 SDL_ReadLE16 (SDL_RWops *src)
542 Uint16 value;
544 SDL_RWread(src, &value, (sizeof value), 1);
545 return(SDL_SwapLE16(value));
547 Uint16 SDL_ReadBE16 (SDL_RWops *src)
549 Uint16 value;
551 SDL_RWread(src, &value, (sizeof value), 1);
552 return(SDL_SwapBE16(value));
554 Uint32 SDL_ReadLE32 (SDL_RWops *src)
556 Uint32 value;
558 SDL_RWread(src, &value, (sizeof value), 1);
559 return(SDL_SwapLE32(value));
561 Uint32 SDL_ReadBE32 (SDL_RWops *src)
563 Uint32 value;
565 SDL_RWread(src, &value, (sizeof value), 1);
566 return(SDL_SwapBE32(value));
568 Uint64 SDL_ReadLE64 (SDL_RWops *src)
570 Uint64 value;
572 SDL_RWread(src, &value, (sizeof value), 1);
573 return(SDL_SwapLE64(value));
575 Uint64 SDL_ReadBE64 (SDL_RWops *src)
577 Uint64 value;
579 SDL_RWread(src, &value, (sizeof value), 1);
580 return(SDL_SwapBE64(value));
583 int SDL_WriteLE16 (SDL_RWops *dst, Uint16 value)
585 value = SDL_SwapLE16(value);
586 return(SDL_RWwrite(dst, &value, (sizeof value), 1));
588 int SDL_WriteBE16 (SDL_RWops *dst, Uint16 value)
590 value = SDL_SwapBE16(value);
591 return(SDL_RWwrite(dst, &value, (sizeof value), 1));
593 int SDL_WriteLE32 (SDL_RWops *dst, Uint32 value)
595 value = SDL_SwapLE32(value);
596 return(SDL_RWwrite(dst, &value, (sizeof value), 1));
598 int SDL_WriteBE32 (SDL_RWops *dst, Uint32 value)
600 value = SDL_SwapBE32(value);
601 return(SDL_RWwrite(dst, &value, (sizeof value), 1));
603 int SDL_WriteLE64 (SDL_RWops *dst, Uint64 value)
605 value = SDL_SwapLE64(value);
606 return(SDL_RWwrite(dst, &value, (sizeof value), 1));
608 int SDL_WriteBE64 (SDL_RWops *dst, Uint64 value)
610 value = SDL_SwapBE64(value);
611 return(SDL_RWwrite(dst, &value, (sizeof value), 1));