Mercurial > SDL_sound_CoreAudio
annotate playsound/physfsrwops.c @ 503:a0297d442b6a
From stable branch: fixed up timidity and mpglib Makefile.am files.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Tue, 06 Dec 2005 12:17:39 +0000 |
parents | 35dfa9d9782e |
children | 50bb9a6cebfe |
rev | line source |
---|---|
286 | 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 | |
479
35dfa9d9782e
Fixed automake nonsense.
Ryan C. Gordon <icculus@icculus.org>
parents:
296
diff
changeset
|
22 #if SUPPORT_PHYSFS |
35dfa9d9782e
Fixed automake nonsense.
Ryan C. Gordon <icculus@icculus.org>
parents:
296
diff
changeset
|
23 |
286 | 24 #include <stdio.h> /* used for SEEK_SET, SEEK_CUR, SEEK_END ... */ |
25 #include "physfsrwops.h" | |
26 | |
27 static int physfsrwops_seek(SDL_RWops *rw, int offset, int whence) | |
28 { | |
29 PHYSFS_file *handle = (PHYSFS_file *) rw->hidden.unknown.data1; | |
30 int pos = 0; | |
31 | |
32 if (whence == SEEK_SET) | |
33 { | |
34 pos = offset; | |
35 } /* if */ | |
36 | |
37 else if (whence == SEEK_CUR) | |
38 { | |
296
d8c0315deba9
Updated to fix bugs and deal with API breakage in PhysicsFS.
Ryan C. Gordon <icculus@icculus.org>
parents:
286
diff
changeset
|
39 PHYSFS_sint64 current = PHYSFS_tell(handle); |
286 | 40 if (current == -1) |
41 { | |
42 SDL_SetError("Can't find position in file: %s", | |
43 PHYSFS_getLastError()); | |
44 return(-1); | |
45 } /* if */ | |
46 | |
296
d8c0315deba9
Updated to fix bugs and deal with API breakage in PhysicsFS.
Ryan C. Gordon <icculus@icculus.org>
parents:
286
diff
changeset
|
47 pos = (int) current; |
d8c0315deba9
Updated to fix bugs and deal with API breakage in PhysicsFS.
Ryan C. Gordon <icculus@icculus.org>
parents:
286
diff
changeset
|
48 if ( ((PHYSFS_sint64) pos) != current ) |
d8c0315deba9
Updated to fix bugs and deal with API breakage in PhysicsFS.
Ryan C. Gordon <icculus@icculus.org>
parents:
286
diff
changeset
|
49 { |
d8c0315deba9
Updated to fix bugs and deal with API breakage in PhysicsFS.
Ryan C. Gordon <icculus@icculus.org>
parents:
286
diff
changeset
|
50 SDL_SetError("Can't fit current file position in an int!"); |
d8c0315deba9
Updated to fix bugs and deal with API breakage in PhysicsFS.
Ryan C. Gordon <icculus@icculus.org>
parents:
286
diff
changeset
|
51 return(-1); |
d8c0315deba9
Updated to fix bugs and deal with API breakage in PhysicsFS.
Ryan C. Gordon <icculus@icculus.org>
parents:
286
diff
changeset
|
52 } /* if */ |
d8c0315deba9
Updated to fix bugs and deal with API breakage in PhysicsFS.
Ryan C. Gordon <icculus@icculus.org>
parents:
286
diff
changeset
|
53 |
286 | 54 if (offset == 0) /* this is a "tell" call. We're done. */ |
296
d8c0315deba9
Updated to fix bugs and deal with API breakage in PhysicsFS.
Ryan C. Gordon <icculus@icculus.org>
parents:
286
diff
changeset
|
55 return(pos); |
286 | 56 |
296
d8c0315deba9
Updated to fix bugs and deal with API breakage in PhysicsFS.
Ryan C. Gordon <icculus@icculus.org>
parents:
286
diff
changeset
|
57 pos += offset; |
286 | 58 } /* else if */ |
59 | |
60 else if (whence == SEEK_END) | |
61 { | |
296
d8c0315deba9
Updated to fix bugs and deal with API breakage in PhysicsFS.
Ryan C. Gordon <icculus@icculus.org>
parents:
286
diff
changeset
|
62 PHYSFS_sint64 len = PHYSFS_fileLength(handle); |
286 | 63 if (len == -1) |
64 { | |
65 SDL_SetError("Can't find end of file: %s", PHYSFS_getLastError()); | |
66 return(-1); | |
67 } /* if */ | |
68 | |
296
d8c0315deba9
Updated to fix bugs and deal with API breakage in PhysicsFS.
Ryan C. Gordon <icculus@icculus.org>
parents:
286
diff
changeset
|
69 pos = (int) len; |
d8c0315deba9
Updated to fix bugs and deal with API breakage in PhysicsFS.
Ryan C. Gordon <icculus@icculus.org>
parents:
286
diff
changeset
|
70 if ( ((PHYSFS_sint64) pos) != len ) |
d8c0315deba9
Updated to fix bugs and deal with API breakage in PhysicsFS.
Ryan C. Gordon <icculus@icculus.org>
parents:
286
diff
changeset
|
71 { |
d8c0315deba9
Updated to fix bugs and deal with API breakage in PhysicsFS.
Ryan C. Gordon <icculus@icculus.org>
parents:
286
diff
changeset
|
72 SDL_SetError("Can't fit end-of-file position in an int!"); |
d8c0315deba9
Updated to fix bugs and deal with API breakage in PhysicsFS.
Ryan C. Gordon <icculus@icculus.org>
parents:
286
diff
changeset
|
73 return(-1); |
d8c0315deba9
Updated to fix bugs and deal with API breakage in PhysicsFS.
Ryan C. Gordon <icculus@icculus.org>
parents:
286
diff
changeset
|
74 } /* if */ |
d8c0315deba9
Updated to fix bugs and deal with API breakage in PhysicsFS.
Ryan C. Gordon <icculus@icculus.org>
parents:
286
diff
changeset
|
75 |
d8c0315deba9
Updated to fix bugs and deal with API breakage in PhysicsFS.
Ryan C. Gordon <icculus@icculus.org>
parents:
286
diff
changeset
|
76 pos += offset; |
286 | 77 } /* else if */ |
78 | |
79 else | |
80 { | |
81 SDL_SetError("Invalid 'whence' parameter."); | |
82 return(-1); | |
83 } /* else */ | |
84 | |
296
d8c0315deba9
Updated to fix bugs and deal with API breakage in PhysicsFS.
Ryan C. Gordon <icculus@icculus.org>
parents:
286
diff
changeset
|
85 if ( pos < 0 ) |
d8c0315deba9
Updated to fix bugs and deal with API breakage in PhysicsFS.
Ryan C. Gordon <icculus@icculus.org>
parents:
286
diff
changeset
|
86 { |
d8c0315deba9
Updated to fix bugs and deal with API breakage in PhysicsFS.
Ryan C. Gordon <icculus@icculus.org>
parents:
286
diff
changeset
|
87 SDL_SetError("Attempt to seek past start of file."); |
d8c0315deba9
Updated to fix bugs and deal with API breakage in PhysicsFS.
Ryan C. Gordon <icculus@icculus.org>
parents:
286
diff
changeset
|
88 return(-1); |
d8c0315deba9
Updated to fix bugs and deal with API breakage in PhysicsFS.
Ryan C. Gordon <icculus@icculus.org>
parents:
286
diff
changeset
|
89 } /* if */ |
d8c0315deba9
Updated to fix bugs and deal with API breakage in PhysicsFS.
Ryan C. Gordon <icculus@icculus.org>
parents:
286
diff
changeset
|
90 |
d8c0315deba9
Updated to fix bugs and deal with API breakage in PhysicsFS.
Ryan C. Gordon <icculus@icculus.org>
parents:
286
diff
changeset
|
91 if (!PHYSFS_seek(handle, (PHYSFS_uint64) pos)) |
286 | 92 { |
93 SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError()); | |
94 return(-1); | |
95 } /* if */ | |
96 | |
97 return(pos); | |
98 } /* physfsrwops_seek */ | |
99 | |
100 | |
101 static int physfsrwops_read(SDL_RWops *rw, void *ptr, int size, int maxnum) | |
102 { | |
103 PHYSFS_file *handle = (PHYSFS_file *) rw->hidden.unknown.data1; | |
296
d8c0315deba9
Updated to fix bugs and deal with API breakage in PhysicsFS.
Ryan C. Gordon <icculus@icculus.org>
parents:
286
diff
changeset
|
104 PHYSFS_sint64 rc = PHYSFS_read(handle, ptr, size, maxnum); |
286 | 105 if (rc != maxnum) |
106 { | |
107 if (!PHYSFS_eof(handle)) /* not EOF? Must be an error. */ | |
108 SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError()); | |
109 } /* if */ | |
110 | |
296
d8c0315deba9
Updated to fix bugs and deal with API breakage in PhysicsFS.
Ryan C. Gordon <icculus@icculus.org>
parents:
286
diff
changeset
|
111 return((int) rc); |
286 | 112 } /* physfsrwops_read */ |
113 | |
114 | |
115 static int physfsrwops_write(SDL_RWops *rw, const void *ptr, int size, int num) | |
116 { | |
117 PHYSFS_file *handle = (PHYSFS_file *) rw->hidden.unknown.data1; | |
296
d8c0315deba9
Updated to fix bugs and deal with API breakage in PhysicsFS.
Ryan C. Gordon <icculus@icculus.org>
parents:
286
diff
changeset
|
118 PHYSFS_sint64 rc = PHYSFS_write(handle, ptr, size, num); |
286 | 119 if (rc != num) |
120 SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError()); | |
121 | |
296
d8c0315deba9
Updated to fix bugs and deal with API breakage in PhysicsFS.
Ryan C. Gordon <icculus@icculus.org>
parents:
286
diff
changeset
|
122 return((int) rc); |
286 | 123 } /* physfsrwops_write */ |
124 | |
125 | |
126 static int physfsrwops_close(SDL_RWops *rw) | |
127 { | |
128 PHYSFS_file *handle = (PHYSFS_file *) rw->hidden.unknown.data1; | |
129 if (!PHYSFS_close(handle)) | |
130 { | |
131 SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError()); | |
132 return(-1); | |
133 } /* if */ | |
134 | |
135 SDL_FreeRW(rw); | |
136 return(0); | |
137 } /* physfsrwops_close */ | |
138 | |
139 | |
140 static SDL_RWops *create_rwops(PHYSFS_file *handle) | |
141 { | |
142 SDL_RWops *retval = NULL; | |
143 | |
144 if (handle == NULL) | |
145 SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError()); | |
146 else | |
147 { | |
148 retval = SDL_AllocRW(); | |
149 if (retval != NULL) | |
150 { | |
151 retval->seek = physfsrwops_seek; | |
152 retval->read = physfsrwops_read; | |
153 retval->write = physfsrwops_write; | |
154 retval->close = physfsrwops_close; | |
155 retval->hidden.unknown.data1 = handle; | |
156 } /* if */ | |
157 } /* else */ | |
158 | |
159 return(retval); | |
160 } /* create_rwops */ | |
161 | |
162 | |
163 SDL_RWops *PHYSFSRWOPS_makeRWops(PHYSFS_file *handle) | |
164 { | |
165 SDL_RWops *retval = NULL; | |
166 if (handle == NULL) | |
167 SDL_SetError("NULL pointer passed to PHYSFSRWOPS_makeRWops()."); | |
168 else | |
169 retval = create_rwops(handle); | |
170 | |
171 return(retval); | |
172 } /* PHYSFSRWOPS_makeRWops */ | |
173 | |
174 | |
175 SDL_RWops *PHYSFSRWOPS_openRead(const char *fname) | |
176 { | |
177 return(create_rwops(PHYSFS_openRead(fname))); | |
178 } /* PHYSFSRWOPS_openRead */ | |
179 | |
180 | |
181 SDL_RWops *PHYSFSRWOPS_openWrite(const char *fname) | |
182 { | |
183 return(create_rwops(PHYSFS_openWrite(fname))); | |
184 } /* PHYSFSRWOPS_openWrite */ | |
185 | |
186 | |
187 SDL_RWops *PHYSFSRWOPS_openAppend(const char *fname) | |
188 { | |
189 return(create_rwops(PHYSFS_openAppend(fname))); | |
190 } /* PHYSFSRWOPS_openAppend */ | |
191 | |
479
35dfa9d9782e
Fixed automake nonsense.
Ryan C. Gordon <icculus@icculus.org>
parents:
296
diff
changeset
|
192 #endif |
286 | 193 |
194 /* end of physfsrwops.c ... */ | |
195 |