2006-02-24 04:48:15 +00:00
|
|
|
#ifndef FILES_H
|
|
|
|
#define FILES_H
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <zlib.h>
|
2009-03-10 23:07:37 +00:00
|
|
|
#include "bzlib.h"
|
2006-02-24 04:48:15 +00:00
|
|
|
#include "doomtype.h"
|
|
|
|
#include "m_swap.h"
|
|
|
|
|
2010-04-17 20:49:18 +00:00
|
|
|
class FileReaderBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~FileReaderBase() {}
|
|
|
|
virtual long Read (void *buffer, long len) = 0;
|
|
|
|
|
|
|
|
FileReaderBase &operator>> (BYTE &v)
|
|
|
|
{
|
|
|
|
Read (&v, 1);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileReaderBase &operator>> (SBYTE &v)
|
|
|
|
{
|
|
|
|
Read (&v, 1);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileReaderBase &operator>> (WORD &v)
|
|
|
|
{
|
|
|
|
Read (&v, 2);
|
|
|
|
v = LittleShort(v);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileReaderBase &operator>> (SWORD &v)
|
|
|
|
{
|
|
|
|
Read (&v, 2);
|
|
|
|
v = LittleShort(v);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileReaderBase &operator>> (DWORD &v)
|
|
|
|
{
|
|
|
|
Read (&v, 4);
|
|
|
|
v = LittleLong(v);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileReaderBase &operator>> (fixed_t &v)
|
|
|
|
{
|
|
|
|
Read (&v, 4);
|
|
|
|
v = LittleLong(v);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class FileReader : public FileReaderBase
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
|
|
|
public:
|
2008-04-12 21:36:10 +00:00
|
|
|
FileReader ();
|
2006-02-24 04:48:15 +00:00
|
|
|
FileReader (const char *filename);
|
|
|
|
FileReader (FILE *file);
|
|
|
|
FileReader (FILE *file, long length);
|
2008-04-12 21:36:10 +00:00
|
|
|
bool Open (const char *filename);
|
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; }
|
2009-04-23 22:49:38 +00:00
|
|
|
virtual const char *GetBuffer() const { return NULL; }
|
2006-02-24 04:48:15 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
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
|
2010-04-17 20:49:18 +00:00
|
|
|
class FileReaderZ : public FileReaderBase
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
|
|
|
public:
|
2006-04-11 16:27:41 +00:00
|
|
|
FileReaderZ (FileReader &file, bool zip=false);
|
2006-02-24 04:48:15 +00:00
|
|
|
~FileReaderZ ();
|
|
|
|
|
2010-04-17 20:49:18 +00:00
|
|
|
virtual long Read (void *buffer, long len);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2009-03-10 23:07:37 +00:00
|
|
|
// Wraps around a FileReader to decompress a bzip2 stream
|
2010-04-17 20:49:18 +00:00
|
|
|
class FileReaderBZ2 : public FileReaderBase
|
2009-03-10 23:07:37 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
FileReaderBZ2 (FileReader &file);
|
|
|
|
~FileReaderBZ2 ();
|
|
|
|
|
|
|
|
long Read (void *buffer, long len);
|
|
|
|
|
|
|
|
FileReaderBZ2 &operator>> (BYTE &v)
|
|
|
|
{
|
|
|
|
Read (&v, 1);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileReaderBZ2 &operator>> (SBYTE &v)
|
|
|
|
{
|
|
|
|
Read (&v, 1);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileReaderBZ2 &operator>> (WORD &v)
|
|
|
|
{
|
|
|
|
Read (&v, 2);
|
|
|
|
v = LittleShort(v);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileReaderBZ2 &operator>> (SWORD &v)
|
|
|
|
{
|
|
|
|
Read (&v, 2);
|
|
|
|
v = LittleShort(v);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileReaderBZ2 &operator>> (DWORD &v)
|
|
|
|
{
|
|
|
|
Read (&v, 4);
|
|
|
|
v = LittleLong(v);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileReaderBZ2 &operator>> (fixed_t &v)
|
|
|
|
{
|
|
|
|
Read (&v, 4);
|
|
|
|
v = LittleLong(v);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum { BUFF_SIZE = 4096 };
|
|
|
|
|
|
|
|
FileReader &File;
|
|
|
|
bool SawEOF;
|
|
|
|
bz_stream Stream;
|
|
|
|
BYTE InBuff[BUFF_SIZE];
|
|
|
|
|
|
|
|
void FillBuffer ();
|
|
|
|
|
|
|
|
FileReaderBZ2 &operator= (const FileReaderBZ2 &) { return *this; }
|
|
|
|
};
|
|
|
|
|
|
|
|
// Wraps around a FileReader to decompress a lzma stream
|
2010-04-17 20:49:18 +00:00
|
|
|
class FileReaderLZMA : public FileReaderBase
|
2009-03-10 23:07:37 +00:00
|
|
|
{
|
2014-06-26 07:43:51 +00:00
|
|
|
struct StreamPointer;
|
|
|
|
|
2009-03-10 23:07:37 +00:00
|
|
|
public:
|
|
|
|
FileReaderLZMA (FileReader &file, size_t uncompressed_size, bool zip);
|
|
|
|
~FileReaderLZMA ();
|
|
|
|
|
|
|
|
long Read (void *buffer, long len);
|
|
|
|
|
|
|
|
FileReaderLZMA &operator>> (BYTE &v)
|
|
|
|
{
|
|
|
|
Read (&v, 1);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileReaderLZMA &operator>> (SBYTE &v)
|
|
|
|
{
|
|
|
|
Read (&v, 1);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileReaderLZMA &operator>> (WORD &v)
|
|
|
|
{
|
|
|
|
Read (&v, 2);
|
|
|
|
v = LittleShort(v);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileReaderLZMA &operator>> (SWORD &v)
|
|
|
|
{
|
|
|
|
Read (&v, 2);
|
|
|
|
v = LittleShort(v);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileReaderLZMA &operator>> (DWORD &v)
|
|
|
|
{
|
|
|
|
Read (&v, 4);
|
|
|
|
v = LittleLong(v);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileReaderLZMA &operator>> (fixed_t &v)
|
|
|
|
{
|
|
|
|
Read (&v, 4);
|
|
|
|
v = LittleLong(v);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum { BUFF_SIZE = 4096 };
|
|
|
|
|
|
|
|
FileReader &File;
|
|
|
|
bool SawEOF;
|
2014-06-26 07:43:51 +00:00
|
|
|
StreamPointer *Streamp; // anonymous pointer to LKZA decoder struct - to avoid including the LZMA headers globally
|
2009-03-10 23:07:37 +00:00
|
|
|
size_t Size;
|
|
|
|
size_t InPos, InSize;
|
|
|
|
size_t OutProcessed;
|
|
|
|
BYTE InBuff[BUFF_SIZE];
|
|
|
|
|
|
|
|
void FillBuffer ();
|
|
|
|
|
|
|
|
FileReaderLZMA &operator= (const FileReaderLZMA &) { return *this; }
|
|
|
|
};
|
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);
|
2009-04-23 22:49:38 +00:00
|
|
|
virtual const char *GetBuffer() const { return bufptr; }
|
2006-04-11 16:27:41 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
const char * bufptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
#endif
|