]> icculus.org git repositories - divverent/darkplaces.git/blob - quakeio.c
fix for skybox
[divverent/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 //#ifdef _MSC_VER
43 //# define _POSIX_
44 //#endif
45
46 #include <stdarg.h>
47 #include <stdlib.h>
48 #include <limits.h>
49
50 #ifndef PATH_MAX
51 # define PATH_MAX 512
52 #endif
53
54 #include "quakeio.h"
55
56 #ifdef WIN32
57 # ifndef __BORLANDC__
58 #  define setmode _setmode
59 #  define O_BINARY _O_BINARY
60 # endif
61 #endif
62
63 void
64 Qexpand_squiggle (const char *path, char *dest)
65 {
66         char       *home;
67
68 #ifndef _WIN32
69         struct passwd *pwd_ent;
70 #endif
71
72         if (strncmp (path, "~/", 2) != 0) {
73                 strcpy (dest, path);
74                 return;
75         }
76
77 #ifdef _WIN32
78         // LordHavoc: first check HOME to duplicate previous version behavior
79         // (also handy if someone wants it somewhere other than their
80         //  windows directory)
81         home = getenv ("HOME");
82         if (!home || !home[0])
83                 home = getenv ("WINDIR");
84 #else
85         if ((pwd_ent = getpwuid (getuid ()))) {
86                 home = pwd_ent->pw_dir;
87         } else
88                 home = getenv ("HOME");
89 #endif
90
91         if (home) {
92                 strcpy (dest, home);
93                 strncat (dest, path + 1, MAX_OSPATH - strlen (dest));   // skip
94                                                                                                                                 // leading ~
95         } else
96                 strcpy (dest, path);
97 }
98
99 int
100 Qrename (const char *old, const char *new)
101 {
102         char        e_old[PATH_MAX];
103         char        e_new[PATH_MAX];
104
105         Qexpand_squiggle (old, e_old);
106         Qexpand_squiggle (new, e_new);
107         return rename (e_old, e_new);
108 }
109
110 QFile *
111 Qopen (const char *path, const char *mode)
112 {
113         QFile      *file;
114         char        m[80], *p;
115         int         zip = 0;
116         char        e_path[PATH_MAX];
117
118         Qexpand_squiggle (path, e_path);
119         path = e_path;
120
121         for (p = m; *mode && p - m < (sizeof (m) - 1); mode++) {
122                 if (*mode == 'z') {
123                         zip = 1;
124                         continue;
125                 }
126 #ifndef HAVE_ZLIB
127                 if (strchr ("0123456789fh", *mode)) {
128                         continue;
129                 }
130 #endif
131                 *p++ = *mode;
132         }
133         *p = 0;
134
135         file = calloc (sizeof (*file), 1);
136         if (!file)
137                 return 0;
138 #ifdef HAVE_ZLIB
139         if (zip) {
140                 file->gzfile = gzopen (path, m);
141                 if (!file->gzfile) {
142                         free (file);
143                         return 0;
144                 }
145         } else
146 #endif
147         {
148                 file->file = fopen (path, m);
149                 if (!file->file) {
150                         free (file);
151                         return 0;
152                 }
153         }
154         return file;
155 }
156
157 QFile *
158 Qdopen (int fd, const char *mode)
159 {
160         QFile      *file;
161         char        m[80], *p;
162         int         zip = 0;
163
164         for (p = m; *mode && p - m < (sizeof (m) - 1); mode++) {
165                 if (*mode == 'z') {
166                         zip = 1;
167                         continue;
168                 }
169                 *p++ = *mode;
170         }
171
172         *p = 0;
173
174         file = calloc (sizeof (*file), 1);
175         if (!file)
176                 return 0;
177 #ifdef HAVE_ZLIB
178         if (zip) {
179                 file->gzfile = gzdopen (fd, m);
180                 if (!file->gzfile) {
181                         free (file);
182                         return 0;
183                 }
184         } else
185 #endif
186         {
187                 file->file = fdopen (fd, m);
188                 if (!file->file) {
189                         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         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         int         len;
372
373         if (!buf)
374                 buf = malloc (size);
375
376         if (!Qgets (file, buf, size))
377                 return 0;
378
379         len = strlen (buf);
380         while (buf[len - 1] != '\n') {
381                 char       *t = realloc (buf, size + 256);
382
383                 if (!t)
384                         return 0;
385                 buf = t;
386                 size += 256;
387                 if (!Qgets (file, buf + len, size - len))
388                         break;
389                 len = strlen (buf);
390         }
391         return buf;
392 }