2001-02-19 21:15:25 +00:00
|
|
|
/*
|
|
|
|
quakeio.c
|
|
|
|
|
|
|
|
(description)
|
|
|
|
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
Copyright (C) 1999,2000 contributors of the QuakeForge project
|
|
|
|
Please see the file "AUTHORS" for a list of contributors
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2003-01-15 15:31:36 +00:00
|
|
|
|
|
|
|
static __attribute__ ((unused)) const char rcsid[] =
|
|
|
|
"$Id$";
|
|
|
|
|
2001-05-31 05:33:13 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
# include <zlib.h>
|
|
|
|
#endif
|
2001-02-19 21:15:25 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
2002-09-19 05:11:42 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2001-02-19 21:15:25 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
# define _POSIX_
|
|
|
|
#endif
|
|
|
|
|
2001-08-22 22:03:16 +00:00
|
|
|
#include <limits.h>
|
2001-02-19 21:15:25 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
2002-09-27 05:46:30 +00:00
|
|
|
#include <errno.h>
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2002-03-08 23:11:42 +00:00
|
|
|
#include "QF/dstring.h"
|
2002-09-19 05:11:42 +00:00
|
|
|
#include "QF/qendian.h"
|
2002-08-27 07:16:28 +00:00
|
|
|
#include "QF/quakefs.h"
|
|
|
|
#include "QF/quakeio.h"
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
# ifndef __BORLANDC__
|
|
|
|
# define setmode _setmode
|
|
|
|
# define O_BINARY _O_BINARY
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2002-09-19 05:11:42 +00:00
|
|
|
struct QFile_s {
|
2001-05-31 05:33:13 +00:00
|
|
|
FILE *file;
|
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
gzFile *gzfile;
|
|
|
|
#endif
|
2002-09-19 05:11:42 +00:00
|
|
|
off_t size;
|
2002-09-27 04:27:19 +00:00
|
|
|
off_t start;
|
2003-04-10 21:11:40 +00:00
|
|
|
int c;
|
2001-05-31 05:33:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
int
|
|
|
|
Qrename (const char *old, const char *new)
|
|
|
|
{
|
2003-05-23 17:17:01 +00:00
|
|
|
return rename (old, new);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
2002-10-16 04:59:34 +00:00
|
|
|
int
|
|
|
|
Qremove (const char *path)
|
|
|
|
{
|
2003-05-23 17:17:01 +00:00
|
|
|
return remove (path);
|
2002-10-16 04:59:34 +00:00
|
|
|
}
|
|
|
|
|
2002-09-19 05:11:42 +00:00
|
|
|
int
|
|
|
|
Qfilesize (QFile *file)
|
|
|
|
{
|
|
|
|
return file->size;
|
|
|
|
}
|
|
|
|
|
2002-09-27 04:27:19 +00:00
|
|
|
static int
|
|
|
|
check_file (int fd, int offs, int len, int *zip)
|
|
|
|
{
|
|
|
|
unsigned char id[2], len_bytes[4];
|
|
|
|
|
|
|
|
if (offs < 0 || len < 0) {
|
|
|
|
// normal file
|
|
|
|
offs = 0;
|
|
|
|
len = lseek (fd, 0, SEEK_END);
|
|
|
|
lseek (fd, 0, SEEK_SET);
|
|
|
|
}
|
|
|
|
if (*zip) {
|
2003-04-10 16:11:09 +00:00
|
|
|
int r;
|
|
|
|
|
2002-09-27 04:27:19 +00:00
|
|
|
lseek (fd, offs, SEEK_SET);
|
2003-04-10 16:11:09 +00:00
|
|
|
r = read (fd, id, 2);
|
|
|
|
if (r == 2 && id[0] == 0x1f && id[1] == 0x8b && len >= 6) {
|
2002-09-27 04:27:19 +00:00
|
|
|
lseek (fd, offs + len - 4, SEEK_SET);
|
|
|
|
read (fd, len_bytes, 4);
|
|
|
|
len = ((len_bytes[3] << 24)
|
|
|
|
| (len_bytes[2] << 16)
|
|
|
|
| (len_bytes[1] << 8)
|
|
|
|
| (len_bytes[0]));
|
|
|
|
} else {
|
|
|
|
*zip = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lseek (fd, offs, SEEK_SET);
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2002-08-27 07:16:28 +00:00
|
|
|
QFile *
|
2001-02-19 21:15:25 +00:00
|
|
|
Qopen (const char *path, const char *mode)
|
|
|
|
{
|
2002-08-27 07:16:28 +00:00
|
|
|
QFile *file;
|
2003-07-08 22:39:45 +00:00
|
|
|
char *m, *p;
|
2002-09-19 05:11:42 +00:00
|
|
|
int reading = 0;
|
2001-02-19 21:15:25 +00:00
|
|
|
int zip = 0;
|
2002-09-19 05:11:42 +00:00
|
|
|
int size = -1;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2003-07-08 22:39:45 +00:00
|
|
|
m = alloca (strlen (mode) + 1);
|
2003-04-17 00:01:48 +00:00
|
|
|
for (p = m; *mode && p - m < ((int) sizeof (m) - 1); mode++) {
|
2001-02-19 21:15:25 +00:00
|
|
|
if (*mode == 'z') {
|
|
|
|
zip = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2002-09-19 05:11:42 +00:00
|
|
|
if (*mode == 'r') {
|
|
|
|
reading = 1;
|
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
#ifndef HAVE_ZLIB
|
|
|
|
if (strchr ("0123456789fh", *mode)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
*p++ = *mode;
|
|
|
|
}
|
|
|
|
*p = 0;
|
|
|
|
|
2002-09-19 05:11:42 +00:00
|
|
|
if (reading) {
|
|
|
|
int fd = open (path, O_RDONLY);
|
|
|
|
if (fd != -1) {
|
2002-09-27 04:27:19 +00:00
|
|
|
size = check_file (fd, -1, -1, &zip);
|
2002-09-19 05:11:42 +00:00
|
|
|
close (fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
file = calloc (sizeof (*file), 1);
|
|
|
|
if (!file)
|
|
|
|
return 0;
|
2002-09-19 05:11:42 +00:00
|
|
|
file->size = size;
|
2001-02-19 21:15:25 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
if (zip) {
|
|
|
|
file->gzfile = gzopen (path, m);
|
|
|
|
if (!file->gzfile) {
|
|
|
|
free (file);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
file->file = fopen (path, m);
|
|
|
|
if (!file->file) {
|
|
|
|
free (file);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2003-04-10 21:11:40 +00:00
|
|
|
file->c = -1;
|
2001-02-19 21:15:25 +00:00
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2002-08-27 07:16:28 +00:00
|
|
|
QFile *
|
2001-02-19 21:15:25 +00:00
|
|
|
Qdopen (int fd, const char *mode)
|
|
|
|
{
|
2002-08-27 07:16:28 +00:00
|
|
|
QFile *file;
|
2003-07-08 22:39:45 +00:00
|
|
|
char *m, *p;
|
2001-02-19 21:15:25 +00:00
|
|
|
int zip = 0;
|
|
|
|
|
2003-07-08 22:39:45 +00:00
|
|
|
m = alloca (strlen (mode) + 1);
|
2002-09-27 04:27:19 +00:00
|
|
|
#ifdef WIN32
|
2002-09-30 16:40:06 +00:00
|
|
|
setmode (fd, O_BINARY);
|
2002-09-27 04:27:19 +00:00
|
|
|
#endif
|
2003-04-17 00:01:48 +00:00
|
|
|
for (p = m; *mode && p - m < ((int) sizeof (m) - 1); mode++) {
|
2001-02-19 21:15:25 +00:00
|
|
|
if (*mode == 'z') {
|
|
|
|
zip = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*p++ = *mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p = 0;
|
|
|
|
|
|
|
|
file = calloc (sizeof (*file), 1);
|
|
|
|
if (!file)
|
|
|
|
return 0;
|
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
if (zip) {
|
|
|
|
file->gzfile = gzdopen (fd, m);
|
|
|
|
if (!file->gzfile) {
|
|
|
|
free (file);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
file->file = fdopen (fd, m);
|
|
|
|
if (!file->file) {
|
|
|
|
free (file);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2003-04-10 21:11:40 +00:00
|
|
|
file->c = -1;
|
2002-09-27 04:27:19 +00:00
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
QFile *
|
|
|
|
Qsubopen (const char *path, int offs, int len, int zip)
|
|
|
|
{
|
|
|
|
int fd = open (path, O_RDONLY);
|
|
|
|
QFile *file;
|
|
|
|
|
|
|
|
if (fd == -1)
|
|
|
|
return 0;
|
2001-02-19 21:15:25 +00:00
|
|
|
#ifdef WIN32
|
2002-09-27 04:27:19 +00:00
|
|
|
setmode (fd, O_BINARY);
|
2001-02-19 21:15:25 +00:00
|
|
|
#endif
|
2002-09-27 04:27:19 +00:00
|
|
|
|
|
|
|
len = check_file (fd, offs, len, &zip);
|
|
|
|
file = Qdopen (fd, zip ? "rbz" : "rb");
|
|
|
|
file->size = len;
|
|
|
|
file->start = offs;
|
2001-02-19 21:15:25 +00:00
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-08-27 07:16:28 +00:00
|
|
|
Qclose (QFile *file)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
if (file->file)
|
|
|
|
fclose (file->file);
|
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
else
|
|
|
|
gzclose (file->gzfile);
|
|
|
|
#endif
|
|
|
|
free (file);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2002-08-27 07:16:28 +00:00
|
|
|
Qread (QFile *file, void *buf, int count)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2003-04-10 21:11:40 +00:00
|
|
|
int offs = 0;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (file->c != -1) {
|
|
|
|
char *b = buf;
|
|
|
|
*b++ = file->c;
|
|
|
|
buf = b;
|
|
|
|
offs = 1;
|
|
|
|
file->c = -1;
|
|
|
|
count--;
|
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
if (file->file)
|
2003-04-10 21:11:40 +00:00
|
|
|
ret = fread (buf, 1, count, file->file);
|
2001-02-19 21:15:25 +00:00
|
|
|
else
|
2003-04-10 21:11:40 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
ret = gzread (file->gzfile, buf, count);
|
2001-02-19 21:15:25 +00:00
|
|
|
#else
|
2003-04-10 21:11:40 +00:00
|
|
|
return -1;
|
2001-02-19 21:15:25 +00:00
|
|
|
#endif
|
2003-04-10 21:11:40 +00:00
|
|
|
return ret == -1 ? ret : ret + offs;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2002-08-27 07:16:28 +00:00
|
|
|
Qwrite (QFile *file, const void *buf, int count)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
if (file->file)
|
|
|
|
return fwrite (buf, 1, count, file->file);
|
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
else
|
2001-07-22 18:38:59 +00:00
|
|
|
return gzwrite (file->gzfile, (const voidp)buf, count);
|
2001-02-19 21:15:25 +00:00
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2002-08-27 07:16:28 +00:00
|
|
|
Qprintf (QFile *file, const char *fmt, ...)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
va_start (args, fmt);
|
|
|
|
if (file->file)
|
|
|
|
ret = vfprintf (file->file, fmt, args);
|
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
else {
|
2002-03-08 23:11:42 +00:00
|
|
|
static dstring_t *buf;
|
|
|
|
|
|
|
|
if (!buf)
|
|
|
|
buf = dstring_new ();
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
va_start (args, fmt);
|
2002-03-08 23:11:42 +00:00
|
|
|
dvsprintf (buf, fmt, args);
|
2001-02-19 21:15:25 +00:00
|
|
|
va_end (args);
|
2002-03-08 23:11:42 +00:00
|
|
|
ret = strlen (buf->str);
|
2001-02-19 21:15:25 +00:00
|
|
|
if (ret > 0)
|
|
|
|
ret = gzwrite (file->gzfile, buf, (unsigned) ret);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
va_end (args);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2002-08-07 18:43:35 +00:00
|
|
|
int
|
2002-08-27 07:16:28 +00:00
|
|
|
Qputs (QFile *file, const char *buf)
|
2002-08-07 18:43:35 +00:00
|
|
|
{
|
|
|
|
if (file->file)
|
|
|
|
return fputs (buf, file->file);
|
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
else
|
|
|
|
return gzputs (file->gzfile, buf);
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
char *
|
2002-08-27 07:16:28 +00:00
|
|
|
Qgets (QFile *file, char *buf, int count)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2003-04-10 21:11:40 +00:00
|
|
|
char *ret = buf;
|
|
|
|
|
|
|
|
if (file->c != -1) {
|
|
|
|
*buf++ = file->c;
|
|
|
|
count--;
|
|
|
|
file->c = -1;
|
|
|
|
if (!count)
|
|
|
|
return ret;
|
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
if (file->file)
|
2003-04-10 21:11:40 +00:00
|
|
|
buf = fgets (buf, count, file->file);
|
2001-02-19 21:15:25 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
else
|
2003-04-10 21:11:40 +00:00
|
|
|
buf = gzgets (file->gzfile, buf, count);
|
2001-02-19 21:15:25 +00:00
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
2003-04-10 21:11:40 +00:00
|
|
|
return buf ? ret : 0;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2002-08-27 07:16:28 +00:00
|
|
|
Qgetc (QFile *file)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2003-04-10 21:11:40 +00:00
|
|
|
if (file->c != -1) {
|
|
|
|
int c = file->c;
|
|
|
|
file->c = -1;
|
|
|
|
return c;
|
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
if (file->file)
|
|
|
|
return fgetc (file->file);
|
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
else
|
|
|
|
return gzgetc (file->gzfile);
|
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2002-08-27 07:16:28 +00:00
|
|
|
Qputc (QFile *file, int c)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
if (file->file)
|
|
|
|
return fputc (c, file->file);
|
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
else
|
|
|
|
return gzputc (file->gzfile, c);
|
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-04-10 21:11:40 +00:00
|
|
|
int
|
|
|
|
Qungetc (QFile *file, int c)
|
|
|
|
{
|
|
|
|
if (file->c == -1)
|
|
|
|
file->c = (byte) c;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
int
|
2002-08-27 07:16:28 +00:00
|
|
|
Qseek (QFile *file, long offset, int whence)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2003-04-10 21:11:40 +00:00
|
|
|
file->c = -1;
|
2002-09-27 04:27:19 +00:00
|
|
|
if (file->file) {
|
2002-09-27 05:46:30 +00:00
|
|
|
int res;
|
|
|
|
switch (whence) {
|
|
|
|
case SEEK_SET:
|
|
|
|
res = fseek (file->file, file->start + offset, whence);
|
|
|
|
break;
|
|
|
|
case SEEK_CUR:
|
|
|
|
res = fseek (file->file, offset, whence);
|
|
|
|
break;
|
|
|
|
case SEEK_END:
|
2002-10-24 22:36:15 +00:00
|
|
|
if (file->size == -1) {
|
|
|
|
// we don't know the size (due to writing) so punt and
|
|
|
|
// pass on the request as-is
|
|
|
|
res = fseek (file->file, offset, SEEK_END);
|
|
|
|
} else {
|
|
|
|
res = fseek (file->file,
|
|
|
|
file->start + file->size - offset, SEEK_SET);
|
|
|
|
}
|
2002-09-27 05:46:30 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (res != -1)
|
|
|
|
res = ftell (file->file) - file->start;
|
|
|
|
return res;
|
2002-09-27 04:27:19 +00:00
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
2002-09-27 04:27:19 +00:00
|
|
|
else {
|
|
|
|
// libz seems to keep track of the true start position itself
|
2002-09-27 05:46:30 +00:00
|
|
|
// doesn't support SEEK_END, though
|
2001-02-19 21:15:25 +00:00
|
|
|
return gzseek (file->gzfile, offset, whence);
|
2002-09-27 04:27:19 +00:00
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
long
|
2002-08-27 07:16:28 +00:00
|
|
|
Qtell (QFile *file)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2003-04-10 21:11:40 +00:00
|
|
|
int offs;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
offs = (file->c != -1) ? 1 : 0;
|
2001-02-19 21:15:25 +00:00
|
|
|
if (file->file)
|
2003-04-10 21:11:40 +00:00
|
|
|
ret = ftell (file->file) - file->start;
|
2001-02-19 21:15:25 +00:00
|
|
|
else
|
2003-04-10 21:11:40 +00:00
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
ret = gztell (file->gzfile); //FIXME does gztell do the right thing?
|
2001-02-19 21:15:25 +00:00
|
|
|
#else
|
2003-04-10 21:11:40 +00:00
|
|
|
return -1;
|
2001-02-19 21:15:25 +00:00
|
|
|
#endif
|
2003-04-10 21:11:40 +00:00
|
|
|
return ret == -1 ? ret : ret - offs;
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2002-08-27 07:16:28 +00:00
|
|
|
Qflush (QFile *file)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
if (file->file)
|
|
|
|
return fflush (file->file);
|
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
else
|
|
|
|
return gzflush (file->gzfile, Z_SYNC_FLUSH);
|
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2002-08-27 07:16:28 +00:00
|
|
|
Qeof (QFile *file)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2003-04-10 21:11:40 +00:00
|
|
|
if (file->c != -1)
|
|
|
|
return 0;
|
2001-02-19 21:15:25 +00:00
|
|
|
if (file->file)
|
|
|
|
return feof (file->file);
|
|
|
|
#ifdef HAVE_ZLIB
|
|
|
|
else
|
|
|
|
return gzeof (file->gzfile);
|
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Qgetline
|
|
|
|
|
|
|
|
Dynamic length version of Qgets. DO NOT free the buffer.
|
|
|
|
*/
|
2001-07-15 07:04:17 +00:00
|
|
|
const char *
|
2002-08-27 07:16:28 +00:00
|
|
|
Qgetline (QFile *file)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
static int size = 256;
|
|
|
|
static char *buf = 0;
|
|
|
|
int len;
|
|
|
|
|
2001-10-24 22:50:06 +00:00
|
|
|
if (!buf) {
|
2001-02-19 21:15:25 +00:00
|
|
|
buf = malloc (size);
|
2001-10-24 22:50:06 +00:00
|
|
|
if (!buf)
|
|
|
|
return 0;
|
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
if (!Qgets (file, buf, size))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
len = strlen (buf);
|
|
|
|
while (buf[len - 1] != '\n') {
|
|
|
|
char *t = realloc (buf, size + 256);
|
|
|
|
|
|
|
|
if (!t)
|
|
|
|
return 0;
|
|
|
|
buf = t;
|
|
|
|
size += 256;
|
|
|
|
if (!Qgets (file, buf + len, size - len))
|
|
|
|
break;
|
|
|
|
len = strlen (buf);
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|