Use unsigned ints for stream sizes and positions.

This commit is contained in:
castano 2008-10-15 07:15:50 +00:00
parent f047043eb2
commit f402f28643
2 changed files with 38 additions and 29 deletions

View File

@ -47,25 +47,25 @@ public:
/** @name Stream implementation. */ /** @name Stream implementation. */
//@{ //@{
virtual void seek( int pos ) virtual void seek( uint pos )
{ {
nvDebugCheck(m_fp != NULL); nvDebugCheck(m_fp != NULL);
nvDebugCheck(pos >= 0 && pos < size()); nvDebugCheck(pos >= 0 && pos < size());
fseek(m_fp, pos, SEEK_SET); fseek(m_fp, pos, SEEK_SET);
} }
virtual int tell() const virtual uint tell() const
{ {
nvDebugCheck(m_fp != NULL); nvDebugCheck(m_fp != NULL);
return ftell(m_fp); return ftell(m_fp);
} }
virtual int size() const virtual uint size() const
{ {
nvDebugCheck(m_fp != NULL); nvDebugCheck(m_fp != NULL);
int pos = ftell(m_fp); uint pos = ftell(m_fp);
fseek(m_fp, 0, SEEK_END); fseek(m_fp, 0, SEEK_END);
int end = ftell(m_fp); uint end = ftell(m_fp);
fseek(m_fp, pos, SEEK_SET); fseek(m_fp, pos, SEEK_SET);
return end; return end;
} }
@ -117,11 +117,11 @@ public:
/** @name Stream implementation. */ /** @name Stream implementation. */
//@{ //@{
/// Write data. /// Write data.
virtual void serialize( void * data, int len ) virtual uint serialize( void * data, uint len )
{ {
nvDebugCheck(data != NULL); nvDebugCheck(data != NULL);
nvDebugCheck(m_fp != NULL); nvDebugCheck(m_fp != NULL);
fwrite(data, len, 1, m_fp); return (uint)fwrite(data, 1, len, m_fp);
} }
virtual bool isLoading() const virtual bool isLoading() const
@ -156,11 +156,11 @@ public:
/** @name Stream implementation. */ /** @name Stream implementation. */
//@{ //@{
/// Read data. /// Read data.
virtual void serialize( void * data, int len ) virtual uint serialize( void * data, uint len )
{ {
nvDebugCheck(data != NULL); nvDebugCheck(data != NULL);
nvDebugCheck(m_fp != NULL); nvDebugCheck(m_fp != NULL);
fread(data, len, 1, m_fp); return (uint)fread(data, 1, len, m_fp);
} }
virtual bool isLoading() const virtual bool isLoading() const
@ -184,33 +184,40 @@ class NVCORE_CLASS MemoryInputStream : public Stream
public: public:
/// Ctor. /// Ctor.
MemoryInputStream( const uint8 * mem, int size ) : MemoryInputStream( const uint8 * mem, uint size ) :
m_mem(mem), m_ptr(mem), m_size(size) { } m_mem(mem), m_ptr(mem), m_size(size) { }
/** @name Stream implementation. */ /** @name Stream implementation. */
//@{ //@{
/// Read data. /// Read data.
virtual void serialize( void * data, int len ) virtual uint serialize( void * data, uint len )
{ {
nvDebugCheck(data != NULL); nvDebugCheck(data != NULL);
nvDebugCheck(!isError()); nvDebugCheck(!isError());
uint left = m_size - tell();
if (len > left) len = left;
memcpy( data, m_ptr, len ); memcpy( data, m_ptr, len );
m_ptr += len; m_ptr += len;
return len;
} }
virtual void seek( int pos ) virtual void seek( uint pos )
{ {
nvDebugCheck(!isError()); nvDebugCheck(!isError());
m_ptr = m_mem + pos; m_ptr = m_mem + pos;
nvDebugCheck(!isError()); 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; return m_size;
} }
@ -252,7 +259,7 @@ private:
const uint8 * m_mem; const uint8 * m_mem;
const uint8 * m_ptr; const uint8 * m_ptr;
int m_size; uint m_size;
}; };
@ -286,17 +293,19 @@ public:
/** @name Stream implementation. */ /** @name Stream implementation. */
//@{ //@{
/// Read data. /// Read data.
virtual void serialize( void * data, int len ) virtual uint serialize( void * data, uint len )
{ {
nvDebugCheck(data != NULL); nvDebugCheck(data != NULL);
m_s->serialize( data, len ); len = m_s->serialize( data, len );
if( m_s->isError() ) { if( m_s->isError() ) {
throw std::exception(); throw std::exception();
} }
return len;
} }
virtual void seek( int pos ) virtual void seek( uint pos )
{ {
m_s->seek( pos ); m_s->seek( pos );
@ -305,12 +314,12 @@ public:
} }
} }
virtual int tell() const virtual uint tell() const
{ {
return m_s->tell(); return m_s->tell();
} }
virtual int size() const virtual uint size() const
{ {
return m_s->size(); return m_s->size();
} }

View File

@ -41,17 +41,17 @@ public:
ByteOrder byteOrder() const { return m_byteOrder; } ByteOrder byteOrder() const { return m_byteOrder; }
/// Serialize the given data. @@ Should return bytes serialized? /// Serialize the given data.
virtual void serialize( void * data, int len ) = 0; virtual uint serialize( void * data, uint len ) = 0;
/// Move to the given position in the archive. /// 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. /// Return the current position in the archive.
virtual int tell() const = 0; virtual uint tell() const = 0;
/// Return the current size of the archive. /// Return the current size of the archive.
virtual int size() const = 0; virtual uint size() const = 0;
/// Determine if there has been any error. /// Determine if there has been any error.
virtual bool isError() const = 0; virtual bool isError() const = 0;
@ -136,13 +136,13 @@ public:
protected: protected:
/// Serialize in the stream byte order. /// Serialize in the stream byte order.
Stream & byteOrderSerialize( void * v, int len ) { Stream & byteOrderSerialize( void * v, uint len ) {
if( m_byteOrder == getSystemByteOrder() ) { if( m_byteOrder == getSystemByteOrder() ) {
serialize( v, len ); serialize( v, len );
} }
else { else {
for( int i=len-1; i>=0; i-- ) { for( uint i = len; i > 0; i-- ) {
serialize( (uint8 *)v + i, 1 ); serialize( (uint8 *)v + i - 1, 1 );
} }
} }
return *this; return *this;