Browse Source

API Change: simplify seeking and data reading/writing methods

Daniele Bartolini 13 năm trước cách đây
mục cha
commit
c3037b8bd2

+ 29 - 12
src/core/streams/FileStream.cpp

@@ -55,6 +55,31 @@ FileStream::~FileStream()
 	m_file = 0;
 }
 
+//-----------------------------------------------------------------------------
+void FileStream::seek(size_t position)
+{
+	check_valid();
+
+	//flush(); <<<---?
+	fseek(m_file->get_handle(), position, SEEK_SET);
+}
+
+//-----------------------------------------------------------------------------
+void FileStream::seek_to_end()
+{
+	check_valid();
+
+	fseek(m_file->get_handle(), 0, SEEK_END);
+}
+
+//-----------------------------------------------------------------------------
+void FileStream::skip(size_t bytes)
+{
+	check_valid();
+
+	fseek(m_file->get_handle(), bytes, SEEK_CUR);
+}
+
 //-----------------------------------------------------------------------------
 uint8_t FileStream::read_byte()
 {
@@ -77,7 +102,7 @@ uint8_t FileStream::read_byte()
 }
 
 //-----------------------------------------------------------------------------
-void FileStream::read_data_block(void* buffer, size_t size)
+void FileStream::read(void* buffer, size_t size)
 {
 	check_valid();
 
@@ -121,7 +146,7 @@ bool FileStream::copy_to(Stream* stream, size_t size)
 			{
 				if (readBytes != 0)
 				{
-					stream->write_data_block(buff, readBytes);
+					stream->write(buff, readBytes);
 				}
 			}
 
@@ -130,7 +155,7 @@ bool FileStream::copy_to(Stream* stream, size_t size)
 			return false;
 		}
 
-		stream->write_data_block(buff, readBytes);
+		stream->write(buff, readBytes);
 		totReadBytes += readBytes;
 	}
 
@@ -175,7 +200,7 @@ void FileStream::write_byte(uint8_t val)
 }
 
 //-----------------------------------------------------------------------------
-void FileStream::write_data_block(const void* buffer, size_t size)
+void FileStream::write(const void* buffer, size_t size)
 {
 	check_valid();
 
@@ -198,14 +223,6 @@ void FileStream::flush()
 	fflush(m_file->get_handle());
 }
 
-//-----------------------------------------------------------------------------
-void FileStream::seek(int32_t position, SeekMode mode)
-{
-	check_valid();
-	//flush(); <<<---?
-	fseek(m_file->get_handle(), position, (mode==SM_FROM_BEGIN)?SEEK_SET:(mode==SM_FROM_CURRENT)?SEEK_CUR:SEEK_END);
-}
-
 //-----------------------------------------------------------------------------
 size_t FileStream::position() const
 {

+ 15 - 10
src/core/streams/FileStream.h

@@ -53,18 +53,23 @@ public:
 						Destructor
 					*/
 	virtual			~FileStream();
-					/** @copydoc Stream::Seek() */
-	void			seek(int32_t position, SeekMode mode);
+
+					/** @copydoc Stream::seek() */
+	void			seek(size_t position);
+					/** @copydoc Stream::seek_to_end() */
+	void			seek_to_end();
+					/** @copydoc Stream::skip() */
+	void			skip(size_t bytes);
 					/** @copydoc Stream::ReadByte() */
 	uint8_t			read_byte();
 					/** @copydoc Stream::ReadDataBlock() */
-	void			read_data_block(void* buffer, size_t size);
-					/** @copydoc Stream::CopyTo() */
-	bool			copy_to(Stream* stream, size_t size = 0);
+	void			read(void* buffer, size_t size);
 					/** @copydoc Stream::WriteByte() */
 	void			write_byte(uint8_t val);
 					/** @copydoc Stream::WriteDataBlock() */
-	void			write_data_block(const void* buffer, size_t size);
+	void			write(const void* buffer, size_t size);
+					/** @copydoc Stream::CopyTo() */
+	bool			copy_to(Stream* stream, size_t size = 0);
 					/** @copydoc Stream::Flush() */
 	void			flush();
 					/** @copydoc Stream::EndOfStream() */
@@ -87,10 +92,10 @@ protected:
 	File*			m_file;
 	bool			m_last_was_read;
 
-	inline void check_valid() const
-	{
-		assert(m_file != NULL);
-	}
+	inline void		check_valid() const
+					{
+						assert(m_file != NULL);
+					}
 };
 
 } // namespace crown

+ 25 - 20
src/core/streams/MemoryStream.cpp

