src/file/SDL_rwops.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 27 Feb 2006 04:16:44 +0000
changeset 1447 515df0086eb7
parent 1446 47bf1767c4ca
child 1453 2eb75f8a32ca
permissions -rw-r--r--
Prefer the raw Win32 API over stdio for file RWops
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     1
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     2
    SDL - Simple DirectMedia Layer
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1269
diff changeset
     3
    Copyright (C) 1997-2006 Sam Lantinga
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     4
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     5
    This library is free software; you can redistribute it and/or
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1269
diff changeset
     6
    modify it under the terms of the GNU Lesser General Public
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     7
    License as published by the Free Software Foundation; either
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1269
diff changeset
     8
    version 2.1 of the License, or (at your option) any later version.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     9
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    10
    This library is distributed in the hope that it will be useful,
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1269
diff changeset
    13
    Lesser General Public License for more details.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    14
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1269
diff changeset
    15
    You should have received a copy of the GNU Lesser General Public
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1269
diff changeset
    16
    License along with this library; if not, write to the Free Software
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1269
diff changeset
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    18
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    19
    Sam Lantinga
252
e8157fcb3114 Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents: 36
diff changeset
    20
    slouken@libsdl.org
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    21
*/
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    22
#include "SDL_config.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    23
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    24
/* This file provides a general interface for SDL to read and write
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    25
   data sources.  It can easily be extended to files, memory, etc.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    26
*/
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    27
1354
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
    28
#include "SDL_endian.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    29
#include "SDL_rwops.h"
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
    30
1354
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
    31
1447
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
    32
#ifdef __WIN32__
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    33
1447
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
    34
