comparison playsound/physfsrwops.c @ 286:a6453cae7512

Initial add.
author Ryan C. Gordon <icculus@icculus.org>
date Sat, 16 Mar 2002 08:46:49 +0000
parents
children d8c0315deba9
comparison
equal deleted inserted replaced
285:95082495d437 286:a6453cae7512
1 /*
2 * This code provides a glue layer between PhysicsFS and Simple Directmedia
3 * Layer's (SDL) RWops i/o abstraction.
4 *
5 * License: this code is public domain. I make no warranty that it is useful,
6 * correct, harmless, or environmentally safe.
7 *
8 * This particular file may be used however you like, including copying it
9 * verbatim into a closed-source project, exploiting it commercially, and
10 * removing any trace of my name from the source (although I hope you won't
11 * do that). I welcome enhancements and corrections to this file, but I do
12 * not require you to send me patches if you make changes.
13 *
14 * Unless otherwise stated, the rest of PhysicsFS falls under the GNU Lesser
15 * General Public License: http://www.gnu.org/licenses/lgpl.txt
16 *
17 * SDL falls under the LGPL, too. You can get SDL at http://www.libsdl.org/
18 *
19 * This file was written by Ryan C. Gordon. (icculus@clutteredmind.org).
20 */
21
22 #include <stdio.h> /* used for SEEK_SET, SEEK_CUR, SEEK_END ... */
23 #include "physfsrwops.h"
24
25 static int physfsrwops_seek(SDL_RWops *rw, int offset, int whence)
26 {
27 PHYSFS_file *handle = (PHYSFS_file *) rw->hidden.unknown.data1;
28 int pos = 0;
29
30 if (whence == SEEK_SET)
31 {
32 pos = offset;
33 } /* if */
34
35 else if (whence == SEEK_CUR)
36 {
37 int current = PHYSFS_tell(handle);
38 if (current == -1)
39 {
40 SDL_SetError("Can't find position in file: %s",
41 PHYSFS_getLastError());
42 return(-1);
43 } /* if */
44
45 if (offset == 0) /* this is a "tell" call. We're done. */
46 return(offset);
47
48 pos = current + offset;
49 } /* else if */
50
51 else if (whence == SEEK_END)
52 {
53 int len = PHYSFS_fileLength(handle);
54 if (len == -1)
55 {
56 SDL_SetError("Can't find end of file: %s", PHYSFS_getLastError());
57 return(-1);
58 } /* if */
59
60 pos = len + offset;
61 } /* else if */
62
63 else
64 {
65 SDL_SetError("Invalid 'whence' parameter.");
66 return(-1);
67 } /* else */
68
69 if (!PHYSFS_seek(handle, pos))
70 {
71 SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
72 return(-1);
73 } /* if */
74
75 return(pos);
76 } /* physfsrwops_seek */
77
78
79 static int physfsrwops_read(SDL_RWops *rw, void *ptr, int size, int maxnum)
80 {
81 PHYSFS_file *handle = (PHYSFS_file *) rw->hidden.unknown.data1;
82 int rc = PHYSFS_read(handle, ptr, size, maxnum);
83 if (rc != maxnum)
84 {
85 if (!PHYSFS_eof(handle)) /* not EOF? Must be an error. */
86 SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
87 } /* if */
88
89 return(rc);
90 } /* physfsrwops_read */
91
92
93 static int physfsrwops_write(SDL_RWops *rw, const void *ptr, int size, int num)
94 {
95 PHYSFS_file *handle = (PHYSFS_file *) rw->hidden.unknown.data1;
96 int rc = PHYSFS_write(handle, ptr, size, num);
97 if (rc != num)
98 SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
99
100 return(rc);
101 } /* physfsrwops_write */
102
103
104 static int physfsrwops_close(SDL_RWops *rw)
105 {
106 PHYSFS_file *handle = (PHYSFS_file *) rw->hidden.unknown.data1;
107 if (!PHYSFS_close(handle))
108 {
109 SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
110 return(-1);
111 } /* if */
112
113 SDL_FreeRW(rw);
114 return(0);
115 } /* physfsrwops_close */
116
117
118 static SDL_RWops *create_rwops(PHYSFS_file *handle)
119 {
120 SDL_RWops *retval = NULL;
121
122 if (handle == NULL)
123 SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError());
124 else
125 {
126 retval = SDL_AllocRW();
127 if (retval != NULL)
128 {
129 retval->seek = physfsrwops_seek;
130 retval->read = physfsrwops_read;
131 retval->write = physfsrwops_write;
132 retval->close = physfsrwops_close;
133 retval->hidden.unknown.data1 = handle;
134 } /* if */
135 } /* else */
136
137 return(retval);
138 } /* create_rwops */
139
140
141 SDL_RWops *PHYSFSRWOPS_makeRWops(PHYSFS_file *handle)
142 {
143 SDL_RWops *retval = NULL;
144 if (handle == NULL)
145 SDL_SetError("NULL pointer passed to PHYSFSRWOPS_makeRWops().");
146 else
147 retval = create_rwops(handle);
148
149 return(retval);
150 } /* PHYSFSRWOPS_makeRWops */
151
152
153 SDL_RWops *PHYSFSRWOPS_openRead(const char *fname)
154 {
155 return(create_rwops(PHYSFS_openRead(fname)));
156 } /* PHYSFSRWOPS_openRead */
157
158
159 SDL_RWops *PHYSFSRWOPS_openWrite(const char *fname)
160 {
161 return(create_rwops(PHYSFS_openWrite(fname)));
162 } /* PHYSFSRWOPS_openWrite */
163
164
165 SDL_RWops *PHYSFSRWOPS_openAppend(const char *fname)
166 {
167 return(create_rwops(PHYSFS_openAppend(fname)));
168 } /* PHYSFSRWOPS_openAppend */
169
170
171 /* end of physfsrwops.c ... */
172