| 1 | // -------------------------------------------------------------------------- |
|---|
| 2 | // |
|---|
| 3 | // File |
|---|
| 4 | // Name: ReadLoggingStream.cpp |
|---|
| 5 | // Purpose: Buffering wrapper around IOStreams |
|---|
| 6 | // Created: 2007/01/16 |
|---|
| 7 | // |
|---|
| 8 | // -------------------------------------------------------------------------- |
|---|
| 9 | |
|---|
| 10 | #include "Box.h" |
|---|
| 11 | |
|---|
| 12 | #include <string.h> |
|---|
| 13 | |
|---|
| 14 | #include "ReadLoggingStream.h" |
|---|
| 15 | #include "CommonException.h" |
|---|
| 16 | #include "Logging.h" |
|---|
| 17 | |
|---|
| 18 | #include "MemLeakFindOn.h" |
|---|
| 19 | |
|---|
| 20 | // -------------------------------------------------------------------------- |
|---|
| 21 | // |
|---|
| 22 | // Function |
|---|
| 23 | // Name: ReadLoggingStream::ReadLoggingStream(const char *, int, int) |
|---|
| 24 | // Purpose: Constructor, set up buffer |
|---|
| 25 | // Created: 2007/01/16 |
|---|
| 26 | // |
|---|
| 27 | // -------------------------------------------------------------------------- |
|---|
| 28 | ReadLoggingStream::ReadLoggingStream(IOStream& rSource, Logger& rLogger) |
|---|
| 29 | : mrSource(rSource), |
|---|
| 30 | mOffset(0), |
|---|
| 31 | mLength(mrSource.BytesLeftToRead()), |
|---|
| 32 | mTotalRead(0), |
|---|
| 33 | mStartTime(GetCurrentBoxTime()), |
|---|
| 34 | mrLogger(rLogger) |
|---|
| 35 | { } |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | // -------------------------------------------------------------------------- |
|---|
| 39 | // |
|---|
| 40 | // Function |
|---|
| 41 | // Name: ReadLoggingStream::Read(void *, int) |
|---|
| 42 | // Purpose: Reads bytes from the file |
|---|
| 43 | // Created: 2007/01/16 |
|---|
| 44 | // |
|---|
| 45 | // -------------------------------------------------------------------------- |
|---|
| 46 | int ReadLoggingStream::Read(void *pBuffer, int NBytes, int Timeout) |
|---|
| 47 | { |
|---|
| 48 | int numBytesRead = mrSource.Read(pBuffer, NBytes, Timeout); |
|---|
| 49 | |
|---|
| 50 | if (numBytesRead > 0) |
|---|
| 51 | { |
|---|
| 52 | mTotalRead += numBytesRead; |
|---|
| 53 | mOffset += numBytesRead; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | if (mLength == 0) |
|---|
| 57 | { |
|---|
| 58 | mrLogger.Log(numBytesRead, mOffset); |
|---|
| 59 | } |
|---|
| 60 | else if (mTotalRead == 0) |
|---|
| 61 | { |
|---|
| 62 | mrLogger.Log(numBytesRead, mOffset, mLength); |
|---|
| 63 | } |
|---|
| 64 | else |
|---|
| 65 | { |
|---|
| 66 | box_time_t timeNow = GetCurrentBoxTime(); |
|---|
| 67 | box_time_t elapsed = timeNow - mStartTime; |
|---|
| 68 | box_time_t finish = (elapsed * mLength) / mTotalRead; |
|---|
| 69 | // box_time_t remain = finish - elapsed; |
|---|
| 70 | mrLogger.Log(numBytesRead, mOffset, mLength, elapsed, finish); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | return numBytesRead; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | // -------------------------------------------------------------------------- |
|---|
| 78 | // |
|---|
| 79 | // Function |
|---|
| 80 | // Name: ReadLoggingStream::BytesLeftToRead() |
|---|
| 81 | // Purpose: Returns number of bytes to read (may not be most efficient function ever) |
|---|
| 82 | // Created: 2007/01/16 |
|---|
| 83 | // |
|---|
| 84 | // -------------------------------------------------------------------------- |
|---|
| 85 | IOStream::pos_type ReadLoggingStream::BytesLeftToRead() |
|---|
| 86 | { |
|---|
| 87 | return mLength - mOffset; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | // -------------------------------------------------------------------------- |
|---|
| 92 | // |
|---|
| 93 | // Function |
|---|
| 94 | // Name: ReadLoggingStream::Write(void *, int) |
|---|
| 95 | // Purpose: Writes bytes to the underlying stream (not supported) |
|---|
| 96 | // Created: 2003/07/31 |
|---|
| 97 | // |
|---|
| 98 | // -------------------------------------------------------------------------- |
|---|
| 99 | void ReadLoggingStream::Write(const void *pBuffer, int NBytes) |
|---|
| 100 | { |
|---|
| 101 | THROW_EXCEPTION(CommonException, NotSupported); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | // -------------------------------------------------------------------------- |
|---|
| 106 | // |
|---|
| 107 | // Function |
|---|
| 108 | // Name: ReadLoggingStream::GetPosition() |
|---|
| 109 | // Purpose: Get position in stream |
|---|
| 110 | // Created: 2003/08/21 |
|---|
| 111 | // |
|---|
| 112 | // -------------------------------------------------------------------------- |
|---|
| 113 | IOStream::pos_type ReadLoggingStream::GetPosition() const |
|---|
| 114 | { |
|---|
| 115 | return mOffset; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | // -------------------------------------------------------------------------- |
|---|
| 120 | // |
|---|
| 121 | // Function |
|---|
| 122 | // Name: ReadLoggingStream::Seek(pos_type, int) |
|---|
| 123 | // Purpose: Seeks within file, as lseek, invalidate buffer |
|---|
| 124 | // Created: 2003/07/31 |
|---|
| 125 | // |
|---|
| 126 | // -------------------------------------------------------------------------- |
|---|
| 127 | void ReadLoggingStream::Seek(IOStream::pos_type Offset, int SeekType) |
|---|
| 128 | { |
|---|
| 129 | mrSource.Seek(Offset, SeekType); |
|---|
| 130 | |
|---|
| 131 | switch (SeekType) |
|---|
| 132 | { |
|---|
| 133 | case SeekType_Absolute: |
|---|
| 134 | { |
|---|
| 135 | // just go there |
|---|
| 136 | mOffset = Offset; |
|---|
| 137 | } |
|---|
| 138 | break; |
|---|
| 139 | |
|---|
| 140 | case SeekType_Relative: |
|---|
| 141 | { |
|---|
| 142 | // Actual underlying file position is |
|---|
| 143 | // (mBufferSize - mBufferPosition) ahead of us. |
|---|
| 144 | // Need to subtract that amount from the seek |
|---|
| 145 | // to seek forward that much less, putting the |
|---|
| 146 | // real pointer in the right place. |
|---|
| 147 | mOffset += Offset; |
|---|
| 148 | } |
|---|
| 149 | break; |
|---|
| 150 | |
|---|
| 151 | case SeekType_End: |
|---|
| 152 | { |
|---|
| 153 | // Actual underlying file position is |
|---|
| 154 | // (mBufferSize - mBufferPosition) ahead of us. |
|---|
| 155 | // Need to add that amount to the seek |
|---|
| 156 | // to seek backwards that much more, putting the |
|---|
| 157 | // real pointer in the right place. |
|---|
| 158 | mOffset = mLength - Offset; |
|---|
| 159 | } |
|---|
| 160 | } |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | // -------------------------------------------------------------------------- |
|---|
| 165 | // |
|---|
| 166 | // Function |
|---|
| 167 | // Name: ReadLoggingStream::Close() |
|---|
| 168 | // Purpose: Closes the underlying stream (not needed) |
|---|
| 169 | // Created: 2003/07/31 |
|---|
| 170 | // |
|---|
| 171 | // -------------------------------------------------------------------------- |
|---|
| 172 | void ReadLoggingStream::Close() |
|---|
| 173 | { |
|---|
| 174 | THROW_EXCEPTION(CommonException, NotSupported); |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | // -------------------------------------------------------------------------- |
|---|
| 179 | // |
|---|
| 180 | // Function |
|---|
| 181 | // Name: ReadLoggingStream::StreamDataLeft() |
|---|
| 182 | // Purpose: Any data left to write? |
|---|
| 183 | // Created: 2003/08/02 |
|---|
| 184 | // |
|---|
| 185 | // -------------------------------------------------------------------------- |
|---|
| 186 | bool ReadLoggingStream::StreamDataLeft() |
|---|
| 187 | { |
|---|
| 188 | return mrSource.StreamDataLeft(); |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | // -------------------------------------------------------------------------- |
|---|
| 192 | // |
|---|
| 193 | // Function |
|---|
| 194 | // Name: ReadLoggingStream::StreamClosed() |
|---|
| 195 | // Purpose: Is the stream closed? |
|---|
| 196 | // Created: 2003/08/02 |
|---|
| 197 | // |
|---|
| 198 | // -------------------------------------------------------------------------- |
|---|
| 199 | bool ReadLoggingStream::StreamClosed() |
|---|
| 200 | { |
|---|
| 201 | return mrSource.StreamClosed(); |
|---|
| 202 | } |
|---|
| 203 | |
|---|