From ecc191f590f76584a14c9c51727412b0b7b3086e Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 24 May 2010 15:10:19 +0000 Subject: Changed the Bu::Stream API, setSize is now standard. There may be a few more things that should be added. A few of them still need to be implemented. I know that truncate for Bu::File is possible on windows, I've used it before, but hell if I can find it. Myriad also needs the setSize function completed. --- src/file.cpp | 14 +++++++++++--- src/file.h | 12 ++++-------- src/filter.cpp | 4 ++++ src/filter.h | 6 ++++++ src/membuf.cpp | 9 +++++++++ src/membuf.h | 1 + src/myriad.cpp | 6 ++++++ src/myriad.h | 2 ++ src/myriadstream.cpp | 7 +++++++ src/myriadstream.h | 1 + src/process.cpp | 4 ++++ src/process.h | 2 ++ src/queuebuf.cpp | 4 ++++ src/queuebuf.h | 1 + src/socket.cpp | 4 ++++ src/socket.h | 2 ++ src/stdstream.cpp | 4 ++++ src/stdstream.h | 1 + src/stream.h | 8 ++++++++ src/unit/myriad.unit | 2 +- src/unitsuite.cpp | 14 ++++++++++++++ src/unitsuite.h | 5 +++++ 22 files changed, 101 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/file.cpp b/src/file.cpp index f1f63e4..a0c3fd8 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -187,19 +187,27 @@ void Bu::File::setBlocking( bool bBlocking ) #endif } -#ifndef WIN32 Bu::File Bu::File::tempFile( Bu::FString &sName ) { +#ifndef WIN32 int afh_d = mkstemp( sName.getStr() ); return Bu::File( afh_d ); +#else + return Bu::File( sName, Bu::File::Write|Bu::File::Create ); +#endif } -void Bu::File::truncate( long nSize ) +void Bu::File::setSize( long iSize ) { - ftruncate( fd, nSize ); +#ifndef WIN32 + ftruncate( fd, iSize ); +#else +#warning Bu::File::setSize not implemented on this platform +#endif } +#ifndef WIN32 void Bu::File::chmod( mode_t t ) { fchmod( fd, t ); diff --git a/src/file.h b/src/file.h index 5eca3c1..b2cd2a4 100644 --- a/src/file.h +++ b/src/file.h @@ -69,6 +69,9 @@ namespace Bu WriteNew = 0x0E ///< Create a file (or truncate) for writing. /// Same as Write|Create|Truncate }; + + virtual void setSize( long iSize ); + /** * Create a temp file and return its handle. The file is opened * Read/Write. @@ -77,16 +80,9 @@ namespace Bu * characters. *@returns (Bu::File) A file object representing your temp file. */ -#ifndef WIN32 static Bu::File tempFile( Bu::FString &sName ); - /** - * Set the size of the file to (nSize). You can either grow or shrink - * the file. - *@param nSize (long) The new size of the file. - */ - void truncate( long nSize ); - +#ifndef WIN32 /** * Change the file access permissions. *@param t (mode_t) The new file access permissions. diff --git a/src/filter.cpp b/src/filter.cpp index 4208555..8dc3694 100644 --- a/src/filter.cpp +++ b/src/filter.cpp @@ -87,6 +87,10 @@ void Bu::Filter::setBlocking( bool bBlocking ) rNext.setBlocking( bBlocking ); } +void Bu::Filter::setSize( long iSize ) +{ +} + void Bu::Filter::flush() { rNext.flush(); diff --git a/src/filter.h b/src/filter.h index 67ffce2..5507daa 100644 --- a/src/filter.h +++ b/src/filter.h @@ -62,6 +62,12 @@ namespace Bu virtual bool isBlocking(); virtual void setBlocking( bool bBlocking=true ); + /** + * Most filters won't re-implement this, it doesn't make a lot of sense + * for filters, in general. + */ + virtual void setSize( long iSize ); + protected: Bu::Stream &rNext; diff --git a/src/membuf.cpp b/src/membuf.cpp index 0c1c441..b822641 100644 --- a/src/membuf.cpp +++ b/src/membuf.cpp @@ -140,6 +140,15 @@ void Bu::MemBuf::setBlocking( bool ) { } +void Bu::MemBuf::setSize( long iSize ) +{ + if( iSize < 0 ) + iSize = 0; + sBuf.setSize( iSize ); + if( nPos > iSize ) + nPos = iSize; +} + Bu::FString &Bu::MemBuf::getString() { return sBuf; diff --git a/src/membuf.h b/src/membuf.h index c6c8079..9e406c1 100644 --- a/src/membuf.h +++ b/src/membuf.h @@ -45,6 +45,7 @@ namespace Bu virtual bool isSeekable(); virtual bool isBlocking(); virtual void setBlocking( bool bBlocking=true ); + virtual void setSize( long iSize ); Bu::FString &getString(); void setString( const Bu::FString &sNewData ); diff --git a/src/myriad.cpp b/src/myriad.cpp index 4476d53..044e35e 100644 --- a/src/myriad.cpp +++ b/src/myriad.cpp @@ -426,3 +426,9 @@ void Bu::Myriad::syncBlock( Block *pBlock ) } } +void Bu::Myriad::setStreamSize( Stream *pStream, long iSize ) +{ + sio << "Oh man, you have to implement Bu::Myriad::setStreamSize!!! (line " + << __LINE__ << ")" << sio.nl; +} + diff --git a/src/myriad.h b/src/myriad.h index 9bbc812..8e7cfb1 100644 --- a/src/myriad.h +++ b/src/myriad.h @@ -150,6 +150,8 @@ namespace Bu void releaseBlock( Block *pBlock ); void syncBlock( Block *pBlock ); + void setStreamSize( Stream *pStream, long iSize ); + private: Bu::Stream &sStore; int iBlockSize; diff --git a/src/myriadstream.cpp b/src/myriadstream.cpp index 2218ae4..41c5b53 100644 --- a/src/myriadstream.cpp +++ b/src/myriadstream.cpp @@ -281,3 +281,10 @@ void Bu::MyriadStream::setBlocking( bool /*bBlocking*/ ) { } +void Bu::MyriadStream::setSize( long iSize ) +{ + rMyriad.setStreamSize( pStream, iSize ); + if( iPos > iSize ) + iPos = iSize; +} + diff --git a/src/myriadstream.h b/src/myriadstream.h index e2e3ba7..1a5e552 100644 --- a/src/myriadstream.h +++ b/src/myriadstream.h @@ -43,6 +43,7 @@ namespace Bu virtual bool isSeekable(); virtual bool isBlocking(); virtual void setBlocking( bool bBlocking=true ); + virtual void setSize( long iSize ); private: Myriad &rMyriad; diff --git a/src/process.cpp b/src/process.cpp index 7cb5983..2d8eda0 100644 --- a/src/process.cpp +++ b/src/process.cpp @@ -278,6 +278,10 @@ void Bu::Process::setBlocking( bool bBlocking ) this->bBlocking = bBlocking; } +void Bu::Process::setSize( long iSize ) +{ +} + void Bu::Process::select( bool &bStdOut, bool &bStdErr ) { fd_set rfds; diff --git a/src/process.h b/src/process.h index 3fcf1f2..68678bd 100644 --- a/src/process.h +++ b/src/process.h @@ -68,6 +68,8 @@ namespace Bu virtual bool isBlocking(); virtual void setBlocking( bool bBlocking=true ); + virtual void setSize( long iSize ); + void select( bool &bStdOut, bool &bStdErr ); bool isRunning(); diff --git a/src/queuebuf.cpp b/src/queuebuf.cpp index 01d92f8..1a902bc 100644 --- a/src/queuebuf.cpp +++ b/src/queuebuf.cpp @@ -249,6 +249,10 @@ void Bu::QueueBuf::setBlocking( bool ) { } +void Bu::QueueBuf::setSize( long iSize ) +{ +} + void Bu::QueueBuf::addBlock() { lBlocks.append( new char[iBlockSize] ); diff --git a/src/queuebuf.h b/src/queuebuf.h index 382863d..395c6ba 100644 --- a/src/queuebuf.h +++ b/src/queuebuf.h @@ -45,6 +45,7 @@ namespace Bu virtual bool isSeekable(); virtual bool isBlocking(); virtual void setBlocking( bool bBlocking=true ); + virtual void setSize( long iSize ); private: void addBlock(); diff --git a/src/socket.cpp b/src/socket.cpp index 6a30f44..41e0763 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -405,6 +405,10 @@ void Bu::Socket::setBlocking( bool bBlocking ) #endif } +void Bu::Socket::setSize( long iSize ) +{ +} + void Bu::Socket::flush() { } diff --git a/src/socket.h b/src/socket.h index f907b95..c8f78f0 100644 --- a/src/socket.h +++ b/src/socket.h @@ -92,6 +92,8 @@ namespace Bu virtual bool isBlocking(); virtual void setBlocking( bool bBlocking=true ); + virtual void setSize( long iSize ); + Bu::FString getAddress() const; operator int() const; diff --git a/src/stdstream.cpp b/src/stdstream.cpp index fb6cd36..4c2c828 100644 --- a/src/stdstream.cpp +++ b/src/stdstream.cpp @@ -96,3 +96,7 @@ void Bu::StdStream::setBlocking( bool ) { } +void Bu::StdStream::setSize( long iSize ) +{ +} + diff --git a/src/stdstream.h b/src/stdstream.h index 144a24f..4efeece 100644 --- a/src/stdstream.h +++ b/src/stdstream.h @@ -40,6 +40,7 @@ namespace Bu virtual bool isSeekable(); virtual bool isBlocking(); virtual void setBlocking( bool bBlocking=true ); + virtual void setSize( long iSize ); }; } diff --git a/src/stream.h b/src/stream.h index bfe3ef9..0ea5560 100644 --- a/src/stream.h +++ b/src/stream.h @@ -152,6 +152,14 @@ namespace Bu */ virtual void setBlocking( bool bBlocking=true ) = 0; + /** + * Set the size of the stream, this does not apply to many types of + * streams. For those that it does apply to, data will be added or + * removed from the end of the stream, but the content of the added + * data is undefined. + */ + virtual void setSize( long iSize ) = 0; + public: // Filters diff --git a/src/unit/myriad.unit b/src/unit/myriad.unit index bc05bf7..cf861a3 100644 --- a/src/unit/myriad.unit +++ b/src/unit/myriad.unit @@ -76,7 +76,7 @@ suite Myriad { FString sFileName("myriad-XXXXXXX"); - File::tempFile( sFileName ); + tempFile( sFileName ); File fMyriad( sFileName, File::WriteNew|File::Read ); Myriad m( fMyriad ); m.initialize( 64 ); diff --git a/src/unitsuite.cpp b/src/unitsuite.cpp index a66160e..51cca10 100644 --- a/src/unitsuite.cpp +++ b/src/unitsuite.cpp @@ -6,6 +6,9 @@ */ #include "bu/unitsuite.h" +#include "bu/file.h" + +#include Bu::UnitSuite::UnitSuite() : iOptions( 0 ) @@ -102,9 +105,20 @@ int Bu::UnitSuite::run( int /*argc*/, char * /*argv */ [] ) if( iUPass == 0 && iUFail == 0 ) printf("\tNothing unexpected.\n\n"); + for( StrList::iterator i = lFileCleanup.begin(); i; i++ ) + { + unlink( (*i).getStr() ); + } + return 0; } +void Bu::UnitSuite::tempFile( Bu::FString &sFileName ) +{ + Bu::File::tempFile( sFileName ); + lFileCleanup.append( sFileName ); +} + void Bu::UnitSuite::add( Test fTest, const Bu::FString &sName, Expect e ) { TestInfo ti; diff --git a/src/unitsuite.h b/src/unitsuite.h index ad6bedb..7d026c2 100644 --- a/src/unitsuite.h +++ b/src/unitsuite.h @@ -61,6 +61,8 @@ namespace Bu int run( int argc=0, char *argv[]=NULL ); + void tempFile( Bu::FString &sFileName ); + typedef void (UnitSuite::*Test)(); class Failed @@ -105,6 +107,9 @@ namespace Bu FString sSuiteName; int iOptions; + + typedef Bu::List StrList; + StrList lFileCleanup; }; } -- cgit v1.2.3