src/file/SDL_rwops.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 31 Dec 2011 09:16:08 -0500
branchSDL-1.2
changeset 6137 4720145f848b
parent 5988 bf927e528813
permissions -rw-r--r--
Happy New Year!
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@6137
     3
    Copyright (C) 1997-2012 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* This file provides a general interface for SDL to read and write
slouken@0
    25
   data sources.  It can easily be extended to files, memory, etc.
slouken@0
    26
*/
slouken@0
    27
slouken@1354
    28
#include "SDL_endian.h"
slouken@0
    29
#include "SDL_rwops.h"
slouken@1330
    30
slouken@1354
    31
icculus@3975
    32
#if defined(__WIN32__) && !defined(__SYMBIAN32__)
slouken@0
    33
slouken@1447
    34
/* Functions to read/write Win32 API file pointers */
slouken@1465
    35
/* Will not use it on WinCE because stdio is buffered, it means
slouken@1465
    36
   faster, and all stdio functions anyway are embedded in coredll.dll - 
slouken@1465
    37
   the main wince dll*/
slouken@0
    38
slouken@1446
    39
#define WINDOWS_LEAN_AND_MEAN
slouken@1446
    40
#include <windows.h>
slouken@1446
    41
slouken@1465
    42
#ifndef INVALID_SET_FILE_POINTER
slouken@1465
    43
#define INVALID_SET_FILE_POINTER 0xFFFFFFFF
slouken@1465
    44
#endif
slouken@1465
    45
slouken@4026
    46
#define READAHEAD_BUFFER_SIZE	1024
slouken@4026
    47
slouken@1769
    48
