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
58 # define setmode _setmode
59 # define O_BINARY _O_BINARY
63 mempool_t *quakeio_mempool;
66 Qexpand_squiggle (const char *path, char *dest)
71 struct passwd *pwd_ent;
74 if (strncmp (path, "~/", 2) != 0) {
80 // LordHavoc: first check HOME to duplicate previous version behavior
81 // (also handy if someone wants it somewhere other than their
83 home = getenv ("HOME");
84 if (!home || !home[0])
85 home = getenv ("WINDIR");
87 if ((pwd_ent = getpwuid (getuid ()))) {
88 home = pwd_ent->pw_dir;
90 home = getenv ("HOME");
95 strncat (dest, path + 1, MAX_OSPATH - strlen (dest)); // skip
102 Qrename (const char *old, const char *new)
104 char e_old[PATH_MAX];
105 char e_new[PATH_MAX];
107 Qexpand_squiggle (old, e_old);
108 Qexpand_squiggle (new, e_new);
109 return rename (e_old, e_new);
113 Qopen (const char *path, const char *mode)
118 char e_path[PATH_MAX];
120 Qexpand_squiggle (path, e_path);
123 for (p = m; *mode && p - m < (sizeof (m) - 1); mode++) {
129 if (strchr ("0123456789fh", *mode)) {
137 file = Mem_Alloc(quakeio_mempool, sizeof (*file));
138 memset(file, 0, sizeof(*file));
143 file->gzfile = gzopen (path, m);
151 file->file = fopen (path, m);
161 Qdopen (int fd, const char *mode)
167 for (p = m; *mode && p - m < (sizeof (m) - 1); mode++) {
177 file = Mem_Alloc(quakeio_mempool, sizeof (*file));
178 memset(file, 0, sizeof(*file));
183 file->gzfile = gzdopen (fd, m);
191 file->file = fdopen (fd, m);
199 setmode (_fileno (file->file), O_BINARY);
211 gzclose (file->gzfile);
217 Qread (QFile *file, void *buf, int count)
220 return fread (buf, 1, count, file->file);
223 return gzread (file->gzfile, buf, count);
230 Qwrite (QFile *file, void *buf, int count)
233 return fwrite (buf, 1, count, file->file);
236 return gzwrite (file->gzfile, buf, count);
243 Qprintf (QFile *file, const char *fmt, ...)
248 va_start (args, fmt);
250 ret = vfprintf (file->file, fmt, args);
255 va_start (args, fmt);
256 #ifdef HAVE_VSNPRINTF
257 (void) vsnprintf (buf, sizeof (buf), fmt, args);
259 (void) vsprintf (buf, fmt, args);
262 ret = strlen (buf); /* some *snprintf don't return the nb
265 ret = gzwrite (file->gzfile, buf, (unsigned) ret);
273 Qgets (QFile *file, char *buf, int count)
276 return fgets (buf, count, file->file);
279 return gzgets (file->gzfile, buf, count);
289 return fgetc (file->file);
292 return gzgetc (file->gzfile);
299 Qputc (QFile *file, int c)
302 return fputc (c, file->file);
305 return gzputc (file->gzfile, c);
312 Qseek (QFile *file, long offset, int whence)
315 return fseek (file->file, offset, whence);
318 return gzseek (file->gzfile, offset, whence);
328 return ftell (file->file);
331 return gztell (file->gzfile);
341 return fflush (file->file);
344 return gzflush (file->gzfile, Z_SYNC_FLUSH);
354 return feof (file->file);
357 return gzeof (file->gzfile);
367 Dynamic length version of Qgets. DO NOT free the buffer.
371 Qgetline (QFile *file)
373 static int size = 256;
374 static char *buf = 0;
379 buf = Mem_Alloc(quakeio_mempool, size);
381 if (!Qgets (file, buf, size))
385 while (buf[len - 1] != '\n' && buf[len - 1] != '\r')
387 t = Mem_Alloc(quakeio_mempool, size + 256);
388 memcpy(t, buf, size);
392 if (!Qgets (file, buf + len, size - len))
396 while ((len = strlen(buf)) && (buf[len - 1] == '\n' || buf[len - 1] == '\r'))
401 void QuakeIO_Init(void)
403 quakeio_mempool = Mem_AllocPool("file management");