| 1 | // -------------------------------------------------------------------------- |
|---|
| 2 | // |
|---|
| 3 | // File |
|---|
| 4 | // Name: IOStream.cpp |
|---|
| 5 | // Purpose: I/O Stream abstraction |
|---|
| 6 | // Created: 2003/07/31 |
|---|
| 7 | // |
|---|
| 8 | // -------------------------------------------------------------------------- |
|---|
| 9 | |
|---|
| 10 | #include "Box.h" |
|---|
| 11 | #include "IOStream.h" |
|---|
| 12 | #include "CommonException.h" |
|---|
| 13 | #include "Guards.h" |
|---|
| 14 | |
|---|
| 15 | #include "MemLeakFindOn.h" |
|---|
| 16 | |
|---|
| 17 | // -------------------------------------------------------------------------- |
|---|
| 18 | // |
|---|
| 19 | // Function |
|---|
| 20 | // Name: IOStream::IOStream() |
|---|
| 21 | // Purpose: Constructor |
|---|
| 22 | // Created: 2003/07/31 |
|---|
| 23 | // |
|---|
| 24 | // -------------------------------------------------------------------------- |
|---|
| 25 | IOStream::IOStream() |
|---|
| 26 | { |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | // -------------------------------------------------------------------------- |
|---|
| 30 | // |
|---|
| 31 | // Function |
|---|
| 32 | // Name: IOStream::~IOStream() |
|---|
| 33 | // Purpose: Destructor |
|---|
| 34 | // Created: 2003/07/31 |
|---|
| 35 | // |
|---|
| 36 | // -------------------------------------------------------------------------- |
|---|
| 37 | IOStream::~IOStream() |
|---|
| 38 | { |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | // -------------------------------------------------------------------------- |
|---|
| 42 | // |
|---|
| 43 | // Function |
|---|
| 44 | // Name: IOStream::Close() |
|---|
| 45 | // Purpose: Close the stream |
|---|
| 46 | // Created: 2003/07/31 |
|---|
| 47 | // |
|---|
| 48 | // -------------------------------------------------------------------------- |
|---|
| 49 | void IOStream::Close() |
|---|
| 50 | { |
|---|
| 51 | // Do nothing by default -- let the destructor clear everything up. |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | // -------------------------------------------------------------------------- |
|---|
| 55 | // |
|---|
| 56 | // Function |
|---|
| 57 | // Name: IOStream::Seek(int, int) |
|---|
| 58 | // Purpose: Seek in stream (if supported) |
|---|
| 59 | // Created: 2003/07/31 |
|---|
| 60 | // |
|---|
| 61 | // -------------------------------------------------------------------------- |
|---|
| 62 | void IOStream::Seek(IOStream::pos_type Offset, int SeekType) |
|---|
| 63 | { |
|---|
| 64 | THROW_EXCEPTION(CommonException, NotSupported) |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | // -------------------------------------------------------------------------- |
|---|
| 69 | // |
|---|
| 70 | // Function |
|---|
| 71 | // Name: IOStream::GetPosition() |
|---|
| 72 | // Purpose: Returns current position in stream (if supported) |
|---|
| 73 | // Created: 2003/08/21 |
|---|
| 74 | // |
|---|
| 75 | // -------------------------------------------------------------------------- |
|---|
| 76 | IOStream::pos_type IOStream::GetPosition() const |
|---|
| 77 | { |
|---|
| 78 | THROW_EXCEPTION(CommonException, NotSupported) |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | // -------------------------------------------------------------------------- |
|---|
| 82 | // |
|---|
| 83 | // Function |
|---|
| 84 | // Name: IOStream::ConvertSeekTypeToOSWhence(int) |
|---|
| 85 | // Purpose: Return an whence arg for lseek given a IOStream seek type |
|---|
| 86 | // Created: 2003/08/21 |
|---|
| 87 | // |
|---|
| 88 | // -------------------------------------------------------------------------- |
|---|
| 89 | int IOStream::ConvertSeekTypeToOSWhence(int SeekType) |
|---|
| 90 | { |
|---|
| 91 | // Should be nicely optimised out as values are choosen in header file to match OS values. |
|---|
| 92 | int ostype = SEEK_SET; |
|---|
| 93 | switch(SeekType) |
|---|
| 94 | { |
|---|
| 95 | #ifdef WIN32 |
|---|
| 96 | case SeekType_Absolute: |
|---|
| 97 | ostype = FILE_BEGIN; |
|---|
| 98 | break; |
|---|
| 99 | case SeekType_Relative: |
|---|
| 100 | ostype = FILE_CURRENT; |
|---|
| 101 | break; |
|---|
| 102 | case SeekType_End: |
|---|
| 103 | ostype = FILE_END; |
|---|
| 104 | break; |
|---|
| 105 | #else // ! WIN32 |
|---|
| 106 | case SeekType_Absolute: |
|---|
| 107 | ostype = SEEK_SET; |
|---|
| 108 | break; |
|---|
| 109 | case SeekType_Relative: |
|---|
| 110 | ostype = SEEK_CUR; |
|---|
| 111 | break; |
|---|
| 112 | case SeekType_End: |
|---|
| 113 | ostype = SEEK_END; |
|---|
| 114 | break; |
|---|
| 115 | #endif // WIN32 |
|---|
| 116 | |
|---|
| 117 | default: |
|---|
| 118 | THROW_EXCEPTION(CommonException, IOStreamBadSeekType) |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | return ostype; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | // -------------------------------------------------------------------------- |
|---|
| 126 | // |
|---|
| 127 | // Function |
|---|
| 128 | // Name: IOStream::ReadFullBuffer(void *, int, int) |
|---|
| 129 | // Purpose: Reads bytes into buffer, returning whether or not it managed to |
|---|
| 130 | // get all the bytes required. Exception and abort use of stream |
|---|
| 131 | // if this returns false. |
|---|
| 132 | // Created: 2003/08/26 |
|---|
| 133 | // |
|---|
| 134 | // -------------------------------------------------------------------------- |
|---|
| 135 | bool IOStream::ReadFullBuffer(void *pBuffer, int NBytes, int *pNBytesRead, int Timeout) |
|---|
| 136 | { |
|---|
| 137 | int bytesToGo = NBytes; |
|---|
| 138 | char *buffer = (char*)pBuffer; |
|---|
| 139 | if(pNBytesRead) (*pNBytesRead) = 0; |
|---|
| 140 | |
|---|
| 141 | while(bytesToGo > 0) |
|---|
| 142 | { |
|---|
| 143 | int bytesRead = Read(buffer, bytesToGo, Timeout); |
|---|
| 144 | if(bytesRead == 0) |
|---|
| 145 | { |
|---|
| 146 | // Timeout or something |
|---|
| 147 | return false; |
|---|
| 148 | } |
|---|
| 149 | // Increment things |
|---|
| 150 | bytesToGo -= bytesRead; |
|---|
| 151 | buffer += bytesRead; |
|---|
| 152 | if(pNBytesRead) (*pNBytesRead) += bytesRead; |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | // Got everything |
|---|
| 156 | return true; |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | // -------------------------------------------------------------------------- |
|---|
| 161 | // |
|---|
| 162 | // Function |
|---|
| 163 | // Name: IOStream::WriteAllBuffered() |
|---|
| 164 | // Purpose: Ensures that any data which has been buffered is written to the stream |
|---|
| 165 | // Created: 2003/08/26 |
|---|
| 166 | // |
|---|
| 167 | // -------------------------------------------------------------------------- |
|---|
| 168 | void IOStream::WriteAllBuffered() |
|---|
| 169 | { |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | // -------------------------------------------------------------------------- |
|---|
| 174 | // |
|---|
| 175 | // Function |
|---|
| 176 | // Name: IOStream::BytesLeftToRead() |
|---|
| 177 | // Purpose: Numbers of bytes left to read in the stream, or |
|---|
| 178 | // IOStream::SizeOfStreamUnknown if this isn't known. |
|---|
| 179 | // Created: 2003/08/26 |
|---|
| 180 | // |
|---|
| 181 | // -------------------------------------------------------------------------- |
|---|
| 182 | IOStream::pos_type IOStream::BytesLeftToRead() |
|---|
| 183 | { |
|---|
| 184 | return IOStream::SizeOfStreamUnknown; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | // -------------------------------------------------------------------------- |
|---|
| 188 | // |
|---|
| 189 | // Function |
|---|
| 190 | // Name: IOStream::CopyStreamTo(IOStream &, int Timeout) |
|---|
| 191 | // Purpose: Copies the entire stream to another stream (reading from this, |
|---|
| 192 | // writing to rCopyTo). Returns whether the copy completed (ie |
|---|
| 193 | // StreamDataLeft() returns false) |
|---|
| 194 | // Created: 2003/08/26 |
|---|
| 195 | // |
|---|
| 196 | // -------------------------------------------------------------------------- |
|---|
| 197 | bool IOStream::CopyStreamTo(IOStream &rCopyTo, int Timeout, int BufferSize) |
|---|
| 198 | { |
|---|
| 199 | // Make sure there's something to do before allocating that buffer |
|---|
| 200 | if(!StreamDataLeft()) |
|---|
| 201 | { |
|---|
| 202 | return true; // complete, even though nothing happened |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | // Buffer |
|---|
| 206 | MemoryBlockGuard<char*> buffer(BufferSize); |
|---|
| 207 | |
|---|
| 208 | // Get copying! |
|---|
| 209 | while(StreamDataLeft()) |
|---|
| 210 | { |
|---|
| 211 | // Read some data |
|---|
| 212 | int bytes = Read(buffer, BufferSize, Timeout); |
|---|
| 213 | if(bytes == 0 && StreamDataLeft()) |
|---|
| 214 | { |
|---|
| 215 | return false; // incomplete, timed out |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | // Write some data |
|---|
| 219 | if(bytes != 0) |
|---|
| 220 | { |
|---|
| 221 | rCopyTo.Write(buffer, bytes); |
|---|
| 222 | } |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | return true; // completed |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | // -------------------------------------------------------------------------- |
|---|
| 229 | // |
|---|
| 230 | // Function |
|---|
| 231 | // Name: IOStream::Flush(int Timeout) |
|---|
| 232 | // Purpose: Read and discard all remaining data in stream. |
|---|
| 233 | // Useful for protocol streams which must be flushed |
|---|
| 234 | // to avoid breaking the protocol. |
|---|
| 235 | // Created: 2008/08/20 |
|---|
| 236 | // |
|---|
| 237 | // -------------------------------------------------------------------------- |
|---|
| 238 | void IOStream::Flush(int Timeout) |
|---|
| 239 | { |
|---|
| 240 | char buffer[4096]; |
|---|
| 241 | |
|---|
| 242 | while(StreamDataLeft()) |
|---|
| 243 | { |
|---|
| 244 | Read(buffer, sizeof(buffer), Timeout); |
|---|
| 245 | } |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | void IOStream::Write(const char *pBuffer) |
|---|
| 249 | { |
|---|
| 250 | Write(pBuffer, strlen(pBuffer)); |
|---|
| 251 | } |
|---|