From d1e486136db52b466b788c1d81a5c88b6f24acd9 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 2 Nov 2006 06:14:41 +0000 Subject: Added the SBuffer class, a memory buffer that can be used like a stream. Yay! --- src/sbuffer.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/sbuffer.cpp (limited to 'src/sbuffer.cpp') diff --git a/src/sbuffer.cpp b/src/sbuffer.cpp new file mode 100644 index 0000000..00324b5 --- /dev/null +++ b/src/sbuffer.cpp @@ -0,0 +1,62 @@ +#include +#include "sbuffer.h" + +SBuffer::SBuffer() : + bOpen( true ), + nPos( 0 ) +{ +} + +SBuffer::~SBuffer() +{ +} + +void SBuffer::close() +{ + bOpen = false; + fbData.clearData(); +} + +size_t SBuffer::read( char *pBuf, size_t nBytes ) +{ + long nLeft = fbData.getLength() - nPos; + if( nBytes > nLeft ) + nBytes = nLeft; + + if( nLeft == 0 ) + return 0; + + memcpy( pBuf, fbData.getData()+nPos, nBytes ); + nPos += nBytes; + + return nBytes; +} + +size_t SBuffer::write( const char *pBuf, size_t nBytes ) +{ + fbData.appendData( pBuf, nBytes ); + nPos += nBytes; + + return nBytes; +} + +long SBuffer::tell() +{ + return nPos; +} + +void SBuffer::seek( long offset ) +{ + nPos += offset; +} + +void SBuffer::setPos( long pos ) +{ + nPos = pos; +} + +void SBuffer::setPosEnd( long pos ) +{ + nPos = fbData.getLength() - pos; +} + -- cgit v1.2.3