static int SDLCALL win32_file_open(SDL_RWops *context, const char *filename, const char *mode)
slouken@1465
    49
{
slouken@1465
    50
#ifndef _WIN32_WCE
slouken@1446
    51
	UINT	old_error_mode;
slouken@1465
    52
#endif
slouken@1446
    53
	HANDLE	h;
slouken@1446
    54
	DWORD	r_right, w_right;
slouken@1446
    55
	DWORD	must_exist, truncate;
slouken@1446
    56
	int		a_mode;
slouken@1446
    57
slouken@1455
    58
	if (!context)
slouken@4038
    59
		return -1; /* failed (invalid call) */
icculus@4039
    60
		
icculus@4039
    61
	context->hidden.win32io.h = INVALID_HANDLE_VALUE; /* mark this as unusable */
icculus@4039
    62
	context->hidden.win32io.buffer.data = NULL;
icculus@4039
    63
	context->hidden.win32io.buffer.size = 0;
icculus@4039
    64
	context->hidden.win32io.buffer.left = 0;
slouken@4026
    65
slouken@1446
    66
	/* "r" = reading, file must exist */
slouken@1446
    67
	/* "w" = writing, truncate existing, file may not exist */
slouken@1446
    68
	/* "r+"= reading or writing, file must exist            */
slouken@1446
    69
	/* "a" = writing, append file may not exist             */
slouken@1446
    70
	/* "a+"= append + read, file may not exist              */
slouken@1446
    71
	/* "w+" = read, write, truncate. file may not exist    */
slouken@1446
    72
	
slouken@1446
    73
	must_exist = ( SDL_strchr(mode,'r') != NULL ) ? OPEN_EXISTING : 0;
slouken@1446
    74
	truncate   = ( SDL_strchr(mode,'w') != NULL ) ? CREATE_ALWAYS : 0;
slouken@1446
    75
	r_right    = ( SDL_strchr(mode,'+') != NULL || must_exist ) ? GENERIC_READ : 0;
slouken@1453
    76
	a_mode     = ( SDL_strchr(mode,'a') != NULL ) ? OPEN_ALWAYS : 0;
slouken@1453
    77
	w_right    = ( a_mode || SDL_strchr(mode,'+') || truncate ) ? GENERIC_WRITE : 0;
slouken@1446
    78
slouken@1446
    79
	if (!r_right && !w_right) /* inconsistent mode */
slouken@4038
    80
		return -1; /* failed (invalid call) */
icculus@4035
    81
icculus@4039
    82
	context->hidden.win32io.buffer.data = (char *)SDL_malloc(READAHEAD_BUFFER_SIZE);
icculus@4039
    83
	if (!context->hidden.win32io.buffer.data) {
icculus@4039
    84
		SDL_OutOfMemory();
icculus@4039
    85
		return -1;
icculus@4039
    86
	}
icculus@4039
    87
slouken@1465
    88
#ifdef _WIN32_WCE
slouken@1465
    89
	{
slouken@1465
    90
		size_t size = SDL_strlen(filename)+1;
slouken@1465
    91
		wchar_t *filenameW = SDL_stack_alloc(wchar_t, size);
slouken@1465
    92
slouken@1465
    93
		if ( MultiByteToWideChar(CP_UTF8, 0, filename, -1, filenameW, size) == 0 ) {
slouken@1465
    94
			SDL_stack_free(filenameW);
icculus@4039
    95
			SDL_free(context->hidden.win32io.buffer.data);
icculus@4039
    96
			context->hidden.win32io.buffer.data = NULL;
icculus@4040
    97
			SDL_SetError("Unable to convert filename to Unicode");
slouken@1465
    98
			return -1;
slouken@1465
    99
		}
slouken@1465
   100
		h = CreateFile(filenameW, (w_right|r_right), (w_right)? 0 : FILE_SHARE_READ, 
slouken@1465
   101
					   NULL, (must_exist|truncate|a_mode), FILE_ATTRIBUTE_NORMAL,NULL);
slouken@1465
   102
		SDL_stack_free(filenameW);
slouken@1465
   103
	}
slouken@1465
   104
#else
icculus@4284
   105
	{
slouken@1465
   106
icculus@4284
   107
	/* handle Unicode filenames.  We do some tapdancing here to make sure this
icculus@4284
   108
	   works on Win9x, which doesn't support anything but 1-byte codepages. */
icculus@4284
   109
	const size_t size = SDL_strlen(filename)+1;
icculus@4284
   110
	static int unicode_support = -1;
slouken@1465
   111
icculus@4284
   112
	if (unicode_support == -1) {
icculus@4284
   113
		OSVERSIONINFO osVerInfo;     /* Information about the OS */
icculus@4284
   114
		osVerInfo.dwOSVersionInfoSize = sizeof(osVerInfo);
icculus@4284
   115
		if (!GetVersionEx(&osVerInfo)) {
icculus@4284
   116
			unicode_support = 0;
icculus@4284
   117
		} else if (osVerInfo.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS) {
icculus@4284
   118
			unicode_support = 1;  /* Not Win95/98/ME. */
icculus@4284
   119
		} else {
icculus@4284
   120
			unicode_support = 0;
icculus@4284
   121
		}
icculus@4284
   122
	}
icculus@4284
   123
icculus@4284
   124
	if (unicode_support) {  /* everything but Win95/98/ME. */
icculus@4284
   125
		wchar_t *filenameW = SDL_stack_alloc(wchar_t, size);
icculus@4284
   126
		if ( MultiByteToWideChar(CP_UTF8, 0, filename, -1, filenameW, size) == 0 ) {
icculus@4284
   127
			SDL_stack_free(filenameW);
icculus@4284
   128
			SDL_free(context->hidden.win32io.buffer.data);
icculus@4284
   129
			context->hidden.win32io.buffer.data = NULL;
icculus@4284
   130
			SDL_SetError("Unable to convert filename to Unicode");
icculus@4284
   131
			return -1;
icculus@4284
   132
		}
icculus@4284
   133
icculus@4284
   134
		/* Do not open a dialog box if failure */
icculus@4284
   135
		old_error_mode = SetErrorMode(SEM_NOOPENFILEERRORBOX|SEM_FAILCRITICALERRORS);
icculus@4284
   136
		h = CreateFileW(filenameW, (w_right|r_right), (w_right)? 0 : FILE_SHARE_READ,
icculus@4284
   137
					   NULL, (must_exist|truncate|a_mode), FILE_ATTRIBUTE_NORMAL,NULL);
icculus@4284
   138
		/* restore old behaviour */
icculus@4284
   139
		SetErrorMode(old_error_mode);
icculus@4284
   140
icculus@4284
   141
		SDL_stack_free(filenameW);
icculus@4284
   142
	} else {
icculus@4284
   143
		/* CP_UTF8 might not be supported (Win95), so use SDL_iconv to get wchars. */
icculus@4284
   144
		/* Use UCS2: no UTF-16 support here. Try again in SDL 1.3.  :) */
icculus@4284
   145
		char *utf16 = SDL_iconv_string("UCS2", "UTF8", filename, SDL_strlen(filename) + 1);
icculus@4284
   146
		char *filenameA = SDL_stack_alloc(char, size * 6);  /* 6, just in case. */
icculus@4284
   147
		BOOL bDefCharUsed = FALSE;
icculus@4284
   148
icculus@4284
   149
		/* Dither down to a codepage and hope for the best. */
icculus@4284
   150
		if (!utf16 ||
icculus@4284
   151
			!WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)utf16, -1, filenameA, size*6, 0, &bDefCharUsed) ||
icculus@4284
   152
			bDefCharUsed) {
icculus@4284
   153
			SDL_stack_free(filenameA);
icculus@4284
   154
			SDL_free(utf16);
icculus@4284
   155
			SDL_free(context->hidden.win32io.buffer.data);
icculus@4284
   156
			context->hidden.win32io.buffer.data = NULL;
icculus@4284
   157
			SDL_SetError("Unable to convert filename to Unicode");
icculus@4284
   158
			return -1;
icculus@4284
   159
		}
icculus@4284
   160
icculus@4284
   161
		/* Do not open a dialog box if failure */
icculus@4284
   162
		old_error_mode = SetErrorMode(SEM_NOOPENFILEERRORBOX|SEM_FAILCRITICALERRORS);
icculus@4284
   163
		h = CreateFile(filenameA, (w_right|r_right), (w_right)? 0 : FILE_SHARE_READ,
icculus@4284
   164
		               NULL, (must_exist|truncate|a_mode), FILE_ATTRIBUTE_NORMAL,NULL);
icculus@4284
   165
		/* restore old behaviour */
icculus@4284
   166
		SetErrorMode(old_error_mode);
icculus@4284
   167
icculus@4284
   168
		SDL_stack_free(filenameA);
icculus@4284
   169
		SDL_free(utf16);
icculus@4284
   170
	}