@@ -103,28 +103,33 @@ MemoryStream::~MemoryStream()
 }
 
 //-----------------------------------------------------------------------------
-void MemoryStream::seek(int32_t position, SeekMode mode)
+void MemoryStream::seek(size_t position)
 {
 	check_valid();
 	
-	switch (mode)
-	{
-		case SM_FROM_BEGIN:
-			m_memory_offset = position;
-			break;
-		case SM_FROM_CURRENT:
-			m_memory_offset += position;
-			break;
-		case SM_FROM_END:
-			m_memory_offset = m_memory->size()-1;
-			break;
-	}
+	m_memory_offset = position;
 
 	//Allow seek to m_memory->getSize() position, that means end of stream, reading not allowed but you can write if it's dynamic
-	if (m_memory_offset > m_memory->size())
-	{
-		Log::E("Seek beyond the end of stream.");
-	}
+	assert(m_memory_offset <= m_memory->size());
+}
+
+//-----------------------------------------------------------------------------
+void MemoryStream::seek_to_end()
+{
+	check_valid();
+
+	m_memory_offset = m_memory->size() - 1;
+}
+
+//-----------------------------------------------------------------------------
+void MemoryStream::skip(size_t bytes)
+{
+	check_valid();
+
+	m_memory_offset += bytes;
+
+	//Allow seek to m_memory->getSize() position, that means end of stream, reading not allowed but you can write if it's dynamic
+	assert(m_memory_offset <= m_memory->size());
 }
 
 //-----------------------------------------------------------------------------
@@ -141,7 +146,7 @@ uint8_t MemoryStream::read_byte()
 }
 
 //-----------------------------------------------------------------------------
-void MemoryStream::read_data_block(void* buffer, size_t size)
+void MemoryStream::read(void* buffer, size_t size)
 {
 	check_valid();
 	uint8_t* src = m_memory->data();
@@ -165,7 +170,7 @@ bool MemoryStream::copy_to(Stream* stream, size_t size)
 {
 	check_valid();
 
-	stream->write_data_block(&(m_memory->data()[m_memory_offset]), math::min(m_memory->size()-m_memory_offset, size));
+	stream->write(&(m_memory->data()[m_memory_offset]), math::min(m_memory->size()-m_memory_offset, size));
 
 	return true;
 }
@@ -179,7 +184,7 @@ void MemoryStream::write_byte(uint8_t val)
 }
 
 //-----------------------------------------------------------------------------
