diff options
Diffstat (limited to 'engines/titanic/compressed_file.cpp')
-rw-r--r-- | engines/titanic/compressed_file.cpp | 115 |
1 files changed, 15 insertions, 100 deletions
diff --git a/engines/titanic/compressed_file.cpp b/engines/titanic/compressed_file.cpp index c200370eae..d0cd37671a 100644 --- a/engines/titanic/compressed_file.cpp +++ b/engines/titanic/compressed_file.cpp @@ -27,13 +27,8 @@ namespace Titanic { #define BUFFER_SIZE 1024 CompressedFile::CompressedFile() : SimpleFile() { - _fileMode = COMPMODE_NONE; - Common::fill(&_writeBuffer[0], &_writeBuffer[516], 0); - _dataStartPtr = nullptr; - _dataPtr = nullptr; - _dataRemaining = 0; - _dataMaxSize = 0; - _dataCount = 0; + _readStream = nullptr; + _writeStream = nullptr; } CompressedFile::~CompressedFile() { @@ -41,119 +36,39 @@ CompressedFile::~CompressedFile() { void CompressedFile::open(const Common::String &name) { SimpleFile::open(name); - - _compression.initDecompress(); - _fileMode = COMPMODE_READ; - _dataPtr = _dataStartPtr = new byte[BUFFER_SIZE]; - _dataMaxSize = BUFFER_SIZE; - _dataRemaining = 0; - _dataCount = 0; + _readStream = Common::wrapCompressedReadStream(&_file); } void CompressedFile::open(Common::SeekableReadStream *stream) { SimpleFile::open(stream); - - _compression.initDecompress(); - _fileMode = COMPMODE_READ; - _dataPtr = _dataStartPtr = new byte[BUFFER_SIZE]; - _dataMaxSize = BUFFER_SIZE; - _dataRemaining = 0; - _dataCount = 0; + _readStream = Common::wrapCompressedReadStream(&_file); } void CompressedFile::open(Common::OutSaveFile *stream) { SimpleFile::open(stream); - - _compression.initCompress(); - _fileMode = COMPMODE_WRITE; + _writeStream = Common::wrapCompressedWriteStream(stream); } void CompressedFile::close() { - int result; - - switch (_fileMode) { - case COMPMODE_WRITE: - do { - _compression._destPtr = _writeBuffer; - _compression._destCount = 512; - result = _compression.compress(4); - int count = 512 - _compression._destCount; + delete _readStream; + delete _writeStream; + _readStream = nullptr; + _writeStream = nullptr; - if (count) - write(_writeBuffer, count); - } while (!result); - break; - case COMPMODE_READ: - _compression.close(); - delete[] _dataStartPtr; - _dataStartPtr = _dataPtr = nullptr; - _dataRemaining = _dataMaxSize = 0; - - SimpleFile::close(); - break; - default: - break; - } + SimpleFile::close(); } size_t CompressedFile::unsafeRead(void *dst, size_t count) { - assert(_file.isOpen()); + assert(_readStream); if (count == 0) return 0; - // Ensure there's enough data queued in the buffer - decompress(); - - // Pass the data to the output buffer - size_t bytesRead = 0; - byte *dataPtr = (byte *)dst; - - while (count > 0) { - if (!_dataRemaining) { - decompress(); - if (!_dataRemaining) - break; - } - - *dataPtr++ = *_dataPtr++; - --_dataRemaining; - } - - return bytesRead; + // Read data and decompress + return _readStream->read(dst, count); } -void CompressedFile::decompress() { - const size_t COUNT = 1; - byte fileByte; - int count; - - _dataPtr = _dataStartPtr; - _compression._destPtr = _dataStartPtr; - _compression._destCount = _dataMaxSize; - - if (_dataMaxSize < 0x100) - return; - - // Loop to get data from the file as needed and decompress - do { - if (!_compression._srcCount) { - // Read in next byte from the source file - if (!SimpleFile::unsafeRead(&fileByte, 1)) - break; - - // Set up the decompressor to process the data - _compression._srcCount = COUNT; - _compression._srcPtr = &fileByte; - } - - int count = _compression.decompress(COUNT); - _dataRemaining = _dataMaxSize - _compression._destCount; - - if (count == COUNT) { - _dataCount = COUNT; - break; - } - } while (!count && _compression._destCount > 0x100); +size_t CompressedFile::write(const void *src, size_t count) { + return _writeStream->write(src, count); } } // End of namespace Titanic |