[Scummvm-cvs-logs] SF.net SVN: scummvm: [24713] scummvm/trunk/common

fingolfin at users.sourceforge.net fingolfin at users.sourceforge.net
Mon Nov 13 21:58:21 CET 2006


Revision: 24713
          http://svn.sourceforge.net/scummvm/?rev=24713&view=rev
Author:   fingolfin
Date:     2006-11-13 12:58:21 -0800 (Mon, 13 Nov 2006)

Log Message:
-----------
Patch #1583931: (Seekable)SubReadStream

Modified Paths:
--------------
    scummvm/trunk/common/stream.cpp
    scummvm/trunk/common/stream.h

Modified: scummvm/trunk/common/stream.cpp
===================================================================
--- scummvm/trunk/common/stream.cpp	2006-11-13 20:56:11 UTC (rev 24712)
+++ scummvm/trunk/common/stream.cpp	2006-11-13 20:58:21 UTC (rev 24713)
@@ -105,7 +105,7 @@
 
 		c = readByte();
 	}
-	
+
 	// This should fix a bug while using readLine with Common::File
 	// it seems that it sets the eos flag after an invalid read
 	// and at the same time the ioFailed flag
@@ -118,5 +118,43 @@
 	return buf;
 }
 
+uint32 SubReadStream::read(void *dataPtr, uint32 dataSize) {
+	dataSize = MIN(dataSize, _end - _pos);
 
+	dataSize = _parentStream->read(dataPtr, dataSize);
+	_pos += dataSize;
+
+	return dataSize;
+}
+
+SeekableSubReadStream::SeekableSubReadStream(SeekableReadStream *parentStream, uint32 begin, uint32 end)
+	: SubReadStream(parentStream, end),
+	_parentStream(parentStream),
+	_begin(begin) {
+	assert(_begin <= _end);
+	_pos = _begin;
+	_parentStream->seek(_pos);
+}
+
+void SeekableSubReadStream::seek(int32 offset, int whence) {
+	assert(_pos >= _begin);
+	assert(_pos <= _end);
+
+	switch(whence) {
+	case SEEK_END:
+		offset = size() - offset;
+		// fallthrough
+	case SEEK_SET:
+		_pos = _begin + offset;
+		break;
+	case SEEK_CUR:
+		_pos += offset;
+	}
+
+	assert(_pos >= _begin);
+	assert(_pos <= _end);
+
+	_parentStream->seek(_pos);
+}
+
 }	// End of namespace Common

Modified: scummvm/trunk/common/stream.h
===================================================================
--- scummvm/trunk/common/stream.h	2006-11-13 20:56:11 UTC (rev 24712)
+++ scummvm/trunk/common/stream.h	2006-11-13 20:58:21 UTC (rev 24713)
@@ -210,14 +210,14 @@
  * @todo We really need better error handling here!
  *       Like seek should somehow indicate whether it failed.
  */
-class SeekableReadStream : public ReadStream {
+class SeekableReadStream : virtual public ReadStream {
 public:
 
 	virtual uint32 pos() const = 0;
 	virtual uint32 size() const = 0;
 
 	virtual void seek(int32 offset, int whence = SEEK_SET) = 0;
-	
+
 	void skip(uint32 offset) { seek(offset, SEEK_CUR); }
 
 	/**
@@ -233,7 +233,44 @@
 	virtual char *readLine(char *buf, size_t bufSize);
 };
 
+/**
+ * SubReadStream provides access to a ReadStream restricted to the range
+ * [currentPosition, currentPosition+end).
+ * Manipulating the parent stream directly /will/ mess up a substream.
+ * Likewise, manipulating two substreams of a parent stream will cause them to
+ * step on each others toes.
+ */
+class SubReadStream : virtual public ReadStream {
+protected:
+	ReadStream *_parentStream;
+	uint32 _pos;
+	uint32 _end;
+public:
+	SubReadStream(ReadStream *parentStream, uint32 end)
+		: _parentStream(parentStream), _pos(0), _end(end) {}
 
+	virtual bool eos() const { return _pos == _end; }
+	virtual uint32 read(void *dataPtr, uint32 dataSize);
+};
+
+/*
+ * SeekableSubReadStream provides access to a SeekableReadStream restricted to
+ * the range [begin, end).
+ * The same caveats apply to SeekableSubReadStream as do to SeekableReadStream.
+ */
+class SeekableSubReadStream : public SubReadStream, public SeekableReadStream {
+protected:
+	SeekableReadStream *_parentStream;
+	uint32 _begin;
+public:
+	SeekableSubReadStream(SeekableReadStream *parentStream, uint32 begin, uint32 end);
+
+	virtual uint32 pos() const { return _pos - _begin; }
+	virtual uint32 size() const { return _end - _begin; }
+
+	virtual void seek(int32 offset, int whence = SEEK_SET);
+};
+
 /**
  * XORReadStream is a wrapper around an arbitrary other ReadStream,
  * which 'decrypts' the data being read by XORing all data bytes with the given


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list