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

Bluddy at users.sourceforge.net Bluddy at users.sourceforge.net
Tue Aug 24 13:18:48 CEST 2010


Revision: 52325
          http://scummvm.svn.sourceforge.net/scummvm/?rev=52325&view=rev
Author:   Bluddy
Date:     2010-08-24 11:18:48 +0000 (Tue, 24 Aug 2010)

Log Message:
-----------
COMMON: implemented BufferedWriteStream and fixed bug in BufferedReadStream

I need the write buffering for the new version of the PSP streams and thought the simplest way to implement it would be along the lines of BufferedReadStream. Sadly, I found a nasty little bug in BRS but that's taken care of.
Also, I adapted these streams for target-specific memory allocation by using virtual functions for allocation/deallocation.

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

Modified: scummvm/trunk/common/stream.cpp
===================================================================
--- scummvm/trunk/common/stream.cpp	2010-08-24 09:47:04 UTC (rev 52324)
+++ scummvm/trunk/common/stream.cpp	2010-08-24 11:18:48 UTC (rev 52325)
@@ -230,13 +230,21 @@
 	_realBufSize(bufSize) {
 
 	assert(parentStream);
-	_buf = new byte[bufSize];
+	allocBuf(bufSize);
 	assert(_buf);
 }
 
+void BufferedReadStream::allocBuf(uint32 bufSize) {
+	_buf = new byte[bufSize];
+}
+
 BufferedReadStream::~BufferedReadStream() {
 	if (_disposeParentStream)
 		delete _parentStream;
+	deallocBuf();
+}
+
+void BufferedReadStream::deallocBuf() {
 	delete[] _buf;
 }
 
@@ -259,7 +267,7 @@
 
 		// At this point the buffer is empty. Now if the read request
 		// exceeds the buffer size, just satisfy it directly.
-		if (dataSize > _bufSize)
+		if (dataSize > _realBufSize)
 			return alreadyRead + _parentStream->read(dataPtr, dataSize);
 
 		// Refill the buffer.
@@ -303,6 +311,67 @@
 	return true;	// FIXME: STREAM REWRITE
 }
 
+BufferedWriteStream::BufferedWriteStream(WriteStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream)
+	: _parentStream(parentStream),
+	_disposeParentStream(disposeParentStream),
+	_pos(0),
+	_bufSize(bufSize) {
+
+	assert(parentStream);
+	allocBuf(bufSize);
+	assert(_buf);
+}
+
+BufferedWriteStream::~BufferedWriteStream() {
+	assert(flush());
+	
+	if (_disposeParentStream)
+		delete _parentStream;
+		
+	deallocBuf();
+}
+
+void BufferedWriteStream::allocBuf(uint32 bufSize) {
+	_buf = new byte[bufSize];
+}
+
+void BufferedWriteStream::deallocBuf() {
+	delete[] _buf;
+}
+
+uint32 BufferedWriteStream::write(const void *dataPtr, uint32 dataSize) {
+	// check if we have enough space for writing to the buffer
+	if (_bufSize - _pos >= dataSize) {
+		memcpy(_buf + _pos, dataPtr, dataSize);
+		_pos += dataSize;			
+	} else if (_bufSize >= dataSize) {	// check if we can flush the buffer and load the data
+		// flush the buffer
+		assert(flushBuffer());
+		memcpy(_buf, dataPtr, dataSize);
+		_pos += dataSize;
+	} else	{	// too big for our buffer
+		// flush the buffer
+		assert(flushBuffer());			
+		return _parentStream->write(dataPtr, dataSize);
+	}
+	return dataSize;
+}
+
+bool BufferedWriteStream::flushBuffer() {
+	uint32 bytesToWrite = _pos;
+	
+	if (bytesToWrite) {
+		_pos = 0;
+		if (_parentStream->write(_buf, bytesToWrite) != bytesToWrite)
+			return false;
+	}
+	return true;
+}
+
+bool BufferedWriteStream::flush() {
+	return flushBuffer();
+}
+
 bool MemoryWriteStreamDynamic::seek(int32 offs, int whence) {
 	// Pre-Condition
 	assert(_pos <= _size);

Modified: scummvm/trunk/common/stream.h
===================================================================
--- scummvm/trunk/common/stream.h	2010-08-24 09:47:04 UTC (rev 52324)
+++ scummvm/trunk/common/stream.h	2010-08-24 11:18:48 UTC (rev 52325)
@@ -149,7 +149,6 @@
 	void writeString(const String &str);
 };
 
-
 /**
  * Generic interface for a readable data stream.
  */
@@ -497,10 +496,12 @@
 	uint32 _pos;
 	uint32 _bufSize;
 	uint32 _realBufSize;
+	virtual void allocBuf(uint32 bufSize);	// virtual functions to allocate/deallocate the buffer
+	virtual void deallocBuf();
 
 public:
 	BufferedReadStream(ReadStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::NO);
-	~BufferedReadStream();
+	virtual ~BufferedReadStream();
 
 	virtual bool eos() const { return (_pos == _bufSize) && _parentStream->eos(); }
 	virtual bool err() const { return _parentStream->err(); }
@@ -525,7 +526,27 @@
 	virtual bool seek(int32 offset, int whence = SEEK_SET);
 };
 
+/**
+ * Wrapper class which adds buffering to any WriteStream.
+ */
+class BufferedWriteStream : public WriteStream {
+protected:
+	WriteStream *_parentStream;
+	DisposeAfterUse::Flag _disposeParentStream;
+	byte *_buf;
+	uint32 _pos;
+	uint32 _bufSize;
+	bool flushBuffer();						// write out the data in the buffer
+	virtual void allocBuf(uint32 bufSize);	// virtual functions to allocate/deallocate the buffer
+	virtual void deallocBuf();	
 
+public:
+	BufferedWriteStream(WriteStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::NO);
+	virtual ~BufferedWriteStream();
+	
+	virtual uint32 write(const void *dataPtr, uint32 dataSize);
+	virtual bool flush();
+};
 
 /**
  * Simple memory based 'stream', which implements the ReadStream interface for


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