diff options
Diffstat (limited to 'engines/gob/dataio.cpp')
| -rw-r--r-- | engines/gob/dataio.cpp | 151 | 
1 files changed, 116 insertions, 35 deletions
diff --git a/engines/gob/dataio.cpp b/engines/gob/dataio.cpp index 7ded953427..361627caf4 100644 --- a/engines/gob/dataio.cpp +++ b/engines/gob/dataio.cpp @@ -33,6 +33,92 @@  namespace Gob { +DataStream::DataStream(DataIO &io, int16 handle, uint32 dSize, bool dispose) { +	_io = &io; +	_handle = handle; +	_size = dSize; +	_dispose = dispose; + +	_data = 0; +	_stream = 0; +} + +DataStream::DataStream(byte *buf, uint32 dSize, bool dispose) { +	_data = buf; +	_size = dSize; +	_stream = new Common::MemoryReadStream(_data, _size); +	_dispose = dispose; + +	_io = 0; +	_handle = -1; +} + +DataStream::~DataStream() { +	delete _stream; + +	if (_dispose) { +		delete[] _data; +		if ((_handle >= 0) && _io) +			_io->closeData(_handle); +	} +} + +uint32 DataStream::pos() const { +	if (_stream) +		return _stream->pos(); + +	uint32 resPos = _io->getChunkPos(_handle); +	if (resPos != 0xFFFFFFFF) +		return resPos; + +	return _io->file_getHandle(_handle)->pos(); +} + +uint32 DataStream::size() const { +	if (_stream) +		return _stream->size(); + +	return _size; +} + +void DataStream::seek(int32 offset, int whence) { +	if (_stream) +		_stream->seek(offset, whence); + +	int32 resPos = _io->seekChunk(_handle, offset, whence); +	if (resPos != -1) +		return; + +	_io->file_getHandle(_handle)->seek(offset, whence); +} + +bool DataStream::eos() const { +	if (_stream) +		return _stream->eos(); + +	return pos() >= size(); +} + +uint32 DataStream::read(void *dataPtr, uint32 dataSize) { +	if (_stream) +		return _stream->read(dataPtr, dataSize); + +	if ((_handle < 50) || (_handle >= 128)) +		return _io->file_getHandle(_handle)->read((byte *) dataPtr, dataSize); + +	byte *data = (byte *) dataPtr; +	uint32 haveRead = 0; +	while (dataSize > 0x3FFF) { +		_io->readChunk(_handle, (byte *) data, 0x3FFF); +		dataSize -= 0x3FFF; +		data += 0x3FFF; +		haveRead += 0x3FFF; +	} +	_io->readChunk(_handle, (byte *) data, dataSize); + +	return haveRead + dataSize; +} +  DataIO::DataIO(GobEngine *vm) : _vm(vm) {  	for (int i = 0; i < MAX_DATA_FILES; i++) {  		_dataFiles[i] = 0; @@ -115,6 +201,10 @@ Common::File *DataIO::file_getHandle(int16 handle) {  	return &_filesHandles[handle];  } +const Common::File *DataIO::file_getHandle(int16 handle) const { +	return &_filesHandles[handle]; +} +  int16 DataIO::file_open(const char *path, Common::File::AccessMode mode) {  	int16 i; @@ -226,7 +316,7 @@ int16 DataIO::seekChunk(int16 handle, int32 pos, int16 from) {  	return _chunkPos[file * MAX_SLOT_COUNT + slot];  } -uint32 DataIO::getChunkPos(int16 handle) { +uint32 DataIO::getChunkPos(int16 handle) const {  	int16 file;  	int16 slot; @@ -390,39 +480,23 @@ int16 DataIO::openData(const char *path, Common::File::AccessMode mode) {  	return file_open(path, mode);  } -int32 DataIO::readData(int16 handle, byte *buf, uint16 size) { -	int32 res; - -	res = readChunk(handle, buf, size); -	if (res >= 0) -		return res; - -	return file_getHandle(handle)->read(buf, size); -} +DataStream *DataIO::openAsStream(int16 handle, bool dispose) { +	uint32 curPos = getPos(handle); +	seekData(handle, 0, SEEK_END); +	uint32 size = getPos(handle); +	seekData(handle, curPos, SEEK_SET); -byte DataIO::readByte(int16 handle) { -	byte buf; - -	readData(handle, &buf, 1); -	return ((byte) buf); +	return new DataStream(*this, handle, size, dispose);  } -uint16 DataIO::readUint16(int16 handle) { -	byte buf[2]; - -	readData(handle, buf, 2); -	return READ_LE_UINT16(buf); -} - -uint32 DataIO::readUint32(int16 handle) { -	byte buf[4]; +uint32 DataIO::getPos(int16 handle) { +	uint32 resPos; -	readData(handle, buf, 4); -	return READ_LE_UINT32(buf); -} +	resPos = getChunkPos(handle); +	if (resPos != 0xFFFFFFFF) +		return resPos; -int32 DataIO::writeData(int16 handle, byte *buf, uint16 size) { -	return file_getHandle(handle)->write(buf, size); +	return file_getHandle(handle)->pos();  }  void DataIO::seekData(int16 handle, int32 pos, int16 from) { @@ -435,14 +509,14 @@ void DataIO::seekData(int16 handle, int32 pos, int16 from) {  	file_getHandle(handle)->seek(pos, from);  } -uint32 DataIO::getPos(int16 handle) { -	uint32 resPos; +int32 DataIO::readData(int16 handle, byte *buf, uint16 size) { +	int32 res; -	resPos = getChunkPos(handle); -	if (resPos != 0xFFFFFFFF) -		return resPos; +	res = readChunk(handle, buf, size); +	if (res >= 0) +		return res; -	return file_getHandle(handle)->pos(); +	return file_getHandle(handle)->read(buf, size);  }  int32 DataIO::getDataSize(const char *name) { @@ -492,4 +566,11 @@ byte *DataIO::getData(const char *path) {  	return data;  } +DataStream *DataIO::getDataStream(const char *path) { +	uint32 size = getDataSize(path); +	byte *data = getData(path); + +	return new DataStream(data, size); +} +  } // End of namespace Gob  | 
