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).
This commit is contained in:
Bill Currie 2002-09-19 05:11:42 +00:00
parent 48f53e1c88
commit 86f542a971
3 changed files with 52 additions and 13 deletions

View file

@ -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

View file

@ -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);

View file

@ -41,15 +41,17 @@ static const char rcsid[] =
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#ifdef WIN32
#ifdef HAVE_IO_H
# include <io.h>
# include <fcntl.h>
#else
# include <pwd.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifdef _MSC_VER
# define _POSIX_
#endif
@ -59,6 +61,7 @@ static const char rcsid[] =
#include <stdlib.h>
#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);