summaryrefslogtreecommitdiff
path: root/lib/common/PartialReadStream.cpp
diff options
context:
space:
mode:
authorBen Summers <ben@fluffy.co.uk>2005-10-14 08:50:54 +0000
committerBen Summers <ben@fluffy.co.uk>2005-10-14 08:50:54 +0000
commit99f8ce096bc5569adbfea1911dbcda24c28d8d8b (patch)
tree049c302161fea1f2f6223e1e8f3c40d9e8aadc8b /lib/common/PartialReadStream.cpp
Box Backup 0.09 with a few tweeks
Diffstat (limited to 'lib/common/PartialReadStream.cpp')
-rwxr-xr-xlib/common/PartialReadStream.cpp135
1 files changed, 135 insertions, 0 deletions
diff --git a/lib/common/PartialReadStream.cpp b/lib/common/PartialReadStream.cpp
new file mode 100755
index 00000000..0b5c4cf6
--- /dev/null
+++ b/lib/common/PartialReadStream.cpp
@@ -0,0 +1,135 @@
+// --------------------------------------------------------------------------
+//
+// File
+// Name: PartialReadStream.h
+// Purpose: Read part of another stream
+// Created: 2003/08/26
+//
+// --------------------------------------------------------------------------
+
+#include "Box.h"
+#include "PartialReadStream.h"
+#include "CommonException.h"
+
+#include "MemLeakFindOn.h"
+
+// --------------------------------------------------------------------------
+//
+// Function
+// Name: PartialReadStream::PartialReadStream(IOStream &, int)
+// Purpose: Constructor, taking another stream and the number of bytes
+// to be read from it.
+// Created: 2003/08/26
+//
+// --------------------------------------------------------------------------
+PartialReadStream::PartialReadStream(IOStream &rSource, int BytesToRead)
+ : mrSource(rSource),
+ mBytesLeft(BytesToRead)
+{
+ ASSERT(BytesToRead > 0);
+}
+
+// --------------------------------------------------------------------------
+//
+// Function
+// Name: PartialReadStream::~PartialReadStream()
+// Purpose: Destructor. Won't absorb any unread bytes.
+// Created: 2003/08/26
+//
+// --------------------------------------------------------------------------
+PartialReadStream::~PartialReadStream()
+{
+ // Warn in debug mode
+ if(mBytesLeft != 0)
+ {
+ TRACE1("PartialReadStream::~PartialReadStream when mBytesLeft = %d\n", mBytesLeft);
+ }
+}
+
+// --------------------------------------------------------------------------
+//
+// Function
+// Name: PartialReadStream::Read(void *, int, int)
+// Purpose: As interface.
+// Created: 2003/08/26
+//
+// --------------------------------------------------------------------------
+int PartialReadStream::Read(void *pBuffer, int NBytes, int Timeout)
+{
+ // Finished?
+ if(mBytesLeft <= 0)
+ {
+ return 0;
+ }
+
+ // Asking for more than is allowed?
+ if(NBytes > mBytesLeft)
+ {
+ // Adjust downwards
+ NBytes = mBytesLeft;
+ }
+
+ // Route the request to the source
+ int read = mrSource.Read(pBuffer, NBytes, Timeout);
+ ASSERT(read <= mBytesLeft);
+
+ // Adjust the count
+ mBytesLeft -= read;
+
+ // Return the number read
+ return read;
+}
+
+// --------------------------------------------------------------------------
+//
+// Function
+// Name: PartialReadStream::BytesLeftToRead()
+// Purpose: As interface.
+// Created: 2003/08/26
+//
+// --------------------------------------------------------------------------
+IOStream::pos_type PartialReadStream::BytesLeftToRead()
+{
+ return mBytesLeft;
+}
+
+// --------------------------------------------------------------------------
+//
+// Function
+// Name: PartialReadStream::Write(const void *, int)
+// Purpose: As interface. But will exception.
+// Created: 2003/08/26
+//
+// --------------------------------------------------------------------------
+void PartialReadStream::Write(const void *pBuffer, int NBytes)
+{
+ THROW_EXCEPTION(CommonException, CantWriteToPartialReadStream)
+}
+
+// --------------------------------------------------------------------------
+//
+// Function
+// Name: PartialReadStream::StreamDataLeft()
+// Purpose: As interface.
+// Created: 2003/08/26
+//
+// --------------------------------------------------------------------------
+bool PartialReadStream::StreamDataLeft()
+{
+ return mBytesLeft != 0;
+}
+
+// --------------------------------------------------------------------------
+//
+// Function
+// Name: PartialReadStream::StreamClosed()
+// Purpose: As interface.
+// Created: 2003/08/26
+//
+// --------------------------------------------------------------------------
+bool PartialReadStream::StreamClosed()
+{
+ // always closed
+ return true;
+}
+