icculus@4284
   171
icculus@4284
   172
	}
slouken@1465
   173
#endif /* _WIN32_WCE */
slouken@1446
   174
slouken@1446
   175
	if (h==INVALID_HANDLE_VALUE) {
icculus@4039
   176
		SDL_free(context->hidden.win32io.buffer.data);
icculus@4039
   177
		context->hidden.win32io.buffer.data = NULL;
icculus@4040
   178
		SDL_SetError("Couldn't open %s",filename);
slouken@1446
   179
		return -2; /* failed (CreateFile) */
slouken@1446
   180
	}
slouken@1446
   181
	context->hidden.win32io.h = h;
slouken@1446
   182
	context->hidden.win32io.append = a_mode;
icculus@4035
   183
slouken@1446
   184
	return 0; /* ok */
slouken@1446
   185
}
slouken@1769
   186
static int SDLCALL win32_file_seek(SDL_RWops *context, int offset, int whence)
slouken@1465
   187
{
slouken@1446
   188
	DWORD win32whence;
slouken@1446
   189
	int   file_pos;
slouken@1446
   190
	
slouken@1446
   191
	if (!context || context->hidden.win32io.h == INVALID_HANDLE_VALUE) {
slouken@1446
   192
		SDL_SetError("win32_file_seek: invalid context/file not opened");
slouken@1446
   193
		return -1;
slouken@1446
   194
	}
slouken@4026
   195
slouken@4026
   196
	/* FIXME: We may be able to satisfy the seek within buffered data */
slouken@4026
   197
	if (whence == RW_SEEK_CUR && context->hidden.win32io.buffer.left) {
slouken@4026
   198
		offset -= context->hidden.win32io.buffer.left;
slouken@4026
   199
    }
slouken@4026
   200
    context->hidden.win32io.buffer.left = 0;
slouken@4026
   201
slouken@1446
   202
	switch (whence) {
slouken@1446
   203
		case RW_SEEK_SET:		
slouken@1446
   204
			win32whence = FILE_BEGIN; break;
slouken@1446
   205
		case RW_SEEK_CUR:
slouken@1446
   206
			win32whence = FILE_CURRENT; break;
slouken@1446
   207
		case RW_SEEK_END:
slouken@1446
   208
			win32whence = FILE_END; break;
slouken@1446
   209
		default:
slouken@1446
   210
			SDL_SetError("win32_file_seek: Unknown value for 'whence'");			
slouken@1446
   211
			return -1;
slouken@1446
   212
	}
slouken@4026
   213
slouken@1446
   214
	file_pos = SetFilePointer(context->hidden.win32io.h,offset,NULL,win32whence);
slouken@1446
   215
slouken@1446
   216
	if ( file_pos != INVALID_SET_FILE_POINTER )
slouken@1446
   217
		return file_pos; /* success */
slouken@1446
   218
	
slouken@1446
   219
	SDL_Error(SDL_EFSEEK);
slouken@1446
   220
	return -1; /* error */
slouken@1446
   221
}
slouken@1769
   222