/* Functions to read/write Win32 API file pointers */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    35
1446
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    36
#define WINDOWS_LEAN_AND_MEAN
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    37
#include <windows.h>
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    38
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    39
static int win32_file_open(SDL_RWops *context, const char *filename, const char *mode) {
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    40
	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    41
	UINT	old_error_mode;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    42
	HANDLE	h;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    43
	DWORD	r_right, w_right;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    44
	DWORD	must_exist, truncate;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    45
	int		a_mode;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    46
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    47
	if (!context || !filename || !mode)
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    48
		return -1;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    49
		
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    50
	context->hidden.win32io.h = INVALID_HANDLE_VALUE; /* mark this as unusable */
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    51
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    52
	/* "r" = reading, file must exist */
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    53
	/* "w" = writing, truncate existing, file may not exist */
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    54
	/* "r+"= reading or writing, file must exist            */
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    55
	/* "a" = writing, append file may not exist             */
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    56
	/* "a+"= append + read, file may not exist              */
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    57
	/* "w+" = read, write, truncate. file may not exist    */
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    58
	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    59
	must_exist = ( SDL_strchr(mode,'r') != NULL ) ? OPEN_EXISTING : 0;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    60
	truncate   = ( SDL_strchr(mode,'w') != NULL ) ? CREATE_ALWAYS : 0;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    61
	r_right    = ( SDL_strchr(mode,'+') != NULL || must_exist ) ? GENERIC_READ : 0;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    62
	a_mode     = ( SDL_strchr(mode,'a') != NULL );
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    63
	w_right    = ( a_mode || SDL_strchr(mode,'w') || truncate ) ? GENERIC_WRITE : 0;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    64
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    65
	if (!r_right && !w_right) /* inconsistent mode */
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    66
		return -1; /* failed (invalid call)*/
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    67
	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    68
	/* Do not open a dialog box if failure */
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    69
	old_error_mode = SetErrorMode(SEM_NOOPENFILEERRORBOX|SEM_FAILCRITICALERRORS);	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    70
	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    71
	h = CreateFile(filename, (w_right|r_right), (w_right)? 0 : FILE_SHARE_READ, 
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    72
		           NULL, (must_exist|truncate), FILE_ATTRIBUTE_NORMAL,NULL);
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    73
	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    74
	/* restore old behaviour */
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    75
	SetErrorMode(old_error_mode);
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    76
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    77
	if (h==INVALID_HANDLE_VALUE) {
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    78
		SDL_SetError("Couldn't open %s",filename);
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    79
		return -2; /* failed (CreateFile) */
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    80
	}
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    81
	context->hidden.win32io.h = h;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    82
	context->hidden.win32io.append = a_mode;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    83
	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    84
	return 0; /* ok */
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    85
}
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    86
static int win32_file_seek(SDL_RWops *context, int offset, int whence) {
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    87
	DWORD win32whence;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    88
	int   file_pos;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    89
	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    90
	if (!context || context->hidden.win32io.h == INVALID_HANDLE_VALUE) {
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    91
		SDL_SetError("win32_file_seek: invalid context/file not opened");
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    92
		return -1;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    93
	}
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    94
	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    95
	switch (whence) {
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    96
		case RW_SEEK_SET:		
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    97
			win32whence = FILE_BEGIN; break;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    98
		case RW_SEEK_CUR:
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    99
			win32whence = FILE_CURRENT; break;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   100
		case RW_SEEK_END:
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   101
			win32whence = FILE_END; break;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   102
		default:
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   103
			SDL_SetError("win32_file_seek: Unknown value for 'whence'");			
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   104
			return -1;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   105
	}
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   106
	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   107
	file_pos = SetFilePointer(context->hidden.win32io.h,offset,NULL,win32whence);
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   108
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   109
	if ( file_pos != INVALID_SET_FILE_POINTER )
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   110
		return file_pos; /* success */
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   111
	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   112
	SDL_Error(SDL_EFSEEK);
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   113
	return -1; /* error */
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   114
}
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   115
static int win32_file_read(SDL_RWops *context, void *ptr, int size, int maxnum) {
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   116
	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   117
	int		total_bytes; 
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   118
	DWORD	byte_read,nread;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   119
	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   120
	total_bytes = size*maxnum;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   121
	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   122
	if (!context || context->hidden.win32io.h == INVALID_HANDLE_VALUE || total_bytes<=0 || !size) 	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   123
		return 0;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   124
	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   125
	if (!ReadFile(context->hidden.win32io.h,ptr,total_bytes,&byte_read,NULL)) {
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   126
		SDL_Error(SDL_EFREAD);
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   127
		return 0;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   128
	}
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   129
	nread = byte_read/size;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   130
	return nread;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   131
}
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   132
static int win32_file_write(SDL_RWops *context, const void *ptr, int size, int num) {
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   133
	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   134
	int		total_bytes; 
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   135
	DWORD	byte_written,nwritten;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   136
	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   137
	total_bytes = size*num;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   138
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   139
	if (!context || context->hidden.win32io.h==INVALID_HANDLE_VALUE || total_bytes<=0 || !size) 	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   140
		return 0;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   141
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   142
	/* if in append mode, we must go to the EOF before write */
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   143
	if (context->hidden.win32io.append) {
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   144
		if ( SetFilePointer(context->hidden.win32io.h,0L,NULL,FILE_END) == INVALID_SET_FILE_POINTER ) {
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   145
			SDL_Error(SDL_EFWRITE);
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   146
			return 0;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   147
		}
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   148
	}
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   149
	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   150
	if (!WriteFile(context->hidden.win32io.h,ptr,total_bytes,&byte_written,NULL)) {
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   151
		SDL_Error(SDL_EFWRITE);
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   152
		return 0;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   153
	}
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   154
	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   155
	nwritten = byte_written/size;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   156
	return nwritten;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   157
}
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   158
static int win32_file_close(SDL_RWops *context) {
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   159
	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   160
	if ( context ) {								
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   161
		if (context->hidden.win32io.h != INVALID_HANDLE_VALUE) {
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   162
			CloseHandle(context->hidden.win32io.h);
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   163
			context->hidden.win32io.h = INVALID_HANDLE_VALUE; /* to be sure */
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   164
		}
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   165
		SDL_FreeRW(context);
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   166
	}
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   167
	return(0);
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   168
}
1447
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   169
#endif /* __WIN32__ */
1446
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   170
1447
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   171
#ifdef HAVE_STDIO_H
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   172
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   173
/* Functions to read/write stdio file pointers */
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   174
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   175
static int stdio_seek(SDL_RWops *context, int offset, int whence)
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   176
{
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   177
	if ( fseek(context->hidden.stdio.fp, offset, whence) == 0 ) {
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   178
		return(ftell(context->hidden.stdio.fp));
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   179
	} else {
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   180
		SDL_Error(SDL_EFSEEK);
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   181
		return(-1);
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   182
	}
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   183
}
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   184
static int stdio_read(SDL_RWops *context, void *ptr, int size, int maxnum)
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   185
{
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   186
	size_t nread;
1446
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   187
1447
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   188
	nread = fread(ptr, size, maxnum, context->hidden.stdio.fp); 
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   189
	if ( nread == 0 && ferror(context->hidden.stdio.fp) ) {
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   190
		SDL_Error(SDL_EFREAD);
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   191
	}
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   192
	return(nread);
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   193
}
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   194
static int stdio_write(SDL_RWops *context, const void *ptr, int size, int num)
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   195
{
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   196
	size_t nwrote;
1446
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   197
1447
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   198
	nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   199
	if ( nwrote == 0 && ferror(context->hidden.stdio.fp) ) {
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   200
		SDL_Error(SDL_EFWRITE);
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   201
	}
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   202
	return(nwrote);
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   203
}
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   204
static int stdio_close(SDL_RWops *context)
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   205
{
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   206
	if ( context ) {
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   207
		if ( context->hidden.stdio.autoclose ) {
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   208
			/* WARNING:  Check the return value here! */
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   209
			fclose(context->hidden.stdio.fp);
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   210
		}
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   211
		SDL_FreeRW(context);
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   212
	}
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   213
	return(0);
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   214
}
1446
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   215
#endif /* !HAVE_STDIO_H */
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
   216
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   217
/* Functions to read/write memory pointers */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   218
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   219
static int mem_seek(SDL_RWops *context, int offset, int whence)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   220
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   221
	Uint8 *newpos;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   222
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   223
	switch (whence) {
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
   224
		case RW_SEEK_SET:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   225
			newpos = context->hidden.mem.base+offset;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   226
			break;
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
   227
		case RW_SEEK_CUR:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   228
			newpos = context->hidden.mem.here+offset;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   229
			break;
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
   230
		case RW_SEEK_END:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   231
			newpos = context->hidden.mem.stop+offset;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   232
			break;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   233
		default:
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   234
			SDL_SetError("Unknown value for 'whence'");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   235
			return(-1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   236
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   237
	if ( newpos < context->hidden.mem.base ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   238
		newpos = context->hidden.mem.base;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   239
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   240
	if ( newpos > context->hidden.mem.stop ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   241
		newpos = context->hidden.mem.stop;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   242
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   243
	context->hidden.mem.here = newpos;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   244
	return(context->hidden.mem.here-context->hidden.mem.base);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   245
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   246
static int mem_read(SDL_RWops *context, void *ptr, int size, int maxnum)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   247
{
1078
e2ef6b7001fd Patch from Antonio SJ Musumeci:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
   248
	int total_bytes;
e2ef6b7001fd Patch from Antonio SJ Musumeci:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
   249
	int mem_available;
e2ef6b7001fd Patch from Antonio SJ Musumeci:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
   250
e2ef6b7001fd Patch from Antonio SJ Musumeci:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
   251
	total_bytes = (maxnum * size);
e2ef6b7001fd Patch from Antonio SJ Musumeci:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
   252
	if ( (maxnum <= 0) || (size <= 0) || ((total_bytes / maxnum) != size) ) {
e2ef6b7001fd Patch from Antonio SJ Musumeci:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
   253
		return 0;
e2ef6b7001fd Patch from Antonio SJ Musumeci:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
   254
	}
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   255
1078
e2ef6b7001fd Patch from Antonio SJ Musumeci:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
   256
	mem_available = (context->hidden.mem.stop - context->hidden.mem.here);
e2ef6b7001fd Patch from Antonio SJ Musumeci:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
   257
	if (total_bytes > mem_available) {
e2ef6b7001fd Patch from Antonio SJ Musumeci:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
   258
		total_bytes = mem_available;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   259
	}
1078
e2ef6b7001fd Patch from Antonio SJ Musumeci:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
   260
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1330
diff changeset
   261
	SDL_memcpy(ptr, context->hidden.mem.here, total_bytes);
1078
e2ef6b7001fd Patch from Antonio SJ Musumeci:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
   262
	context->hidden.mem.here += total_bytes;
e2ef6b7001fd Patch from Antonio SJ Musumeci:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
   263
e2ef6b7001fd Patch from Antonio SJ Musumeci:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
   264
	return (total_bytes / size);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   265
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   266
static int mem_write(SDL_RWops *context, const void *ptr, int size, int num)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   267
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   268
	if ( (context->hidden.mem.here + (num*size)) > context->hidden.mem.stop ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   269
		num = (context->hidden.mem.stop-context->hidden.mem.here)/size;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   270
	}
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1330
diff changeset
   271
	SDL_memcpy(context->hidden.mem.here, ptr, num*size);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   272
	context->hidden.mem.here += num*size;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   273
	return(num);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   274
}
764
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   275
static int mem_writeconst(SDL_RWops *context, const void *ptr, int size, int num)
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   276
{
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   277
	SDL_SetError("Can't write to read-only memory");
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   278
	return(-1);
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   279
}
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   280
static int mem_close(SDL_RWops *context)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   281
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   282
	if ( context ) {
1202
0748fbb272e7 Matched SDL_AllowRW and SDL_FreeRW calls in SDL_rwops.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 1078
diff changeset
   283
		SDL_FreeRW(context);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   284
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   285
	return(0);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   286
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   287
1447
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   288
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   289
/* Functions to create SDL_RWops structures from various data sources */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   290
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   291
#ifdef __MACOS__
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   292
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   293
 * translate unix-style slash-separated filename to mac-style colon-separated
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   294
 * name; return malloced string
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   295
 */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   296
static char *unix_to_mac(const char *file)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   297
{
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1330
diff changeset
   298
	int flen = SDL_strlen(file);
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1330
diff changeset
   299
	char *path = SDL_malloc(flen + 2);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   300
	const char *src = file;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   301
	char *dst = path;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   302
	if(*src == '/') {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   303
		/* really depends on filesystem layout, hope for the best */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   304
		src++;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   305
	} else {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   306
		/* Check if this is a MacOS path to begin with */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   307
		if(*src != ':')
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   308
			*dst++ = ':';   /* relative paths begin with ':' */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   309
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   310
	while(src < file + flen) {
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1330
diff changeset
   311
		const char *end = SDL_strchr(src, '/');
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   312
		int len;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   313
		if(!end)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   314
			end = file + flen; /* last component */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   315
		len = end - src;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   316
		if(len == 0 || (len == 1 && src[0] == '.')) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   317
			/* remove repeated slashes and . */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   318
		} else {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   319
			if(len == 2 && src[0] == '.' && src[1] == '.') {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   320
				/* replace .. with the empty string */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   321
			} else {
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1330
diff changeset
   322
				SDL_memcpy(dst, src, len);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   323
				dst += len;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   324
			}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   325
			if(end < file + flen)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   326
				*dst++ = ':';
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   327
		}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   328
		src = end + 1;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   329
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   330
	*dst++ = '\0';
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   331
	return path;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   332
}
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   333
#endif /* __MACOS__ */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   334
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   335
SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   336
{
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
   337
	SDL_RWops *rwops = NULL;
1447
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   338
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   339
#ifdef __WIN32__
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   340
	rwops = SDL_AllocRW();
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   341
	rwops->hidden.win32io.h = INVALID_HANDLE_VALUE;
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   342
	if (win32_file_open(rwops,file,mode)) {
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   343
		SDL_FreeRW(rwops);
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   344
		return NULL;
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   345
	}	
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   346
	rwops->seek  = win32_file_seek;
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   347
	rwops->read  = win32_file_read;
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   348
	rwops->write = win32_file_write;
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   349
	rwops->close = win32_file_close;
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   350
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   351
#elif HAVE_STDIO_H
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   352
	FILE *fp;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   353
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   354
#ifdef __MACOS__
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   355
	{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   356
		char *mpath = unix_to_mac(file);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   357
		fp = fopen(mpath, mode);
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1330
diff changeset
   358
		SDL_free(mpath);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   359
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   360
#else
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   361
	fp = fopen(file, mode);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   362
#endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   363
	if ( fp == NULL ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   364
		SDL_SetError("Couldn't open %s", file);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   365
	} else {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   366
		rwops = SDL_RWFromFP(fp, 1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   367
	}
1447
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   368
#else
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   369
	SDL_SetError("SDL not compiled with stdio support");
1446
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   370
#endif /* !HAVE_STDIO_H */
1447
515df0086eb7 Prefer the raw Win32 API over stdio for file RWops
Sam Lantinga <slouken@libsdl.org>
parents: 1446
diff changeset
   371
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   372
	return(rwops);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   373
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   374
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
   375
#ifdef HAVE_STDIO_H
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   376
SDL_RWops *SDL_RWFromFP(FILE *fp, int autoclose)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   377
{
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
   378
	SDL_RWops *rwops = NULL;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   379
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   380
	rwops = SDL_AllocRW();
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   381
	if ( rwops != NULL ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   382
		rwops->seek = stdio_seek;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   383
		rwops->read = stdio_read;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   384
		rwops->write = stdio_write;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   385
		rwops->close = stdio_close;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   386
		rwops->hidden.stdio.fp = fp;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   387
		rwops->hidden.stdio.autoclose = autoclose;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   388
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   389
	return(rwops);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   390
}
1330
450721ad5436 It's now possible to build SDL without any C runtime at all on Windows,
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
   391
#endif /* HAVE_STDIO_H */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   392
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   393
SDL_RWops *SDL_RWFromMem(void *mem, int size)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   394
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   395
	SDL_RWops *rwops;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   396
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   397
	rwops = SDL_AllocRW();
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   398
	if ( rwops != NULL ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   399
		rwops->seek = mem_seek;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   400
		rwops->read = mem_read;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   401
		rwops->write = mem_write;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   402
		rwops->close = mem_close;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   403
		rwops->hidden.mem.base = (Uint8 *)mem;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   404
		rwops->hidden.mem.here = rwops->hidden.mem.base;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   405
		rwops->hidden.mem.stop = rwops->hidden.mem.base+size;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   406
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   407
	return(rwops);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   408
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   409
764
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   410
SDL_RWops *SDL_RWFromConstMem(const void *mem, int size)
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   411
{
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   412
	SDL_RWops *rwops;
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   413
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   414
	rwops = SDL_AllocRW();
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   415
	if ( rwops != NULL ) {
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   416
		rwops->seek = mem_seek;
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   417
		rwops->read = mem_read;
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   418
		rwops->write = mem_writeconst;
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   419
		rwops->close = mem_close;
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   420
		rwops->hidden.mem.base = (Uint8 *)mem;
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   421
		rwops->hidden.mem.here = rwops->hidden.mem.base;
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   422
		rwops->hidden.mem.stop = rwops->hidden.mem.base+size;
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   423
	}
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   424
	return(rwops);
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   425
}
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   426
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   427
SDL_RWops *SDL_AllocRW(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   428
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   429
	SDL_RWops *area;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   430
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1330
diff changeset
   431
	area = (SDL_RWops *)SDL_malloc(sizeof *area);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   432
	if ( area == NULL ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   433
		SDL_OutOfMemory();
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   434
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   435
	return(area);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   436
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   437
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   438
void SDL_FreeRW(SDL_RWops *area)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   439
{
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1330
diff changeset
   440
	SDL_free(area);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   441
}
1354
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   442
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   443
/* Functions for dynamically reading and writing endian-specific values */
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   444
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   445
Uint16 SDL_ReadLE16 (SDL_RWops *src)
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   446
{
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   447
	Uint16 value;
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   448
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   449
	SDL_RWread(src, &value, (sizeof value), 1);
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   450
	return(SDL_SwapLE16(value));
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   451
}
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   452
Uint16 SDL_ReadBE16 (SDL_RWops *src)
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   453
{
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   454
	Uint16 value;
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   455
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   456
	SDL_RWread(src, &value, (sizeof value), 1);
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   457
	return(SDL_SwapBE16(value));
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   458
}
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   459
Uint32 SDL_ReadLE32 (SDL_RWops *src)
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   460
{
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   461
	Uint32 value;
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   462
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   463
	SDL_RWread(src, &value, (sizeof value), 1);
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   464
	return(SDL_SwapLE32(value));
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   465
}
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   466
Uint32 SDL_ReadBE32 (SDL_RWops *src)
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   467
{
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   468
	Uint32 value;
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   469
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   470
	SDL_RWread(src, &value, (sizeof value), 1);
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   471
	return(SDL_SwapBE32(value));
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   472
}
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   473
Uint64 SDL_ReadLE64 (SDL_RWops *src)
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   474
{
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   475
	Uint64 value;
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   476
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   477
	SDL_RWread(src, &value, (sizeof value), 1);
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   478
	return(SDL_SwapLE64(value));
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   479
}
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   480
Uint64 SDL_ReadBE64 (SDL_RWops *src)
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   481
{
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   482
	Uint64 value;
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   483
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   484
	SDL_RWread(src, &value, (sizeof value), 1);
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   485
	return(SDL_SwapBE64(value));
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   486
}
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   487
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   488
int SDL_WriteLE16 (SDL_RWops *dst, Uint16 value)
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   489
{
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   490
	value = SDL_SwapLE16(value);
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   491
	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   492
}
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   493
int SDL_WriteBE16 (SDL_RWops *dst, Uint16 value)
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   494
{
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   495
	value = SDL_SwapBE16(value);
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   496
	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   497
}
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   498
int SDL_WriteLE32 (SDL_RWops *dst, Uint32 value)
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   499
{
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   500
	value = SDL_SwapLE32(value);
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   501
	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   502
}
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   503
int SDL_WriteBE32 (SDL_RWops *dst, Uint32 value)
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   504
{
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   505
	value = SDL_SwapBE32(value);
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   506
	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   507
}
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   508
int SDL_WriteLE64 (SDL_RWops *dst, Uint64 value)
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   509
{
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   510
	value = SDL_SwapLE64(value);
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   511
	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   512
}
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   513
int SDL_WriteBE64 (SDL_RWops *dst, Uint64 value)
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   514
{
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   515
	value = SDL_SwapBE64(value);
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   516
	return(SDL_RWwrite(dst, &value, (sizeof value), 1));
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   517
}