[Scummvm-cvs-logs] SF.net SVN: scummvm: [31498] scummvm/trunk/common/stream.h

thebluegr at users.sourceforge.net thebluegr at users.sourceforge.net
Mon Apr 14 01:46:55 CEST 2008


Revision: 31498
          http://scummvm.svn.sourceforge.net/scummvm/?rev=31498&view=rev
Author:   thebluegr
Date:     2008-04-13 16:46:55 -0700 (Sun, 13 Apr 2008)

Log Message:
-----------
Added eriktorbjorn's MemoryWriteStreamDynamic class - a stream that grows as it's written to

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

Modified: scummvm/trunk/common/stream.h
===================================================================
--- scummvm/trunk/common/stream.h	2008-04-13 07:38:33 UTC (rev 31497)
+++ scummvm/trunk/common/stream.h	2008-04-13 23:46:55 UTC (rev 31498)
@@ -487,6 +487,62 @@
 	uint32 size() const { return _bufSize; }
 };
 
+/** 
+ * A sort of hybrid between MemoryWriteStream and Array classes. A stream
+ * that grows as it's written to. 
+ */
+class MemoryWriteStreamDynamic : public Common::WriteStream {
+private:
+	uint32 _capacity;
+	uint32 _size;
+	byte *_ptr;
+	byte *_data;
+	uint32 _pos;
+	bool _disposeMemory;
+
+	void ensureCapacity(uint32 new_len) {
+		if (new_len <= _capacity)
+			return;
+
+		byte *old_data = _data;
+
+		_capacity = new_len + 32;
+		_data = new byte[_capacity];
+		_ptr = _data + _pos;
+
+		if (old_data) {
+			// Copy old data
+			memcpy(_data, old_data, _size);
+			delete[] old_data;
+		}
+
+		_size = new_len;
+	}
+public:
+	MemoryWriteStreamDynamic(bool disposeMemory = false) : _capacity(0), _size(0), _ptr(0), _data(0), _pos(0), _disposeMemory(disposeMemory) {}
+
+	~MemoryWriteStreamDynamic() {
+		if (_disposeMemory)
+			delete[] _data;
+	}
+
+	uint32 write(const void *dataPtr, uint32 dataSize) {
+		ensureCapacity(_pos + dataSize);
+		memcpy(_ptr, dataPtr, dataSize);
+		_ptr += dataSize;
+		_pos += dataSize;
+		if (_pos > _size)
+			_size = _pos;
+		return dataSize;
+	}
+
+	bool eos() const { return false; }
+	uint32 pos() const { return _pos; }
+	uint32 size() const { return _size; }
+
+	byte *getData() { return _data; }
+};
+
 }	// End of namespace Common
 
 #endif


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