From 86f542a9719c9979c2c647e7941d93827cdfe19e Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Thu, 19 Sep 2002 05:11:42 +0000 Subject: [PATCH] kill the last VFile ref and make the calculate the file size on opening for reading (Qfilesize works only when Qopen is used in read mode). --- include/QF/qendian.h | 18 +++++++++--------- include/QF/quakeio.h | 3 ++- libs/util/quakeio.c | 44 +++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 52 insertions(+), 13 deletions(-) diff --git a/include/QF/qendian.h b/include/QF/qendian.h index 982f8b7ec..61700a91c 100644 --- a/include/QF/qendian.h +++ b/include/QF/qendian.h @@ -69,14 +69,14 @@ float FloatSwap (float f); float FloatNoSwap (float f); // NOTE: these /always/ read and write /little/ endian entities. -struct VFile_s; -void WriteFloat (struct VFile_s *file, float f); -void WriteByte (struct VFile_s *file, int b); -void WriteShort (struct VFile_s *file, unsigned int s); -void WriteLong (struct VFile_s *file, unsigned int l); -float ReadFloat (struct VFile_s *file); -byte ReadByte (struct VFile_s *file); -unsigned short ReadShort (struct VFile_s *file); -unsigned long ReadLong (struct VFile_s *file); +struct QFile_s; +void WriteFloat (struct QFile_s *file, float f); +void WriteByte (struct QFile_s *file, int b); +void WriteShort (struct QFile_s *file, unsigned int s); +void WriteLong (struct QFile_s *file, unsigned int l); +float ReadFloat (struct QFile_s *file); +byte ReadByte (struct QFile_s *file); +unsigned short ReadShort (struct QFile_s *file); +unsigned long ReadLong (struct QFile_s *file); #endif // __qendian_h diff --git a/include/QF/quakeio.h b/include/QF/quakeio.h index ad2e9dd0a..9e3a12864 100644 --- a/include/QF/quakeio.h +++ b/include/QF/quakeio.h @@ -34,10 +34,11 @@ #include "QF/gcc_attr.h" -typedef struct VFile_s QFile; +typedef struct QFile_s QFile; void Qexpand_squiggle(const char *path, char *dest); int Qrename(const char *old, const char *new); +int Qfilesize (QFile *file); QFile *Qopen(const char *path, const char *mode); QFile *Qdopen(int fd, const char *mode); void Qclose(QFile *file); diff --git a/libs/util/quakeio.c b/libs/util/quakeio.c index 00ac583f1..7a8293539 100644 --- a/libs/util/quakeio.c +++ b/libs/util/quakeio.c @@ -41,15 +41,17 @@ static const char rcsid[] = #ifdef HAVE_STRINGS_H # include #endif -#ifdef WIN32 +#ifdef HAVE_IO_H # include -# include #else # include #endif #ifdef HAVE_UNISTD_H # include #endif +#include +#include +#include #ifdef _MSC_VER # define _POSIX_ #endif @@ -59,6 +61,7 @@ static const char rcsid[] = #include #include "QF/dstring.h" +#include "QF/qendian.h" #include "QF/quakefs.h" #include "QF/quakeio.h" @@ -69,11 +72,12 @@ static const char rcsid[] = # endif #endif -struct VFile_s { +struct QFile_s { FILE *file; #ifdef HAVE_ZLIB gzFile *gzfile; #endif + off_t size; }; @@ -123,12 +127,20 @@ Qrename (const char *old, const char *new) return rename (e_old, e_new); } +int +Qfilesize (QFile *file) +{ + return file->size; +} + QFile * Qopen (const char *path, const char *mode) { QFile *file; char m[80], *p; + int reading = 0; int zip = 0; + int size = -1; char e_path[PATH_MAX]; Qexpand_squiggle (path, e_path); @@ -139,6 +151,9 @@ Qopen (const char *path, const char *mode) zip = 1; continue; } + if (*mode == 'r') { + reading = 1; + } #ifndef HAVE_ZLIB if (strchr ("0123456789fh", *mode)) { continue; @@ -148,9 +163,32 @@ Qopen (const char *path, const char *mode) } *p = 0; + if (reading) { + int fd = open (path, O_RDONLY); + if (fd != -1) { + size = lseek (fd, 0, SEEK_END); + lseek (fd, 0, SEEK_SET); +#ifdef HAVE_ZLIB + if (zip) { + byte id[2]; + + read (fd, id, 2); + if (id[0] == 0x1f && id[1] == 0x8b) { + lseek (fd, -4, SEEK_END); + read (fd, &size, 4); + size = LittleLong (size); + } + lseek (fd, 0, SEEK_SET); + } +#endif + close (fd); + } + } + file = calloc (sizeof (*file), 1); if (!file) return 0; + file->size = size; #ifdef HAVE_ZLIB if (zip) { file->gzfile = gzopen (path, m);