2006-02-24 04:48:15 +00:00
|
|
|
#ifndef FILES_H
|
|
|
|
#define FILES_H
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <zlib.h>
|
|
|
|
#include "doomtype.h"
|
|
|
|
#include "m_swap.h"
|
|
|
|
|
|
|
|
class FileReader
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FileReader (const char *filename);
|
|
|
|
FileReader (FILE *file);
|
|
|
|
FileReader (FILE *file, long length);
|
2006-04-11 16:27:41 +00:00
|
|
|
virtual ~FileReader ();
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2006-04-11 16:27:41 +00:00
|
|
|
virtual long Tell () const;
|
|
|
|
virtual long Seek (long offset, int origin);
|
|
|
|
virtual long Read (void *buffer, long len);
|
2008-04-11 10:16:29 +00:00
|
|
|
virtual char *Gets(char *strbuf, int len);
|
2006-02-24 04:48:15 +00:00
|
|
|
long GetLength () const { return Length; }
|
|
|
|
|
|
|
|
// If you use the underlying FILE without going through this class,
|
|
|
|
// you must call ResetFilePtr() before using this class again.
|
|
|
|
void ResetFilePtr ();
|
|
|
|
|
|
|
|
FILE *GetFile () const { return File; }
|
|
|
|
|
|
|
|
FileReader &operator>> (BYTE &v)
|
|
|
|
{
|
|
|
|
Read (&v, 1);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileReader &operator>> (SBYTE &v)
|
|
|
|
{
|
|
|
|
Read (&v, 1);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileReader &operator>> (WORD &v)
|
|
|
|
{
|
|
|
|
Read (&v, 2);
|
|
|
|
v = LittleShort(v);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileReader &operator>> (SWORD &v)
|
|
|
|
{
|
|
|
|
Read (&v, 2);
|
|
|
|
v = LittleShort(v);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileReader &operator>> (DWORD &v)
|
|
|
|
{
|
|
|
|
Read (&v, 4);
|
|
|
|
v = LittleLong(v);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2008-04-11 10:16:29 +00:00
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
protected:
|
|
|
|
FileReader (const FileReader &other, long length);
|
|
|
|
FileReader ();
|
|
|
|
|
2008-04-11 10:16:29 +00:00
|
|
|
char *GetsFromBuffer(const char * bufptr, char *strbuf, int len);
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
FILE *File;
|
|
|
|
long Length;
|
|
|
|
long StartPos;
|
|
|
|
long FilePos;
|
|
|
|
|
|
|
|
private:
|
|
|
|
long CalcFileLen () const;
|
2006-04-11 16:27:41 +00:00
|
|
|
protected:
|
2006-02-24 04:48:15 +00:00
|
|
|
bool CloseOnDestruct;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Wraps around a FileReader to decompress a zlib stream
|
|
|
|
class FileReaderZ
|
|
|
|
{
|
|
|
|
public:
|
2006-04-11 16:27:41 +00:00
|
|
|
FileReaderZ (FileReader &file, bool zip=false);
|
2006-02-24 04:48:15 +00:00
|
|
|
~FileReaderZ ();
|
|
|
|
|
|
|
|
long Read (void *buffer, long len);
|
|
|
|
|
|
|
|
FileReaderZ &operator>> (BYTE &v)
|
|
|
|
{
|
|
|
|
Read (&v, 1);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileReaderZ &operator>> (SBYTE &v)
|
|
|
|
{
|
|
|
|
Read (&v, 1);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileReaderZ &operator>> (WORD &v)
|
|
|
|
{
|
|
|
|
Read (&v, 2);
|
|
|
|
v = LittleShort(v);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileReaderZ &operator>> (SWORD &v)
|
|
|
|
{
|
|
|
|
Read (&v, 2);
|
|
|
|
v = LittleShort(v);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileReaderZ &operator>> (DWORD &v)
|
|
|
|
{
|
|
|
|
Read (&v, 4);
|
|
|
|
v = LittleLong(v);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileReaderZ &operator>> (fixed_t &v)
|
|
|
|
{
|
|
|
|
Read (&v, 4);
|
|
|
|
v = LittleLong(v);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum { BUFF_SIZE = 4096 };
|
|
|
|
|
|
|
|
FileReader &File;
|
|
|
|
bool SawEOF;
|
|
|
|
z_stream Stream;
|
|
|
|
BYTE InBuff[BUFF_SIZE];
|
|
|
|
|
|
|
|
void FillBuffer ();
|
2006-12-21 04:34:43 +00:00
|
|
|
|
|
|
|
FileReaderZ &operator= (const FileReaderZ &) { return *this; }
|
2006-02-24 04:48:15 +00:00
|
|
|
};
|
|
|
|
|
2006-04-11 16:27:41 +00:00
|
|
|
|
|
|
|
class MemoryReader : public FileReader
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MemoryReader (const char *buffer, long length);
|
|
|
|
~MemoryReader ();
|
|
|
|
|
|
|
|
virtual long Tell () const;
|
|
|
|
virtual long Seek (long offset, int origin);
|
|
|
|
virtual long Read (void *buffer, long len);
|
2008-04-11 10:16:29 +00:00
|
|
|
virtual char *Gets(char *strbuf, int len);
|
2006-04-11 16:27:41 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
const char * bufptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
#endif
|