From f402f28643b93d3ec7744ca408dee5588767cd78 Mon Sep 17 00:00:00 2001 From: castano Date: Wed, 15 Oct 2008 07:15:50 +0000 Subject: [PATCH] Use unsigned ints for stream sizes and positions. --- src/nvcore/StdStream.h | 51 +++++++++++++++++++++++++----------------- src/nvcore/Stream.h | 16 ++++++------- 2 files changed, 38 insertions(+), 29 deletions(-) diff --git a/src/nvcore/StdStream.h b/src/nvcore/StdStream.h index ee3055b..4268ab1 100644 --- a/src/nvcore/StdStream.h +++ b/src/nvcore/StdStream.h @@ -47,25 +47,25 @@ public: /** @name Stream implementation. */ //@{ - virtual void seek( int pos ) + virtual void seek( uint pos ) { nvDebugCheck(m_fp != NULL); nvDebugCheck(pos >= 0 && pos < size()); fseek(m_fp, pos, SEEK_SET); } - virtual int tell() const + virtual uint tell() const { nvDebugCheck(m_fp != NULL); return ftell(m_fp); } - virtual int size() const + virtual uint size() const { nvDebugCheck(m_fp != NULL); - int pos = ftell(m_fp); + uint pos = ftell(m_fp); fseek(m_fp, 0, SEEK_END); - int end = ftell(m_fp); + uint end = ftell(m_fp); fseek(m_fp, pos, SEEK_SET); return end; } @@ -117,11 +117,11 @@ public: /** @name Stream implementation. */ //@{ /// Write data. - virtual void serialize( void * data, int len ) + virtual uint serialize( void * data, uint len ) { nvDebugCheck(data != NULL); nvDebugCheck(m_fp != NULL); - fwrite(data, len, 1, m_fp); + return (uint)fwrite(data, 1, len, m_fp); } virtual bool isLoading() const @@ -156,11 +156,11 @@ public: /** @name Stream implementation. */ //@{ /// Read data. - virtual void serialize( void * data, int len ) + virtual uint serialize( void * data, uint len ) { nvDebugCheck(data != NULL); nvDebugCheck(m_fp != NULL); - fread(data, len, 1, m_fp); + return (uint)fread(data, 1, len, m_fp); } virtual bool isLoading() const @@ -184,33 +184,40 @@ class NVCORE_CLASS MemoryInputStream : public Stream public: /// Ctor. - MemoryInputStream( const uint8 * mem, int size ) : + MemoryInputStream( const uint8 * mem, uint size ) : m_mem(mem), m_ptr(mem), m_size(size) { } /** @name Stream implementation. */ //@{ /// Read data. - virtual void serialize( void * data, int len ) + virtual uint serialize( void * data, uint len ) { nvDebugCheck(data != NULL); nvDebugCheck(!isError()); + + uint left = m_size - tell(); + if (len > left) len = left; + memcpy( data, m_ptr, len ); m_ptr += len; + + return len; } - virtual void seek( int pos ) + virtual void seek( uint pos ) { nvDebugCheck(!isError()); m_ptr = m_mem + pos; nvDebugCheck(!isError()); } - virtual int tell() const + virtual uint tell() const { - return int(m_ptr - m_mem); + nvDebugCheck(m_ptr >= m_mem); + return uint(m_ptr - m_mem); } - virtual int size() const + virtual uint size() const { return m_size; } @@ -252,7 +259,7 @@ private: const uint8 * m_mem; const uint8 * m_ptr; - int m_size; + uint m_size; }; @@ -286,17 +293,19 @@ public: /** @name Stream implementation. */ //@{ /// Read data. - virtual void serialize( void * data, int len ) + virtual uint serialize( void * data, uint len ) { nvDebugCheck(data != NULL); - m_s->serialize( data, len ); + len = m_s->serialize( data, len ); if( m_s->isError() ) { throw std::exception(); } + + return len; } - virtual void seek( int pos ) + virtual void seek( uint pos ) { m_s->seek( pos ); @@ -305,12 +314,12 @@ public: } } - virtual int tell() const + virtual uint tell() const { return m_s->tell(); } - virtual int size() const + virtual uint size() const { return m_s->size(); } diff --git a/src/nvcore/Stream.h b/src/nvcore/Stream.h index ec18717..4a35120 100644 --- a/src/nvcore/Stream.h +++ b/src/nvcore/Stream.h @@ -41,17 +41,17 @@ public: ByteOrder byteOrder() const { return m_byteOrder; } - /// Serialize the given data. @@ Should return bytes serialized? - virtual void serialize( void * data, int len ) = 0; + /// Serialize the given data. + virtual uint serialize( void * data, uint len ) = 0; /// Move to the given position in the archive. - virtual void seek( int pos ) = 0; + virtual void seek( uint pos ) = 0; /// Return the current position in the archive. - virtual int tell() const = 0; + virtual uint tell() const = 0; /// Return the current size of the archive. - virtual int size() const = 0; + virtual uint size() const = 0; /// Determine if there has been any error. virtual bool isError() const = 0; @@ -136,13 +136,13 @@ public: protected: /// Serialize in the stream byte order. - Stream & byteOrderSerialize( void * v, int len ) { + Stream & byteOrderSerialize( void * v, uint len ) { if( m_byteOrder == getSystemByteOrder() ) { serialize( v, len ); } else { - for( int i=len-1; i>=0; i-- ) { - serialize( (uint8 *)v + i, 1 ); + for( uint i = len; i > 0; i-- ) { + serialize( (uint8 *)v + i - 1, 1 ); } } return *this;