From f1e3f25d9b7a12cdedb99e4cb0bfa66157a1a972 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 27 Aug 2024 13:37:36 -0700 Subject: Making progress. --- src/stable/myriadstream.cpp | 257 ++++++-------------------------------------- 1 file changed, 34 insertions(+), 223 deletions(-) (limited to 'src/stable/myriadstream.cpp') diff --git a/src/stable/myriadstream.cpp b/src/stable/myriadstream.cpp index 3c78bb0..cbbd4fe 100644 --- a/src/stable/myriadstream.cpp +++ b/src/stable/myriadstream.cpp @@ -1,254 +1,79 @@ -/* - * Copyright (C) 2007-2023 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/myriadstream.h" -#include - -// #define MYRIAD_STREAM_DEBUG 1 - -#ifdef MYRIAD_STREAM_DEBUG -#include "bu/sio.h" - -using Bu::sio; -using Bu::Fmt; -#endif -#include "bu/sio.h" - -// #define TRACE( x ) Bu::println("%1:%2: %3: %4 - %5").arg(__FILE__).arg( __LINE__ ).arg(__PRETTY_FUNCTION__).arg(rMyriad.sStore.getLocation()).arg(x) -#define TRACE( x ) (void)0 +#include "bu/mutexlocker.h" Bu::MyriadStream::MyriadStream( Bu::Myriad &rMyriad, - Bu::Myriad::Stream *pStream ) : + Bu::Myriad::Stream *pStream, Bu::Myriad::Mode eMode ) : rMyriad( rMyriad ), pStream( pStream ), - pCurBlock( NULL ), - iPos( 0 ) + eMode( eMode ) { -#ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: " << __LINE__ << ": Created, iId=" << pStream->iId << ", iSize=" - << pStream->iSize << sio.nl; -#endif - //pCurBlock = rMyriad.newBlock(); - //rMyriad.getBlock( uStream, pCurBlock ); - //uSize = pCurBlock->uBytesUsed; + if( (eMode&Bu::Myriad::ReadWrite) == 0 ) + { + throw Bu::MyriadException( Bu::MyriadException::invalidParameter, + "MyriadStream must be opened Read or Write or both."); + } + Bu::MutexLocker l( mAccess ); + pStream->open(); } Bu::MyriadStream::~MyriadStream() { - if( pCurBlock ) - rMyriad.releaseBlock( pCurBlock ); - //rMyriad.updateStreamSize( uStream, uSize ); - //rMyriad.deleteBlock( pCurBlock ); + close(); } void Bu::MyriadStream::close() { -} - -Bu::size Bu::MyriadStream::read( void *pBuf, Bu::size nBytes ) -{ -#ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: read: " << __LINE__ << ": Started, asked to read " << nBytes << "b." - << sio.nl; -#endif - if( nBytes > (Bu::size)pStream->getSize()-iPos ) - nBytes = pStream->getSize()-iPos; - if( nBytes <= 0 ) - return 0; - int iLeft = nBytes; -#ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: read: " << __LINE__ << ": Started, going to read " << nBytes << "b." - << sio.nl; -#endif - if( pCurBlock == NULL ) + Bu::MutexLocker l( mAccess ); + if( eMode ) { -#ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: read: " << __LINE__ << ": No block loaded, loading initial block." - << sio.nl; -#endif - pCurBlock = rMyriad.getBlock( - pStream->aBlocks[iPos/rMyriad.iBlockSize] - ); + pStream->close(); + eMode = Bu::Myriad::None; } - while( iLeft > 0 ) - { - int iCurBlock = pStream->aBlocks[iPos/rMyriad.iBlockSize]; - if( pCurBlock->iBlockIndex != iCurBlock ) - { -#ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: read: " << __LINE__ << ": Loading new block " << iCurBlock << "." - << sio.nl; -#endif - rMyriad.releaseBlock( pCurBlock ); - pCurBlock = rMyriad.getBlock( iCurBlock ); - } +} - int iAmnt = Bu::buMin( - Bu::buMin( - rMyriad.iBlockSize - iPos%rMyriad.iBlockSize, - iLeft - ), - pStream->getSize()-iPos - ); -#ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: read: " << __LINE__ << ": Copying out bytes: " - << iPos << "(" << (iPos%rMyriad.iBlockSize) << ")+" - << iAmnt - << ", " << iLeft << "b left." << sio.nl; -#endif - memcpy( - pBuf, - pCurBlock->pData+(iPos%rMyriad.iBlockSize), - iAmnt - ); - iPos += iAmnt; - pBuf = &((char *)pBuf)[iAmnt]; - iLeft -= iAmnt; - } - return nBytes; +Bu::size Bu::MyriadStream::read( void *pBuf, size iBytes ) +{ } -Bu::size Bu::MyriadStream::write( const void *pBuf, Bu::size nBytes ) +Bu::String Bu::MyriadStream::readLine() { - if( nBytes <= 0 ) - return 0; +} -#ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: write: " << __LINE__ << ": Started, asked to write " << nBytes << "b." - << sio.nl; -#endif - if( nBytes <= 0 ) - return 0; - int iLeft = nBytes; - /* - if( pCurBlock == NULL ) - { -#ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: write: No block loaded, loading initial block." - << sio.nl; -#endif - pCurBlock = rMyriad.getBlock( - pStream->aBlocks[iPos/rMyriad.iBlockSize] - ); - }*/ - - while( iLeft > 0 ) - { - int iCurBlock; - if( iPos/rMyriad.iBlockSize < pStream->aBlocks.getSize() ) - { - iCurBlock = pStream->aBlocks[iPos/rMyriad.iBlockSize]; - } - else - { - iCurBlock = rMyriad.streamAddBlock( pStream ); -#ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: write: " << __LINE__ << ": New block allocated and appended: " - << iCurBlock << "." << sio.nl; +Bu::String Bu::MyriadStream::readAll() +{ +} -#endif - } - if( !pCurBlock || pCurBlock->iBlockIndex != iCurBlock ) - { -#ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: write: " << __LINE__ << ": Loading new block " << iCurBlock << "." - << sio.nl; -#endif - rMyriad.releaseBlock( pCurBlock ); - pCurBlock = rMyriad.getBlock( iCurBlock ); - } - pCurBlock->bChanged = true; - - // There are two main writing modes when it comes down to it. - // Overwrite mode and append mode. Append is what pretty much always - // happens when creating a new stream. - if( iPos < pStream->getSize() ) - { - int iAmnt = Bu::buMin( - Bu::buMin( - rMyriad.iBlockSize - iPos%rMyriad.iBlockSize, - iLeft - ), - pStream->getSize()-iPos - ); -#ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: write (ovr): " << __LINE__ << ": Copying in bytes: " - << (iPos%rMyriad.iBlockSize) << "+" - << iAmnt - << ", " << iLeft << "b left." << sio.nl; -#endif - memcpy( - pCurBlock->pData+(iPos%rMyriad.iBlockSize), - pBuf, - iAmnt - ); - iPos += iAmnt; - pBuf = &((char *)pBuf)[iAmnt]; - iLeft -= iAmnt; - } - else - { - int iAmnt = Bu::buMin( - rMyriad.iBlockSize - iPos%rMyriad.iBlockSize, - iLeft - ); -#ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: write (app): " << __LINE__ << ": Copying in bytes: " - << (iPos%rMyriad.iBlockSize) << "+" - << iAmnt - << ", " << iLeft << "b left." << sio.nl; -#endif - memcpy( - pCurBlock->pData+(iPos%rMyriad.iBlockSize), - pBuf, - iAmnt - ); - iPos += iAmnt; - TRACE(Bu::String("Stream=%1 - pStream->iSize(%2) += iAmnt(%3)").arg(pStream->iId).arg( pStream->getSize() ).arg(iAmnt).end()); - pStream->growTo( pStream->getSize()+iAmnt ); - TRACE(Bu::String("Stream=%1 - pStream->iSize = %2").arg(pStream->iId).arg( pStream->getSize() ).end()); - rMyriad.headerChanged(); - pBuf = &((char *)pBuf)[iAmnt]; - iLeft -= iAmnt; - } - } +Bu::size Bu::MyriadStream::write( const void *pBuf, size iBytes ) +{ +} - return nBytes; +Bu::size Bu::MyriadStream::write( const Bu::String &sBuf ) +{ } Bu::size Bu::MyriadStream::tell() { - return iPos; } -void Bu::MyriadStream::seek( Bu::size offset ) +void Bu::MyriadStream::seek( size offset ) { - iPos += offset; } -void Bu::MyriadStream::setPos( Bu::size pos ) +void Bu::MyriadStream::setPos( size pos ) { - iPos = pos; } -void Bu::MyriadStream::setPosEnd( Bu::size pos ) +void Bu::MyriadStream::setPosEnd( size pos ) { - iPos = pStream->getSize()-pos; } bool Bu::MyriadStream::isEos() { - return iPos >= pStream->getSize(); } bool Bu::MyriadStream::isOpen() { - return true; } void Bu::MyriadStream::flush() @@ -257,59 +82,45 @@ void Bu::MyriadStream::flush() bool Bu::MyriadStream::canRead() { - return true; } bool Bu::MyriadStream::canWrite() { - return true; } bool Bu::MyriadStream::isReadable() { - return true; } bool Bu::MyriadStream::isWritable() { - return true; } bool Bu::MyriadStream::isSeekable() { - return true; } bool Bu::MyriadStream::isBlocking() { - return true; } -void Bu::MyriadStream::setBlocking( bool /*bBlocking*/ ) +void Bu::MyriadStream::setBlocking( bool bBlocking ) { } -void Bu::MyriadStream::setSize( Bu::size iSize ) +void Bu::MyriadStream::setSize( size iSize ) { - if( iSize < 0 ) - iSize = 0; - rMyriad.setStreamSize( pStream, iSize ); - if( iPos > iSize ) - iPos = iSize; } Bu::size Bu::MyriadStream::getSize() const { - return pStream->getSize(); } Bu::size Bu::MyriadStream::getBlockSize() const { - return rMyriad.getBlockSize(); } -Bu::String Bu::MyriadStream::getLocation() const +Bu::String getLocation() const { - return Bu::String("%1").arg( pStream->iId ); } -- cgit v1.2.3