]> icculus.org git repositories - divverent/darkplaces.git/blob - quakeio.c
made coronas small and intense, much more noticable now
[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 mempool_t *quakeio_mempool;
64
65 void
66 Qexpand_squiggle (const char *path, char *dest)
67 {
68         char       *home;
69
70 #ifndef _WIN32
71         struct passwd *pwd_ent;
72 #endif
73
74         if (strncmp (path, "~/", 2) != 0) {
75                 strcpy (dest, path);
76                 return;
77         }
78
79 #ifdef _WIN32
80         // LordHavoc: first check HOME to duplicate previous version behavior
81         // (also handy if someone wants it somewhere other than their
82         //  windows directory)
83         home = getenv ("HOME");
84         if (!home || !home[0])
85                 home = getenv ("WINDIR");
86 #else
87         if ((pwd_ent = getpwuid (getuid ()))) {
88                 home = pwd_ent->pw_dir;
89         } else
90                 home = getenv ("HOME");
91 #endif
92
93         if (home) {
94                 strcpy (dest, home);
95                 strncat (dest, path + 1, MAX_OSPATH - strlen (dest));   // skip
96                                                                                                                                 // leading ~
97         } else
98                 strcpy (dest, path);
99 }
100
101 int
102 Qrename (const char *old, const char *new)
103 {
104         char        e_old[PATH_MAX];
105         char        e_new[PATH_MAX];
106
107         Qexpand_squiggle (old, e_old);
108         Qexpand_squiggle (new, e_new);
109         return rename (e_old, e_new);
110 }
111
112 QFile *
113 Qopen (const char *path, const char *mode)
114 {
115         QFile      *file;
116         char        m[80], *p;
117         int         zip = 0;
118         char        e_path[PATH_MAX];
119
120         Qexpand_squiggle (path, e_path);
121         path = e_path;
122
123         for (p = m; *mode && p - m < (sizeof (m) - 1); mode++) {
124                 if (*mode == 'z') {
125                         zip = 1;
126                         continue;
127                 }
128 #ifndef HAVE_ZLIB
129                 if (strchr ("0123456789fh", *mode)) {
130                         continue;
131                 }
132 #endif
133                 *p++ = *mode;
134         }
135         *p = 0;
136
137         file = Mem_Alloc(quakeio_mempool, sizeof (*file));
138         memset(file, 0, sizeof(*file));
139         if (!file)
140                 return 0;
141 #ifdef HAVE_ZLIB
142         if (zip) {
143                 file->gzfile = gzopen (path, m);
144                 if (!file->gzfile) {
145                         Mem_Free(file);
146                         return 0;
147                 }
148         } else
149 #endif
150         {
151                 file->file = fopen (path, m);
152                 if (!file->file) {
153                         Mem_Free(file);
154                         return 0;
155                 }
156         }
157         return file;
158 }
159
160 QFile *
161 Qdopen (int fd, const char *mode)
162 {
163         QFile      *file;
164         char        m[80], *p;
165         int         zip = 0;
166
167         for (p = m; *mode && p - m < (sizeof (m) - 1); mode++) {
168                 if (*mode == 'z') {
169                         zip = 1;
170                         continue;
171                 }
172                 *p++ = *mode;
173         }
174
175         *p = 0;
176
177         file = Mem_Alloc(quakeio_mempool, sizeof (*file));
178         memset(file, 0, sizeof(*file));
179         if (!file)
180                 return 0;
181 #ifdef HAVE_ZLIB
182         if (zip) {
183                 file->gzfile = gzdopen (fd, m);
184                 if (!file->gzfile) {
185                         Mem_Free(file);
186                         return 0;
187                 }
188         } else
189 #endif
190         {
191                 file->file = fdopen (fd, m);
192                 if (!file->file) {
193                         Mem_Free(file);
194                         return 0;
195                 }
196         }
197 #ifdef WIN32
198         if (file->file)
199                 setmode (_fileno (file->file), O_BINARY);
200 #endif
201         return file;
202 }
203
204 void
205 Qclose (QFile *file)
206 {
207         if (file->file)
208                 fclose (file->file);
209 #ifdef HAVE_ZLIB
210         else
211                 gzclose (file->gzfile);
212 #endif
213         Mem_Free(file);
214 }
215
216 int
217 Qread (QFile *file, void *buf, int count)
218 {
219         if (file->file)
220                 return fread (buf, 1, count, file->file);
221 #ifdef HAVE_ZLIB
222         else
223                 return gzread (file->gzfile, buf, count);
224 #else
225         return -1;
226 #endif
227 }
228
229 int
230 Qwrite (QFile *file, void *buf, int count)
231 {
232         if (file->file)
233                 return fwrite (buf, 1, count, file->file);
234 #ifdef HAVE_ZLIB
235         else
236                 return gzwrite (file->gzfile, buf, count);
237 #else
238         return -1;
239 #endif
240 }
241
242 int
243 Qprintf (QFile *file, const char *fmt, ...)
244 {
245         va_list     args;
246         int         ret = -1;
247
248         va_start (args, fmt);
249         if (file->file)
250                 ret = vfprintf (file->file, fmt, args);
251 #ifdef HAVE_ZLIB
252         else {
253                 char        buf[4096];
254
255                 va_start (args, fmt);
256 #ifdef HAVE_VSNPRINTF
257                 (void) vsnprintf (buf, sizeof (buf), fmt, args);
258 #else
259                 (void) vsprintf (buf, fmt, args);
260 #endif
261                 va_end (args);
262                 ret = strlen (buf);                             /* some *snprintf don't return the nb 
263                                                                                    of bytes written */
264                 if (ret > 0)
265                         ret = gzwrite (file->gzfile, buf, (unsigned) ret);
266         }
267 #endif
268         va_end (args);
269         return ret;
270 }
271
272 char *
273 Qgets (QFile *file, char *buf, int count)
274 {
275         if (file->file)
276                 return fgets (buf, count, file->file);
277 #ifdef HAVE_ZLIB
278         else
279                 return gzgets (file->gzfile, buf, count);
280 #else
281         return 0;
282 #endif
283 }
284
285 int
286 Qgetc (QFile *file)
287 {
288         if (file->file)
289                 return fgetc (file->file);
290 #ifdef HAVE_ZLIB
291         else
292                 return gzgetc (file->gzfile);
293 #else
294         return -1;
295 #endif
296 }
297
298 int
299 Qputc (QFile *file, int c)
300 {
301         if (file->file)
302                 return fputc (c, file->file);
303 #ifdef HAVE_ZLIB
304         else
305                 return gzputc (file->gzfile, c);
306 #else
307         return -1;
308 #endif
309 }
310
311 int
312 Qseek (QFile *file, long offset, int whence)
313 {
314         if (file->file)
315                 return fseek (file->file, offset, whence);
316 #ifdef HAVE_ZLIB
317         else
318                 return gzseek (file->gzfile, offset, whence);
319 #else
320         return -1;
321 #endif
322 }
323
324 long
325 Qtell (QFile *file)
326 {
327         if (file->file)
328                 return ftell (file->file);
329 #ifdef HAVE_ZLIB
330         else
331                 return gztell (file->gzfile);
332 #else
333         return -1;
334 #endif
335 }
336
337 int
338 Qflush (QFile *file)
339 {
340         if (file->file)
341                 return fflush (file->file);
342 #ifdef HAVE_ZLIB
343         else
344                 return gzflush (file->gzfile, Z_SYNC_FLUSH);
345 #else
346         return -1;
347 #endif
348 }
349
350 int
351 Qeof (QFile *file)
352 {
353         if (file->file)
354                 return feof (file->file);
355 #ifdef HAVE_ZLIB
356         else
357                 return gzeof (file->gzfile);
358 #else
359         return -1;
360 #endif
361 }
362
363 /*
364
365         Qgetline
366
367         Dynamic length version of Qgets. DO NOT free the buffer.
368
369 */
370 char *
371 Qgetline (QFile *file)
372 {
373         static int  size = 256;
374         static char *buf = 0;
375         char        *t;
376         int         len;
377
378         if (!buf)
379                 buf = Mem_Alloc(quakeio_mempool, size);
380
381         if (!Qgets (file, buf, size))
382                 return 0;
383
384         len = strlen (buf);
385         while (buf[len - 1] != '\n' && buf[len - 1] != '\r')
386         {
387                 t = Mem_Alloc(quakeio_mempool, size + 256);
388                 memcpy(t, buf, size);
389                 Mem_Free(buf);
390                 size += 256;
391                 buf = t;
392                 if (!Qgets (file, buf + len, size - len))
393                         break;
394                 len = strlen (buf);
395         }
396         while ((len = strlen(buf)) && (buf[len - 1] == '\n' || buf[len - 1] == '\r'))
397                 buf[len - 1] = 0;
398         return buf;
399 }
400
401 void QuakeIO_Init(void)
402 {
403         quakeio_mempool = Mem_AllocPool("file management");
404 }