static int SDLCALL win32_file_read(SDL_RWops *context, void *ptr, int size, int maxnum)
slouken@1465
   223
{
slouken@4026
   224
	int		total_need; 
slouken@4026
   225
	int		total_read = 0; 
slouken@4026
   226
    int     read_ahead;
slouken@4026
   227
	DWORD	byte_read;
slouken@1446
   228
	
slouken@4026
   229
	total_need = size*maxnum;
slouken@1446
   230
	
slouken@4026
   231
	if (!context || context->hidden.win32io.h == INVALID_HANDLE_VALUE || total_need<=0 || !size) 	
slouken@1446
   232
		return 0;
slouken@4026
   233
slouken@4026
   234
    if (context->hidden.win32io.buffer.left > 0) {
slouken@4026
   235
        void *data = (char *)context->hidden.win32io.buffer.data +
slouken@4026
   236
                             context->hidden.win32io.buffer.size -
slouken@4026
   237
                             context->hidden.win32io.buffer.left;
slouken@4026
   238
        read_ahead = SDL_min(total_need, context->hidden.win32io.buffer.left); 
slouken@4026
   239
        SDL_memcpy(ptr, data, read_ahead);
slouken@4026
   240
        context->hidden.win32io.buffer.left -= read_ahead;
slouken@4026
   241
slouken@4026
   242
        if (read_ahead == total_need) {
slouken@4026
   243
            return maxnum;
slouken@4026
   244
        }
slouken@4026
   245
        ptr = (char *)ptr + read_ahead;
slouken@4026
   246
        total_need -= read_ahead;       
slouken@4026
   247
		total_read += read_ahead;
slouken@4026
   248
    }
slouken@4026
   249
slouken@4026
   250
    if (total_need < READAHEAD_BUFFER_SIZE) {
slouken@4026
   251
        if (!ReadFile(context->hidden.win32io.h,context->hidden.win32io.buffer.data,READAHEAD_BUFFER_SIZE,&byte_read,NULL)) {
slouken@4026
   252
            SDL_Error(SDL_EFREAD);
slouken@4026
   253
            return 0;
slouken@4026
   254
        }
slouken@4027
   255
        read_ahead = SDL_min(total_need, (int)byte_read);
slouken@4026
   256
        SDL_memcpy(ptr, context->hidden.win32io.buffer.data, read_ahead);
slouken@4026
   257
        context->hidden.win32io.buffer.size = byte_read;
slouken@4026
   258
        context->hidden.win32io.buffer.left = byte_read-read_ahead;
slouken@4026
   259
        total_read += read_ahead;
slouken@4026
   260
    } else {
slouken@4026
   261
        if (!ReadFile(context->hidden.win32io.h,ptr,total_need,&byte_read,NULL)) {
slouken@4026
   262
            SDL_Error(SDL_EFREAD);
slouken@4026
   263
            return 0;
slouken@4026
   264
        }
slouken@4026
   265
        total_read += byte_read;
slouken@4026
   266
    }
slouken@4026
   267
	return (total_read/size);
slouken@1446
   268
}
slouken@1769
   269
static int SDLCALL win32_file_write(SDL_RWops *context, const void *ptr, int size, int num)
slouken@1465
   270
{
slouken@1446
   271
	
slouken@1446
   272
	int		total_bytes; 
slouken@1446
   273
	DWORD	byte_written,nwritten;
slouken@1446
   274
	
slouken@1446
   275
	total_bytes = size*num;
slouken@1446
   276
slouken@1446
   277
	if (!context || context->hidden.win32io.h==INVALID_HANDLE_VALUE || total_bytes<=0 || !size) 	
slouken@1446
   278
		return 0;
slouken@1446
   279
slouken@4026
   280
	if (context->hidden.win32io.buffer.left) {
slouken@4026
   281
		SetFilePointer(context->hidden.win32io.h,-context->hidden.win32io.buffer.left,NULL,FILE_CURRENT);
slouken@4026
   282
		context->hidden.win32io.buffer.left = 0;
slouken@4026
   283
	}
slouken@4026
   284
slouken@1446
   285
	/* if in append mode, we must go to the EOF before write */
slouken@1446
   286
	if (context->hidden.win32io.append) {
slouken@1446
   287
		if ( SetFilePointer(context->hidden.win32io.h,0L,NULL,FILE_END) == INVALID_SET_FILE_POINTER ) {
slouken@1446
   288
			SDL_Error(SDL_EFWRITE);
slouken@1446
   289
			return 0;
slouken@1446
   290
		}
slouken@1446
   291
	}
slouken@1446
   292
	
slouken@1446
   293
	if (!WriteFile(context->hidden.win32io.h,ptr,total_bytes,&byte_written,NULL)) {
slouken@1446
   294
		SDL_Error(SDL_EFWRITE);
slouken@1446
   295
		return 0;
slouken@1446
   296
	}
slouken@1446
   297
	
slouken@1446
   298
	nwritten = byte_written/size;
slouken@1446
   299
	return nwritten;
slouken@1446
   300
}
slouken@1769
   301
static int SDLCALL win32_file_close(SDL_RWops *context)
slouken@1465
   302
{
slouken@1446
   303
	
slouken@1446
   304
	if ( context ) {								
slouken@1446
   305
		if (context->hidden.win32io.h != INVALID_HANDLE_VALUE) {
slouken@1446
   306
			CloseHandle(context->hidden.win32io.h);
slouken@1446
   307
			context->hidden.win32io.h = INVALID_HANDLE_VALUE; /* to be sure */
slouken@1446
   308
		}
slouken@4026
   309
		if (context->hidden.win32io.buffer.data) {
slouken@4026
   310
			SDL_free(context->hidden.win32io.buffer.data);
slouken@4026
   311
			context->hidden.win32io.buffer.data = NULL;
slouken@4026
   312
		}
slouken@1446
   313
		SDL_FreeRW(context);
slouken@1446
   314
	}
slouken@1446
   315
	return(0);
slouken@1446
   316
}
slouken@1447
   317
