| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_IO_READ_FILEDATA_STDSTREAM_HPP |
| 4 |
#define MPT_IO_READ_FILEDATA_STDSTREAM_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/memory.hpp" |
| 9 |
#include "mpt/base/namespace.hpp" |
| 10 |
#include "mpt/base/saturate_cast.hpp" |
| 11 |
#include "mpt/io/base.hpp" |
| 12 |
#include "mpt/io/io_stdstream.hpp" |
| 13 |
#include "mpt/io_read/filedata.hpp" |
| 14 |
#include "mpt/io_read/filedata_base_buffered.hpp" |
| 15 |
#include "mpt/io_read/filedata_base_unseekable.hpp" |
| 16 |
|
| 17 |
#include <ios> |
| 18 |
#include <istream> |
| 19 |
#include <ostream> |
| 20 |
|
| 21 |
#include <cstddef> |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
namespace mpt { |
| 26 |
inline namespace MPT_INLINE_NS { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
namespace IO { |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class FileDataStdStream { |
| 35 |
|
| 36 |
public: |
| 37 |
static bool IsSeekable(std::istream & stream) { |
| 38 |
return mpt::IO::IsReadSeekable(stream); |
| 39 |
} |
| 40 |
|
| 41 |
static IFileData::pos_type GetLength(std::istream & stream) { |
| 42 |
stream.clear(); |
| 43 |
std::streampos oldpos = stream.tellg(); |
| 44 |
stream.seekg(0, std::ios::end); |
| 45 |
std::streamoff length = static_cast<std::streamoff>(stream.tellg()); |
| 46 |
stream.seekg(oldpos); |
| 47 |
return mpt::saturate_cast<IFileData::pos_type>(static_cast<int64>(length)); |
| 48 |
} |
| 49 |
}; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
class FileDataStdStreamSeekable : public FileDataSeekableBuffered { |
| 54 |
|
| 55 |
private: |
| 56 |
std::istream & stream; |
| 57 |
|
| 58 |
public: |
| 59 |
FileDataStdStreamSeekable(std::istream & s) |
| 60 |
: FileDataSeekableBuffered(FileDataStdStream::GetLength(s)) |
| 61 |
, stream(s) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
private: |
| 66 |
mpt::byte_span InternalReadBuffered(pos_type pos, mpt::byte_span dst) const override { |
| 67 |
stream.clear(); // tellg needs eof and fail bits unset |
| 68 |
std::streampos currentpos = stream.tellg(); |
| 69 |
if ((currentpos == std::streampos(std::streamoff(-1))) || (static_cast<std::streamoff>(pos) != static_cast<std::streamoff>(currentpos))) { |
| 70 |
// inefficient istream implementations might invalidate their buffer when seeking, even when seeking to the current position |
| 71 |
stream.seekg(static_cast<std::streamoff>(pos), std::ios::beg); |
| 72 |
} |
| 73 |
std::size_t bytesToRead = dst.size(); |
| 74 |
std::size_t bytesRead = 0; |
| 75 |
while (bytesToRead > 0) { |
| 76 |
std::streamsize bytesChunkToRead = mpt::saturate_cast<std::streamsize>(bytesToRead); |
| 77 |
stream.read(mpt::byte_cast<char *>(dst.data()) + bytesRead, bytesChunkToRead); |
| 78 |
std::streamsize bytesChunkRead = stream.gcount(); |
| 79 |
bytesRead += static_cast<std::size_t>(bytesChunkRead); |
| 80 |
bytesToRead -= static_cast<std::size_t>(bytesChunkRead); |
| 81 |
if (bytesChunkRead != bytesChunkToRead) { |
| 82 |
break; |
| 83 |
} |
| 84 |
} |
| 85 |
return dst.first(bytesRead); |
| 86 |
} |
| 87 |
}; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
class FileDataStdStreamUnseekable : public FileDataUnseekable { |
| 92 |
|
| 93 |
private: |
| 94 |
std::istream & stream; |
| 95 |
|
| 96 |
public: |
| 97 |
FileDataStdStreamUnseekable(std::istream & s) |
| 98 |
: stream(s) { |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
private: |
| 103 |
bool InternalEof() const override { |
| 104 |
if (stream) { |
| 105 |
return false; |
| 106 |
} else { |
| 107 |
return true; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
mpt::byte_span InternalReadUnseekable(mpt::byte_span dst) const override { |
| 112 |
std::size_t bytesToRead = dst.size(); |
| 113 |
std::size_t bytesRead = 0; |
| 114 |
while (bytesToRead > 0) { |
| 115 |
std::streamsize bytesChunkToRead = mpt::saturate_cast<std::streamsize>(bytesToRead); |
| 116 |
stream.read(mpt::byte_cast<char *>(dst.data()) + bytesRead, bytesChunkToRead); |
| 117 |
std::streamsize bytesChunkRead = stream.gcount(); |
| 118 |
bytesRead += static_cast<std::size_t>(bytesChunkRead); |
| 119 |
bytesToRead -= static_cast<std::size_t>(bytesChunkRead); |
| 120 |
if (bytesChunkRead != bytesChunkToRead) { |
| 121 |
break; |
| 122 |
} |
| 123 |
} |
| 124 |
return dst.first(bytesRead); |
| 125 |
} |
| 126 |
}; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
} // namespace IO |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
} // namespace MPT_INLINE_NS |
| 135 |
} // namespace mpt |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
#endif // MPT_IO_READ_FILEDATA_STDSTREAM_HPP |