From 84c93fbf0f371bcd26893d1c33e3a5bf1a61223e Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 30 Sep 2024 10:23:55 -0700 Subject: Forgot about erase, it seems to be working now. --- src/stable/myriad.cpp | 24 +++++++++++++++++++++++- src/stable/myriad.h | 4 +++- src/tools/myriad.cpp | 18 ++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/stable/myriad.cpp b/src/stable/myriad.cpp index 0bd2ff2..eba1ebf 100644 --- a/src/stable/myriad.cpp +++ b/src/stable/myriad.cpp @@ -129,8 +129,30 @@ Bu::MyriadStream Bu::Myriad::open( Bu::Myriad::StreamId iStream, return Bu::MyriadStream( *this, pStream, eMode ); } -void Bu::Myriad::erase( Bu::Myriad::StreamId /*iStream*/ ) +void Bu::Myriad::erase( Bu::Myriad::StreamId iStream ) { + // For now, let's prevent you from erasing a stream if it's open. + Bu::MutexLocker l( mhStream ); + if( !hStream.has( iStream ) ) + { + throw Bu::MyriadException( Bu::MyriadException::noSuchStream, + "No such stream exists."); + } + Stream *pStream = hStream.get( iStream ); + Bu::MutexLocker sl( pStream->mAccess ); + if( pStream->iOpenCount > 0 ) + { + throw Bu::MyriadException( Bu::MyriadException::streamOpen, + "Cannot currently erase a stream while it is open."); + } + + for( Bu::Array::iterator i = pStream->aBlocks.begin(); i; i++ ) + { + releaseBlock( *i, false ); + } + pStream->aBlocks.clear(); + hStream.erase( iStream ); + delete pStream; } void Bu::Myriad::setSize( Bu::Myriad::StreamId iStream, diff --git a/src/stable/myriad.h b/src/stable/myriad.h index c3f682d..60c5a39 100644 --- a/src/stable/myriad.h +++ b/src/stable/myriad.h @@ -25,6 +25,7 @@ namespace Bu invalidParameter, invalidBackingStream, badMode, + streamOpen, }; subExceptionDeclEnd(); @@ -68,7 +69,8 @@ namespace Bu * 4 - 7: Size of stream in bytes * 8 - ...: List of blocks in stream (4 bytes per block */ - Myriad( Bu::Stream &rBacking, int32_t iBlockSize=-1, int32_t iPreallocateBlocks=-1 ); + Myriad( Bu::Stream &rBacking, int32_t iBlockSize=-1, + int32_t iPreallocateBlocks=-1 ); virtual ~Myriad(); MyriadStream create( Mode eMode, int32_t iPreallocateBytes=-1 ); diff --git a/src/tools/myriad.cpp b/src/tools/myriad.cpp index 1dc73ec..346cd85 100644 --- a/src/tools/myriad.cpp +++ b/src/tools/myriad.cpp @@ -20,6 +20,7 @@ enum Mode modeCreate, modeInfo, modeStreamNew, + modeStreamErase, modeStreamDump, modeStreamPut, modeStreamGet, @@ -44,6 +45,8 @@ public: "Display some info about a Myriad file." ); addOption( eMode, 'n', "new", "Create a new sub-stream in a Myriad file."); + addOption( eMode, 'e', "erase", + "Erase sub-stream in a Myriad file."); addOption( eMode, 'd', "dump", "Display a hexdump of a stream from a Myriad file."); addOption( eMode, "get", @@ -67,6 +70,7 @@ public: setOverride( "create", modeCreate ); setOverride( "info", modeInfo ); setOverride( "new", modeStreamNew ); + setOverride( "erase", modeStreamErase ); setOverride( "dump", modeStreamDump ); setOverride( "put", modeStreamPut ); setOverride( "get", modeStreamGet ); @@ -153,6 +157,20 @@ int main( int argc, char *argv[] ) } break; + case modeStreamErase: + if( !opts.sFile.isSet() ) + { + sio << "Please specify a file manipulate." << sio.nl; + return 0; + } + else + { + File fOut( opts.sFile, File::Write|File::Read ); + Myriad m( fOut ); + m.erase( opts.iStream ); + } + break; + case modeStreamDump: if( !opts.sFile.isSet() ) { -- cgit v1.2.3