#endif /* __WIN32__ */
slouken@1446
   318
slouken@1447
   319
#ifdef HAVE_STDIO_H
slouken@1446
   320
slouken@1447
   321
/* Functions to read/write stdio file pointers */
slouken@1446
   322
slouken@1769
   323
static int SDLCALL stdio_seek(SDL_RWops *context, int offset, int whence)
slouken@1447
   324
{
slouken@1447
   325
	if ( fseek(context->hidden.stdio.fp, offset, whence) == 0 ) {
slouken@1447
   326
		return(ftell(context->hidden.stdio.fp));
slouken@1447
   327
	} else {
slouken@1447
   328
		SDL_Error(SDL_EFSEEK);
slouken@1447
   329
		return(-1);
slouken@1447
   330
	}
slouken@1447
   331
}
slouken@1769
   332
static int SDLCALL stdio_read(SDL_RWops *context, void *ptr, int size, int maxnum)
slouken@1447
   333
{
slouken@1447
   334
	size_t nread;
slouken@1447
   335
slouken@1447
   336
	nread = fread(ptr, size, maxnum, context->hidden.stdio.fp); 
slouken@1447
   337
	if ( nread == 0 && ferror(context->hidden.stdio.fp) ) {
slouken@1447
   338
		SDL_Error(SDL_EFREAD);
slouken@1447
   339
	}
slouken@1447
   340
	return(nread);
slouken@1447
   341
}
slouken@1769
   342
static int SDLCALL stdio_write(SDL_RWops *context, const void *ptr, int size, int num)
slouken@1447
   343
{
slouken@1447
   344
	size_t nwrote;
slouken@1447
   345
slouken@1447
   346
	nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
slouken@1447
   347
	if ( nwrote == 0 && ferror(context->hidden.stdio.fp) ) {
slouken@1447
   348
		SDL_Error(SDL_EFWRITE);
slouken@1447
   349
	}
slouken@1447
   350
	return(nwrote);
slouken@1447
   351
}
slouken@1769
   352
static int SDLCALL stdio_close(SDL_RWops *context)
slouken@1447
   353
{
slouken@1447
   354
	if ( context ) {
slouken@1447
   355
		if ( context->hidden.stdio.autoclose ) {
slouken@1447
   356
			/* WARNING:  Check the return value here! */
slouken@1447
   357
			fclose(context->hidden.stdio.fp);
slouken@1447
   358
		}
slouken@1447
   359
		SDL_FreeRW(context);
slouken@1447
   360
	}
slouken@1447
   361
	return(0);
slouken@1447
   362
}
slouken@1446
   363
#endif /* !HAVE_STDIO_H */
slouken@1330
   364
slouken@0
   365
/* Functions to read/write memory pointers */
slouken@0
   366
slouken@1769
   367
static int SDLCALL mem_seek(SDL_RWops *context, int offset, int whence)
slouken@0
   368
{
slouken@0
   369
	Uint8 *newpos;
slouken@0
   370
slouken@0
   371
	switch (whence) {
slouken@1330
   372
		case RW_SEEK_SET:
slouken@0
   373
			newpos = context->hidden.mem.base+offset;
slouken@0
   374
			break;
slouken@1330
   375
		case RW_SEEK_CUR:
slouken@0
   376
			newpos = context->hidden.mem.here+offset;
slouken@0
   377
			break;
slouken@1330
   378
		case RW_SEEK_END:
slouken@0
   379
			newpos = context->hidden.mem.stop+offset;
slouken@0
   380
			break;
slouken@0
   381
		default:
slouken@0
   382
			SDL_SetError("Unknown value for 'whence'");
slouken@0
   383
			return(-1);
slouken@0
   384
	}
slouken@0
   385
	if ( newpos < context->hidden.mem.base ) {
slouken@0
   386
		newpos = context->hidden.mem.base;
slouken@0
   387
	}
slouken@0
   388
	if ( newpos > context->hidden.mem.stop ) {
slouken@0
   389
		newpos = context->hidden.mem.stop;
slouken@0
   390
	}
slouken@0
   391
	context->hidden.mem.here = newpos;
slouken@0
   392
	return(context->hidden.mem.here-context->hidden.mem.base);
slouken@0
   393
}
slouken@1769
   394
