From 23f63e7531120ec67324e8cfcb6581cb4e2a3f3e Mon Sep 17 00:00:00 2001 From: Vladimir Menshakov Date: Sat, 20 Mar 2010 13:52:08 +0000 Subject: added TransientFilePack svn-id: r48314 --- engines/teenagent/pack.cpp | 78 +++++++++++++++++++++++++++++++++++++++++++++- engines/teenagent/pack.h | 16 ++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) (limited to 'engines') diff --git a/engines/teenagent/pack.cpp b/engines/teenagent/pack.cpp index 842fc5655b..38a361fd60 100644 --- a/engines/teenagent/pack.cpp +++ b/engines/teenagent/pack.cpp @@ -76,12 +76,88 @@ uint32 FilePack::read(uint32 id, byte *dst, uint32 size) const { } Common::SeekableReadStream *FilePack::getStream(uint32 id) const { + if (id < 1 || id > _fileCount) + return NULL; + //debug(0, "stream: %04x-%04x", offsets[id - 1], offsets[id]); + file.seek(offsets[id - 1]); + uint32 size = offsets[id] - offsets[id - 1]; + byte *ptr = (byte *)malloc(size); + if (ptr == NULL) + return NULL; + uint32 r = file.read(ptr, size); + return new Common::MemoryReadStream(ptr, r, DisposeAfterUse::YES); +} + + +TransientFilePack::TransientFilePack() : offsets(0) {} + +TransientFilePack::~TransientFilePack() { + close(); +} + +void TransientFilePack::close() { + delete[] offsets; + offsets = NULL; + _filename.clear(); +} + +bool TransientFilePack::open(const Common::String &filename) { + _filename = filename; + + Common::File file; + if (!file.open(filename)) + return false; + + _fileCount = file.readUint32LE(); + debug(0, "opened %s, found %u entries", filename.c_str(), _fileCount); + offsets = new uint32[_fileCount + 1]; + for (uint32 i = 0; i <= _fileCount; ++i) { + offsets[i] = file.readUint32LE(); + } + return true; +} + +uint32 TransientFilePack::getSize(uint32 id) const { + if (id < 1 || id > _fileCount) + return 0; + return offsets[id] - offsets[id - 1]; +} + +uint32 TransientFilePack::read(uint32 id, byte *dst, uint32 size) const { if (id < 1 || id > _fileCount) return 0; + + Common::File file; + if (!file.open(_filename)) + return 0; + + file.seek(offsets[id - 1]); + uint32 rsize = offsets[id] - offsets[id - 1]; + uint32 r = file.read(dst, MIN(rsize, size)); + file.close(); + //debug(0, "read(%u, %u) = %u", id, size, r); + return r; +} + +Common::SeekableReadStream *TransientFilePack::getStream(uint32 id) const { + if (id < 1 || id > _fileCount) + return NULL; //debug(0, "stream: %04x-%04x", offsets[id - 1], offsets[id]); - return new Common::SeekableSubReadStream(&file, offsets[id - 1], offsets[id], DisposeAfterUse::NO); + Common::File file; + if (!file.open(_filename)) + return NULL; + + file.seek(offsets[id - 1]); + uint32 size = offsets[id] - offsets[id - 1]; + byte *ptr = (byte *)malloc(size); + if (ptr == NULL) + return NULL; + uint32 r = file.read(ptr, size); + file.close(); + return new Common::MemoryReadStream(ptr, r, DisposeAfterUse::YES); } + void MemoryPack::close() { chunks.clear(); } diff --git a/engines/teenagent/pack.h b/engines/teenagent/pack.h index 6c51782b37..53fdf80d33 100644 --- a/engines/teenagent/pack.h +++ b/engines/teenagent/pack.h @@ -62,6 +62,22 @@ public: virtual Common::SeekableReadStream *getStream(uint32 id) const; }; +class TransientFilePack : public Pack { + uint32 *offsets; + Common::String _filename; + +public: + TransientFilePack(); + ~TransientFilePack(); + + virtual bool open(const Common::String &filename); + virtual void close(); + + virtual uint32 getSize(uint32 id) const; + virtual uint32 read(uint32 id, byte *dst, uint32 size) const; + virtual Common::SeekableReadStream *getStream(uint32 id) const; +}; + class MemoryPack : public Pack { struct Chunk { byte *data; -- cgit v1.2.3