From a83e9babede7ab5bc8e1ac6c7ee3784b91bd8452 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 4 Aug 2010 15:17:11 +0000 Subject: Alright, NullStream compiles and works. --- src/nullstream.cpp | 114 +++++++++++++++++++++++++++++++++++++++++++---------- src/nullstream.h | 19 +++++++++ 2 files changed, 113 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/nullstream.cpp b/src/nullstream.cpp index 364b58a..40c636c 100644 --- a/src/nullstream.cpp +++ b/src/nullstream.cpp @@ -1,6 +1,8 @@ #include "bu/nullstream.h" -Bu::NullStream::NullStream() +Bu::NullStream::NullStream() : + sRead( 0 ), + sWrote( 0 ) { } @@ -8,24 +10,96 @@ Bu::NullStream::~NullStream() { } -void Bu::NullStream::close(); -size_t Bu::NullStream::read( void *pBuf, size_t nBytes ); -Bu::FString Bu::NullStream::readLine(); -size_t Bu::NullStream::write( const void *pBuf, size_t nBytes ); -long Bu::NullStream::tell(); -void Bu::NullStream::seek( long offset ); -void Bu::NullStream::setPos( long pos ); -void Bu::NullStream::setPosEnd( long pos ); -bool Bu::NullStream::isEos(); -bool Bu::NullStream::isOpen(); -void Bu::NullStream::flush(); -bool Bu::NullStream::canRead(); -bool Bu::NullStream::canWrite(); -bool Bu::NullStream::isReadable(); -bool Bu::NullStream::isWritable(); -bool Bu::NullStream::isSeekable(); -bool Bu::NullStream::isBlocking(); -void Bu::NullStream::setBlocking( bool bBlocking=true ); -void Bu::NullStream::setSize( long iSize ); +void Bu::NullStream::close() +{ + sRead = sWrote = 0; +} + +size_t Bu::NullStream::read( void *pBuf, size_t nBytes ) +{ + memset( pBuf, 0, nBytes ); + sRead += nBytes; + return nBytes; +} + +Bu::FString Bu::NullStream::readLine() +{ + sRead++; + return Bu::FString("\0", 1 ); +} + +size_t Bu::NullStream::write( const void *, size_t nBytes ) +{ + sWrote += nBytes; + return nBytes; +} + +long Bu::NullStream::tell() +{ + return sRead + sWrote; +} +void Bu::NullStream::seek( long ) +{ +} + +void Bu::NullStream::setPos( long ) +{ +} + +void Bu::NullStream::setPosEnd( long ) +{ +} + +bool Bu::NullStream::isEos() +{ + return false; +} + +bool Bu::NullStream::isOpen() +{ + return true; +} + +void Bu::NullStream::flush() +{ +} + +bool Bu::NullStream::canRead() +{ + return true; +} + +bool Bu::NullStream::canWrite() +{ + return true; +} + +bool Bu::NullStream::isReadable() +{ + return true; +} + +bool Bu::NullStream::isWritable() +{ + return true; +} + +bool Bu::NullStream::isSeekable() +{ + return false; +} + +bool Bu::NullStream::isBlocking() +{ + return true; +} + +void Bu::NullStream::setBlocking( bool ) +{ +} + +void Bu::NullStream::setSize( long ) +{ +} diff --git a/src/nullstream.h b/src/nullstream.h index 5440af6..1537ffb 100644 --- a/src/nullstream.h +++ b/src/nullstream.h @@ -5,6 +5,18 @@ namespace Bu { + /** + * Works a lot like /dev/null on *nix style systems. This class allows + * infinite reading and writing. All operatorns "succeed" even if they + * don't seem to do anything. This is great for testing writing code or + * doing dry runs. When reading, it will produce NULL bytes, so any + * application that would like the ability to produce null streams as a + * snap-in replacement for any other Bu::Stream, this is the right option. + * + * As an added feature, the NullStream will track how many bytes it was + * asked to read and write, allowing you to use it to determine how many + * bytes a write opretion would use without actually writing anything. + */ class NullStream : public Bu::Stream { public: @@ -31,6 +43,13 @@ namespace Bu virtual bool isBlocking(); virtual void setBlocking( bool bBlocking=true ); virtual void setSize( long iSize ); + + size_t getBytesRead() { return sRead; } + size_t getByetsWritten() { return sWrote; } + + private: + size_t sRead; + size_t sWrote; }; }; -- cgit v1.2.3