static int SDLCALL mem_read(SDL_RWops *context, void *ptr, int size, int maxnum)
slouken@0
   395
{
slouken@1456
   396
	size_t total_bytes;
slouken@1456
   397
	size_t mem_available;
slouken@0
   398
icculus@1078
   399
	total_bytes = (maxnum * size);
slouken@1465
   400
	if ( (maxnum <= 0) || (size <= 0) || ((total_bytes / maxnum) != (size_t) size) ) {
icculus@1078
   401
		return 0;
slouken@0
   402
	}
icculus@1078
   403
icculus@1078
   404
	mem_available = (context->hidden.mem.stop - context->hidden.mem.here);
icculus@1078
   405
	if (total_bytes > mem_available) {
icculus@1078
   406
		total_bytes = mem_available;
icculus@1078
   407
	}
icculus@1078
   408
slouken@1336
   409
	SDL_memcpy(ptr, context->hidden.mem.here, total_bytes);
icculus@1078
   410
	context->hidden.mem.here += total_bytes;
icculus@1078
   411
icculus@1078
   412
	return (total_bytes / size);
slouken@0
   413
}
slouken@1769
   414
static int SDLCALL mem_write(SDL_RWops *context, const void *ptr, int size, int num)
slouken@0
   415
{
slouken@0
   416
	if ( (context->hidden.mem.here + (num*size)) > context->hidden.mem.stop ) {
slouken@0
   417
		num = (context->hidden.mem.stop-context->hidden.mem.here)/size;
slouken@0
   418
	}
slouken@1336
   419
	SDL_memcpy(context->hidden.mem.here, ptr, num*size);
slouken@0
   420
	context->hidden.mem.here += num*size;
slouken@0
   421
	return(num);
slouken@0
   422
}
slouken@1769
   423
static int SDLCALL mem_writeconst(SDL_RWops *context, const void *ptr, int size, int num)
slouken@764
   424
{
slouken@764
   425
	SDL_SetError("Can't write to read-only memory");
slouken@764
   426
	return(-1);
slouken@764
   427
}
slouken@1769
   428
static int SDLCALL mem_close(SDL_RWops *context)
slouken@0
   429
{
slouken@0
   430
	if ( context ) {
icculus@1202
   431
		SDL_FreeRW(context);
slouken@0
   432
	}
slouken@0
   433
	return(0);
slouken@0
   434
}
slouken@0
   435
slouken@1447
   436
slouken@0
   437
/* Functions to create SDL_RWops structures from various data sources */
slouken@0
   438
slouken@1402
   439
#ifdef __MACOS__
slouken@0
   440
/*
slouken@0
   441
 * translate unix-style slash-separated filename to mac-style colon-separated
slouken@0
   442
 * name; return malloced string
slouken@0
   443
 */
slouken@0
   444
static char *unix_to_mac(const char *file)
slouken@0
   445
{
slouken@1336
   446
	int flen = SDL_strlen(file);
slouken@1336
   447
	char *path = SDL_malloc(flen + 2);
slouken@0
   448
	const char *src = file;
slouken@0
   449
	char *dst = path;
slouken@0
   450
	if(*src == '/') {
slouken@0
   451
		/* really depends on filesystem layout, hope for the best */
slouken@0
   452
		src++;
slouken@0
   453
	} else {
slouken@0
   454
		/* Check if this is a MacOS path to begin with */
slouken@0
   455
		if(*src != ':')
slouken@0
   456
			*dst++ = ':';   /* relative paths begin with ':' */
slouken@0
   457
	}
slouken@0
   458
	while(src < file + flen) {
slouken@1336
   459
		const char *end = SDL_strchr(src, '/');
slouken@0
   460
		int len;
slouken@0
   461
		if(!end)
slouken@0
   462
			end = file + flen; /* last component */
slouken@0
   463
		len = end - src;
slouken@0
   464
		if(len == 0 || (len == 1 && src[0] == '.')) {
slouken@0
   465
			/* remove repeated slashes and . */
slouken@0
   466
		} else {
slouken@0
   467
			if(len == 2 && src[0] == '.' && src[1] == '.') {
slouken@0
   468
				/* replace .. with the empty string */
slouken@0
   469
			} else {
slouken@1336
   470
				SDL_memcpy(dst, src, len);
slouken@0
   471
				dst += len;
slouken@0
   472
			}
slouken@0
   473
			if(end < file + flen)
slouken@0
   474
				*dst++ = ':';
slouken@0
   475
		}
slouken@0
   476
		src = end + 1;
slouken@0
   477
	}
slouken@0
   478
	*dst++ = '\0';
slouken@0
   479
	return path;
slouken@0
   480
}
slouken@1402
   481
#endif /* __MACOS__ */
slouken@0
   482
slouken@0
   483
SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
slouken@0
   484
{
slouken@1330
   485
	SDL_RWops *rwops = NULL;
slouken@1465
   486
#ifdef HAVE_STDIO_H
slouken@1465
   487
	FILE *fp = NULL;
icculus@5988
   488
	(void) fp;
slouken@1465
   489
#endif
slouken@1455
   490
	if ( !file || !*file || !mode || !*mode ) {
slouken@1455
   491
		SDL_SetError("SDL_RWFromFile(): No file or no mode specified");
slouken@1455
   492
		return NULL;
slouken@1455
   493
	}
slouken@1455
   494
icculus@3975
   495
#if defined(__WIN32__) && !defined(__SYMBIAN32__)
slouken@1447
   496
	rwops = SDL_AllocRW();
slouken@1453
   497
	if (!rwops)
slouken@1453
   498
		return NULL; /* SDL_SetError already setup by SDL_AllocRW() */
slouken@4038
   499
	if (win32_file_open(rwops,file,mode) < 0) {
slouken@1447
   500
		SDL_FreeRW(rwops);
slouken@1447
   501
		return NULL;
slouken@1447
   502
	}	
slouken@1447
   503
	rwops->seek  = win32_file_seek;
slouken@1447
   504
	rwops->read  = win32_file_read;
slouken@1447
   505
	rwops->write = win32_file_write;
slouken@1447
   506
	rwops->close = win32_file_close;
slouken@1447
   507
slouken@1447
   508
#elif HAVE_STDIO_H
slouken@0
   509
slouken@1402
   510
#ifdef __MACOS__
slouken@0
   511
	{
slouken@0
   512
		char *mpath = unix_to_mac(file);
slouken@0
   513
		fp = fopen(mpath, mode);
slouken@1336
   514
		SDL_free(mpath);
slouken@0
   515
	}
slouken@0
   516
#else
slouken@0
   517
	fp = fopen(file, mode);
slouken@0
   518
#endif
slouken@0
   519
	if ( fp == NULL ) {
slouken@0
   520
		SDL_SetError("Couldn't open %s", file);
slouken@0
   521
	} else {
slouken@0
   522
		rwops = SDL_RWFromFP(fp, 1);
slouken@1447
   523
	}
slouken@0
   524
#else
slouken@1447
   525
	SDL_SetError("SDL not compiled with stdio support");
slouken@1446
   526
#endif /* !HAVE_STDIO_H */
slouken@1447
   527
slouken@0
   528
	return(rwops);
slouken@0
   529
}
slouken@0
   530
slouken@1330
   531
#ifdef HAVE_STDIO_H
slouken@0
   532
SDL_RWops *SDL_RWFromFP(FILE *fp, int autoclose)
slouken@0
   533
{
slouken@1330
   534
	SDL_RWops *rwops = NULL;
slouken@0
   535
slouken@0
   536
	rwops = SDL_AllocRW();
slouken@0
   537
	if ( rwops != NULL ) {
slouken@0
   538
		rwops->seek = stdio_seek;
slouken@0
   539
		rwops->read = stdio_read;
slouken@0
   540
		rwops->write = stdio_write;
slouken@0
   541
		rwops->close = stdio_close;
slouken@0
   542
		rwops->hidden.stdio.fp = fp;
slouken@0
   543
		rwops->hidden.stdio.autoclose = autoclose;
slouken@0
   544
	}
slouken@0
   545
	return(rwops);
slouken@0
   546
}
slouken@1330
   547
#endif /* HAVE_STDIO_H */
slouken@0
   548
slouken@0
   549
SDL_RWops *SDL_RWFromMem(void *mem, int size)
slouken@0
   550
{
slouken@0
   551
	SDL_RWops *rwops;
slouken@0
   552
slouken@0
   553
	rwops = SDL_AllocRW();
slouken@0
   554
	if ( rwops != NULL ) {
slouken@0
   555
		rwops->seek = mem_seek;
slouken@0
   556
		rwops->read = mem_read;
slouken@0
   557
		rwops->write = mem_write;
slouken@0
   558
		rwops->close = mem_close;
slouken@0
   559
		rwops->hidden.mem.base = (Uint8 *)mem;
slouken@0
   560
		rwops->hidden.mem.here = rwops->hidden.mem.base;
slouken@0
   561
		rwops->hidden.mem.stop = rwops->hidden.mem.base+size;
slouken@0
   562
	}
slouken@0
   563
	return(rwops);
slouken@0
   564
}
slouken@0
   565
slouken@764
   566
SDL_RWops *SDL_RWFromConstMem(const void *mem, int size)
slouken@764
   567
{
slouken@764
   568
	SDL_RWops *rwops;
slouken@764
   569
slouken@764
   570
	rwops = SDL_AllocRW();
slouken@764
   571
	if ( rwops != NULL ) {
slouken@764
   572
		rwops->seek = mem_seek;
slouken@764
   573
		rwops->read = mem_read;
slouken@764
   574
		rwops->write = mem_writeconst;
slouken@764
   575
		rwops->close = mem_close;
slouken@764
   576
		rwops->hidden.mem.base = (Uint8 *)mem;
slouken@764
   577
		rwops->hidden.mem.here = rwops->hidden.mem.base;
slouken@764
   578
		rwops->hidden.mem.stop = rwops->hidden.mem.base+size;
slouken@764
   579
	}
slouken@764
   580
	return(rwops);
slouken@764
   581
}
slouken@764
   582