-void MemoryStream::write_data_block(const void* buffer, size_t size)
+void MemoryStream::write(const void* buffer, size_t size)
 {
 	check_valid();
 	m_memory->write((uint8_t*)buffer, m_memory_offset, size);

+ 9 - 5
src/core/streams/MemoryStream.h

@@ -93,14 +93,18 @@ public:
 						MemoryStream(MemoryBuffer* buffer, StreamOpenMode mode);
 	virtual				~MemoryStream();
 
-	void				seek(int32_t position, SeekMode mode);
+
+	void				seek(size_t position);
+	void				seek_to_end();
+	void				skip(size_t bytes);
 
 	uint8_t				read_byte();
-	void				read_data_block(void* buffer, size_t size);
+	void				read(void* buffer, size_t size);
+	void				write_byte(uint8_t val);
+	void				write(const void* buffer, size_t size);
+
 	bool				copy_to(Stream* stream, size_t size = 0);
 
-	void				write_byte(uint8_t val);
-	void				write_data_block(const void* buffer, size_t size);
 	void				flush();
 
 	bool				end_of_stream() const { return size() == m_memory_offset; }
@@ -117,7 +121,7 @@ public:
 
 protected:
 
-	inline void check_valid() { assert(m_memory != NULL); }
+	inline void			check_valid() { assert(m_memory != NULL); }
 
 	MemoryBuffer*		m_memory;
 	size_t				m_memory_offset;

+ 59 - 60
src/core/streams/NullStream.h

@@ -40,84 +40,83 @@ class NullStream: public Stream
 
 public:
 
-				/** @copydoc Stream::Stream() */
+				/// @copydoc Stream::Stream()
 				NullStream(StreamOpenMode mode) : Stream(mode) {}
-				/** @copydoc Stream::~Stream() */
+				/// @copydoc Stream::~Stream()
 	virtual		~NullStream() {}
 
-				/** @copydoc Stream::Seek() */
-	void		seek(int32_t /*position*/, uint8_t /*mode*/) {}
-				/**
-				@copydoc Stream::ReadByte()
-				@note
-					Returns always zero
-				*/
+				/// @copydoc Stream::seek()
+	void		seek(size_t position) {}
+
+				/// @copydoc Stream::seek_to_end()
+	void		seek_to_end() {}
+
+				/// @copydoc Stream::skip()
+	void		skip(size_t bytes) {}
+
+				/// @copydoc Stream::ReadByte()
+				/// @note
+				///	Returns always zero
 	uint8_t		read_byte() { return 0; }
-				/**
-				@copydoc Stream::ReadDataBlock()
-				@note
-					Fills buffer with zeroes
-				*/
-	void		read_data_block(void* buffer, size_t size)
+				
+				/// @copydoc Stream::ReadDataBlock()
+				/// @note
+				///	Fills buffer with zeroes
+	void		read(void* buffer, size_t size)
 				{
 					for (size_t i = 0; i < size; i++)
 					{
 						((uint8_t*)buffer)[i] = 0;
 					}
 				}
-				/**
-				@copydoc Stream::CopyTo()
-				@note
-					Returns always false
-				*/
-	bool		copy_to(Stream* /*stream*/, size_t /*size = 0*/) { return false; }
-				/** @copydoc Stream::WriteByte() */
+
+				/// @copydoc Stream::WriteByte()
 	void		write_byte(uint8_t /*val*/) {};
-				/** @copydoc Stream::WriteDataBlock() */
-	void		write_data_block(const void* /*buffer*/, size_t /*size*/) {};
-				/** @copydoc Stream::Flush() */
+
+				/// @copydoc Stream::WriteDataBlock()
+	void		write(const void* /*buffer*/, size_t /*size*/) {};
+
+				/// @copydoc Stream::CopyTo()
+				/// @note
+				///	Returns always false
+	bool		copy_to(Stream* /*stream*/, size_t /*size = 0*/) { return false; }
+
+				/// @copydoc Stream::Flush()
 	void		flush() {};
-				/**
-				@copydoc Stream::IsValid()
-				@note
-					Returns always true
-				*/
+				
+				/// @copydoc Stream::IsValid()
+				/// @note
+				///	Returns always true
 	bool		is_valid() { return true; }
-				/**
-				@copydoc Stream::EndOfStream()
-				@note
-					Returns always false
-				*/
+				
+				/// @copydoc Stream::EndOfStream()
+				/// @note
+				///	Returns always false
 	bool		end_of_stream() { return false; }
-				/**
-				@copydoc Stream::GetSize()
-				@note
-					Returns always 0xFFFFFFFF
-				*/
+				
+				/// @copydoc Stream::GetSize()
+				/// @note
+				///	Returns always 0xFFFFFFFF
 	size_t		size() { return ~0; }
-				/**
-				@copydoc Stream::GetPosition()
-				@note
-					Returns always zero
-				*/
+				
+				/// @copydoc Stream::GetPosition()
+				/// @note
+				///	Returns always zero
 	size_t		position() { return 0; }
-				/**
-				@copydoc Stream::CanRead()
-				@note
-					Returns always true
-				*/
+				
+				/// @copydoc Stream::CanRead()
+				/// @note
+				///	Returns always true
 	bool		can_read() { return true; }
-				/**
-				@copydoc Stream::CanWrite()
-				@note
-					Returns always true
-				*/
+				
+				/// @copydoc Stream::CanWrite()
+				/// @note
+				///	Returns always true
 	bool		can_write() { return true; }
-				/**
-				@copydoc Stream::CanSeek()
-				@note
-					Returns always true
-				*/
+				
+				/// @copydoc Stream::CanSeek()
+				/// @note
+				///	Returns always true
 	bool		can_seek() { return true; }
 };
 

+ 52 - 127
src/core/streams/Stream.cpp

@@ -25,128 +25,48 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #include "Stream.h"
 #include "Types.h"
