From 9e4d15b6dec9a7f9358855faeb96b1ac767a15e6 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 25 Jun 2019 16:33:14 -0700 Subject: Got most of the functions in, now to test them. --- src/unstable/blobbuilder.cpp | 129 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 127 insertions(+), 2 deletions(-) (limited to 'src/unstable/blobbuilder.cpp') diff --git a/src/unstable/blobbuilder.cpp b/src/unstable/blobbuilder.cpp index 7ea159d..d4b458b 100644 --- a/src/unstable/blobbuilder.cpp +++ b/src/unstable/blobbuilder.cpp @@ -60,6 +60,22 @@ void Bu::BlobBuilderCore::Chunk::append( const char *&pSrc, int32_t &iLength ) iLength -= iCopy; } +Bu::BlobBuilderCore::Chunk *Bu::BlobBuilderCore::Chunk::split( int32_t iIndex ) +{ + if( iIndex == 0 ) + return NULL; + + if( iIndex >= iLength ) + return NULL; + + Chunk *pNew = new Chunk( pData+iIndex, iLength-iIndex ); + iLength -= iIndex; + pNew->pNext = pNext; + pNext = pNew; + + return pNew; +} + ///// // BlobBuilderCore // @@ -119,12 +135,83 @@ void Bu::BlobBuilderCore::append( const char *pSrc, int32_t iLength ) } } +void Bu::BlobBuilderCore::prepend( const char *pSrc, int32_t iLength ) +{ + if( pFirst == 0 ) + { + pFirst = pLast = new Chunk( pSrc, iLength ); + } + else + { + Chunk *pNew = new Chunk( pSrc, iLength ); + pNew->pNext = pFirst; + pFirst = pNew; + } +} + +void Bu::BlobBuilderCore::insert( int32_t iBefore, const char *pSrc, + int32_t iLength ) +{ + if( iBefore <= 0 ) + { + prepend( pSrc, iLength ); + return; + } + if( iBefore >= this->iLength ) + { + append( pSrc, iLength ); + return; + } + + Chunk *pCur = pFirst; + while( pCur ) + { + if( iBefore == 0 ) + { + // Insert between chunks, no splitting required. + Chunk *pNew = new Chunk( pSrc, iLength ); + pNew->pNext = pCur->pNext; + pCur->pNext = pNew; + if( pLast == pCur ) + pLast = pNew; + } + if( iBefore < pCur->iLength ) + { + // This is the chunk we need to split. + Chunk *pNew = pCur->split( iBefore ); + if( pLast == pCur ) + pLast = pNew; + continue; + } + pCur = pCur->pNext; + } +} + void Bu::BlobBuilderCore::set( const char *pSrc, int32_t iLength ) { clear(); append( pSrc, iLength ); } +void Bu::BlobBuilderCore::copyTo( void *pDestRaw, int32_t iLength ) const +{ + char *pDest = reinterpret_cast(pDestRaw); + + Chunk *pCur = pFirst; + while( pCur && iLength ) + { + int32_t iChunkLen = pCur->iLength; + if( iChunkLen > iLength ) + iChunkLen = iLength; + + memcpy( pDest, pCur->pData, iChunkLen ); + pDest += iChunkLen; + iLength -= iChunkLen; + + pCur = pCur->pNext; + } +} + ////// // BlobBuilder // @@ -154,6 +241,11 @@ void Bu::BlobBuilder::set( const char *pSrc, int32_t iLength ) core->set( pSrc, iLength ); } +void Bu::BlobBuilder::set( const char *pSrc ) +{ + set( pSrc, strlen( pSrc ) ); +} + void Bu::BlobBuilder::append( const Blob &rSrc ) { _hardCopy(); @@ -166,24 +258,44 @@ void Bu::BlobBuilder::append( const char *pSrc, int32_t iLength ) core->append( pSrc, iLength ); } +void Bu::BlobBuilder::append( const char *pSrc ) +{ + append( pSrc, strlen( pSrc ) ); +} + void Bu::BlobBuilder::prepend( const Blob &rSrc ) { _hardCopy(); + core->prepend( rSrc.getData(), rSrc.getSize() ); } void Bu::BlobBuilder::prepend( const char *pSrc, int32_t iLength ) { _hardCopy(); + core->prepend( pSrc, iLength ); +} + +void Bu::BlobBuilder::prepend( const char *pSrc ) +{ + prepend( pSrc, strlen( pSrc ) ); } void Bu::BlobBuilder::insert( int32_t iBefore, const Blob &rSrc ) { _hardCopy(); + core->insert( iBefore, rSrc.getData(), rSrc.getSize() ); } -void Bu::BlobBuilder::insert( int32_t iBefore, const char *pSrc, const Bu::Blob &rSrc ) +void Bu::BlobBuilder::insert( int32_t iBefore, const char *pSrc, + int32_t iLength ) { _hardCopy(); + core->insert( iBefore, pSrc, iLength ); +} + +void Bu::BlobBuilder::insert( int32_t iBefore, const char *pSrc ) +{ + insert( iBefore, pSrc, strlen( pSrc ) ); } void Bu::BlobBuilder::clear() @@ -199,23 +311,36 @@ int32_t Bu::BlobBuilder::getSize() const Bu::Blob Bu::BlobBuilder::getBlob() const { - Blob bRet(); + return Blob( *this ); +} + +void Bu::BlobBuilder::copyTo( void *pDestRaw, int32_t iDestSize ) const +{ + core->copyTo( pDestRaw, iDestSize ); } Bu::BlobBuilder &Bu::BlobBuilder::operator=( const Blob &rSrc ) { + set( rSrc ); + return *this; } Bu::BlobBuilder &Bu::BlobBuilder::operator=( const char *pSrc ) { + set( pSrc ); + return *this; } Bu::BlobBuilder &Bu::BlobBuilder::operator+=( const Blob &rSrc ) { + append( rSrc ); + return *this; } Bu::BlobBuilder &Bu::BlobBuilder::operator+=( const char *pSrc ) { + append( pSrc ); + return *this; } -- cgit v1.2.3