forked from 0ad/0ad
now stores its data in buffer, rather than copying to std::vector
This was SVN commit r1168.
This commit is contained in:
parent
a0f0215c15
commit
69e9f3ef79
@ -13,8 +13,12 @@
|
|||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
// CFileUnpacker constructor
|
// CFileUnpacker constructor
|
||||||
CFileUnpacker::CFileUnpacker() : m_UnpackPos(0), m_Version(0)
|
CFileUnpacker::CFileUnpacker()
|
||||||
{
|
{
|
||||||
|
m_Buf = 0;
|
||||||
|
m_Size = 0;
|
||||||
|
m_UnpackPos = 0;
|
||||||
|
m_Version = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
@ -23,21 +27,25 @@ CFileUnpacker::CFileUnpacker() : m_UnpackPos(0), m_Version(0)
|
|||||||
void CFileUnpacker::Read(const char* filename,const char magicstr[4])
|
void CFileUnpacker::Read(const char* filename,const char magicstr[4])
|
||||||
{
|
{
|
||||||
// load the whole thing into memory
|
// load the whole thing into memory
|
||||||
void* file;
|
Handle hm = vfs_load(filename, m_Buf, m_Size);
|
||||||
size_t size;
|
|
||||||
Handle hm = vfs_load(filename, file, size);
|
|
||||||
if(hm <= 0)
|
if(hm <= 0)
|
||||||
|
{
|
||||||
|
m_Buf = 0;
|
||||||
|
m_Size = 0;
|
||||||
throw CFileOpenError();
|
throw CFileOpenError();
|
||||||
|
}
|
||||||
|
|
||||||
// make sure we read enough for the header
|
// make sure we read enough for the header
|
||||||
if(size < 12)
|
if(m_Size < 12)
|
||||||
{
|
{
|
||||||
mem_free_h(hm);
|
mem_free_h(hm);
|
||||||
|
m_Buf = 0;
|
||||||
|
m_Size = 0;
|
||||||
throw CFileReadError();
|
throw CFileReadError();
|
||||||
}
|
}
|
||||||
|
|
||||||
// extract data from header
|
// extract data from header
|
||||||
u8* header = (u8*)file;
|
u8* header = (u8*)m_Buf;
|
||||||
char* magic = (char*)(header+0);
|
char* magic = (char*)(header+0);
|
||||||
m_Version = *(u32*)(header+4);
|
m_Version = *(u32*)(header+4);
|
||||||
u32 datasize = *(u32*)(header+8);
|
u32 datasize = *(u32*)(header+8);
|
||||||
@ -46,62 +54,13 @@ void CFileUnpacker::Read(const char* filename,const char magicstr[4])
|
|||||||
// check we've got the right kind of file
|
// check we've got the right kind of file
|
||||||
// .. and that we read exactly headersize+datasize
|
// .. and that we read exactly headersize+datasize
|
||||||
if(strncmp(magic, magicstr, 4) != 0 ||
|
if(strncmp(magic, magicstr, 4) != 0 ||
|
||||||
size != 12+datasize)
|
m_Size != 12+datasize)
|
||||||
{
|
{
|
||||||
mem_free_h(hm);
|
mem_free_h(hm);
|
||||||
|
m_Buf = 0;
|
||||||
|
m_Size = 0;
|
||||||
throw CFileTypeError();
|
throw CFileTypeError();
|
||||||
}
|
}
|
||||||
|
|
||||||
// allocate memory and read in a big chunk of data
|
|
||||||
m_Data.resize(datasize);
|
|
||||||
memcpy(&m_Data[0], data, datasize);
|
|
||||||
|
|
||||||
mem_free_h(hm);
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
FILE* fp=fopen(filename,"rb");
|
|
||||||
if (!fp) {
|
|
||||||
throw CFileOpenError();
|
|
||||||
}
|
|
||||||
|
|
||||||
// read magic bits
|
|
||||||
char magic[4];
|
|
||||||
if (fread(magic,sizeof(char)*4,1,fp)!=1) {
|
|
||||||
fclose(fp);
|
|
||||||
throw CFileReadError();
|
|
||||||
}
|
|
||||||
|
|
||||||
// check we've got the right kind of file
|
|
||||||
if (strncmp(magic,magicstr,4)!=0) {
|
|
||||||
// nope ..
|
|
||||||
fclose(fp);
|
|
||||||
throw CFileTypeError();
|
|
||||||
}
|
|
||||||
|
|
||||||
// get version
|
|
||||||
if (fread(&m_Version,sizeof(m_Version),1,fp)!=1) {
|
|
||||||
fclose(fp);
|
|
||||||
throw CFileReadError();
|
|
||||||
}
|
|
||||||
|
|
||||||
// get size of anim data
|
|
||||||
u32 datasize;
|
|
||||||
if (fread(&datasize,sizeof(datasize),1,fp)!=1) {
|
|
||||||
fclose(fp);
|
|
||||||
throw CFileReadError();
|
|
||||||
}
|
|
||||||
|
|
||||||
// allocate memory and read in a big chunk of data
|
|
||||||
m_Data.resize(datasize);
|
|
||||||
if (fread(&m_Data[0],datasize,1,fp)!=1) {
|
|
||||||
fclose(fp);
|
|
||||||
throw CFileReadError();
|
|
||||||
}
|
|
||||||
|
|
||||||
// all done
|
|
||||||
fclose(fp);
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
@ -111,15 +70,16 @@ void CFileUnpacker::Read(const char* filename,const char magicstr[4])
|
|||||||
void CFileUnpacker::UnpackRaw(void* rawdata,u32 rawdatalen)
|
void CFileUnpacker::UnpackRaw(void* rawdata,u32 rawdatalen)
|
||||||
{
|
{
|
||||||
// got enough data to unpack?
|
// got enough data to unpack?
|
||||||
if (m_UnpackPos+rawdatalen<=m_Data.size()) {
|
if (m_UnpackPos+rawdatalen<=m_Size)
|
||||||
|
{
|
||||||
// yes .. copy over
|
// yes .. copy over
|
||||||
memcpy(rawdata,&m_Data[m_UnpackPos],rawdatalen);
|
void* src = (char*)m_Buf + m_UnpackPos;
|
||||||
// advance pointer
|
memcpy(rawdata, src, rawdatalen);
|
||||||
m_UnpackPos+=rawdatalen;
|
m_UnpackPos += rawdatalen;
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
// nope - throw exception
|
// nope - throw exception
|
||||||
throw CFileEOFError();
|
throw CFileEOFError();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -45,8 +45,9 @@ public:
|
|||||||
void UnpackString(CStr& result);
|
void UnpackString(CStr& result);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// the input data stream read from file and used during unpack operations
|
// the data read from file and used during unpack operations
|
||||||
std::vector<u8> m_Data;
|
void* m_Buf;
|
||||||
|
size_t m_Size;
|
||||||
// current unpack position in stream
|
// current unpack position in stream
|
||||||
u32 m_UnpackPos;
|
u32 m_UnpackPos;
|
||||||
// version of the file currently being read
|
// version of the file currently being read
|
||||||
|
Loading…
Reference in New Issue
Block a user