-#include "zlib.h"
-#include "MathUtils.h"
+#include "Compressor.h"
+#include "MallocAllocator.h"
 
 namespace crown
 {
 
 //-----------------------------------------------------------------------------
-bool Stream::zip_to(Stream* stream, size_t size, size_t& zipped_size)
-{
-	const size_t CHUNK_SIZE = 16384;
-	int32_t ret, flush;
-	unsigned have;
-	z_stream strm;
-	unsigned char in[CHUNK_SIZE];
-	unsigned char out[CHUNK_SIZE];
-
-	strm.zalloc = Z_NULL;
-	strm.zfree = Z_NULL;
-	strm.opaque = Z_NULL;
-	ret = deflateInit(&strm, 6);
-	if (ret != Z_OK)
-		return false;
-
-	size_t bytes_read = 0;
-	do
-	{
-		size_t this_step_bytes = math::min(CHUNK_SIZE, size - bytes_read);
-		read_data_block(in, this_step_bytes);
+bool Stream::compress_to(Stream* stream, size_t size, size_t& zipped_size, Compressor* compressor)
+{
+	assert(stream != NULL);
+	assert(compressor != NULL);
 
-		strm.avail_in = this_step_bytes;
-		strm.next_in = in;
+	MallocAllocator allocator;
+	void* in_buffer = (void*)allocator.allocate(size);
 
-		flush = (size - bytes_read) <= CHUNK_SIZE ? Z_FINISH : Z_NO_FLUSH;
+	read(in_buffer, size);
 
-		do
-		{
-			strm.avail_out = CHUNK_SIZE;
-			strm.next_out = out;
+	void* compressed_buffer = compressor->compress(in_buffer, size, zipped_size);
 
-			ret = deflate(&strm, flush);    /* no bad return value */
-			assert(ret != Z_STREAM_ERROR);  /* state not clobbered */
+	stream->write(compressed_buffer, zipped_size);
 
-			have = CHUNK_SIZE - strm.avail_out;
-			if (have > 0)
-				stream->write_data_block(out, have);
-			
-		} while (strm.avail_out == 0);
-		assert(strm.avail_in == 0);     /* all input will be used */
+	return true;
+}
 
-		bytes_read += this_step_bytes;
-		/* done when last data in file processed */
-	} while (flush != Z_FINISH);
-	assert(ret == Z_STREAM_END);        /* stream will be complete */
+//-----------------------------------------------------------------------------
+bool Stream::uncompress_to(Stream* stream, size_t& unzipped_size, Compressor* compressor)
+{
+	assert(stream != NULL);
+	assert(compressor != NULL);
 
-	/* clean up and return */
-	(void)deflateEnd(&strm);
+	MallocAllocator allocator;
 
-	zipped_size = strm.total_out;
+	size_t stream_size = size();
+	void* in_buffer = (void*)allocator.allocate(stream_size); 
 
-	return true;
-}
+	read(in_buffer, stream_size);
 
-//-----------------------------------------------------------------------------
-bool Stream::unzip_to(Stream* stream, size_t& /*unzipped_size*/)
-{
-	const size_t CHUNK_SIZE = 16384;
-	int32_t ret;
-	unsigned have;
-	z_stream strm;
-	unsigned char in[CHUNK_SIZE];
-	unsigned char out[CHUNK_SIZE];
-
-	/* allocate inflate state */
-	strm.zalloc = Z_NULL;
-	strm.zfree = Z_NULL;
-	strm.opaque = Z_NULL;
-	strm.avail_in = 0;
-	strm.next_in = Z_NULL;
-	ret = inflateInit(&strm);
-	if (ret != Z_OK)
-			return false;
-
-	size_t size = this->size();
-	size_t bytes_read = 0;
-
-	/* decompress until deflate stream ends or end of file */
-	do
-	{
-		size_t this_step_bytes = math::min(CHUNK_SIZE, size - bytes_read);
-		read_data_block(in, this_step_bytes);
-
-		strm.avail_in = this_step_bytes;
-		strm.next_in = in;
-		if (strm.avail_in == 0)
-				break;
-
-		/* run inflate() on input until output buffer not full */
-		do {
-				strm.avail_out = CHUNK_SIZE;
-				strm.next_out = out;
-				ret = inflate(&strm, Z_NO_FLUSH);
-				assert(ret != Z_STREAM_ERROR);  /* state not clobbered */
-				switch (ret) {
-				case Z_NEED_DICT:
-						ret = Z_DATA_ERROR;     /* and fall through */
-				case Z_DATA_ERROR:
-				case Z_MEM_ERROR:
-						(void)inflateEnd(&strm);
-						return false;
-				}
-				have = CHUNK_SIZE - strm.avail_out;
-				if (have > 0)
-					stream->write_data_block(out, have);
-		} while (strm.avail_out == 0);
-
-		bytes_read += this_step_bytes;
-		/* done when inflate() says it's done */
-	} while (ret != Z_STREAM_END);
-
-	/* clean up and return */
-	(void)inflateEnd(&strm);
-	return ret == Z_STREAM_END;
+	void* uncompressed_buffer = compressor->uncompress(in_buffer, stream_size, unzipped_size);
+
+	stream->write(uncompressed_buffer, unzipped_size);
+
+	return true;
 }
 
 //-----------------------------------------------------------------------------
@@ -165,7 +85,7 @@ BinaryReader::~BinaryReader()
 int8_t BinaryReader::read_byte()
 {
 	int8_t buffer;
-	m_stream->read_data_block(&buffer, sizeof(int8_t));
+	m_stream->read(&buffer, sizeof(int8_t));
 	return buffer;
 }
 
@@ -173,7 +93,7 @@ int8_t BinaryReader::read_byte()
 int16_t BinaryReader::read_int16()
 {
 	int16_t buffer;
-	m_stream->read_data_block(&buffer, sizeof(int16_t));
+	m_stream->read(&buffer, sizeof(int16_t));
 	return buffer;
 }
 
@@ -181,7 +101,7 @@ int16_t BinaryReader::read_int16()
 uint16_t BinaryReader::read_uint16()
 {
 	uint16_t buffer;
-	m_stream->read_data_block(&buffer, sizeof(uint16_t));
+	m_stream->read(&buffer, sizeof(uint16_t));
 	return buffer;
 }
 
@@ -189,7 +109,7 @@ uint16_t BinaryReader::read_uint16()
 int32_t BinaryReader::read_int32()
 {
 	int32_t buffer;
-	m_stream->read_data_block(&buffer, sizeof(int32_t));
+	m_stream->read(&buffer, sizeof(int32_t));
 	return buffer;
 }
 
@@ -197,7 +117,7 @@ int32_t BinaryReader::read_int32()
 uint32_t BinaryReader::read_uint32()
 {
 	uint32_t buffer;
-	m_stream->read_data_block(&buffer, sizeof(uint32_t));
+	m_stream->read(&buffer, sizeof(uint32_t));
 	return buffer;
 }
 
@@ -205,7 +125,7 @@ uint32_t BinaryReader::read_uint32()
 int64_t BinaryReader::read_int64()
 {
 	int64_t buffer;
-	m_stream->read_data_block(&buffer, sizeof(int64_t));
+	m_stream->read(&buffer, sizeof(int64_t));
 	return buffer;
 }
 
@@ -213,14 +133,14 @@ int64_t BinaryReader::read_int64()
 double BinaryReader::read_double()
 {
 	double buffer;
-	m_stream->read_data_block(&buffer, sizeof(double));
+	m_stream->read(&buffer, sizeof(double));
 	return buffer;
 }
 
 float BinaryReader::read_float()
 {
 	float buffer;
-	m_stream->read_data_block(&buffer, sizeof(float));
+	m_stream->read(&buffer, sizeof(float));
 	return buffer;
 }
 
@@ -239,49 +159,49 @@ BinaryWriter::~BinaryWriter()
 //-----------------------------------------------------------------------------
 void BinaryWriter::write_byte(int8_t buffer)
 {
-	m_stream->write_data_block(&buffer, sizeof(int8_t));
+	m_stream->write(&buffer, sizeof(int8_t));
 }
 
 //-----------------------------------------------------------------------------
 void BinaryWriter::write_int16(int16_t buffer)
 {
-	m_stream->write_data_block(&buffer, sizeof(int16_t));
+	m_stream->write(&buffer, sizeof(int16_t));
 }
 
 //-----------------------------------------------------------------------------
 void BinaryWriter::write_uint16(uint16_t buffer)
 {
-	m_stream->write_data_block(&buffer, sizeof(uint16_t));
+	m_stream->write(&buffer, sizeof(uint16_t));
 }
 
 //-----------------------------------------------------------------------------
 void BinaryWriter::write_int32(int32_t buffer)
 {
-	m_stream->write_data_block(&buffer, sizeof(int32_t));
+	m_stream->write(&buffer, sizeof(int32_t));
 }
 
 //-----------------------------------------------------------------------------
 void BinaryWriter::write_uint32(uint32_t buffer)
 {
-	m_stream->write_data_block(&buffer, sizeof(uint32_t));
+	m_stream->write(&buffer, sizeof(uint32_t));
 }
 
 //-----------------------------------------------------------------------------
 void BinaryWriter::write_int64(int64_t buffer)
 {
-	m_stream->write_data_block(&buffer, sizeof(int64_t));
+	m_stream->write(&buffer, sizeof(int64_t));
 }
 
 //-----------------------------------------------------------------------------
 void BinaryWriter::write_double(double buffer)
 {
-	m_stream->write_data_block(&buffer, sizeof(double));
+	m_stream->write(&buffer, sizeof(double));
 }
 
 //-----------------------------------------------------------------------------
 void BinaryWriter::write_float(float buffer)
 {
-	m_stream->write_data_block(&buffer, sizeof(float));
+	m_stream->write(&buffer, sizeof(float));
 }
 
 //-----------------------------------------------------------------------------
@@ -289,11 +209,16 @@ void BinaryWriter::insert_byte(int8_t val, size_t offset)
 {
 	size_t tmpSize = m_stream->size() - offset;
 	int8_t* tmp = new int8_t[tmpSize];
-	m_stream->seek(offset, SM_FROM_BEGIN);
-	m_stream->read_data_block(tmp, tmpSize);
-	m_stream->seek(offset, SM_FROM_BEGIN);
+
+	m_stream->seek(offset);
+	m_stream->read(tmp, tmpSize);
+
+	m_stream->seek(offset);
+
 	m_stream->write_byte(val);
-	m_stream->write_data_block(tmp, tmpSize);
+
+	m_stream->write(tmp, tmpSize);
+
 	delete[] tmp;
 }
 

+ 70 - 90
src/core/streams/Stream.h

@@ -30,113 +30,93 @@ OTHER DEALINGS IN THE SOFTWARE.
 namespace crown
 {
 
-enum SeekMode
-{
-	SM_FROM_BEGIN	= 0,
-	SM_FROM_CURRENT	= 1,
-	SM_FROM_END		= 2
-};
-
 enum StreamOpenMode
 {
 	SOM_READ		= 1,
 	SOM_WRITE		= 2
 };
 
-/**
-	An abstraction to access data streams.
+class Compressor;
 
-	It represents a flow of data attached to a 'file' which can be an archived file,
-	a regular file, a location in memory or anything that can be read or wrote.
-	A Stream is an abstraction to interact with these in an uniform way; every stream
-	comes with a convenient set of methods to facilitate reading from it, writing to
-	it and so on.
-*/
+/// An abstraction to access data streams.
+/// 
+/// It represents a flow of data attached to a 'file' which can be an archived file,
+/// a regular file, a location in memory or anything that can be read or wrote.
+/// A Stream is an abstraction to interact with these in an uniform way; every stream
+/// comes with a convenient set of methods to facilitate reading from it, writing to
+/// it and so on.
 class Stream
 {
 public:
 
-						/**
-							Constructor.
-						*/
+						/// Constructor
 						Stream(StreamOpenMode mode) : m_open_mode(mode) {}
-						/**
-							Destructor.
-						*/
+
+						/// Destructor
 	virtual				~Stream() {};
-						/**
-							Sets the position indicator of the stream to position.
-						*/
-	virtual void		seek(int32_t position, SeekMode mode) = 0;
-						/**
-							Reads a byte from the stream starting at current position.
-						*/
+
+						/// Sets the position indicator of the stream to position.
+	virtual void		seek(size_t position) = 0;
+
+						/// Sets the position indicator to the end of the stream
+	virtual void		seek_to_end() = 0;
+
+						/// Sets the position indicator to bytes after current position
+	virtual void		skip(size_t bytes) = 0;
+
+						/// Reads a byte from the stream starting at current position.
 	virtual uint8_t		read_byte() = 0;
-						/**
-							Reads a block of data from the stream.
-						*/
-	virtual void		read_data_block(void* buffer, size_t size) = 0;
-						/**
-							Copies a chunk of 'size' bytes of data from this to another stream.
-						*/
-	virtual bool		copy_to(Stream* stream, size_t size = 0) = 0;
-						/**
-							Zips a chunk of 'size' bytes of data from this to another stream. A default implementation is given
-						*/
-	virtual bool		zip_to(Stream* stream, size_t size, size_t& zipped_size);
-						/**
-							Unzip a zipped stream of data from this to another stream. A default implementation is given
-						*/
-	virtual bool		unzip_to(Stream* stream, size_t& unzipped_size);
-						/**
-							Writes a byte to the stream starting at current position.
-						*/
+
+						/// Reads a block of data from the stream.
+	virtual void		read(void* buffer, size_t size) = 0;
+
+						/// Writes a byte to the stream starting at current position.
 	virtual void		write_byte(uint8_t val) = 0;
-						/**
-							Writes a block of data to the stream.
-						*/
-	virtual void		write_data_block(const void* buffer, size_t size) = 0;
-						/**
-							Forces the previouses write operations to complete.
-							Generally, when a Stream is attached to a file,
-							write operations are not performed instantly, the output data
-							may be stored to a temporary buffer before making its way to
-							the file. This method forces all the pending output operations
-							to be written to the stream.
-						*/
+
+						/// Writes a block of data to the stream.
+	virtual void		write(const void* buffer, size_t size) = 0;
+
+						/// Copies a chunk of 'size' bytes of data from this to another stream.
+	virtual bool		copy_to(Stream* stream, size_t size = 0) = 0;
+
+						/// Zips a chunk of 'size' bytes of data from this to another stream using compressor.
+	virtual bool		compress_to(Stream* stream, size_t size, size_t& compressed_size, Compressor* compressor);
+
+						/// Unzip a zipped stream of data from this to another stream using compressor.
+	virtual bool		uncompress_to(Stream* stream, size_t& uncompressed_size, Compressor* compressor);
+
+						/// Forces the previouses write operations to complete.
+						/// Generally, when a Stream is attached to a file,
+						/// write operations are not performed instantly, the output data
+						/// may be stored to a temporary buffer before making its way to
+						/// the file. This method forces all the pending output operations
+						/// to be written to the stream.
 	virtual void		flush() = 0;
-						/**
-							Returns whether the stream is valid.
-							A stream is valid when the buffer where it operates
-							exists. (i.e. a file descriptor is attached to the stream, 
-							a memory area is attached to the stream etc.)
-						*/
+
+						/// Returns whether the stream is valid.
+						/// A stream is valid when the buffer where it operates
+						/// exists. (i.e. a file descriptor is attached to the stream, 
+						/// a memory area is attached to the stream etc.)
 	virtual bool		is_valid() const = 0;
-						/**
-							Returns whether the position is at end of stream.
-						*/
+
+						/// Returns whether the position is at end of stream.
 	virtual bool		end_of_stream() const = 0;
-						/**
-							Returns the size of stream in bytes.
-						*/
+
+						/// Returns the size of stream in bytes.
 	virtual size_t		size() const = 0;
-						/**
-							Returns the current position in stream.
-							Generally, for binary data, it means the number of bytes
-							from the beginning of the stream.
-						*/
+
+						/// Returns the current position in stream.
+						/// Generally, for binary data, it means the number of bytes
+						/// from the beginning of the stream.
 	virtual size_t		position() const = 0;
-						/**
-							Returns whether the stream can be read.
-						*/
+
+						/// Returns whether the stream can be read.
 	virtual bool		can_read() const = 0;
-						/**
-							Returns whether the stream can be wrote.
-						*/
+
+						/// Returns whether the stream can be wrote.
 	virtual bool		can_write() const = 0;
-						/**
-							Returns whether the stream can be sought.
-						*/
+
+						/// Returns whether the stream can be sought.
 	virtual bool		can_seek() const = 0;
 
 protected:
@@ -144,7 +124,7 @@ protected:
 	StreamOpenMode		m_open_mode;
 };
 
-//! A reader that offers a convenient way to read from a stream
+///! A reader that offers a convenient way to read from a stream
 class BinaryReader
 {
 
@@ -170,7 +150,7 @@ private:
 	Stream*				m_stream;
 };
 
-//! A writer that offers a convenient way to write to a stream
+///! A writer that offers a convenient way to write to a stream
 class BinaryWriter
 {
 
@@ -198,7 +178,7 @@ private:
 	Stream*				m_stream;
 };
 
-//! A reader that offers a convenient way to read text to a stream
+///! A reader that offers a convenient way to read text to a stream
 class TextReader
 {
 
@@ -228,7 +208,7 @@ private:
 	Stream*				m_stream;
 };
 
-//! A reader that offers a convenient way to write text to a stream
+///! A reader that offers a convenient way to write text to a stream
 class TextWriter
 {
 
@@ -254,5 +234,5 @@ private:
 	Stream*				m_stream;
 };
 
-} // namespace crown
+} /// namespace crown
 

+ 8 - 8
src/loaders/BMPImageLoader.cpp

@@ -92,7 +92,7 @@ Image* BMPImageLoader::LoadFile(const char* relativePath)
 		return NULL;
 	}
 
