]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - quakeio.c
fixed targa quake sky loading
[xonotic/darkplaces.git] / quakeio.c
1 /*
2         quakeio.c
3
4         (description)
5
6         Copyright (C) 1996-1997  Id Software, Inc.
7         Copyright (C) 1999,2000  contributors of the QuakeForge project
8         Please see the file "AUTHORS" for a list of contributors
9
10         This program is free software; you can redistribute it and/or
11         modify it under the terms of the GNU General Public License
12         as published by the Free Software Foundation; either version 2
13         of the License, or (at your option) any later version.
14
15         This program is distributed in the hope that it will be useful,
16         but WITHOUT ANY WARRANTY; without even the implied warranty of
17         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18
19         See the GNU General Public License for more details.
20
21         You should have received a copy of the GNU General Public License
22         along with this program; if not, write to:
23
24                 Free Software Foundation, Inc.
25                 59 Temple Place - Suite 330
26                 Boston, MA  02111-1307, USA
27
28         $Id$
29 */
30
31 #include "quakedef.h"
32 #include <stdlib.h>
33 #include <string.h>
34 #ifdef WIN32
35 # include <io.h>
36 # include <fcntl.h>
37 #else
38 # include <pwd.h>
39 # include <unistd.h>
40 #endif
41
42 #include <stdarg.h>
43 #include <stdlib.h>
44 #include <limits.h>
45
46 #ifndef PATH_MAX
47 # define PATH_MAX 512
48 #endif
49
50 #include "quakeio.h"
51
52 #ifdef WIN32
53 # ifndef __BORLANDC__
54 #  define setmode _setmode
55 #  define O_BINARY _O_BINARY
56 # endif
57 #endif
58
59 mempool_t *quakeio_mempool;
60
61 void
62 Qexpand_squiggle (const char *path, char *dest)
63 {
64         char       *home;
65
66 #ifndef _WIN32
67         struct passwd *pwd_ent;
68 #endif
69
70         if (strncmp (path, "~/", 2) != 0) {
71                 strcpy (dest, path);
72                 return;
73         }
74
75 #ifdef _WIN32
76         // LordHavoc: first check HOME to duplicate previous version behavior
77         // (also handy if someone wants it somewhere other than their
78         //  windows directory)
79         home = getenv ("HOME");
80         if (!home || !home[0])
81                 home = getenv ("WINDIR");
82 #else
83         if ((pwd_ent = getpwuid (getuid ()))) {
84                 home = pwd_ent->pw_dir;
85         } else
86                 home = getenv ("HOME");
87 #endif
88
89         if (home) {
90                 strcpy (dest, home);
91                 strncat (dest, path + 1, MAX_OSPATH - strlen (dest));   // skip
92                                                                                                                                 // leading ~
93         } else
94                 strcpy (dest, path);
95 }
96
97 int
98 Qrename (const char *old, const char *new)
99 {
100         char        e_old[PATH_MAX];
101         char        e_new[PATH_MAX];
102
103         Qexpand_squiggle (old, e_old);
104         Qexpand_squiggle (new, e_new);
105         return rename (e_old, e_new);
106 }
107
108 QFile *
109 Qopen (const char *path, const char *mode)
110 {
111         QFile      *file;
112         char        m[80], *p;
113         int         zip = 0;
114         char        e_path[PATH_MAX];
115
116         Qexpand_squiggle (path, e_path);
117         path = e_path;
118
119         for (p = m; *mode && p - m < (int)(sizeof (m) - 1); mode++) {
120                 if (*mode == 'z') {
121                         zip = 1;
122                         continue;
123                 }
124 #ifndef HAVE_ZLIB
125                 if (strchr ("0123456789fh", *mode)) {
126                         continue;
127                 }
128 #endif
129                 *p++ = *mode;
130         }
131         *p = 0;
132
133         file = Mem_Alloc(quakeio_mempool, sizeof (*file));
134         memset(file, 0, sizeof(*file));
135         if (!file)
136                 return 0;
137 #ifdef HAVE_ZLIB
138         if (zip) {
139                 file->gzfile = gzopen (path, m);
140                 if (!file->gzfile) {
141                         Mem_Free(file);
142                         return 0;
143                 }
144         } else
145 #endif
146         {
147                 file->file = fopen (path, m);
148                 if (!file->file) {
149                         Mem_Free(file);
150                         return 0;
151                 }
152         }
153         return file;
154 }
155
156 QFile *
157 Qdopen (int fd, const char *mode)
158 {
159         QFile      *file;
160         char        m[80], *p;
161         int         zip = 0;
162
163         for (p = m; *mode && p - m < (int)(sizeof (m) - 1); mode++) {
164                 if (*mode == 'z') {
165                         zip = 1;
166                         continue;
167                 }
168                 *p++ = *mode;
169         }
170
171         *p = 0;
172
173         file = Mem_Alloc(quakeio_mempool, sizeof (*file));
174         memset(file, 0, sizeof(*file));
175         if (!file)
176                 return 0;
177 #ifdef HAVE_ZLIB
178         if (zip) {
179                 file->gzfile = gzdopen (fd, m);
180                 if (!file->gzfile) {
181                         Mem_Free(file);
182                         return 0;
183                 }
184         } else
185 #endif
186         {
187                 file->file = fdopen (fd, m);
188                 if (!file->file) {
189                         Mem_Free(file);
190                         return 0;
191                 }
192         }
193 #ifdef WIN32
194         if (file->file)
195                 setmode (_fileno (file->file), O_BINARY);
196 #endif
197         return file;
198 }
199
200 void
201 Qclose (QFile *file)
202 {
203         if (file->file)
204                 fclose (file->file);
205 #ifdef HAVE_ZLIB
206         else
207                 gzclose (file->gzfile);
208 #endif
209         Mem_Free(file);
210 }
211
212 int
213 Qread (QFile *file, void *buf, int count)
214 {
215         if (file->file)
216                 return fread (buf, 1, count, file->file);
217 #ifdef HAVE_ZLIB
218         else
219                 return gzread (file->gzfile, buf, count);
220 #else
221         return -1;
222 #endif
223 }
224
225 int
226 Qwrite (QFile *file, void *buf, int count)
227 {
228         if (file->file)
229                 return fwrite (buf, 1, count, file->file);
230 #ifdef HAVE_ZLIB
231         else
232                 return gzwrite (file->gzfile, buf, count);
233 #else
234         return -1;
235 #endif
236 }
237
238 int
239 Qprintf (QFile *file, const char *fmt, ...)
240 {
241         va_list     args;
242         int         ret = -1;
243
244         va_start (args, fmt);
245         if (file->file)
246                 ret = vfprintf (file->file, fmt, args);
247 #ifdef HAVE_ZLIB
248         else {
249                 char        buf[4096];
250
251                 va_start (args, fmt);
252 #ifdef HAVE_VSNPRINTF
253                 (void) vsnprintf (buf, sizeof (buf), fmt, args);
254 #else
255                 (void) vsprintf (buf, fmt, args);
256 #endif
257                 va_end (args);
258                 ret = strlen (buf);                             /* some *snprintf don't return the nb 
259                                                                                    of bytes written */
260                 if (ret > 0)
261                         ret = gzwrite (file->gzfile, buf, (unsigned) ret);
262         }
263 #endif
264         va_end (args);
265         return ret;
266 }
267
268 char *
269 Qgets (QFile *file, char *buf, int count)
270 {
271         if (file->file)
272                 return fgets (buf, count, file->file);
273 #ifdef HAVE_ZLIB
274         else
275                 return gzgets (file->gzfile, buf, count);
276 #else
277         return 0;
278 #endif
279 }
280
281 int
282 Qgetc (QFile *file)
283 {
284         if (file->file)
285                 return fgetc (file->file);
286 #ifdef HAVE_ZLIB
287         else
288                 return gzgetc (file->gzfile);
289 #else
290         return -1;
291 #endif
292 }
293
294 int
295 Qputc (QFile *file, int c)
296 {
297         if (file->file)
298                 return fputc (c, file->file);
299 #ifdef HAVE_ZLIB
300         else
301                 return gzputc (file->gzfile, c);
302 #else
303         return -1;
304 #endif
305 }
306
307 int
308 Qseek (QFile *file, long offset, int whence)
309 {
310         if (file->file)
311                 return fseek (file->file, offset, whence);
312 #ifdef HAVE_ZLIB
313         else
314                 return gzseek (file->gzfile, offset, whence);
315 #else
316         return -1;
317 #endif
318 }
319
320 long
321 Qtell (QFile *file)
322 {
323         if (file->file)
324                 return ftell (file->file);
325 #ifdef HAVE_ZLIB
326         else
327                 return gztell (file->gzfile);
328 #else
329         return -1;
330 #endif
331 }
332
333 int
334 Qflush (QFile *file)
335 {
336         if (file->file)
337                 return fflush (file->file);
338 #ifdef HAVE_ZLIB
339         else
340                 return gzflush (file->gzfile, Z_SYNC_FLUSH);
341 #else
342         return -1;
343 #endif
344 }
345
346 int
347 Qeof (QFile *file)
348 {
349         if (file->file)
350                 return feof (file->file);
351 #ifdef HAVE_ZLIB
352         else
353                 return gzeof (file->gzfile);
354 #else
355         return -1;
356 #endif
357 }
358
359 /*
360
361         Qgetline
362
363         Dynamic length version of Qgets. DO NOT free the buffer.
364
365 */
366 char *
367 Qgetline (QFile *file)
368 {
369         static int  size = 256;
370         static char *buf = 0;
371         char        *t;
372         int         len;
373
374         if (!buf)
375                 buf = Mem_Alloc(quakeio_mempool, size);
376
377         if (!Qgets (file, buf, size))
378                 return 0;
379
380         len = strlen (buf);
381         while (buf[len - 1] != '\n' && buf[len - 1] != '\r')
382         {
383                 t = Mem_Alloc(quakeio_mempool, size + 256);
384                 memcpy(t, buf, size);
385                 Mem_Free(buf);
386                 size += 256;
387                 buf = t;
388                 if (!Qgets (file, buf + len, size - len))
389                         break;
390                 len = strlen (buf);
391         }
392         while ((len = strlen(buf)) && (buf[len - 1] == '\n' || buf[len - 1] == '\r'))
393                 buf[len - 1] = 0;
394         return buf;
395 }
396
397 void QuakeIO_Init(void)
398 {
399         quakeio_mempool = Mem_AllocPool("file management");
400 }
401