1
0
forked from 0ad/0ad

Let ScEd open maps with non-VFS filenames. Probably stopped leaking memory.

This was SVN commit r1588.
This commit is contained in:
Ykkrosh 2004-12-28 16:17:17 +00:00
parent 9e2413acab
commit 94f89e130b
2 changed files with 60 additions and 0 deletions

View File

@ -21,11 +21,25 @@ CFileUnpacker::CFileUnpacker()
m_Version = 0;
}
CFileUnpacker::~CFileUnpacker()
{
if (m_Buf)
mem_free(m_Buf);
}
////////////////////////////////////////////////////////////////////////////////////////
// Read: open and read in given file, check magic bits against those given; throw
// variety of exceptions for missing files etc
void CFileUnpacker::Read(const char* filename,const char magicstr[4])
{
#ifdef SCED
// HACK: to make it work with absolute pathnames, e.g. from the map
// loading dialog box.
// Check for "x:\<something>" style paths:
if (! (filename[1] == ':' && filename[2] == '\\'))
{
#endif // SCED
// avoid vfs_load complaining about missing data files (which happens
// too often). better to check here than squelch internal VFS error
// reporting. we disable this in release mode to avoid a speed hit.
@ -71,6 +85,50 @@ void CFileUnpacker::Read(const char* filename,const char magicstr[4])
}
m_UnpackPos = 12;
#ifdef SCED
// HACK: continued from earlier
}
else
{
FILE* fp=fopen(filename,"rb");
if (!fp)
throw CFileOpenError();
m_Buf = 0;
m_Size = 0;
char magic[4];
u32 datasize;
if (
// read magic bits
fread(magic,sizeof(char)*4,1,fp)!=1
// check we've got the right kind of file
|| strncmp(magic,magicstr,4)!=0
// get version
|| fread(&m_Version,sizeof(m_Version),1,fp)!=1
// get size of anim data
|| fread(&datasize,sizeof(datasize),1,fp)!=1
) {
fclose(fp);
throw CFileReadError();
}
// allocate memory and read in a big chunk of data
m_Buf = mem_alloc(datasize);
if (fread(m_Buf,datasize,1,fp)!=1) {
mem_free(m_Buf);
m_Buf = 0;
fclose(fp);
throw CFileReadError();
}
// all done
m_Size = datasize;
fclose(fp);
}
#endif // SCED
}
////////////////////////////////////////////////////////////////////////////////////////

View File

@ -29,6 +29,8 @@ public:
public:
// constructor
CFileUnpacker();
~CFileUnpacker();
// Read: open and read in given file, check magic bits against those given; throw
// variety of exceptions for missing files etc