src/file/SDL_rwops.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 27 Feb 2006 03:48:48 +0000
changeset 1446 47bf1767c4ca
parent 1402 d910939febfa
child 1447 515df0086eb7
permissions -rw-r--r--
Date: Mon, 27 Feb 2006 02:17:29 +0100 From: "William Petiot [exoide]" Subject: Re: [SDL] cvs version: questions regarding HAVE_STDIO_H in standard w I managed to get a first implementation of it, which I tested for "simple" use with tests sample apps, they work ok. I think this needs more tests. Here is the diff against CVS/SDL12, attached.
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
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
    32
#ifdef HAVE_STDIO_H
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    33
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    34
/* Functions to read/write stdio file pointers */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    35
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    36
static int stdio_seek(SDL_RWops *context, int offset, int whence)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    37
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    38
	if ( fseek(context->hidden.stdio.fp, offset, whence) == 0 ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    39
		return(ftell(context->hidden.stdio.fp));
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    40
	} else {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    41
		SDL_Error(SDL_EFSEEK);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    42
		return(-1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    43
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    44
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    45
static int stdio_read(SDL_RWops *context, void *ptr, int size, int maxnum)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    46
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    47
	size_t nread;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    48
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    49
	nread = fread(ptr, size, maxnum, context->hidden.stdio.fp); 
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    50
	if ( nread == 0 && ferror(context->hidden.stdio.fp) ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    51
		SDL_Error(SDL_EFREAD);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    52
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    53
	return(nread);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    54
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    55
static int stdio_write(SDL_RWops *context, const void *ptr, int size, int num)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    56
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    57
	size_t nwrote;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    58
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    59
	nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    60
	if ( nwrote == 0 && ferror(context->hidden.stdio.fp) ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    61
		SDL_Error(SDL_EFWRITE);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    62
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    63
	return(nwrote);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    64
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    65
static int stdio_close(SDL_RWops *context)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    66
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    67
	if ( context ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    68
		if ( context->hidden.stdio.autoclose ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    69
			/* WARNING:  Check the return value here! */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    70
			fclose(context->hidden.stdio.fp);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    71
		}
1202
0748fbb272e7 Matched SDL_AllowRW and SDL_FreeRW calls in SDL_rwops.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 1078
diff changeset
    72
		SDL_FreeRW(context);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    73
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    74
	return(0);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    75
}
1446
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    76
#else /* HAVE_STDIO_H */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    77
1446
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    78
#ifdef __WIN32__
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    79
#define WINDOWS_LEAN_AND_MEAN
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    80
#include <windows.h>
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    81
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    82
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
    83
	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    84
	UINT	old_error_mode;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    85
	HANDLE	h;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    86
	DWORD	r_right, w_right;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    87
	DWORD	must_exist, truncate;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    88
	int		a_mode;
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 || !filename || !mode)
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    91
		return -1;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    92
		
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    93
	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
    94
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    95
	/* "r" = reading, file must exist */
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
    96
	/* "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
    97
	/* "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
    98
	/* "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
    99
	/* "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
   100
	/* "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
   101
	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   102
	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
   103
	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
   104
	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
   105
	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
   106
	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
   107
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   108
	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
   109
		return -1; /* failed (invalid call)*/
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   110
	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   111
	/* 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
   112
	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
   113
	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   114
	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
   115
		           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
   116
	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   117
	/* restore old behaviour */
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   118
	SetErrorMode(old_error_mode);
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
	if (h==INVALID_HANDLE_VALUE) {
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   121
		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
   122
		return -2; /* failed (CreateFile) */
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   123
	}
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   124
	context->hidden.win32io.h = h;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   125
	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
   126
	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   127
	return 0; /* ok */
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
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   130
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
   131
	DWORD win32whence;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   132
	int   file_pos;
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
	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
   135
		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
   136
		return -1;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   137
	}
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
	switch (whence) {
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   140
		case RW_SEEK_SET:		
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   141
			win32whence = FILE_BEGIN; break;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   142
		case RW_SEEK_CUR:
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   143
			win32whence = FILE_CURRENT; break;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   144
		case RW_SEEK_END:
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   145
			win32whence = FILE_END; break;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   146
		default:
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   147
			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
   148
			return -1;
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
	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   151
	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
   152
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   153
	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
   154
		return file_pos; /* success */
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   155
	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   156
	SDL_Error(SDL_EFSEEK);
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   157
	return -1; /* error */
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   158
}
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
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
   161
	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   162
	int		total_bytes; 
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   163
	DWORD	byte_read,nread;
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
	total_bytes = size*maxnum;
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
	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
   168
		return 0;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   169
	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   170
	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
   171
		SDL_Error(SDL_EFREAD);
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   172
		return 0;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   173
	}
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   174
	nread = byte_read/size;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   175
	return nread;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   176
}
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   177
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   178
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
   179
	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   180
	int		total_bytes; 
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   181
	DWORD	byte_written,nwritten;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   182
	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   183
	total_bytes = size*num;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   184
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   185
	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
   186
		return 0;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   187
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   188
	/* 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
   189
	if (context->hidden.win32io.append) {
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   190
		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
   191
			SDL_Error(SDL_EFWRITE);
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   192
			return 0;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   193
		}
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   194
	}
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   195
	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   196
	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
   197
		SDL_Error(SDL_EFWRITE);
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   198
		return 0;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   199
	}
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   200
	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   201
	nwritten = byte_written/size;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   202
	return nwritten;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   203
}
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   204
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   205
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
   206
	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   207
	if ( context ) {								
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   208
		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
   209
			CloseHandle(context->hidden.win32io.h);
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   210
			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
   211
		}
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   212
		SDL_FreeRW(context);
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   213
	}
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   214
	return(0);
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   215
}
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   216
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   217
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   218
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   219
#endif /* __WIN32__ */
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   220
#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
   221
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   222
/* Functions to read/write memory pointers */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   223
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   224
static int mem_seek(SDL_RWops *context, int offset, int whence)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   225
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   226
	Uint8 *newpos;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   227
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   228
	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
   229
		case RW_SEEK_SET:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   230
			newpos = context->hidden.mem.base+offset;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   231
			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
   232
		case RW_SEEK_CUR:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   233
			newpos = context->hidden.mem.here+offset;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   234
			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
   235
		case RW_SEEK_END:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   236
			newpos = context->hidden.mem.stop+offset;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   237
			break;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   238
		default:
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   239
			SDL_SetError("Unknown value for 'whence'");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   240
			return(-1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   241
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   242
	if ( newpos < context->hidden.mem.base ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   243
		newpos = context->hidden.mem.base;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   244
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   245
	if ( newpos > context->hidden.mem.stop ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   246
		newpos = context->hidden.mem.stop;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   247
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   248
	context->hidden.mem.here = newpos;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   249
	return(context->hidden.mem.here-context->hidden.mem.base);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   250
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   251
static int mem_read(SDL_RWops *context, void *ptr, int size, int maxnum)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   252
{
1078
e2ef6b7001fd Patch from Antonio SJ Musumeci:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
   253
	int total_bytes;
e2ef6b7001fd Patch from Antonio SJ Musumeci:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
   254
	int mem_available;
e2ef6b7001fd Patch from Antonio SJ Musumeci:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
   255
e2ef6b7001fd Patch from Antonio SJ Musumeci:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
   256
	total_bytes = (maxnum * size);
e2ef6b7001fd Patch from Antonio SJ Musumeci:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
   257
	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
   258
		return 0;
e2ef6b7001fd Patch from Antonio SJ Musumeci:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
   259
	}
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   260
1078
e2ef6b7001fd Patch from Antonio SJ Musumeci:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
   261
	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
   262
	if (total_bytes > mem_available) {
e2ef6b7001fd Patch from Antonio SJ Musumeci:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
   263
		total_bytes = mem_available;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   264
	}
1078
e2ef6b7001fd Patch from Antonio SJ Musumeci:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
   265
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1330
diff changeset
   266
	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
   267
	context->hidden.mem.here += total_bytes;
e2ef6b7001fd Patch from Antonio SJ Musumeci:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
   268
e2ef6b7001fd Patch from Antonio SJ Musumeci:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
   269
	return (total_bytes / size);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   270
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   271
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
   272
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   273
	if ( (context->hidden.mem.here + (num*size)) > context->hidden.mem.stop ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   274
		num = (context->hidden.mem.stop-context->hidden.mem.here)/size;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   275
	}
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1330
diff changeset
   276
	SDL_memcpy(context->hidden.mem.here, ptr, num*size);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   277
	context->hidden.mem.here += num*size;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   278
	return(num);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   279
}
764
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   280
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
   281
{
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   282
	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
   283
	return(-1);
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   284
}
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   285
static int mem_close(SDL_RWops *context)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   286
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   287
	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
   288
		SDL_FreeRW(context);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   289
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   290
	return(0);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   291
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   292
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   293
/* Functions to create SDL_RWops structures from various data sources */
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   294
#ifdef __WIN32__
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   295
/* Aggh.  You can't (apparently) open a file in an application and
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   296
   read from it in a DLL.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   297
*/
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   298
static int in_sdl = 0;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   299
#endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   300
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   301
#ifdef __MACOS__
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   302
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   303
 * translate unix-style slash-separated filename to mac-style colon-separated
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   304
 * name; return malloced string
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   305
 */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   306
static char *unix_to_mac(const char *file)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   307
{
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1330
diff changeset
   308
	int flen = SDL_strlen(file);
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1330
diff changeset
   309
	char *path = SDL_malloc(flen + 2);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   310
	const char *src = file;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   311
	char *dst = path;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   312
	if(*src == '/') {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   313
		/* really depends on filesystem layout, hope for the best */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   314
		src++;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   315
	} else {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   316
		/* Check if this is a MacOS path to begin with */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   317
		if(*src != ':')
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   318
			*dst++ = ':';   /* relative paths begin with ':' */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   319
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   320
	while(src < file + flen) {
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1330
diff changeset
   321
		const char *end = SDL_strchr(src, '/');
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   322
		int len;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   323
		if(!end)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   324
			end = file + flen; /* last component */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   325
		len = end - src;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   326
		if(len == 0 || (len == 1 && src[0] == '.')) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   327
			/* remove repeated slashes and . */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   328
		} else {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   329
			if(len == 2 && src[0] == '.' && src[1] == '.') {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   330
				/* replace .. with the empty string */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   331
			} else {
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1330
diff changeset
   332
				SDL_memcpy(dst, src, len);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   333
				dst += len;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   334
			}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   335
			if(end < file + flen)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   336
				*dst++ = ':';
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   337
		}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   338
		src = end + 1;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   339
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   340
	*dst++ = '\0';
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   341
	return path;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   342
}
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   343
#endif /* __MACOS__ */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   344
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   345
SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   346
{
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
   347
	SDL_RWops *rwops = NULL;
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
   348
#ifdef HAVE_STDIO_H
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   349
	FILE *fp;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   350
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   351
#ifdef __MACOS__
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   352
	{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   353
		char *mpath = unix_to_mac(file);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   354
		fp = fopen(mpath, mode);
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1330
diff changeset
   355
		SDL_free(mpath);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   356
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   357
#else
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   358
	fp = fopen(file, mode);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   359
#endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   360
	if ( fp == NULL ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   361
		SDL_SetError("Couldn't open %s", file);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   362
	} else {
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   363
#ifdef __WIN32__
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   364
		in_sdl = 1;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   365
		rwops = SDL_RWFromFP(fp, 1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   366
		in_sdl = 0;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   367
#else
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   368
		rwops = SDL_RWFromFP(fp, 1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   369
#endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   370
	}
1446
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   371
#else  /* HAVE_STDIO_H */
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   372
#ifdef __WIN32__
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   373
	rwops = SDL_AllocRW();
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   374
	rwops->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
   375
	if (win32_file_open(rwops,file,mode)) {
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   376
		SDL_FreeRW(rwops);
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   377
		return NULL;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   378
	}	
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   379
	rwops->seek  = win32_file_seek;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   380
	rwops->read  = win32_file_read;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   381
	rwops->write = win32_file_write;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   382
	rwops->close = win32_file_close;
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   383
#endif /* __WIN32__ */
47bf1767c4ca Date: Mon, 27 Feb 2006 02:17:29 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
   384
#endif /* !HAVE_STDIO_H */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   385
	return(rwops);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   386
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   387
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
   388
#ifdef HAVE_STDIO_H
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   389
SDL_RWops *SDL_RWFromFP(FILE *fp, int autoclose)
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
	SDL_RWops *rwops = NULL;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   392
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   393
#ifdef __WIN32__
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   394
	if ( ! in_sdl ) {
1269
905d5b482f2a Some explanation on why SDL_RWFromFP doesn't always work on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 1202
diff changeset
   395
		/* It's when SDL and the app are compiled with different C runtimes */
543
522e5202014d *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   396
		SDL_SetError("You can't pass a FILE pointer to a DLL (?)");
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   397
		/*return(NULL);*/
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   398
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   399
#endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   400
	rwops = SDL_AllocRW();
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   401
	if ( rwops != NULL ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   402
		rwops->seek = stdio_seek;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   403
		rwops->read = stdio_read;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   404
		rwops->write = stdio_write;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   405
		rwops->close = stdio_close;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   406
		rwops->hidden.stdio.fp = fp;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   407
		rwops->hidden.stdio.autoclose = autoclose;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   408
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   409
	return(rwops);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   410
}
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
   411
#endif /* HAVE_STDIO_H */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   412
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   413
SDL_RWops *SDL_RWFromMem(void *mem, int size)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   414
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   415
	SDL_RWops *rwops;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   416
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   417
	rwops = SDL_AllocRW();
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   418
	if ( rwops != NULL ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   419
		rwops->seek = mem_seek;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   420
		rwops->read = mem_read;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   421
		rwops->write = mem_write;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   422
		rwops->close = mem_close;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   423
		rwops->hidden.mem.base = (Uint8 *)mem;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   424
		rwops->hidden.mem.here = rwops->hidden.mem.base;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   425
		rwops->hidden.mem.stop = rwops->hidden.mem.base+size;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   426
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   427
	return(rwops);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   428
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   429
764
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   430
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
   431
{
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   432
	SDL_RWops *rwops;
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   433
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   434
	rwops = SDL_AllocRW();
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   435
	if ( rwops != NULL ) {
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   436
		rwops->seek = mem_seek;
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   437
		rwops->read = mem_read;
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   438
		rwops->write = mem_writeconst;
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   439
		rwops->close = mem_close;
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   440
		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
   441
		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
   442
		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
   443
	}
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   444
	return(rwops);
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   445
}
974c0fb74bf8 Added function to create RWops from const memory: SDL_RWFromConstMem()
Sam Lantinga <slouken@libsdl.org>
parents: 543
diff changeset
   446
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   447
SDL_RWops *SDL_AllocRW(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   448
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   449
	SDL_RWops *area;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   450
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1330
diff changeset
   451
	area = (SDL_RWops *)SDL_malloc(sizeof *area);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   452
	if ( area == NULL ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   453
		SDL_OutOfMemory();
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   454
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   455
	return(area);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   456
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   457
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   458
void SDL_FreeRW(SDL_RWops *area)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   459
{
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1330
diff changeset
   460
	SDL_free(area);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   461
}
1354
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
/* 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
   464
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   465
Uint16 SDL_ReadLE16 (SDL_RWops *src)
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   466
{
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   467
	Uint16 value;
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   468
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   469
	SDL_RWread(src, &value, (sizeof value), 1);
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   470
	return(SDL_SwapLE16(value));
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   471
}
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   472
Uint16 SDL_ReadBE16 (SDL_RWops *src)
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   473
{
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   474
	Uint16 value;
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   475
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   476
	SDL_RWread(src, &value, (sizeof value), 1);
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   477
	return(SDL_SwapBE16(value));
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   478
}
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   479
Uint32 SDL_ReadLE32 (SDL_RWops *src)
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   480
{
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   481
	Uint32 value;
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   482
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   483
	SDL_RWread(src, &value, (sizeof value), 1);
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   484
	return(SDL_SwapLE32(value));
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   485
}
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   486
Uint32 SDL_ReadBE32 (SDL_RWops *src)
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
	Uint32 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
	SDL_RWread(src, &value, (sizeof value), 1);
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   491
	return(SDL_SwapBE32(value));
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
Uint64 SDL_ReadLE64 (SDL_RWops *src)
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
	Uint64 value;
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   496
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   497
	SDL_RWread(src, &value, (sizeof value), 1);
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   498
	return(SDL_SwapLE64(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
Uint64 SDL_ReadBE64 (SDL_RWops *src)
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   501
{
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   502
	Uint64 value;
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   503
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   504
	SDL_RWread(src, &value, (sizeof value), 1);
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   505
	return(SDL_SwapBE64(value));
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   506
}
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_WriteLE16 (SDL_RWops *dst, Uint16 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_SwapLE16(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_WriteBE16 (SDL_RWops *dst, Uint16 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_SwapBE16(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
}
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   518
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
   519
{
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   520
	value = SDL_SwapLE32(value);
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   521
	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
   522
}
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   523
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
   524
{
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   525
	value = SDL_SwapBE32(value);
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   526
	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
   527
}
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   528
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
   529
{
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   530
	value = SDL_SwapLE64(value);
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   531
	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
   532
}
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   533
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
   534
{
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   535
	value = SDL_SwapBE64(value);
22f39393668a Fixed build problem with SDL_string.c
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
   536
	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
   537
}