diff options
Diffstat (limited to 'apps')
-rw-r--r-- | apps/plugins/sdl/progs/quake/sys_sdl.c | 141 |
1 files changed, 103 insertions, 38 deletions
diff --git a/apps/plugins/sdl/progs/quake/sys_sdl.c b/apps/plugins/sdl/progs/quake/sys_sdl.c index 3cc3122bc2..44411bd153 100644 --- a/apps/plugins/sdl/progs/quake/sys_sdl.c +++ b/apps/plugins/sdl/progs/quake/sys_sdl.c | |||
@@ -116,16 +116,25 @@ FILE IO | |||
116 | */ | 116 | */ |
117 | 117 | ||
118 | #define MAX_HANDLES 10 | 118 | #define MAX_HANDLES 10 |
119 | static FILE *sys_handles[MAX_HANDLES]; | 119 | //static FILE *sys_handles[MAX_HANDLES]; |
120 | |||
121 | /* a file fully or partially cached in memory */ | ||
122 | /* We use the FILE handle to track the current position */ | ||
123 | static struct memfile { | ||
124 | FILE *f; /* NULL: unused slot */ | ||
125 | char *buf; | ||
126 | size_t resident_len; /* bytes 0-(resident_len-1) are in memory */ | ||
127 | size_t full_len; | ||
128 | } sys_handles[MAX_HANDLES]; | ||
120 | 129 | ||
121 | void Sys_Shutdown(void) | 130 | void Sys_Shutdown(void) |
122 | { | 131 | { |
123 | for(int i = 0; i < MAX_HANDLES; i++) | 132 | for(int i = 0; i < MAX_HANDLES; i++) |
124 | { | 133 | { |
125 | FILE *f = sys_handles[i]; | 134 | FILE *f = sys_handles[i].f; |
126 | if(f) | 135 | if(f) |
127 | fclose(f); | 136 | fclose(f); |
128 | sys_handles[i] = NULL; | 137 | sys_handles[i].f = NULL; |
129 | } | 138 | } |
130 | } | 139 | } |
131 | 140 | ||
@@ -134,7 +143,7 @@ int findhandle (void) | |||
134 | int i; | 143 | int i; |
135 | 144 | ||
136 | for (i=1 ; i<MAX_HANDLES ; i++) | 145 | for (i=1 ; i<MAX_HANDLES ; i++) |
137 | if (!sys_handles[i]) | 146 | if (!sys_handles[i].f) |
138 | return i; | 147 | return i; |
139 | Sys_Error ("out of handles"); | 148 | Sys_Error ("out of handles"); |
140 | return -1; | 149 | return -1; |
@@ -158,26 +167,60 @@ static int Qfilelength (FILE *f) | |||
158 | return end; | 167 | return end; |
159 | } | 168 | } |
160 | 169 | ||
170 | #define CACHE_THRESHOLD (1024*1024) | ||
171 | |||
172 | /* really rough guesses */ | ||
173 | #if MEMORYSIZE >= 64 | ||
174 | #define MAX_CACHE (32*1024*1024) | ||
175 | #elif MEMORYSIZE >= 32 | ||
176 | #define MAX_CACHE (20*1024*1024) | ||
177 | #else | ||
178 | #define MAX_CACHE 0 | ||
179 | #endif | ||
180 | |||
181 | static int cache_left = MAX_CACHE; | ||
182 | |||
161 | int Sys_FileOpenRead (char *path, int *hndl) | 183 | int Sys_FileOpenRead (char *path, int *hndl) |
162 | { | 184 | { |
163 | FILE *f; | 185 | FILE *f; |
164 | int i; | 186 | int i; |
165 | 187 | ||
166 | i = findhandle (); | 188 | i = findhandle (); |
167 | 189 | ||
168 | f = fopen(path, "rb"); | 190 | f = fopen(path, "rb"); |
169 | if (!f) | 191 | if (!f) |
170 | { | 192 | { |
171 | *hndl = -1; | 193 | *hndl = -1; |
172 | return -1; | 194 | return -1; |
173 | } | 195 | } |
174 | sys_handles[i] = f; | 196 | sys_handles[i].f = f; |
175 | *hndl = i; | 197 | *hndl = i; |
176 | 198 | ||
177 | //rb->splashf(HZ*2, "Allocating handle %d to %s", i, path); | 199 | //rb->splashf(HZ*2, "Allocating handle %d to %s", i, path); |
178 | 200 | ||
179 | 201 | int len = sys_handles[i].full_len = Qfilelength(f); | |
180 | return Qfilelength(f); | 202 | |
203 | /* cache in memory? */ | ||
204 | if(len > CACHE_THRESHOLD && cache_left > 0) | ||
205 | { | ||
206 | /* cache all or part of it */ | ||
207 | int cachelen = (cache_left > len) ? len : cache_left; | ||
208 | |||
209 | if((sys_handles[i].buf = malloc(cachelen))) | ||
210 | { | ||
211 | cache_left -= cachelen; | ||
212 | |||
213 | /* fill in cache */ | ||
214 | printf("Please wait... caching %d KB of large file", cachelen / 1024); | ||
215 | if(fread(sys_handles[i].buf, 1, cachelen, f) == cachelen) | ||
216 | { | ||
217 | sys_handles[i].resident_len = cachelen; | ||
218 | fseek(f, 0, SEEK_SET); | ||
219 | |||
220 | printf("Success!"); | ||
221 | } | ||
222 | } | ||
223 | } | ||
181 | } | 224 | } |
182 | 225 | ||
183 | int Sys_FileOpenWrite (char *path) | 226 | int Sys_FileOpenWrite (char *path) |
@@ -190,7 +233,7 @@ int Sys_FileOpenWrite (char *path) | |||
190 | f = fopen(path, "wb"); | 233 | f = fopen(path, "wb"); |
191 | if (!f) | 234 | if (!f) |
192 | Sys_Error ("Error opening %s: %s", path,strerror(errno)); | 235 | Sys_Error ("Error opening %s: %s", path,strerror(errno)); |
193 | sys_handles[i] = f; | 236 | sys_handles[i].f = f; |
194 | 237 | ||
195 | return i; | 238 | return i; |
196 | } | 239 | } |
@@ -199,15 +242,19 @@ void Sys_FileClose (int handle) | |||
199 | { | 242 | { |
200 | if ( handle >= 0 ) { | 243 | if ( handle >= 0 ) { |
201 | //rb->splashf(HZ, "Close handle %d", handle); | 244 | //rb->splashf(HZ, "Close handle %d", handle); |
202 | fclose (sys_handles[handle]); | 245 | fclose (sys_handles[handle].f); |
203 | sys_handles[handle] = NULL; | 246 | |
247 | cache_left += sys_handles[handle].resident_len; | ||
248 | |||
249 | // clear all fields so we can safely reuse | ||
250 | memset(sys_handles + handle, 0, sizeof(sys_handles[0])); | ||
204 | } | 251 | } |
205 | } | 252 | } |
206 | 253 | ||
207 | void Sys_FileSeek (int handle, int position) | 254 | void Sys_FileSeek (int handle, int position) |
208 | { | 255 | { |
209 | if ( handle >= 0 ) { | 256 | if ( handle >= 0 ) { |
210 | fseek (sys_handles[handle], position, SEEK_SET); | 257 | fseek (sys_handles[handle].f, position, SEEK_SET); |
211 | } | 258 | } |
212 | } | 259 | } |
213 | 260 | ||
@@ -218,20 +265,38 @@ int Sys_FileRead (int handle, void *dst, int count) | |||
218 | 265 | ||
219 | size = 0; | 266 | size = 0; |
220 | if ( handle >= 0 ) { | 267 | if ( handle >= 0 ) { |
221 | data = dst; | 268 | FILE *f = sys_handles[handle].f; |
222 | while ( count > 0 ) { | 269 | |
223 | done = fread (data, 1, count, sys_handles[handle]); | 270 | data = dst; |
224 | if ( done == 0 ) { | 271 | |
225 | break; | 272 | /* partially or fully in memory */ |
226 | } | 273 | int pos = ftell(f); |
227 | else if(done < 0) | 274 | if(pos < sys_handles[handle].resident_len) |
228 | { | 275 | { |
229 | Sys_Error("stream error %d, file is %d = %d", done, handle, sys_handles[handle]); | 276 | int memleft = sys_handles[handle].resident_len - pos; |
230 | } | 277 | int memlen = MIN(count, memleft); |
231 | data += done; | 278 | |
232 | count -= done; | 279 | memcpy(data, sys_handles[handle].buf + pos, memlen); |
233 | size += done; | 280 | data += memlen; |
234 | } | 281 | count -= memlen; |
282 | size += memlen; | ||
283 | |||
284 | fseek(f, memlen, SEEK_CUR); | ||
285 | } | ||
286 | |||
287 | while ( count > 0 ) { | ||
288 | done = fread (data, 1, count, sys_handles[handle].f); | ||
289 | if ( done == 0 ) { | ||
290 | break; | ||
291 | } | ||
292 | else if(done < 0) | ||
293 | { | ||
294 | Sys_Error("stream error %d, file is %d = %d", done, handle, sys_handles[handle]); | ||
295 | } | ||
296 | data += done; | ||
297 | count -= done; | ||
298 | size += done; | ||
299 | } | ||
235 | } | 300 | } |
236 | return size; | 301 | return size; |
237 | 302 | ||
@@ -246,7 +311,7 @@ int Sys_FileWrite (int handle, void *src, int count) | |||
246 | if ( handle >= 0 ) { | 311 | if ( handle >= 0 ) { |
247 | data = src; | 312 | data = src; |
248 | while ( count > 0 ) { | 313 | while ( count > 0 ) { |
249 | done = fread (data, 1, count, sys_handles[handle]); | 314 | done = fread (data, 1, count, sys_handles[handle].f); |
250 | if ( done == 0 ) { | 315 | if ( done == 0 ) { |
251 | break; | 316 | break; |
252 | } | 317 | } |