| 1 | // -------------------------------------------------------------------------- |
|---|
| 2 | // |
|---|
| 3 | // File |
|---|
| 4 | // Name: MemBlockStream.cpp |
|---|
| 5 | // Purpose: Stream out data from any memory block |
|---|
| 6 | // Created: 2003/09/05 |
|---|
| 7 | // |
|---|
| 8 | // -------------------------------------------------------------------------- |
|---|
| 9 | |
|---|
| 10 | #include "Box.h" |
|---|
| 11 | |
|---|
| 12 | #include <string.h> |
|---|
| 13 | |
|---|
| 14 | #include "MemBlockStream.h" |
|---|
| 15 | #include "CommonException.h" |
|---|
| 16 | #include "StreamableMemBlock.h" |
|---|
| 17 | #include "CollectInBufferStream.h" |
|---|
| 18 | |
|---|
| 19 | #include "MemLeakFindOn.h" |
|---|
| 20 | |
|---|
| 21 | // -------------------------------------------------------------------------- |
|---|
| 22 | // |
|---|
| 23 | // Function |
|---|
| 24 | // Name: MemBlockStream::MemBlockStream() |
|---|
| 25 | // Purpose: Constructor (doesn't copy block, careful with lifetimes) |
|---|
| 26 | // Created: 2003/09/05 |
|---|
| 27 | // |
|---|
| 28 | // -------------------------------------------------------------------------- |
|---|
| 29 | MemBlockStream::MemBlockStream(const void *pBuffer, int Size) |
|---|
| 30 | : mpBuffer((char*)pBuffer), |
|---|
| 31 | mBytesInBuffer(Size), |
|---|
| 32 | mReadPosition(0) |
|---|
| 33 | { |
|---|
| 34 | ASSERT(pBuffer != 0); |
|---|
| 35 | ASSERT(Size >= 0); |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | // -------------------------------------------------------------------------- |
|---|
| 39 | // |
|---|
| 40 | // Function |
|---|
| 41 | // Name: MemBlockStream::MemBlockStream(const StreamableMemBlock &) |
|---|
| 42 | // Purpose: Constructor (doesn't copy block, careful with lifetimes) |
|---|
| 43 | // Created: 2003/09/05 |
|---|
| 44 | // |
|---|
| 45 | // -------------------------------------------------------------------------- |
|---|
| 46 | MemBlockStream::MemBlockStream(const StreamableMemBlock &rBlock) |
|---|
| 47 | : mpBuffer((char*)rBlock.GetBuffer()), |
|---|
| 48 | mBytesInBuffer(rBlock.GetSize()), |
|---|
| 49 | mReadPosition(0) |
|---|
| 50 | { |
|---|
| 51 | ASSERT(mpBuffer != 0); |
|---|
| 52 | ASSERT(mBytesInBuffer >= 0); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | // -------------------------------------------------------------------------- |
|---|
| 56 | // |
|---|
| 57 | // Function |
|---|
| 58 | // Name: MemBlockStream::MemBlockStream(const StreamableMemBlock &) |
|---|
| 59 | // Purpose: Constructor (doesn't copy block, careful with lifetimes) |
|---|
| 60 | // Created: 2003/09/05 |
|---|
| 61 | // |
|---|
| 62 | // -------------------------------------------------------------------------- |
|---|
| 63 | MemBlockStream::MemBlockStream(const CollectInBufferStream &rBuffer) |
|---|
| 64 | : mpBuffer((char*)rBuffer.GetBuffer()), |
|---|
| 65 | mBytesInBuffer(rBuffer.GetSize()), |
|---|
| 66 | mReadPosition(0) |
|---|
| 67 | { |
|---|
| 68 | ASSERT(mpBuffer != 0); |
|---|
| 69 | ASSERT(mBytesInBuffer >= 0); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | // -------------------------------------------------------------------------- |
|---|
| 74 | // |
|---|
| 75 | // Function |
|---|
| 76 | // Name: MemBlockStream::MemBlockStream(const MemBlockStream &) |
|---|
| 77 | // Purpose: Copy constructor |
|---|
| 78 | // Created: 2003/09/05 |
|---|
| 79 | // |
|---|
| 80 | // -------------------------------------------------------------------------- |
|---|
| 81 | MemBlockStream::MemBlockStream(const MemBlockStream &rToCopy) |
|---|
| 82 | : mpBuffer(rToCopy.mpBuffer), |
|---|
| 83 | mBytesInBuffer(rToCopy.mBytesInBuffer), |
|---|
| 84 | mReadPosition(0) |
|---|
| 85 | { |
|---|
| 86 | ASSERT(mpBuffer != 0); |
|---|
| 87 | ASSERT(mBytesInBuffer >= 0); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | // -------------------------------------------------------------------------- |
|---|
| 92 | // |
|---|
| 93 | // Function |
|---|
| 94 | // Name: MemBlockStream::~MemBlockStream() |
|---|
| 95 | // Purpose: Destructor |
|---|
| 96 | // Created: 2003/09/05 |
|---|
| 97 | // |
|---|
| 98 | // -------------------------------------------------------------------------- |
|---|
| 99 | MemBlockStream::~MemBlockStream() |
|---|
| 100 | { |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | // -------------------------------------------------------------------------- |
|---|
| 104 | // |
|---|
| 105 | // Function |
|---|
| 106 | // Name: MemBlockStream::Read(void *, int, int) |
|---|
| 107 | // Purpose: As interface. But only works in read phase |
|---|
| 108 | // Created: 2003/09/05 |
|---|
| 109 | // |
|---|
| 110 | // -------------------------------------------------------------------------- |
|---|
| 111 | int MemBlockStream::Read(void *pBuffer, int NBytes, int Timeout) |
|---|
| 112 | { |
|---|
| 113 | // Adjust to number of bytes left |
|---|
| 114 | if(NBytes > (mBytesInBuffer - mReadPosition)) |
|---|
| 115 | { |
|---|
| 116 | NBytes = (mBytesInBuffer - mReadPosition); |
|---|
| 117 | } |
|---|
| 118 | ASSERT(NBytes >= 0); |
|---|
| 119 | if(NBytes <= 0) return 0; // careful now |
|---|
| 120 | |
|---|
| 121 | // Copy in the requested number of bytes and adjust the read pointer |
|---|
| 122 | ::memcpy(pBuffer, mpBuffer + mReadPosition, NBytes); |
|---|
| 123 | mReadPosition += NBytes; |
|---|
| 124 | |
|---|
| 125 | return NBytes; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | // -------------------------------------------------------------------------- |
|---|
| 129 | // |
|---|
| 130 | // Function |
|---|
| 131 | // Name: MemBlockStream::BytesLeftToRead() |
|---|
| 132 | // Purpose: As interface. But only works in read phase |
|---|
| 133 | // Created: 2003/09/05 |
|---|
| 134 | // |
|---|
| 135 | // -------------------------------------------------------------------------- |
|---|
| 136 | IOStream::pos_type MemBlockStream::BytesLeftToRead() |
|---|
| 137 | { |
|---|
| 138 | return (mBytesInBuffer - mReadPosition); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | // -------------------------------------------------------------------------- |
|---|
| 142 | // |
|---|
| 143 | // Function |
|---|
| 144 | // Name: MemBlockStream::Write(void *, int) |
|---|
| 145 | // Purpose: As interface. But only works in write phase |
|---|
| 146 | // Created: 2003/09/05 |
|---|
| 147 | // |
|---|
| 148 | // -------------------------------------------------------------------------- |
|---|
| 149 | void MemBlockStream::Write(const void *pBuffer, int NBytes) |
|---|
| 150 | { |
|---|
| 151 | THROW_EXCEPTION(CommonException, MemBlockStreamNotSupported) |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | // -------------------------------------------------------------------------- |
|---|
| 155 | // |
|---|
| 156 | // Function |
|---|
| 157 | // Name: MemBlockStream::GetPosition() |
|---|
| 158 | // Purpose: In write phase, returns the number of bytes written, in read |
|---|
| 159 | // phase, the number of bytes to go |
|---|
| 160 | // Created: 2003/09/05 |
|---|
| 161 | // |
|---|
| 162 | // -------------------------------------------------------------------------- |
|---|
| 163 | IOStream::pos_type MemBlockStream::GetPosition() const |
|---|
| 164 | { |
|---|
| 165 | return mReadPosition; |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | // -------------------------------------------------------------------------- |
|---|
| 169 | // |
|---|
| 170 | // Function |
|---|
| 171 | // Name: MemBlockStream::Seek(pos_type, int) |
|---|
| 172 | // Purpose: As interface. |
|---|
| 173 | // Created: 2003/09/05 |
|---|
| 174 | // |
|---|
| 175 | // -------------------------------------------------------------------------- |
|---|
| 176 | void MemBlockStream::Seek(pos_type Offset, int SeekType) |
|---|
| 177 | { |
|---|
| 178 | int newPos = 0; |
|---|
| 179 | switch(SeekType) |
|---|
| 180 | { |
|---|
| 181 | case IOStream::SeekType_Absolute: |
|---|
| 182 | newPos = Offset; |
|---|
| 183 | break; |
|---|
| 184 | case IOStream::SeekType_Relative: |
|---|
| 185 | newPos = mReadPosition + Offset; |
|---|
| 186 | break; |
|---|
| 187 | case IOStream::SeekType_End: |
|---|
| 188 | newPos = mBytesInBuffer + Offset; |
|---|
| 189 | break; |
|---|
| 190 | default: |
|---|
| 191 | THROW_EXCEPTION(CommonException, IOStreamBadSeekType) |
|---|
| 192 | break; |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | // Make sure it doesn't go over |
|---|
| 196 | if(newPos > mBytesInBuffer) |
|---|
| 197 | { |
|---|
| 198 | newPos = mBytesInBuffer; |
|---|
| 199 | } |
|---|
| 200 | // or under |
|---|
| 201 | if(newPos < 0) |
|---|
| 202 | { |
|---|
| 203 | newPos = 0; |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | // Set the new read position |
|---|
| 207 | mReadPosition = newPos; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | // -------------------------------------------------------------------------- |
|---|
| 211 | // |
|---|
| 212 | // Function |
|---|
| 213 | // Name: MemBlockStream::StreamDataLeft() |
|---|
| 214 | // Purpose: As interface |
|---|
| 215 | // Created: 2003/09/05 |
|---|
| 216 | // |
|---|
| 217 | // -------------------------------------------------------------------------- |
|---|
| 218 | bool MemBlockStream::StreamDataLeft() |
|---|
| 219 | { |
|---|
| 220 | return mReadPosition < mBytesInBuffer; |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | // -------------------------------------------------------------------------- |
|---|
| 224 | // |
|---|
| 225 | // Function |
|---|
| 226 | // Name: MemBlockStream::StreamClosed() |
|---|
| 227 | // Purpose: As interface |
|---|
| 228 | // Created: 2003/09/05 |
|---|
| 229 | // |
|---|
| 230 | // -------------------------------------------------------------------------- |
|---|
| 231 | bool MemBlockStream::StreamClosed() |
|---|
| 232 | { |
|---|
| 233 | return true; |
|---|
| 234 | } |
|---|
| 235 | |
|---|