Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
660 lines (560 loc) · 17.3 KB

SDL_rwops.c

File metadata and controls

660 lines (560 loc) · 17.3 KB
 
Apr 26, 2001
Apr 26, 2001
1
/*
Apr 8, 2011
Apr 8, 2011
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Simple DirectMedia Layer
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Apr 26, 2001
Apr 26, 2001
20
*/
Feb 21, 2006
Feb 21, 2006
21
#include "SDL_config.h"
Apr 26, 2001
Apr 26, 2001
22
23
24
25
26
/* This file provides a general interface for SDL to read and write
data sources. It can easily be extended to files, memory, etc.
*/
Feb 9, 2006
Feb 9, 2006
27
#include "SDL_endian.h"
Feb 7, 2006
Feb 7, 2006
28
#include "SDL_rwops.h"
Feb 6, 2006
Feb 6, 2006
29
May 8, 2010
May 8, 2010
30
#ifdef __APPLE__
May 9, 2010
May 9, 2010
31
#include "cocoa/SDL_rwopsbundlesupport.h"
May 8, 2010
May 8, 2010
32
33
#endif /* __APPLE__ */
Aug 27, 2008
Aug 27, 2008
34
35
36
37
#ifdef __NDS__
/* include libfat headers for fatInitDefault(). */
#include <fat.h>
#endif /* __NDS__ */
Feb 9, 2006
Feb 9, 2006
38
Jan 24, 2011
Jan 24, 2011
39
#ifdef __WIN32__
Apr 26, 2001
Apr 26, 2001
40
Feb 27, 2006
Feb 27, 2006
41
/* Functions to read/write Win32 API file pointers */
Mar 4, 2006
Mar 4, 2006
42
43
44
/* Will not use it on WinCE because stdio is buffered, it means
faster, and all stdio functions anyway are embedded in coredll.dll -
the main wince dll*/
Apr 26, 2001
Apr 26, 2001
45
Jan 25, 2011
Jan 25, 2011
46
#include "../core/windows/SDL_windows.h"
Feb 27, 2006
Feb 27, 2006
47
Mar 4, 2006
Mar 4, 2006
48
49
50
51
#ifndef INVALID_SET_FILE_POINTER
#define INVALID_SET_FILE_POINTER 0xFFFFFFFF
#endif
Jul 10, 2007
Jul 10, 2007
52
53
#define READAHEAD_BUFFER_SIZE 1024
Jul 10, 2006
Jul 10, 2006
54
static int SDLCALL
Jan 21, 2011
Jan 21, 2011
55
windows_file_open(SDL_RWops * context, const char *filename, const char *mode)
Mar 4, 2006
Mar 4, 2006
56
57
{
#ifndef _WIN32_WCE
Jul 10, 2006
Jul 10, 2006
58
UINT old_error_mode;
Mar 4, 2006
Mar 4, 2006
59
#endif
Jul 10, 2006
Jul 10, 2006
60
61
62
63
64
65
HANDLE h;
DWORD r_right, w_right;
DWORD must_exist, truncate;
int a_mode;
if (!context)
Jul 10, 2007
Jul 10, 2007
66
return -1; /* failed (invalid call) */
Jul 10, 2007
Jul 10, 2007
67
Jan 21, 2011
Jan 21, 2011
68
69
70
71
context->hidden.windowsio.h = INVALID_HANDLE_VALUE; /* mark this as unusable */
context->hidden.windowsio.buffer.data = NULL;
context->hidden.windowsio.buffer.size = 0;
context->hidden.windowsio.buffer.left = 0;
Jul 10, 2007
Jul 10, 2007
72
Jul 10, 2006
Jul 10, 2006
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* "r" = reading, file must exist */
/* "w" = writing, truncate existing, file may not exist */
/* "r+"= reading or writing, file must exist */
/* "a" = writing, append file may not exist */
/* "a+"= append + read, file may not exist */
/* "w+" = read, write, truncate. file may not exist */
must_exist = (SDL_strchr(mode, 'r') != NULL) ? OPEN_EXISTING : 0;
truncate = (SDL_strchr(mode, 'w') != NULL) ? CREATE_ALWAYS : 0;
r_right = (SDL_strchr(mode, '+') != NULL
|| must_exist) ? GENERIC_READ : 0;
a_mode = (SDL_strchr(mode, 'a') != NULL) ? OPEN_ALWAYS : 0;
w_right = (a_mode || SDL_strchr(mode, '+')
|| truncate) ? GENERIC_WRITE : 0;
if (!r_right && !w_right) /* inconsistent mode */
return -1; /* failed (invalid call) */
Mar 4, 2006
Mar 4, 2006
90
Jan 21, 2011
Jan 21, 2011
91
context->hidden.windowsio.buffer.data =
Jul 11, 2007
Jul 11, 2007
92
(char *) SDL_malloc(READAHEAD_BUFFER_SIZE);
Jan 21, 2011
Jan 21, 2011
93
if (!context->hidden.windowsio.buffer.data) {
Jul 10, 2007
Jul 10, 2007
94
95
96
SDL_OutOfMemory();
return -1;
}
Mar 4, 2006
Mar 4, 2006
97
#ifdef _WIN32_WCE
Jul 10, 2006
Jul 10, 2006
98
{
Jan 25, 2011
Jan 25, 2011
99
100
LPTSTR tstr = WIN_UTF8ToString(filename);
h = CreateFile(tstr, (w_right | r_right),
Jul 10, 2006
Jul 10, 2006
101
102
103
(w_right) ? 0 : FILE_SHARE_READ, NULL,
(must_exist | truncate | a_mode),
FILE_ATTRIBUTE_NORMAL, NULL);
Jan 25, 2011
Jan 25, 2011
104
SDL_free(tstr);
Jul 10, 2006
Jul 10, 2006
105
}
Mar 4, 2006
Mar 4, 2006
106
#else
Jul 10, 2006
Jul 10, 2006
107
108
109
/* Do not open a dialog box if failure */
old_error_mode =
SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
Mar 4, 2006
Mar 4, 2006
110
Jan 25, 2011
Jan 25, 2011
111
112
113
114
115
116
117
118
{
LPTSTR tstr = WIN_UTF8ToString(filename);
h = CreateFile(tstr, (w_right | r_right),
(w_right) ? 0 : FILE_SHARE_READ, NULL,
(must_exist | truncate | a_mode),
FILE_ATTRIBUTE_NORMAL, NULL);
SDL_free(tstr);
}
Mar 4, 2006
Mar 4, 2006
119
Jan 25, 2011
Jan 25, 2011
120
/* restore old behavior */
Jul 10, 2006
Jul 10, 2006
121
SetErrorMode(old_error_mode);
Mar 4, 2006
Mar 4, 2006
122
#endif /* _WIN32_WCE */
Feb 27, 2006
Feb 27, 2006
123
Jul 10, 2006
Jul 10, 2006
124
if (h == INVALID_HANDLE_VALUE) {
Jan 21, 2011
Jan 21, 2011
125
126
SDL_free(context->hidden.windowsio.buffer.data);
context->hidden.windowsio.buffer.data = NULL;
Jul 10, 2006
Jul 10, 2006
127
128
129
SDL_SetError("Couldn't open %s", filename);
return -2; /* failed (CreateFile) */
}
Jan 21, 2011
Jan 21, 2011
130
131
context->hidden.windowsio.h = h;
context->hidden.windowsio.append = a_mode ? SDL_TRUE : SDL_FALSE;
Jul 10, 2006
Jul 10, 2006
132
133
return 0; /* ok */
Feb 27, 2006
Feb 27, 2006
134
}
Aug 27, 2008
Aug 27, 2008
135
Jul 10, 2007
Jul 10, 2007
136
static long SDLCALL
Jan 21, 2011
Jan 21, 2011
137
windows_file_seek(SDL_RWops * context, long offset, int whence)
Jul 10, 2006
Jul 10, 2006
138
{
Jan 21, 2011
Jan 21, 2011
139
DWORD windowswhence;
Jul 10, 2007
Jul 10, 2007
140
long file_pos;
Feb 27, 2006
Feb 27, 2006
141
Jan 21, 2011
Jan 21, 2011
142
143
if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE) {
SDL_SetError("windows_file_seek: invalid context/file not opened");
Jul 10, 2006
Jul 10, 2006
144
145
return -1;
}
Feb 27, 2006
Feb 27, 2006
146
Jul 10, 2007
Jul 10, 2007
147
/* FIXME: We may be able to satisfy the seek within buffered data */
Jan 21, 2011
Jan 21, 2011
148
149
if (whence == RW_SEEK_CUR && context->hidden.windowsio.buffer.left) {
offset -= (long)context->hidden.windowsio.buffer.left;
Jul 10, 2007
Jul 10, 2007
150
}
Jan 21, 2011
Jan 21, 2011
151
context->hidden.windowsio.buffer.left = 0;
Jul 10, 2007
Jul 10, 2007
152
Jul 10, 2006
Jul 10, 2006
153
154
switch (whence) {
case RW_SEEK_SET:
Jan 21, 2011
Jan 21, 2011
155
windowswhence = FILE_BEGIN;
Jul 10, 2006
Jul 10, 2006
156
157
break;
case RW_SEEK_CUR:
Jan 21, 2011
Jan 21, 2011
158
windowswhence = FILE_CURRENT;
Jul 10, 2006
Jul 10, 2006
159
160
break;
case RW_SEEK_END:
Jan 21, 2011
Jan 21, 2011
161
windowswhence = FILE_END;
Jul 10, 2006
Jul 10, 2006
162
163
break;
default:
Jan 21, 2011
Jan 21, 2011
164
SDL_SetError("windows_file_seek: Unknown value for 'whence'");
Jul 10, 2006
Jul 10, 2006
165
166
return -1;
}
Feb 27, 2006
Feb 27, 2006
167
Jul 10, 2006
Jul 10, 2006
168
file_pos =
Jan 21, 2011
Jan 21, 2011
169
SetFilePointer(context->hidden.windowsio.h, offset, NULL, windowswhence);
Jul 10, 2006
Jul 10, 2006
170
171
172
if (file_pos != INVALID_SET_FILE_POINTER)
return file_pos; /* success */
Feb 27, 2006
Feb 27, 2006
173
Jul 10, 2006
Jul 10, 2006
174
175
SDL_Error(SDL_EFSEEK);
return -1; /* error */
Feb 27, 2006
Feb 27, 2006
176
}
Aug 27, 2008
Aug 27, 2008
177
Jul 10, 2007
Jul 10, 2007
178
static size_t SDLCALL
Jan 21, 2011
Jan 21, 2011
179
windows_file_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
Feb 27, 2006
Feb 27, 2006
180
{
Jul 10, 2007
Jul 10, 2007
181
182
183
size_t total_need;
size_t total_read = 0;
size_t read_ahead;
Jul 10, 2007
Jul 10, 2007
184
DWORD byte_read;
Feb 27, 2006
Feb 27, 2006
185
Jul 10, 2007
Jul 10, 2007
186
total_need = size * maxnum;
Jul 10, 2006
Jul 10, 2006
187
Jan 21, 2011
Jan 21, 2011
188
if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE
Jul 10, 2007
Jul 10, 2007
189
|| !total_need)
Jul 10, 2006
Jul 10, 2006
190
191
return 0;
Jan 21, 2011
Jan 21, 2011
192
193
194
195
if (context->hidden.windowsio.buffer.left > 0) {
void *data = (char *) context->hidden.windowsio.buffer.data +
context->hidden.windowsio.buffer.size -
context->hidden.windowsio.buffer.left;
Jul 12, 2007
Jul 12, 2007
196
read_ahead =
Jan 21, 2011
Jan 21, 2011
197
SDL_min(total_need, context->hidden.windowsio.buffer.left);
Jul 10, 2007
Jul 10, 2007
198
SDL_memcpy(ptr, data, read_ahead);
Jan 21, 2011
Jan 21, 2011
199
context->hidden.windowsio.buffer.left -= read_ahead;
Jul 10, 2007
Jul 10, 2007
200
201
202
203
204
205
206
if (read_ahead == total_need) {
return maxnum;
}
ptr = (char *) ptr + read_ahead;
total_need -= read_ahead;
total_read += read_ahead;
Jul 10, 2006
Jul 10, 2006
207
}
Jul 10, 2007
Jul 10, 2007
208
209
210
if (total_need < READAHEAD_BUFFER_SIZE) {
if (!ReadFile
Jan 21, 2011
Jan 21, 2011
211
(context->hidden.windowsio.h, context->hidden.windowsio.buffer.data,
Jul 10, 2007
Jul 10, 2007
212
213
214
215
216
READAHEAD_BUFFER_SIZE, &byte_read, NULL)) {
SDL_Error(SDL_EFREAD);
return 0;
}
read_ahead = SDL_min(total_need, (int) byte_read);
Jan 21, 2011
Jan 21, 2011
217
218
219
SDL_memcpy(ptr, context->hidden.windowsio.buffer.data, read_ahead);
context->hidden.windowsio.buffer.size = byte_read;
context->hidden.windowsio.buffer.left = byte_read - read_ahead;
Jul 10, 2007
Jul 10, 2007
220
221
222
total_read += read_ahead;
} else {
if (!ReadFile
Jan 21, 2011
Jan 21, 2011
223
(context->hidden.windowsio.h, ptr, (DWORD)total_need, &byte_read, NULL)) {
Jul 10, 2007
Jul 10, 2007
224
225
226
227
228
229
SDL_Error(SDL_EFREAD);
return 0;
}
total_read += byte_read;
}
return (total_read / size);
Feb 27, 2006
Feb 27, 2006
230
}
Aug 27, 2008
Aug 27, 2008
231
Jul 10, 2007
Jul 10, 2007
232
static size_t SDLCALL
Jan 21, 2011
Jan 21, 2011
233
windows_file_write(SDL_RWops * context, const void *ptr, size_t size,
Jul 10, 2007
Jul 10, 2007
234
size_t num)
Feb 27, 2006
Feb 27, 2006
235
{
Feb 6, 2006
Feb 6, 2006
236
Jul 10, 2007
Jul 10, 2007
237
size_t total_bytes;
Sep 5, 2009
Sep 5, 2009
238
239
DWORD byte_written;
size_t nwritten;
Apr 26, 2001
Apr 26, 2001
240
Jul 10, 2006
Jul 10, 2006
241
242
total_bytes = size * num;
Jan 21, 2011
Jan 21, 2011
243
if (!context || context->hidden.windowsio.h == INVALID_HANDLE_VALUE
Jul 10, 2006
Jul 10, 2006
244
245
246
|| total_bytes <= 0 || !size)
return 0;
Jan 21, 2011
Jan 21, 2011
247
248
249
if (context->hidden.windowsio.buffer.left) {
SetFilePointer(context->hidden.windowsio.h,
-(LONG)context->hidden.windowsio.buffer.left, NULL,
Jul 10, 2007
Jul 10, 2007
250
FILE_CURRENT);
Jan 21, 2011
Jan 21, 2011
251
context->hidden.windowsio.buffer.left = 0;
Jul 10, 2007
Jul 10, 2007
252
253
}
Jul 10, 2006
Jul 10, 2006
254
/* if in append mode, we must go to the EOF before write */
Jan 21, 2011
Jan 21, 2011
255
256
if (context->hidden.windowsio.append) {
if (SetFilePointer(context->hidden.windowsio.h, 0L, NULL, FILE_END) ==
Jul 10, 2007
Jul 10, 2007
257
INVALID_SET_FILE_POINTER) {
Jul 10, 2006
Jul 10, 2006
258
259
260
261
SDL_Error(SDL_EFWRITE);
return 0;
}
}
Apr 26, 2001
Apr 26, 2001
262
Jul 10, 2006
Jul 10, 2006
263
if (!WriteFile
Jan 21, 2011
Jan 21, 2011
264
(context->hidden.windowsio.h, ptr, (DWORD)total_bytes, &byte_written, NULL)) {
Jul 10, 2006
Jul 10, 2006
265
266
267
268
269
270
SDL_Error(SDL_EFWRITE);
return 0;
}
nwritten = byte_written / size;
return nwritten;
Apr 26, 2001
Apr 26, 2001
271
}
Aug 27, 2008
Aug 27, 2008
272
Jul 10, 2006
Jul 10, 2006
273
static int SDLCALL
Jan 21, 2011
Jan 21, 2011
274
windows_file_close(SDL_RWops * context)
Apr 26, 2001
Apr 26, 2001
275
276
{
Jul 10, 2006
Jul 10, 2006
277
if (context) {
Jan 21, 2011
Jan 21, 2011
278
279
280
if (context->hidden.windowsio.h != INVALID_HANDLE_VALUE) {
CloseHandle(context->hidden.windowsio.h);
context->hidden.windowsio.h = INVALID_HANDLE_VALUE; /* to be sure */
Jul 10, 2006
Jul 10, 2006
281
}
Jan 21, 2011
Jan 21, 2011
282
283
284
if (context->hidden.windowsio.buffer.data) {
SDL_free(context->hidden.windowsio.buffer.data);
context->hidden.windowsio.buffer.data = NULL;
Jul 10, 2007
Jul 10, 2007
285
}
Jul 10, 2006
Jul 10, 2006
286
287
288
289
SDL_FreeRW(context);
}
return (0);
}
Jan 24, 2011
Jan 24, 2011
290
#endif /* __WIN32__ */
Jun 24, 2005
Jun 24, 2005
291
Jul 10, 2006
Jul 10, 2006
292
#ifdef HAVE_STDIO_H
Jun 24, 2005
Jun 24, 2005
293
Jul 10, 2006
Jul 10, 2006
294
/* Functions to read/write stdio file pointers */
Jun 24, 2005
Jun 24, 2005
295
Jul 10, 2007
Jul 10, 2007
296
297
static long SDLCALL
stdio_seek(SDL_RWops * context, long offset, int whence)
Jul 10, 2006
Jul 10, 2006
298
299
300
301
302
303
304
{
if (fseek(context->hidden.stdio.fp, offset, whence) == 0) {
return (ftell(context->hidden.stdio.fp));
} else {
SDL_Error(SDL_EFSEEK);
return (-1);
}
Apr 26, 2001
Apr 26, 2001
305
}
Aug 27, 2008
Aug 27, 2008
306
Jul 10, 2007
Jul 10, 2007
307
308
static size_t SDLCALL
stdio_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
Apr 26, 2001
Apr 26, 2001
309
{
Jul 10, 2006
Jul 10, 2006
310
311
312
313
314
315
316
size_t nread;
nread = fread(ptr, size, maxnum, context->hidden.stdio.fp);
if (nread == 0 && ferror(context->hidden.stdio.fp)) {
SDL_Error(SDL_EFREAD);
}
return (nread);
Apr 26, 2001
Apr 26, 2001
317
}
Aug 27, 2008
Aug 27, 2008
318
Jul 10, 2007
Jul 10, 2007
319
320
static size_t SDLCALL
stdio_write(SDL_RWops * context, const void *ptr, size_t size, size_t num)
Jan 4, 2004
Jan 4, 2004
321
{
Jul 10, 2006
Jul 10, 2006
322
323
324
325
326
327
328
size_t nwrote;
nwrote = fwrite(ptr, size, num, context->hidden.stdio.fp);
if (nwrote == 0 && ferror(context->hidden.stdio.fp)) {
SDL_Error(SDL_EFWRITE);
}
return (nwrote);
Jan 4, 2004
Jan 4, 2004
329
}
Aug 27, 2008
Aug 27, 2008
330
Jul 10, 2006
Jul 10, 2006
331
332
static int SDLCALL
stdio_close(SDL_RWops * context)
Apr 26, 2001
Apr 26, 2001
333
{
Jul 10, 2007
Jul 10, 2007
334
int status = 0;
Jul 10, 2006
Jul 10, 2006
335
336
337
if (context) {
if (context->hidden.stdio.autoclose) {
/* WARNING: Check the return value here! */
Jul 10, 2007
Jul 10, 2007
338
339
340
341
if (fclose(context->hidden.stdio.fp) != 0) {
SDL_Error(SDL_EFWRITE);
status = -1;
}
Jul 10, 2006
Jul 10, 2006
342
343
344
}
SDL_FreeRW(context);
}
Jul 10, 2007
Jul 10, 2007
345
return status;
Jul 10, 2006
Jul 10, 2006
346
347
348
349
350
}
#endif /* !HAVE_STDIO_H */
/* Functions to read/write memory pointers */
Jul 10, 2007
Jul 10, 2007
351
352
static long SDLCALL
mem_seek(SDL_RWops * context, long offset, int whence)
Jul 10, 2006
Jul 10, 2006
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
{
Uint8 *newpos;
switch (whence) {
case RW_SEEK_SET:
newpos = context->hidden.mem.base + offset;
break;
case RW_SEEK_CUR:
newpos = context->hidden.mem.here + offset;
break;
case RW_SEEK_END:
newpos = context->hidden.mem.stop + offset;
break;
default:
SDL_SetError("Unknown value for 'whence'");
return (-1);
}
if (newpos < context->hidden.mem.base) {
newpos = context->hidden.mem.base;
}
if (newpos > context->hidden.mem.stop) {
newpos = context->hidden.mem.stop;
}
context->hidden.mem.here = newpos;
Sep 5, 2009
Sep 5, 2009
377
return (long)(context->hidden.mem.here - context->hidden.mem.base);
Jul 10, 2006
Jul 10, 2006
378
}
Aug 27, 2008
Aug 27, 2008
379
Jul 10, 2007
Jul 10, 2007
380
381
static size_t SDLCALL
mem_read(SDL_RWops * context, void *ptr, size_t size, size_t maxnum)
Jul 10, 2006
Jul 10, 2006
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
{
size_t total_bytes;
size_t mem_available;
total_bytes = (maxnum * size);
if ((maxnum <= 0) || (size <= 0)
|| ((total_bytes / maxnum) != (size_t) size)) {
return 0;
}
mem_available = (context->hidden.mem.stop - context->hidden.mem.here);
if (total_bytes > mem_available) {
total_bytes = mem_available;
}
SDL_memcpy(ptr, context->hidden.mem.here, total_bytes);
context->hidden.mem.here += total_bytes;
return (total_bytes / size);
}
Aug 27, 2008
Aug 27, 2008
402
Jul 10, 2007
Jul 10, 2007
403
404
static size_t SDLCALL
mem_write(SDL_RWops * context, const void *ptr, size_t size, size_t num)
Jul 10, 2006
Jul 10, 2006
405
406
407
408
409
410
411
412
{
if ((context->hidden.mem.here + (num * size)) > context->hidden.mem.stop) {
num = (context->hidden.mem.stop - context->hidden.mem.here) / size;
}
SDL_memcpy(context->hidden.mem.here, ptr, num * size);
context->hidden.mem.here += num * size;
return (num);
}
Aug 27, 2008
Aug 27, 2008
413
Jul 10, 2007
Jul 10, 2007
414
415
static size_t SDLCALL
mem_writeconst(SDL_RWops * context, const void *ptr, size_t size, size_t num)
Jul 10, 2006
Jul 10, 2006
416
417
418
419
{
SDL_SetError("Can't write to read-only memory");
return (-1);
}
Aug 27, 2008
Aug 27, 2008
420
Jul 10, 2006
Jul 10, 2006
421
422
423
424
425
426
427
static int SDLCALL
mem_close(SDL_RWops * context)
{
if (context) {
SDL_FreeRW(context);
}
return (0);
Apr 26, 2001
Apr 26, 2001
428
429
}
Feb 27, 2006
Feb 27, 2006
430
Apr 26, 2001
Apr 26, 2001
431
432
/* Functions to create SDL_RWops structures from various data sources */
Jul 10, 2006
Jul 10, 2006
433
434
SDL_RWops *
SDL_RWFromFile(const char *file, const char *mode)
Apr 26, 2001
Apr 26, 2001
435
{
Jul 10, 2006
Jul 10, 2006
436
SDL_RWops *rwops = NULL;
Mar 4, 2006
Mar 4, 2006
437
#ifdef HAVE_STDIO_H
Jul 10, 2006
Jul 10, 2006
438
FILE *fp = NULL;
Mar 4, 2006
Mar 4, 2006
439
#endif
Jul 10, 2006
Jul 10, 2006
440
441
442
443
if (!file || !*file || !mode || !*mode) {
SDL_SetError("SDL_RWFromFile(): No file or no mode specified");
return NULL;
}
Jan 24, 2011
Jan 24, 2011
444
#if defined(__WIN32__)
Jul 10, 2006
Jul 10, 2006
445
446
447
rwops = SDL_AllocRW();
if (!rwops)
return NULL; /* SDL_SetError already setup by SDL_AllocRW() */
Jan 21, 2011
Jan 21, 2011
448
if (windows_file_open(rwops, file, mode) < 0) {
Jul 10, 2006
Jul 10, 2006
449
450
451
SDL_FreeRW(rwops);
return NULL;
}
Jan 21, 2011
Jan 21, 2011
452
453
454
455
rwops->seek = windows_file_seek;
rwops->read = windows_file_read;
rwops->write = windows_file_write;
rwops->close = windows_file_close;
Feb 27, 2006
Feb 27, 2006
456
457
#elif HAVE_STDIO_H
May 8, 2010
May 8, 2010
458
459
460
461
462
463
#ifdef __APPLE__
fp = SDL_OpenFPFromBundleOrFallback(file, mode);
#else
fp = fopen(file, mode);
#endif
if (fp == NULL) {
Jul 10, 2006
Jul 10, 2006
464
465
466
467
SDL_SetError("Couldn't open %s", file);
} else {
rwops = SDL_RWFromFP(fp, 1);
}
Feb 27, 2006
Feb 27, 2006
468
#else
Jul 10, 2006
Jul 10, 2006
469
SDL_SetError("SDL not compiled with stdio support");
Feb 27, 2006
Feb 27, 2006
470
#endif /* !HAVE_STDIO_H */
Feb 27, 2006
Feb 27, 2006
471
Jul 10, 2006
Jul 10, 2006
472
return (rwops);
Apr 26, 2001
Apr 26, 2001
473
474
}
Feb 6, 2006
Feb 6, 2006
475
#ifdef HAVE_STDIO_H
Jul 10, 2006
Jul 10, 2006
476
SDL_RWops *
Jul 10, 2007
Jul 10, 2007
477
SDL_RWFromFP(FILE * fp, SDL_bool autoclose)
Jul 10, 2006
Jul 10, 2006
478
479
480
{
SDL_RWops *rwops = NULL;
Aug 27, 2008
Aug 27, 2008
481
482
483
484
485
486
487
#if 0
/*#ifdef __NDS__*/
/* set it up so we can use stdio file function */
fatInitDefault();
printf("called fatInitDefault()");
#endif /* __NDS__ */
Jul 10, 2006
Jul 10, 2006
488
489
490
491
492
493
494
495
496
497
rwops = SDL_AllocRW();
if (rwops != NULL) {
rwops->seek = stdio_seek;
rwops->read = stdio_read;
rwops->write = stdio_write;
rwops->close = stdio_close;
rwops->hidden.stdio.fp = fp;
rwops->hidden.stdio.autoclose = autoclose;
}
return (rwops);
Apr 26, 2001
Apr 26, 2001
498
}
Dec 15, 2009
Dec 15, 2009
499
500
501
502
503
504
505
#else
SDL_RWops *
SDL_RWFromFP(void * fp, SDL_bool autoclose)
{
SDL_SetError("SDL not compiled with stdio support");
return NULL;
}
Feb 6, 2006
Feb 6, 2006
506
#endif /* HAVE_STDIO_H */
Apr 26, 2001
Apr 26, 2001
507
Jul 10, 2006
Jul 10, 2006
508
509
SDL_RWops *
SDL_RWFromMem(void *mem, int size)
Apr 26, 2001
Apr 26, 2001
510
{
Jul 10, 2006
Jul 10, 2006
511
SDL_RWops *rwops;
Apr 26, 2001
Apr 26, 2001
512
Jul 10, 2006
Jul 10, 2006
513
514
515
516
517
518
519
520
521
522
523
rwops = SDL_AllocRW();
if (rwops != NULL) {
rwops->seek = mem_seek;
rwops->read = mem_read;
rwops->write = mem_write;
rwops->close = mem_close;
rwops->hidden.mem.base = (Uint8 *) mem;
rwops->hidden.mem.here = rwops->hidden.mem.base;
rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
}
return (rwops);
Apr 26, 2001
Apr 26, 2001
524
525
}
Jul 10, 2006
Jul 10, 2006
526
527
SDL_RWops *
SDL_RWFromConstMem(const void *mem, int size)
Jan 4, 2004
Jan 4, 2004
528
{
Jul 10, 2006
Jul 10, 2006
529
SDL_RWops *rwops;
Jan 4, 2004
Jan 4, 2004
530
Jul 10, 2006
Jul 10, 2006
531
532
533
534
535
536
537
538
539
540
541
rwops = SDL_AllocRW();
if (rwops != NULL) {
rwops->seek = mem_seek;
rwops->read = mem_read;
rwops->write = mem_writeconst;
rwops->close = mem_close;
rwops->hidden.mem.base = (Uint8 *) mem;
rwops->hidden.mem.here = rwops->hidden.mem.base;
rwops->hidden.mem.stop = rwops->hidden.mem.base + size;
}
return (rwops);
Jan 4, 2004
Jan 4, 2004
542
543
}
Jul 10, 2006
Jul 10, 2006
544
545
SDL_RWops *
SDL_AllocRW(void)
Apr 26, 2001
Apr 26, 2001
546
{
Jul 10, 2006
Jul 10, 2006
547
SDL_RWops *area;
Apr 26, 2001
Apr 26, 2001
548
Jul 10, 2006
Jul 10, 2006
549
550
551
552
553
area = (SDL_RWops *) SDL_malloc(sizeof *area);
if (area == NULL) {
SDL_OutOfMemory();
}
return (area);
Apr 26, 2001
Apr 26, 2001
554
555
}
Jul 10, 2006
Jul 10, 2006
556
557
void
SDL_FreeRW(SDL_RWops * area)
Apr 26, 2001
Apr 26, 2001
558
{
Jul 10, 2006
Jul 10, 2006
559
SDL_free(area);
Apr 26, 2001
Apr 26, 2001
560
}
Feb 9, 2006
Feb 9, 2006
561
562
563
/* Functions for dynamically reading and writing endian-specific values */
Jul 10, 2006
Jul 10, 2006
564
565
Uint16
SDL_ReadLE16(SDL_RWops * src)
Feb 9, 2006
Feb 9, 2006
566
{
Jul 10, 2006
Jul 10, 2006
567
Uint16 value;
Feb 9, 2006
Feb 9, 2006
568
Jul 10, 2006
Jul 10, 2006
569
570
SDL_RWread(src, &value, (sizeof value), 1);
return (SDL_SwapLE16(value));
Feb 9, 2006
Feb 9, 2006
571
}
Jul 10, 2006
Jul 10, 2006
572
573
574
Uint16
SDL_ReadBE16(SDL_RWops * src)
Feb 9, 2006
Feb 9, 2006
575
{
Jul 10, 2006
Jul 10, 2006
576
Uint16 value;
Feb 9, 2006
Feb 9, 2006
577
Jul 10, 2006
Jul 10, 2006
578
579
SDL_RWread(src, &value, (sizeof value), 1);
return (SDL_SwapBE16(value));
Feb 9, 2006
Feb 9, 2006
580
}
Jul 10, 2006
Jul 10, 2006
581
582
583
Uint32
SDL_ReadLE32(SDL_RWops * src)
Feb 9, 2006
Feb 9, 2006
584
{
Jul 10, 2006
Jul 10, 2006
585
Uint32 value;
Feb 9, 2006
Feb 9, 2006
586
Jul 10, 2006
Jul 10, 2006
587
588
SDL_RWread(src, &value, (sizeof value), 1);
return (SDL_SwapLE32(value));
Feb 9, 2006
Feb 9, 2006
589
}
Jul 10, 2006
Jul 10, 2006
590
591
592
Uint32
SDL_ReadBE32(SDL_RWops * src)
Feb 9, 2006
Feb 9, 2006
593
{
Jul 10, 2006
Jul 10, 2006
594
Uint32 value;
Feb 9, 2006
Feb 9, 2006
595
Jul 10, 2006
Jul 10, 2006
596
597
SDL_RWread(src, &value, (sizeof value), 1);
return (SDL_SwapBE32(value));
Feb 9, 2006
Feb 9, 2006
598
}
Jul 10, 2006
Jul 10, 2006
599
600
601
Uint64
SDL_ReadLE64(SDL_RWops * src)
Feb 9, 2006
Feb 9, 2006
602
{
Jul 10, 2006
Jul 10, 2006
603
Uint64 value;
Feb 9, 2006
Feb 9, 2006
604
Jul 10, 2006
Jul 10, 2006
605
606
SDL_RWread(src, &value, (sizeof value), 1);
return (SDL_SwapLE64(value));
Feb 9, 2006
Feb 9, 2006
607
}
Jul 10, 2006
Jul 10, 2006
608
609
610
Uint64
SDL_ReadBE64(SDL_RWops * src)
Feb 9, 2006
Feb 9, 2006
611
{
Jul 10, 2006
Jul 10, 2006
612
Uint64 value;
Feb 9, 2006
Feb 9, 2006
613
Jul 10, 2006
Jul 10, 2006
614
615
SDL_RWread(src, &value, (sizeof value), 1);
return (SDL_SwapBE64(value));
Feb 9, 2006
Feb 9, 2006
616
617
}
Sep 5, 2009
Sep 5, 2009
618
size_t
Jul 10, 2006
Jul 10, 2006
619
SDL_WriteLE16(SDL_RWops * dst, Uint16 value)
Feb 9, 2006
Feb 9, 2006
620
{
Jul 10, 2006
Jul 10, 2006
621
622
value = SDL_SwapLE16(value);
return (SDL_RWwrite(dst, &value, (sizeof value), 1));
Feb 9, 2006
Feb 9, 2006
623
}
Jul 10, 2006
Jul 10, 2006
624
Sep 5, 2009
Sep 5, 2009
625
size_t
Jul 10, 2006
Jul 10, 2006
626
SDL_WriteBE16(SDL_RWops * dst, Uint16 value)
Feb 9, 2006
Feb 9, 2006
627
{
Jul 10, 2006
Jul 10, 2006
628
629
value = SDL_SwapBE16(value);
return (SDL_RWwrite(dst, &value, (sizeof value), 1));
Feb 9, 2006
Feb 9, 2006
630
}
Jul 10, 2006
Jul 10, 2006
631
Sep 5, 2009
Sep 5, 2009
632
size_t
Jul 10, 2006
Jul 10, 2006
633
SDL_WriteLE32(SDL_RWops * dst, Uint32 value)
Feb 9, 2006
Feb 9, 2006
634
{
Jul 10, 2006
Jul 10, 2006
635
636
value = SDL_SwapLE32(value);
return (SDL_RWwrite(dst, &value, (sizeof value), 1));
Feb 9, 2006
Feb 9, 2006
637
}
Jul 10, 2006
Jul 10, 2006
638
Sep 5, 2009
Sep 5, 2009
639
size_t
Jul 10, 2006
Jul 10, 2006
640
SDL_WriteBE32(SDL_RWops * dst, Uint32 value)
Feb 9, 2006
Feb 9, 2006
641
{
Jul 10, 2006
Jul 10, 2006
642
643
value = SDL_SwapBE32(value);
return (SDL_RWwrite(dst, &value, (sizeof value), 1));
Feb 9, 2006
Feb 9, 2006
644
}
Jul 10, 2006
Jul 10, 2006
645
Sep 5, 2009
Sep 5, 2009
646
size_t
Jul 10, 2006
Jul 10, 2006
647
SDL_WriteLE64(SDL_RWops * dst, Uint64 value)
Feb 9, 2006
Feb 9, 2006
648
{
Jul 10, 2006
Jul 10, 2006
649
650
value = SDL_SwapLE64(value);
return (SDL_RWwrite(dst, &value, (sizeof value), 1));
Feb 9, 2006
Feb 9, 2006
651
}
Jul 10, 2006
Jul 10, 2006
652
Sep 5, 2009
Sep 5, 2009
653
size_t
Jul 10, 2006
Jul 10, 2006
654
SDL_WriteBE64(SDL_RWops * dst, Uint64 value)
Feb 9, 2006
Feb 9, 2006
655
{
Jul 10, 2006
Jul 10, 2006
656
657
value = SDL_SwapBE64(value);
return (SDL_RWwrite(dst, &value, (sizeof value), 1));
Feb 9, 2006
Feb 9, 2006
658
}
Jul 10, 2006
Jul 10, 2006
659
660
/* vi: set ts=4 sw=4 expandtab: */