2013-07-29 07:13:03 -04:00
|
|
|
|
|
|
|
// GZipFile.cpp
|
|
|
|
|
|
|
|
// Implements the cGZipFile class representing a RAII wrapper over zlib's GZip file routines
|
|
|
|
|
|
|
|
#include "Globals.h"
|
|
|
|
#include "GZipFile.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cGZipFile::cGZipFile(void) :
|
|
|
|
m_File(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cGZipFile::~cGZipFile()
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cGZipFile::Open(const AString & a_FileName, eMode a_Mode)
|
|
|
|
{
|
|
|
|
if (m_File != NULL)
|
|
|
|
{
|
|
|
|
ASSERT(!"A file is already open in this object");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
m_File = gzopen(a_FileName.c_str(), (a_Mode == fmRead) ? "r" : "w");
|
|
|
|
m_Mode = a_Mode;
|
|
|
|
return (m_File != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cGZipFile::Close(void)
|
|
|
|
{
|
|
|
|
if (m_File != NULL)
|
|
|
|
{
|
|
|
|
gzclose(m_File);
|
|
|
|
m_File = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int cGZipFile::ReadRestOfFile(AString & a_Contents)
|
|
|
|
{
|
|
|
|
if (m_File == NULL)
|
|
|
|
{
|
|
|
|
ASSERT(!"No file has been opened");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_Mode != fmRead)
|
|
|
|
{
|
|
|
|
ASSERT(!"Bad file mode, cannot read");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since the gzip format doesn't really support getting the uncompressed length, we need to read incrementally. Yuck!
|
|
|
|
int NumBytesRead = 0;
|
2014-03-18 08:54:17 -04:00
|
|
|
int TotalBytes = 0;
|
2013-07-29 07:13:03 -04:00
|
|
|
char Buffer[64 KiB];
|
|
|
|
while ((NumBytesRead = gzread(m_File, Buffer, sizeof(Buffer))) > 0)
|
|
|
|
{
|
2014-03-18 08:54:17 -04:00
|
|
|
TotalBytes += NumBytesRead;
|
2014-04-01 09:00:30 -04:00
|
|
|
a_Contents.append(Buffer, (size_t)NumBytesRead);
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
2014-03-18 08:54:17 -04:00
|
|
|
// NumBytesRead is < 0 on error
|
|
|
|
return (NumBytesRead >= 0) ? TotalBytes : NumBytesRead;
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cGZipFile::Write(const char * a_Contents, int a_Size)
|
|
|
|
{
|
|
|
|
if (m_File == NULL)
|
|
|
|
{
|
|
|
|
ASSERT(!"No file has been opened");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_Mode != fmWrite)
|
|
|
|
{
|
|
|
|
ASSERT(!"Bad file mode, cannot write");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-04-01 09:00:30 -04:00
|
|
|
return (gzwrite(m_File, a_Contents, (unsigned int)a_Size) != 0);
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|