Moved from "Terrain" directory to somewhere more applicable.
This was SVN commit r298.
This commit is contained in:
parent
726d5a6adf
commit
7468989b4f
76
source/ps/FilePacker.cpp
Executable file
76
source/ps/FilePacker.cpp
Executable file
@ -0,0 +1,76 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Name: FilePacker.cpp
|
||||
// Author: Rich Cross
|
||||
// Contact: rich@wildfiregames.com
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "FilePacker.h"
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// CFilePacker constructor
|
||||
CFilePacker::CFilePacker()
|
||||
{
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Write: write out any packed data to file, using given version and magic bits
|
||||
void CFilePacker::Write(const char* filename,u32 version,const char magicstr[4])
|
||||
{
|
||||
FILE* fp=fopen(filename,"wb");
|
||||
if (!fp) {
|
||||
throw CFileOpenError();
|
||||
}
|
||||
|
||||
// write magic bits
|
||||
if (fwrite(magicstr,sizeof(char)*4,1,fp)!=1) {
|
||||
fclose(fp);
|
||||
throw CFileWriteError();
|
||||
}
|
||||
|
||||
// write version
|
||||
if (fwrite(&version,sizeof(version),1,fp)!=1) {
|
||||
fclose(fp);
|
||||
throw CFileWriteError();
|
||||
}
|
||||
|
||||
// get size of data
|
||||
u32 datasize=m_Data.size();
|
||||
if (fwrite(&datasize,sizeof(datasize),1,fp)!=1) {
|
||||
fclose(fp);
|
||||
throw CFileWriteError();
|
||||
}
|
||||
|
||||
// write out one big chunk of data
|
||||
if (fwrite(&m_Data[0],datasize,1,fp)!=1) {
|
||||
fclose(fp);
|
||||
throw CFileWriteError();
|
||||
}
|
||||
|
||||
// all done
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// PackRaw: pack given number of bytes onto the end of the data stream
|
||||
void CFilePacker::PackRaw(const void* rawdata,u32 rawdatalen)
|
||||
{
|
||||
u32 start=m_Data.size();
|
||||
m_Data.resize(m_Data.size()+rawdatalen);
|
||||
memcpy(&m_Data[start],rawdata,rawdatalen);
|
||||
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// PackString: pack a string onto the end of the data stream
|
||||
void CFilePacker::PackString(const CStr& str)
|
||||
{
|
||||
u32 len=str.Length();
|
||||
PackRaw(&len,sizeof(len));
|
||||
PackRaw((const char*) str,len);
|
||||
}
|
||||
|
43
source/ps/FilePacker.h
Executable file
43
source/ps/FilePacker.h
Executable file
@ -0,0 +1,43 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Name: FilePacker.h
|
||||
// Author: Rich Cross
|
||||
// Contact: rich@wildfiregames.com
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef _FILEPACKER_H
|
||||
#define _FILEPACKER_H
|
||||
|
||||
#include <vector>
|
||||
#include "res/res.h"
|
||||
#include "CStr.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// CFilePacker: class to assist in writing of binary files
|
||||
class CFilePacker
|
||||
{
|
||||
public:
|
||||
// CFilePacker exceptions
|
||||
class CError { };
|
||||
class CFileOpenError : public CError { };
|
||||
class CFileWriteError : public CError { };
|
||||
|
||||
public:
|
||||
// constructor
|
||||
CFilePacker();
|
||||
|
||||
// Write: write out any packed data to file, using given version and magic bits
|
||||
void Write(const char* filename,u32 version,const char magicstr[4]);
|
||||
|
||||
// PackRaw: pack given number of bytes onto the end of the data stream
|
||||
void PackRaw(const void* rawdata,u32 rawdatalen);
|
||||
// PackString: pack a string onto the end of the data stream
|
||||
void PackString(const CStr& str);
|
||||
|
||||
private:
|
||||
// the output data stream built during pack operations
|
||||
std::vector<u8> m_Data;
|
||||
};
|
||||
|
||||
#endif
|
101
source/ps/FileUnpacker.cpp
Executable file
101
source/ps/FileUnpacker.cpp
Executable file
@ -0,0 +1,101 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Name: FileUnpacker.cpp
|
||||
// Author: Rich Cross
|
||||
// Contact: rich@wildfiregames.com
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "FileUnpacker.h"
|
||||
#include <stdio.h>
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// CFileUnpacker constructor
|
||||
CFileUnpacker::CFileUnpacker() : m_UnpackPos(0), m_Version(0)
|
||||
{
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// 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])
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// UnpackRaw: unpack given number of bytes from the input stream into the given array
|
||||
// - throws CFileEOFError if the end of the data stream is reached before the given
|
||||
// number of bytes have been read
|
||||
void CFileUnpacker::UnpackRaw(void* rawdata,u32 rawdatalen)
|
||||
{
|
||||
// got enough data to unpack?
|
||||
if (m_UnpackPos+rawdatalen<=m_Data.size()) {
|
||||
// yes .. copy over
|
||||
memcpy(rawdata,&m_Data[m_UnpackPos],rawdatalen);
|
||||
// advance pointer
|
||||
m_UnpackPos+=rawdatalen;
|
||||
} else {
|
||||
// nope - throw exception
|
||||
throw CFileEOFError();
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// UnpackString: unpack a string from the raw data stream
|
||||
void CFileUnpacker::UnpackString(CStr& result)
|
||||
{
|
||||
// get string length
|
||||
u32 length;
|
||||
UnpackRaw(&length,sizeof(length));
|
||||
|
||||
// read string into temporary buffer
|
||||
std::vector<char> tmp;
|
||||
tmp.resize(length+1);
|
||||
UnpackRaw(&tmp[0],length);
|
||||
tmp[length]='\0';
|
||||
|
||||
// assign to output
|
||||
result=&tmp[0];
|
||||
}
|
||||
|
56
source/ps/FileUnpacker.h
Executable file
56
source/ps/FileUnpacker.h
Executable file
@ -0,0 +1,56 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Name: FileUnpacker.h
|
||||
// Author: Rich Cross
|
||||
// Contact: rich@wildfiregames.com
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef _FILEUNPACKER_H
|
||||
#define _FILEUNPACKER_H
|
||||
|
||||
#include <vector>
|
||||
#include "res/res.h"
|
||||
#include "CStr.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// CFileUnpacker: class to assist in reading of binary files
|
||||
class CFileUnpacker
|
||||
{
|
||||
public:
|
||||
// exceptions thrown by class
|
||||
class CError { };
|
||||
class CFileTypeError : public CError { };
|
||||
class CFileVersionError : public CError { };
|
||||
class CFileOpenError : public CError { };
|
||||
class CFileReadError : public CError { };
|
||||
class CFileEOFError : public CError { };
|
||||
|
||||
public:
|
||||
// constructor
|
||||
CFileUnpacker();
|
||||
|
||||
// Read: open and read in given file, check magic bits against those given; throw
|
||||
// variety of exceptions for missing files etc
|
||||
void Read(const char* filename,const char magicstr[4]);
|
||||
|
||||
// GetVersion: return stored file version
|
||||
u32 GetVersion() const { return m_Version; }
|
||||
|
||||
// UnpackRaw: unpack given number of bytes from the input stream into the given array
|
||||
// - throws CFileEOFError if the end of the data stream is reached before the given
|
||||
// number of bytes have been read
|
||||
void UnpackRaw(void* rawdata,u32 rawdatalen);
|
||||
// UnpackString: unpack a string from the raw data stream
|
||||
void UnpackString(CStr& result);
|
||||
|
||||
private:
|
||||
// the input data stream read from file and used during unpack operations
|
||||
std::vector<u8> m_Data;
|
||||
// current unpack position in stream
|
||||
u32 m_UnpackPos;
|
||||
// version of the file currently being read
|
||||
u32 m_Version;
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user