slouken@0
   583
SDL_RWops *SDL_AllocRW(void)
slouken@0
   584
{
slouken@0
   585
	SDL_RWops *area;
slouken@0
   586
slouken@1336
   587
	area = (SDL_RWops *)SDL_malloc(sizeof *area);
slouken@0
   588
	if ( area == NULL ) {
slouken@0
   589
		SDL_OutOfMemory();
slouken@0
   590
	}
slouken@0
   591
	return(area);
slouken@0
   592
}
slouken@0
   593
slouken@0
   594
void SDL_FreeRW(SDL_RWops *area)
slouken@0
   595
{
slouken@1336
   596
	SDL_free(area);
slouken@0
   597
}
slouken@1354
   598
slouken@1354
   599
/* Functions for dynamically reading and writing endian-specific values */
slouken@1354
   600
slouken@1354
   601
Uint16 SDL_ReadLE16 (SDL_RWops *src)
slouken@1354
   602
{
slouken@1354
   603
	Uint16 value;
slouken@1354
   604
slouken@1354
   605
	SDL_RWread(src, &value, (sizeof value), 1);
slouken@1354
   606
	return(SDL_SwapLE16(value));
slouken@1354
   607
}
slouken@1354
   608
Uint16 SDL_ReadBE16 (SDL_RWops *src)
slouken@1354
   609
{
slouken@1354
   610
	Uint16 value;
slouken@1354
   611
slouken@1354
   612
	SDL_RWread(src, &value, (sizeof value), 1);
slouken@1354
   613
	return(SDL_SwapBE16(value));
slouken@1354
   614
}
slouken@1354
   615
Uint32 SDL_ReadLE32 (SDL_RWops *src)
slouken@1354
   616
{
slouken@1354
   617
	Uint32 value;
slouken@1354
   618
slouken@1354
   619
	SDL_RWread(src, &value, (sizeof value), 1);
slouken@1354
   620
	return(SDL_SwapLE32(value));
slouken@1354
   621
}
slouken@1354
   622
Uint32 SDL_ReadBE32 (SDL_RWops *src)
slouken@1354
   623
{
slouken@1354
   624
	Uint32 value;
slouken@1354
   625
slouken@1354
   626
	SDL_RWread(src, &value, (sizeof value), 1);
slouken@1354
   627
	return(SDL_SwapBE32(value));
slouken@1354
   628
}
slouken@1354
   629
Uint64 SDL_ReadLE64 (SDL_RWops *src)
slouken@1354
   630
{
slouken@1354
   631
	Uint64 value;
slouken@1354
   632
slouken@1354
   633
	SDL_RWread(src, &value, (sizeof value), 1);
slouken@1354
   634
	return(SDL_SwapLE64(value));
slouken@1354
   635
}
slouken@1354
   636
Uint64 SDL_ReadBE64 (SDL_RWops *src)
slouken@1354
   637
{
slouken@1354
   638
	Uint64 value;
slouken@1354
   639
slouken@1354
   640
	SDL_RWread(src, &value, (sizeof value), 1);
slouken@1354
   641
	return(SDL_SwapBE64(value));
slouken@1354
   642
}
slouken@1354
   643
slouken@1354
   644
int SDL_WriteLE16 (SDL_RWops *dst, Uint16 value)
slouken@1354
   645
{
slouken@1354
   646
	value = SDL_SwapLE16(value);
slouken@1354
   647
	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1354
   648
}
slouken@1354
   649
int SDL_WriteBE16 (SDL_RWops *dst, Uint16 value)
slouken@1354
   650
{
slouken@1354
   651
	value = SDL_SwapBE16(value);
slouken@1354
   652
	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1354
   653
}
slouken@1354
   654
int SDL_WriteLE32 (SDL_RWops *dst, Uint32 value)
slouken@1354
   655
{
slouken@1354
   656
	value = SDL_SwapLE32(value);
slouken@1354
   657
	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1354
   658
}
slouken@1354
   659
int SDL_WriteBE32 (SDL_RWops *dst, Uint32 value)
slouken@1354
   660
{
slouken@1354
   661
	value = SDL_SwapBE32(value);
slouken@1354
   662
	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1354
   663
}
slouken@1354
   664
int SDL_WriteLE64 (SDL_RWops *dst, Uint64 value)
slouken@1354
   665
{
slouken@1354
   666
	value = SDL_SwapLE64(value);
slouken@1354
   667
	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1354
   668
}
slouken@1354
   669
int SDL_WriteBE64 (SDL_RWops *dst, Uint64 value)
slouken@1354
   670
{
slouken@1354
   671
	value = SDL_SwapBE64(value);
slouken@1354
   672
	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
slouken@1354
   673
}