Mercurial > sdl-ios-xcode
annotate src/file/SDL_rwops.c @ 1446:47bf1767c4ca
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.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Mon, 27 Feb 2006 03:48:48 +0000 |
parents | d910939febfa |
children | 515df0086eb7 |
rev | line source |
---|---|
0 | 1 /* |
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 | 4 |
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 | 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 | 9 |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
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 | 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 | 18 |
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 | 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 | 23 |
24 /* This file provides a general interface for SDL to read and write | |
25 data sources. It can easily be extended to files, memory, etc. | |
26 */ | |
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 | 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 | 33 |
34 /* Functions to read/write stdio file pointers */ | |
35 | |
36 static int stdio_seek(SDL_RWops *context, int offset, int whence) | |
37 { | |
38 if ( fseek(context->hidden.stdio.fp, offset, whence) == 0 ) { | |
39 return(ftell(context->hidden.stdio.fp)); | |
40 } else { | |
41 SDL_Error(SDL_EFSEEK); | |
42 return(-1); | |
43 } | |
44 } | |
45 static int stdio_read(SDL_RWops *context, void *ptr, int size, int maxnum) | |
46 { | |
47 size_t nread; | |
48 | |
49 nread = fread(ptr, size, maxnum, context->hidden.stdio.fp); | |
50 if ( nread == 0 && ferror(context->hidden.stdio.fp) ) { | |
51 SDL_Error(SDL_EFREAD); | |
52 } | |
53 return(nread); | |
54 } | |
55 static int stdio_write(SDL_RWops *context, const void *ptr, int size, int num) | |
56 { | |
57 size_t nwrote; | |
58 | |
59 nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp); | |
60 if ( nwrote == 0 && ferror(context->hidden.stdio.fp) ) { | |
61 SDL_Error(SDL_EFWRITE); | |
62 } | |
63 return(nwrote); | |
64 } | |
65 static int stdio_close(SDL_RWops *context) | |
66 { | |
67 if ( context ) { | |
68 if ( context->hidden.stdio.autoclose ) { | |
69 /* WARNING: Check the return value here! */ | |
70 fclose(context->hidden.stdio.fp); | |
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 | 73 } |
74 return(0); | |
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 | 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 | 222 /* Functions to read/write memory pointers */ |
223 | |
224 static int mem_seek(SDL_RWops *context, int offset, int whence) | |
225 { | |
226 Uint8 *newpos; | |
227 | |
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 | 230 newpos = context->hidden.mem.base+offset; |
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 | 233 newpos = context->hidden.mem.here+offset; |
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 | 236 newpos = context->hidden.mem.stop+offset; |
237 break; | |
238 default: | |
239 SDL_SetError("Unknown value for 'whence'"); | |
240 return(-1); | |
241 } | |
242 if ( newpos < context->hidden.mem.base ) { | |
243 newpos = context->hidden.mem.base; | |
244 } | |
245 if ( newpos > context->hidden.mem.stop ) { | |
246 newpos = context->hidden.mem.stop; | |
247 } | |
248 context->hidden.mem.here = newpos; | |
249 return(context->hidden.mem.here-context->hidden.mem.base); | |
250 } | |
251 static int mem_read(SDL_RWops *context, void *ptr, int size, int maxnum) | |
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 | 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 | 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 | 270 } |
271 static int mem_write(SDL_RWops *context, const void *ptr, int size, int num) | |
272 { | |
273 if ( (context->hidden.mem.here + (num*size)) > context->hidden.mem.stop ) { | |
274 num = (context->hidden.mem.stop-context->hidden.mem.here)/size; | |
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 | 277 context->hidden.mem.here += num*size; |
278 return(num); | |
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 | 285 static int mem_close(SDL_RWops *context) |
286 { | |
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 | 289 } |
290 return(0); | |
291 } | |
292 | |
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 | 295 /* Aggh. You can't (apparently) open a file in an application and |
296 read from it in a DLL. | |
297 */ | |
298 static int in_sdl = 0; | |
299 #endif | |
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 | 302 /* |
303 * translate unix-style slash-separated filename to mac-style colon-separated | |
304 * name; return malloced string | |
305 */ | |
306 static char *unix_to_mac(const char *file) | |
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 | 310 const char *src = file; |
311 char *dst = path; | |
312 if(*src == '/') { | |
313 /* really depends on filesystem layout, hope for the best */ | |
314 src++; | |
315 } else { | |
316 /* Check if this is a MacOS path to begin with */ | |
317 if(*src != ':') | |
318 *dst++ = ':'; /* relative paths begin with ':' */ | |
319 } | |
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 | 322 int len; |
323 if(!end) | |
324 end = file + flen; /* last component */ | |
325 len = end - src; | |
326 if(len == 0 || (len == 1 && src[0] == '.')) { | |
327 /* remove repeated slashes and . */ | |
328 } else { | |
329 if(len == 2 && src[0] == '.' && src[1] == '.') { | |
330 /* replace .. with the empty string */ | |
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 | 333 dst += len; |
334 } | |
335 if(end < file + flen) | |
336 *dst++ = ':'; | |
337 } | |
338 src = end + 1; | |
339 } | |
340 *dst++ = '\0'; | |
341 return path; | |
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 | 344 |
345 SDL_RWops *SDL_RWFromFile(const char *file, const char *mode) | |
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 | 349 FILE *fp; |
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 | 352 { |
353 char *mpath = unix_to_mac(file); | |
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 | 356 } |
357 #else | |
358 fp = fopen(file, mode); | |
359 #endif | |
360 if ( fp == NULL ) { | |
361 SDL_SetError("Couldn't open %s", file); | |
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 | 364 in_sdl = 1; |
365 rwops = SDL_RWFromFP(fp, 1); | |
366 in_sdl = 0; | |
367 #else | |
368 rwops = SDL_RWFromFP(fp, 1); | |
369 #endif | |
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 | 385 return(rwops); |
386 } | |
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 | 389 SDL_RWops *SDL_RWFromFP(FILE *fp, int autoclose) |
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 | 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 | 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 | 397 /*return(NULL);*/ |
398 } | |
399 #endif | |
400 rwops = SDL_AllocRW(); | |
401 if ( rwops != NULL ) { | |
402 rwops->seek = stdio_seek; | |
403 rwops->read = stdio_read; | |
404 rwops->write = stdio_write; | |
405 rwops->close = stdio_close; | |
406 rwops->hidden.stdio.fp = fp; | |
407 rwops->hidden.stdio.autoclose = autoclose; | |
408 } | |
409 return(rwops); | |
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 | 412 |
413 SDL_RWops *SDL_RWFromMem(void *mem, int size) | |
414 { | |
415 SDL_RWops *rwops; | |
416 | |
417 rwops = SDL_AllocRW(); | |
418 if ( rwops != NULL ) { | |
419 rwops->seek = mem_seek; | |
420 rwops->read = mem_read; | |
421 rwops->write = mem_write; | |
422 rwops->close = mem_close; | |
423 rwops->hidden.mem.base = (Uint8 *)mem; | |
424 rwops->hidden.mem.here = rwops->hidden.mem.base; | |
425 rwops->hidden.mem.stop = rwops->hidden.mem.base+size; | |
426 } | |
427 return(rwops); | |
428 } | |
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 | 447 SDL_RWops *SDL_AllocRW(void) |
448 { | |
449 SDL_RWops *area; | |
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 | 452 if ( area == NULL ) { |
453 SDL_OutOfMemory(); | |
454 } | |
455 return(area); | |
456 } | |
457 | |
458 void SDL_FreeRW(SDL_RWops *area) | |
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 | 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 } |