/* * Copyright (C) 2007-2019 Xagasoft, All rights reserved. * * This file is part of the libbu++ library and is released under the * terms of the license contained in the file LICENSE. */ #include "bu/blob.h" #include "bu/exceptioninvaliditerator.h" #include Bu::Blob::Blob() : pData( 0 ), iSize( -1 ) { } Bu::Blob::Blob( const Bu::Blob &rSrc ) : pData( 0 ), iSize( rSrc.iSize ) { pData = new char[rSrc.iSize+1]; memcpy( pData, rSrc.pData, iSize+1 ); } Bu::Blob::Blob( const char *pSrc ) : pData( 0 ), iSize( 0 ) { for(; iSize < __INT_MAX__ && pSrc[iSize]; iSize++ ) { } pData = new char[iSize+1]; memcpy( pData, pSrc, iSize+1 ); } Bu::Blob::Blob( const void *pSrc, int32_t iSize ) { this->iSize = iSize; pData = new char[iSize+1]; memcpy( pData, pSrc, iSize ); pData[iSize] = '\0'; } Bu::Blob::~Blob() { delete[] pData; pData = 0; iSize = -1; } int32_t Bu::Blob::getSize() const { return iSize; } char *Bu::Blob::getData() const { return pData; } char *Bu::Blob::c_str() const { return pData; } Bu::Blob &Bu::Blob::operator=( const Bu::Blob &rRhs ) { delete[] pData; iSize = rRhs.iSize; pData = new char[iSize+1]; memcpy( pData, rRhs.pData, iSize+1 ); return *this; } Bu::Blob &Bu::Blob::operator=( const char *pRhs ) { delete[] pData; for(iSize = 0; iSize < __INT_MAX__ && pRhs[iSize]; iSize++ ) { } pData = new char[iSize+1]; memcpy( pData, pRhs, iSize+1 ); return *this; } Bu::Blob::iterator::iterator( Bu::Blob *pBlob, bool bForward ) : pBlob( pBlob ), iIndex( bForward?0:pBlob->iSize-1 ), bForward( bForward ) { } Bu::Blob::iterator::iterator() : pBlob( NULL ), iIndex( -1 ), bForward( true ) { } Bu::Blob::iterator::iterator( const Bu::Blob::iterator &rSrc ) : pBlob( rSrc.pBlob ), iIndex( rSrc.iIndex ), bForward( rSrc.bForward ) { } bool Bu::Blob::iterator::isValid() const { if( !pBlob ) return false; if( iIndex < 0 || iIndex >= pBlob->iSize ) return false; return true; } Bu::Blob::iterator::operator bool() const { return isValid(); } char &Bu::Blob::iterator::operator *() { if( !isValid() ) throw Bu::ExceptionInvalidIterator(); return pBlob->pData[iIndex]; } Bu::Blob::iterator &Bu::Blob::iterator::operator++( int ) { if( bForward ) ++iIndex; else --iIndex; return *this; } Bu::Blob::iterator &Bu::Blob::iterator::operator++() { if( bForward ) ++iIndex; else --iIndex; return *this; } Bu::Blob::iterator &Bu::Blob::iterator::operator--( int ) { if( bForward ) --iIndex; else ++iIndex; return *this; } Bu::Blob::iterator &Bu::Blob::iterator::operator--() { if( bForward ) --iIndex; else ++iIndex; return *this; } bool Bu::Blob::iterator::operator==( const Bu::Blob::iterator &rRhs ) { if( isValid() == false && rRhs.isValid() == false ) return true; return pBlob == rRhs.pBlob && iIndex == rRhs.iIndex; } bool Bu::Blob::iterator::operator==( const Bu::Blob::const_iterator &rRhs ) { if( isValid() == false && rRhs.isValid() == false ) return true; return pBlob == rRhs.pBlob && iIndex == rRhs.iIndex; } bool Bu::Blob::iterator::operator!=( const Bu::Blob::iterator &rRhs ) { if( isValid() != rRhs.isValid() ) return true; return pBlob != rRhs.pBlob || iIndex != rRhs.iIndex; } bool Bu::Blob::iterator::operator!=( const Bu::Blob::const_iterator &rRhs ) { if( isValid() != rRhs.isValid() ) return true; return pBlob != rRhs.pBlob || iIndex != rRhs.iIndex; } Bu::Blob::iterator &Bu::Blob::iterator::operator=( Bu::Blob::iterator &rRhs ) { pBlob = rRhs.pBlob; iIndex = rRhs.iIndex; bForward = rRhs.bForward; return *this; } Bu::Blob::const_iterator::const_iterator( const Blob *pBlob, bool bForward ) : pBlob( pBlob ), iIndex( bForward?0:pBlob->iSize-1 ), bForward( bForward ) { } Bu::Blob::const_iterator::const_iterator() : pBlob( NULL ), iIndex( -1 ), bForward( true ) { } Bu::Blob::const_iterator::const_iterator( const Bu::Blob::const_iterator &rSrc ) : pBlob( rSrc.pBlob ), iIndex( rSrc.iIndex ), bForward( rSrc.bForward ) { } Bu::Blob::const_iterator::const_iterator( const Bu::Blob::iterator &rSrc ) : pBlob( rSrc.pBlob ), iIndex( rSrc.iIndex ), bForward( rSrc.bForward ) { } bool Bu::Blob::const_iterator::isValid() const { if( !pBlob ) return false; if( iIndex < 0 || iIndex >= pBlob->iSize ) return false; return true; } Bu::Blob::const_iterator::operator bool() const { return isValid(); } char Bu::Blob::const_iterator::operator *() const { if( !isValid() ) throw Bu::ExceptionInvalidIterator(); return pBlob->pData[iIndex]; } Bu::Blob::const_iterator &Bu::Blob::const_iterator::operator++( int ) { if( bForward ) ++iIndex; else --iIndex; return *this; } Bu::Blob::const_iterator &Bu::Blob::const_iterator::operator++() { if( bForward ) ++iIndex; else --iIndex; return *this; } Bu::Blob::const_iterator &Bu::Blob::const_iterator::operator--( int ) { if( bForward ) ++iIndex; else --iIndex; return *this; } Bu::Blob::const_iterator &Bu::Blob::const_iterator::operator--() { if( bForward ) ++iIndex; else --iIndex; return *this; } bool Bu::Blob::const_iterator::operator==( const Bu::Blob::iterator &rRhs ) { if( isValid() == false && rRhs.isValid() == false ) return true; return pBlob == rRhs.pBlob && iIndex == rRhs.iIndex; } bool Bu::Blob::const_iterator::operator==( const Bu::Blob::const_iterator &rRhs ) { if( isValid() == false && rRhs.isValid() == false ) return true; return pBlob == rRhs.pBlob && iIndex == rRhs.iIndex; } bool Bu::Blob::const_iterator::operator!=( const Bu::Blob::iterator &rRhs ) { if( isValid() != rRhs.isValid() ) return true; return pBlob != rRhs.pBlob || iIndex != rRhs.iIndex; } bool Bu::Blob::const_iterator::operator!=( const Bu::Blob::const_iterator &rRhs ) { if( isValid() != rRhs.isValid() ) return true; return pBlob != rRhs.pBlob || iIndex != rRhs.iIndex; } Bu::Blob::const_iterator &Bu::Blob::const_iterator::operator=( Bu::Blob::iterator &rRhs ) { pBlob = rRhs.pBlob; iIndex = rRhs.iIndex; bForward = rRhs.bForward; return *this; } Bu::Blob::const_iterator &Bu::Blob::const_iterator::operator=( Bu::Blob::const_iterator &rRhs ) { pBlob = rRhs.pBlob; iIndex = rRhs.iIndex; bForward = rRhs.bForward; return *this; } Bu::Blob::iterator Bu::Blob::begin() { return iterator( this, true ); } Bu::Blob::const_iterator Bu::Blob::begin() const { return const_iterator( this, true ); } Bu::Blob::iterator Bu::Blob::end() { return iterator(); } Bu::Blob::const_iterator Bu::Blob::end() const { return const_iterator(); } Bu::Blob::iterator Bu::Blob::rbegin() { return iterator( this, false ); } Bu::Blob::const_iterator Bu::Blob::rbegin() const { return const_iterator( this, false ); } Bu::Blob::iterator Bu::Blob::rend() { return iterator(); } Bu::Blob::const_iterator Bu::Blob::rend() const { return const_iterator(); }