renamed ContChunk -> MemoryChunk; avoid code duplication by introducing BaseChunk

svn-id: r7935
This commit is contained in:
Max Horn 2003-05-25 11:39:08 +00:00
parent 467666d0fd
commit 80df4a8a00
4 changed files with 82 additions and 119 deletions

View File

@ -84,61 +84,29 @@ const char *Chunk::ChunkString(Chunk::type t) {
return data;
}
FileChunk::FileChunk() :
_data(0),
BaseChunk::BaseChunk() :
_type(0),
_size(0),
_curPos(0) {
}
FileChunk::~FileChunk() {
if(_data)
_data->decRef();
}
FileChunk::FileChunk(const char *fname, const char *directory) {
_data = new FilePtr(fname, directory);
_data->read(&_type, 4);
_type = TO_BE_32(_type);
_data->read(&_size, 4);
_size = TO_BE_32(_size);
_offset = _data->tell();
_curPos = 0;
}
Chunk::type FileChunk::getType() const {
return _type;
}
uint32 FileChunk::getSize() const {
return _size;
}
Chunk *FileChunk::subBlock() {
FileChunk *ptr = new FileChunk;
ptr->_data = _data;
_data->incRef();
_data->seek(_offset + _curPos);
uint32 temp;
_data->read(&temp, 4);
ptr->_type = TO_BE_32(temp);
_data->read(&temp, 4);
ptr->_size = TO_BE_32(temp);
ptr->_offset = _offset + _curPos + 8;
ptr->_curPos = 0;
seek(8 + ptr->getSize());
return ptr;
}
bool FileChunk::eof() const {
bool BaseChunk::eof() const {
return _curPos >= _size;
}
uint32 FileChunk::tell() const {
uint32 BaseChunk::tell() const {
return _curPos;
}
bool FileChunk::seek(int32 delta, seek_type dir) {
Chunk::type BaseChunk::getType() const {
return _type;
}
uint32 BaseChunk::getSize() const {
return _size;
}
bool BaseChunk::seek(int32 delta, seek_type dir) {
switch(dir) {
case seek_cur:
_curPos += delta;
@ -162,6 +130,41 @@ bool FileChunk::seek(int32 delta, seek_type dir) {
return true;
}
FileChunk::FileChunk() :
_data(0) {
}
FileChunk::FileChunk(const char *fname, const char *directory) {
_data = new FilePtr(fname, directory);
_data->read(&_type, 4);
_type = TO_BE_32(_type);
_data->read(&_size, 4);
_size = TO_BE_32(_size);
_offset = _data->tell();
_curPos = 0;
}
FileChunk::~FileChunk() {
if(_data)
_data->decRef();
}
Chunk *FileChunk::subBlock() {
FileChunk *ptr = new FileChunk();
ptr->_data = _data;
_data->incRef();
_data->seek(_offset + _curPos);
uint32 temp;
_data->read(&temp, 4);
ptr->_type = TO_BE_32(temp);
_data->read(&temp, 4);
ptr->_size = TO_BE_32(temp);
ptr->_offset = _offset + _curPos + 8;
ptr->_curPos = 0;
seek(8 + ptr->getSize());
return ptr;
}
bool FileChunk::read(void *buffer, uint32 size) {
if(size <= 0 || (_curPos + size) > _size)
error("invalid buffer read request");
@ -221,7 +224,7 @@ uint32 FileChunk::getDword() {
return TO_LE_32(buffer);
}
ContChunk::ContChunk(byte *data) {
MemoryChunk::MemoryChunk(byte *data) {
if(data == 0)
error("Chunk() called with NULL pointer");
@ -231,49 +234,13 @@ ContChunk::ContChunk(byte *data) {
_curPos = 0;
}
Chunk::type ContChunk::getType() const {
return _type;
}
uint32 ContChunk::getSize() const {
return _size;
}
Chunk *ContChunk::subBlock() {
ContChunk *ptr = new ContChunk(_data + _curPos);
Chunk *MemoryChunk::subBlock() {
MemoryChunk *ptr = new MemoryChunk(_data + _curPos);
seek(sizeof(Chunk::type) + sizeof(uint32) + ptr->getSize());
return ptr;
}
bool ContChunk::eof() const {
return _curPos >= _size;
}
uint32 ContChunk::tell() const {
return _curPos;
}
bool ContChunk::seek(int32 delta, seek_type dir) {
switch(dir) {
case seek_cur:
_curPos += delta;
break;
case seek_start:
if(delta < 0) error("invalid seek request");
_curPos = (uint32)delta;
break;
case seek_end:
if(delta > 0 || _size < (uint32)-delta) error("invalid seek request");
_curPos = (uint32)(_size + delta);
break;
}
if(_curPos > _size) {
error("invalid seek request : %d > %d (delta == %d)", _curPos, _size, delta);
}
return true;
}
bool ContChunk::read(void *buffer, uint32 size) {
bool MemoryChunk::read(void *buffer, uint32 size) {
if(size <= 0 || (_curPos + size) > _size)
error("invalid buffer read request");
@ -282,14 +249,14 @@ bool ContChunk::read(void *buffer, uint32 size) {
return true;
}
int8 ContChunk::getChar() {
int8 MemoryChunk::getChar() {
if(_curPos >= _size)
error("invalid char read request");
return _data[_curPos++];
}
byte ContChunk::getByte() {
byte MemoryChunk::getByte() {
if(_curPos >= _size)
error("invalid byte read request");
@ -298,7 +265,7 @@ byte ContChunk::getByte() {
return *ptr;
}
int16 ContChunk::getShort() {
int16 MemoryChunk::getShort() {
if(_curPos >= _size - 1)
error("invalid int16 read request");
@ -306,7 +273,7 @@ int16 ContChunk::getShort() {
return *((int16 *)&buffer);
}
uint16 ContChunk::getWord() {
uint16 MemoryChunk::getWord() {
if(_curPos >= _size - 1)
error("invalid word read request");
@ -315,7 +282,7 @@ uint16 ContChunk::getWord() {
return READ_LE_UINT16(ptr);
}
uint32 ContChunk::getDword() {
uint32 MemoryChunk::getDword() {
if(_curPos >= _size - 3)
error("invalid dword read request");

View File

@ -26,7 +26,6 @@
class Chunk {
public:
virtual ~Chunk() {};
enum seek_type { seek_start, seek_end, seek_cur };
typedef uint32 type;
@ -47,28 +46,35 @@ public:
class FilePtr;
class FileChunk : public Chunk {
private:
FilePtr *_data;
type _type;
// Common functionality for concrete chunks (FileChunk, MemoryChunk)
class BaseChunk : public Chunk {
protected:
Chunk::type _type;
uint32 _size;
uint32 _offset;
uint32 _curPos;
protected:
FileChunk();
BaseChunk();
public:
FileChunk(const char *fname, const char *directory);
virtual ~FileChunk();
type getType() const;
Chunk::type getType() const;
uint32 getSize() const;
Chunk *subBlock();
bool eof() const;
uint32 tell() const;
bool seek(int32 delta, seek_type dir = seek_cur);
};
class FileChunk : public BaseChunk {
private:
FilePtr *_data;
uint32 _offset;
protected:
FileChunk();
public:
FileChunk(const char *fname, const char *directory);
virtual ~FileChunk();
Chunk *subBlock();
bool read(void *buffer, uint32 size);
int8 getChar();
byte getByte();
@ -77,23 +83,13 @@ public:
uint32 getDword();
};
class ContChunk : public Chunk {
class MemoryChunk : public BaseChunk {
private:
byte *_data;
Chunk::type _type;
uint32 _size;
uint32 _curPos;
public:
ContChunk(byte *data);
Chunk::type getType() const;
uint32 getSize() const;
MemoryChunk(byte *data);
Chunk *subBlock();
bool eof() const;
uint32 tell() const;
bool seek(int32 delta, seek_type dir = seek_cur);
bool read(void *buffer, uint32 size);
int8 getChar();
byte getByte();

View File

@ -216,7 +216,7 @@ bool ImuseChannel::handleSubTags(int32 &offset) {
case TYPE_MAP_:
_inData = false;
if(available_size >= (size + 8)) {
ContChunk c((byte *)_tbuffer + offset);
MemoryChunk c((byte *)_tbuffer + offset);
handleMap(c);
}
break;

View File

@ -52,7 +52,7 @@ bool SaudChannel::handleSubTags(int32 &offset) {
case TYPE_STRK:
_inData = false;
if(available_size >= (size + 8)) {
ContChunk c((byte *)_tbuffer + offset);
MemoryChunk c((byte *)_tbuffer + offset);
handleStrk(c);
}
else
@ -61,7 +61,7 @@ bool SaudChannel::handleSubTags(int32 &offset) {
case TYPE_SMRK:
_inData = false;
if(available_size >= (size + 8)) {
ContChunk c((byte *)_tbuffer + offset);
MemoryChunk c((byte *)_tbuffer + offset);
handleSmrk(c);
}
else
@ -70,7 +70,7 @@ bool SaudChannel::handleSubTags(int32 &offset) {
case TYPE_SHDR:
_inData = false;
if(available_size >= (size + 8)) {
ContChunk c((byte *)_tbuffer + offset);
MemoryChunk c((byte *)_tbuffer + offset);
handleShdr(c);
}
else