-	fileStream->read_data_block(&magicNumber, 2);
+	fileStream->read(&magicNumber, 2);
 
 	if (magicNumber != 19778)
 	{
@@ -102,9 +102,9 @@ Image* BMPImageLoader::LoadFile(const char* relativePath)
 		return NULL;
 	}
 
-	fileStream->seek(0, SM_FROM_BEGIN);
-	fileStream->read_data_block(&bfh, sizeof(BitmapFileHeader));
-	fileStream->read_data_block(&bih, sizeof(BitmapInfoHeader));
+	fileStream->seek(0);
+	fileStream->read(&bfh, sizeof(BitmapFileHeader));
+	fileStream->read(&bih, sizeof(BitmapInfoHeader));
 
 	int32_t bpp = (bih.biBitCount/8);
 
@@ -132,7 +132,7 @@ Image* BMPImageLoader::LoadFile(const char* relativePath)
 
 		for (int32_t i=0; i<bih.biHeight ; i++)
 		{
-			fileStream->read_data_block(tmpdata, bih.biWidth*3);
+			fileStream->read(tmpdata, bih.biWidth*3);
 
 			int32_t offset = bih.biWidth * 4 * i;
 
@@ -146,7 +146,7 @@ Image* BMPImageLoader::LoadFile(const char* relativePath)
 
 			if (padSize)
 			{
-				fileStream->seek(padSize, SM_FROM_CURRENT);
+				fileStream->skip(padSize);
 			}
 		}
 
