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
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.
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.
19 See the GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to:
24 Free Software Foundation, Inc.
25 59 Temple Place - Suite 330
26 Boston, MA 02111-1307, USA
54 # define setmode _setmode
55 # define O_BINARY _O_BINARY
59 mempool_t *quakeio_mempool;
62 Qexpand_squiggle (const char *path, char *dest)
67 struct passwd *pwd_ent;
70 if (strncmp (path, "~/", 2) != 0) {
76 // LordHavoc: first check HOME to duplicate previous version behavior
77 // (also handy if someone wants it somewhere other than their
79 home = getenv ("HOME");
80 if (!home || !home[0])
81 home = getenv ("WINDIR");
83 if ((pwd_ent = getpwuid (getuid ()))) {
84 home = pwd_ent->pw_dir;
86 home = getenv ("HOME");
91 strncat (dest, path + 1, MAX_OSPATH - strlen (dest)); // skip
98 Qrename (const char *old, const char *new)
100 char e_old[PATH_MAX];
101 char e_new[PATH_MAX];
103 Qexpand_squiggle (old, e_old);
104 Qexpand_squiggle (new, e_new);
105 return rename (e_old, e_new);
109 Qopen (const char *path, const char *mode)
114 char e_path[PATH_MAX];
116 Qexpand_squiggle (path, e_path);
119 for (p = m; *mode && p - m < (sizeof (m) - 1); mode++) {
125 if (strchr ("0123456789fh", *mode)) {
133 file = Mem_Alloc(quakeio_mempool, sizeof (*file));
134 memset(file, 0, sizeof(*file));
139 file->gzfile = gzopen (path, m);
147 file->file = fopen (path, m);
157 Qdopen (int fd, const char *mode)
163 for (p = m; *mode && p - m < (sizeof (m) - 1); mode++) {
173 file = Mem_Alloc(quakeio_mempool, sizeof (*file));
174 memset(file, 0, sizeof(*file));
179 file->gzfile = gzdopen (fd, m);
187 file->file = fdopen (fd, m);
195 setmode (_fileno (file->file), O_BINARY);
207 gzclose (file->gzfile);
213 Qread (QFile *file, void *buf, int count)
216 return fread (buf, 1, count, file->file);
219 return gzread (file->gzfile, buf, count);
226 Qwrite (QFile *file, void *buf, int count)
229 return fwrite (buf, 1, count, file->file);
232 return gzwrite (file->gzfile, buf, count);
239 Qprintf (QFile *file, const char *fmt, ...)
244 va_start (args, fmt);
246 ret = vfprintf (file->file, fmt, args);
251 va_start (args, fmt);
252 #ifdef HAVE_VSNPRINTF
253 (void) vsnprintf (buf, sizeof (buf), fmt, args);
255 (void) vsprintf (buf, fmt, args);
258 ret = strlen (buf); /* some *snprintf don't return the nb
261 ret = gzwrite (file->gzfile, buf, (unsigned) ret);
269 Qgets (QFile *file, char *buf, int count)
272 return fgets (buf, count, file->file);
275 return gzgets (file->gzfile, buf, count);
285 return fgetc (file->file);
288 return gzgetc (file->gzfile);
295 Qputc (QFile *file, int c)
298 return fputc (c, file->file);
301 return gzputc (file->gzfile, c);
308 Qseek (QFile *file, long offset, int whence)
311 return fseek (file->file, offset, whence);
314 return gzseek (file->gzfile, offset, whence);
324 return ftell (file->file);
327 return gztell (file->gzfile);
337 return fflush (file->file);
340 return gzflush (file->gzfile, Z_SYNC_FLUSH);
350 return feof (file->file);
353 return gzeof (file->gzfile);
363 Dynamic length version of Qgets. DO NOT free the buffer.
367 Qgetline (QFile *file)
369 static int size = 256;
370 static char *buf = 0;
375 buf = Mem_Alloc(quakeio_mempool, size);
377 if (!Qgets (file, buf, size))
381 while (buf[len - 1] != '\n' && buf[len - 1] != '\r')
383 t = Mem_Alloc(quakeio_mempool, size + 256);
384 memcpy(t, buf, size);
388 if (!Qgets (file, buf + len, size - len))
392 while ((len = strlen(buf)) && (buf[len - 1] == '\n' || buf[len - 1] == '\r'))
397 void QuakeIO_Init(void)
399 quakeio_mempool = Mem_AllocPool("file management");