Changeset 1585
- Timestamp:
- 28/04/2007 18:11:36 (21 months ago)
- Location:
- box/chris/merge/lib/common
- Files:
-
- 2 copied
-
ZeroStream.cpp (copied) (copied from box/chris/merge/lib/common/BufferedStream.cpp) (13 diffs)
-
ZeroStream.h (copied) (copied from box/chris/merge/lib/common/BufferedStream.h) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
box/chris/merge/lib/common/ZeroStream.cpp
r1263 r1585 2 2 // 3 3 // File 4 // Name: BufferedStream.cpp5 // Purpose: Buffering wrapper around IOStreams6 // Created: 2007/0 1/164 // Name: ZeroStream.cpp 5 // Purpose: An IOStream which returns all zeroes up to a certain size 6 // Created: 2007/04/28 7 7 // 8 8 // -------------------------------------------------------------------------- 9 9 10 10 #include "Box.h" 11 #include " BufferedStream.h"11 #include "ZeroStream.h" 12 12 #include "CommonException.h" 13 13 … … 19 19 // 20 20 // Function 21 // Name: BufferedStream::BufferedStream(const char *, int, int)22 // Purpose: Constructor , set up buffer23 // Created: 2007/0 1/1621 // Name: ZeroStream::ZeroStream(IOStream::pos_type) 22 // Purpose: Constructor 23 // Created: 2007/04/28 24 24 // 25 25 // -------------------------------------------------------------------------- 26 BufferedStream::BufferedStream(IOStream& rSource)27 : m rSource(rSource), mBufferSize(0), mBufferPosition(0)26 ZeroStream::ZeroStream(IOStream::pos_type size) 27 : mSize(size), mPosition(0) 28 28 { } 29 29 … … 32 32 // 33 33 // Function 34 // Name: BufferedStream::Read(void *, int)34 // Name: ZeroStream::Read(void *, int) 35 35 // Purpose: Reads bytes from the file 36 36 // Created: 2007/01/16 37 37 // 38 38 // -------------------------------------------------------------------------- 39 int BufferedStream::Read(void *pBuffer, int NBytes, int Timeout)39 int ZeroStream::Read(void *pBuffer, int NBytes, int Timeout) 40 40 { 41 if (mBufferSize == mBufferPosition) 41 ASSERT(NBytes > 0); 42 43 int bytesToRead = NBytes; 44 45 if (bytesToRead > mSize - mPosition) 42 46 { 43 // buffer is empty, fill it. 44 45 int numBytesRead = mrSource.Read(mBuffer, sizeof(mBuffer), 46 Timeout); 47 48 if (numBytesRead < 0) 49 { 50 return numBytesRead; 51 } 52 53 mBufferSize = numBytesRead; 47 bytesToRead = mSize - mPosition; 54 48 } 55 49 56 int sizeToReturn = mBufferSize - mBufferPosition; 50 memset(pBuffer, 0, bytesToRead); 51 mPosition += bytesToRead; 57 52 58 if (sizeToReturn > NBytes) 59 { 60 sizeToReturn = NBytes; 61 } 62 63 memcpy(pBuffer, mBuffer + mBufferPosition, sizeToReturn); 64 mBufferPosition += sizeToReturn; 65 66 if (mBufferPosition == mBufferSize) 67 { 68 // clear out the buffer 69 mBufferSize = 0; 70 mBufferPosition = 0; 71 } 72 73 return sizeToReturn; 53 return bytesToRead; 74 54 } 75 55 … … 78 58 // 79 59 // Function 80 // Name: BufferedStream::BytesLeftToRead()60 // Name: ZeroStream::BytesLeftToRead() 81 61 // Purpose: Returns number of bytes to read (may not be most efficient function ever) 82 62 // Created: 2007/01/16 83 63 // 84 64 // -------------------------------------------------------------------------- 85 IOStream::pos_type BufferedStream::BytesLeftToRead()65 IOStream::pos_type ZeroStream::BytesLeftToRead() 86 66 { 87 return m rSource.BytesLeftToRead() + mBufferSize - mBufferPosition;67 return mSize - mPosition; 88 68 } 89 69 … … 92 72 // 93 73 // Function 94 // Name: BufferedStream::Write(void *, int)74 // Name: ZeroStream::Write(void *, int) 95 75 // Purpose: Writes bytes to the underlying stream (not supported) 96 76 // Created: 2003/07/31 97 77 // 98 78 // -------------------------------------------------------------------------- 99 void BufferedStream::Write(const void *pBuffer, int NBytes)79 void ZeroStream::Write(const void *pBuffer, int NBytes) 100 80 { 101 81 THROW_EXCEPTION(CommonException, NotSupported); … … 106 86 // 107 87 // Function 108 // Name: BufferedStream::GetPosition()88 // Name: ZeroStream::GetPosition() 109 89 // Purpose: Get position in stream 110 90 // Created: 2003/08/21 111 91 // 112 92 // -------------------------------------------------------------------------- 113 IOStream::pos_type BufferedStream::GetPosition() const93 IOStream::pos_type ZeroStream::GetPosition() const 114 94 { 115 return m rSource.GetPosition() - mBufferSize + mBufferPosition;95 return mPosition; 116 96 } 117 97 … … 120 100 // 121 101 // Function 122 // Name: BufferedStream::Seek(pos_type, int)102 // Name: ZeroStream::Seek(pos_type, int) 123 103 // Purpose: Seeks within file, as lseek, invalidate buffer 124 104 // Created: 2003/07/31 125 105 // 126 106 // -------------------------------------------------------------------------- 127 void BufferedStream::Seek(IOStream::pos_type Offset, int SeekType)107 void ZeroStream::Seek(IOStream::pos_type Offset, int SeekType) 128 108 { 129 109 switch (SeekType) … … 131 111 case SeekType_Absolute: 132 112 { 133 // just go there 134 mrSource.Seek(Offset, SeekType); 113 mPosition = Offset; 135 114 } 136 115 break; … … 138 117 case SeekType_Relative: 139 118 { 140 // Actual underlying file position is 141 // (mBufferSize - mBufferPosition) ahead of us. 142 // Need to subtract that amount from the seek 143 // to seek forward that much less, putting the 144 // real pointer in the right place. 145 mrSource.Seek(Offset - mBufferSize + mBufferPosition, 146 SeekType); 119 mPosition += Offset; 147 120 } 148 121 break; … … 150 123 case SeekType_End: 151 124 { 152 // Actual underlying file position is 153 // (mBufferSize - mBufferPosition) ahead of us. 154 // Need to add that amount to the seek 155 // to seek backwards that much more, putting the 156 // real pointer in the right place. 157 mrSource.Seek(Offset + mBufferSize - mBufferPosition, 158 SeekType); 125 mPosition = mSize - Offset; 159 126 } 160 127 } 161 162 // always clear the buffer for now (may be slightly wasteful)163 mBufferSize = 0;164 mBufferPosition = 0;165 128 } 166 129 … … 169 132 // 170 133 // Function 171 // Name: BufferedStream::Close()134 // Name: ZeroStream::Close() 172 135 // Purpose: Closes the underlying stream (not needed) 173 136 // Created: 2003/07/31 174 137 // 175 138 // -------------------------------------------------------------------------- 176 void BufferedStream::Close()139 void ZeroStream::Close() 177 140 { 178 141 THROW_EXCEPTION(CommonException, NotSupported); … … 183 146 // 184 147 // Function 185 // Name: BufferedStream::StreamDataLeft()148 // Name: ZeroStream::StreamDataLeft() 186 149 // Purpose: Any data left to write? 187 150 // Created: 2003/08/02 188 151 // 189 152 // -------------------------------------------------------------------------- 190 bool BufferedStream::StreamDataLeft()153 bool ZeroStream::StreamDataLeft() 191 154 { 192 return mrSource.StreamDataLeft();155 return false; 193 156 } 194 157 … … 196 159 // 197 160 // Function 198 // Name: BufferedStream::StreamClosed()161 // Name: ZeroStream::StreamClosed() 199 162 // Purpose: Is the stream closed? 200 163 // Created: 2003/08/02 201 164 // 202 165 // -------------------------------------------------------------------------- 203 bool BufferedStream::StreamClosed()166 bool ZeroStream::StreamClosed() 204 167 { 205 return mrSource.StreamClosed();168 return false; 206 169 } 207 170 -
box/chris/merge/lib/common/ZeroStream.h
r1263 r1585 2 2 // 3 3 // File 4 // Name: BufferedStream.h5 // Purpose: Buffering wrapper around IOStreams6 // Created: 2007/0 1/164 // Name: ZeroStream.h 5 // Purpose: An IOStream which returns all zeroes up to a certain size 6 // Created: 2007/04/28 7 7 // 8 8 // -------------------------------------------------------------------------- 9 9 10 #ifndef BUFFEREDSTREAM__H11 #define BUFFEREDSTREAM__H10 #ifndef ZEROSTREAM__H 11 #define ZEROSTREAM__H 12 12 13 13 #include "IOStream.h" 14 14 15 class BufferedStream : public IOStream15 class ZeroStream : public IOStream 16 16 { 17 17 private: 18 IOStream& mrSource; 19 char mBuffer[4096]; 20 int mBufferSize; 21 int mBufferPosition; 18 IOStream::pos_type mSize, mPosition; 22 19 23 20 public: 24 BufferedStream(IOStream& rSource);21 ZeroStream(IOStream::pos_type mSize); 25 22 26 23 virtual int Read(void *pBuffer, int NBytes, int Timeout = IOStream::TimeOutInfinite); … … 35 32 36 33 private: 37 BufferedStream(const BufferedStream &rToCopy) 38 : mrSource(rToCopy.mrSource) { /* do not call */ } 34 ZeroStream(const ZeroStream &rToCopy); 39 35 }; 40 36 41 #endif // BUFFEREDSTREAM__H37 #endif // ZEROSTREAM__H 42 38 43 39