@@ -158,11 +158,11 @@ Image* BMPImageLoader::LoadFile(const char* relativePath)
 		{
 			int32_t offset = bih.biWidth * 4 * i;
 
-			fileStream->read_data_block(&data[offset], bih.biWidth*4);
+			fileStream->read(&data[offset], bih.biWidth*4);
 
 			if (padSize)
 			{
-				fileStream->seek(padSize, SM_FROM_CURRENT);
+				fileStream->skip(padSize);
 			}
 		}
 	}

+ 9 - 9
src/loaders/TGAImageLoader.cpp

@@ -52,9 +52,9 @@ Image* TGAImageLoader::LoadFile(const char* relativePath)
 		return NULL;
 	}
 
-	fileStream->read_data_block(&mTGAHeader, sizeof(mTGAHeader));
+	fileStream->read(&mTGAHeader, sizeof(mTGAHeader));
 	// Skip ID
-	fileStream->seek(mTGAHeader.id_length, SM_FROM_CURRENT);
+	fileStream->skip(mTGAHeader.id_length);
 
 	Image* image = NULL;
 
@@ -97,7 +97,7 @@ Image* TGAImageLoader::LoadUncompressedData(Stream* fp)
 		for (uint64_t i = 0; i < size * channels; i++)
 		{
 			uint16_t pixel_data;
-			fp->read_data_block(&pixel_data, sizeof(pixel_data));
+			fp->read(&pixel_data, sizeof(pixel_data));
 			data[j] = (pixel_data & 0x7c) >> 10;
 			data[j+1] = (pixel_data & 0x3e) >> 5;
 			data[j+2] = (pixel_data & 0x1f);
@@ -107,7 +107,7 @@ Image* TGAImageLoader::LoadUncompressedData(Stream* fp)
 	else
 	{
 		data = new uint8_t[(uint32_t)(size * channels)];
-		fp->read_data_block(data, (size_t)(size * channels));
+		fp->read(data, (size_t)(size * channels));
 		SwapRedBlue(data, size * channels, channels);
 	}
 
@@ -140,12 +140,12 @@ Image* TGAImageLoader::LoadCompressedData(Stream* fp)
 
 	while (i < size)
 	{
-		fp->read_data_block(&rle_id, sizeof(uint8_t));
+		fp->read(&rle_id, sizeof(uint8_t));
 
 		if (rle_id & 0x80)   // Se il bit più significativo è ad 1
 		{
 			rle_id -= 127;
-			fp->read_data_block(colors, channels);
+			fp->read(colors, channels);
 
 			while (rle_id)
 			{
@@ -169,7 +169,7 @@ Image* TGAImageLoader::LoadCompressedData(Stream* fp)
 
 			while (rle_id)
 			{
-				fp->read_data_block(colors, channels);
+				fp->read(colors, channels);
 				data[colors_read] = colors[2];
 				data[colors_read+1] = colors[1];
 				data[colors_read+2] = colors[0];
@@ -230,8 +230,8 @@ void TGAImageLoader::SaveFile(const Image* image, const char* relativePath)
 
 	if (fileStream)
 	{
-		fileStream->write_data_block(&header, sizeof(header));
-		fileStream->write_data_block(image->GetBuffer(), image->GetWidth() * image->GetHeight() * image->GetBytesPerPixel());
+		fileStream->write(&header, sizeof(header));
+		fileStream->write(image->GetBuffer(), image->GetWidth() * image->GetHeight() * image->GetBytesPerPixel());
 
 		GetFilesystem()->Close(fileStream);
 	}