| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_IO_IO_STDSTREAM_HPP |
| 4 |
#define MPT_IO_IO_STDSTREAM_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/macros.hpp" |
| 9 |
#include "mpt/base/memory.hpp" |
| 10 |
#include "mpt/base/namespace.hpp" |
| 11 |
#include "mpt/base/saturate_cast.hpp" |
| 12 |
#include "mpt/base/utility.hpp" |
| 13 |
#include "mpt/io/base.hpp" |
| 14 |
|
| 15 |
#include <ios> |
| 16 |
#include <istream> |
| 17 |
#include <ostream> |
| 18 |
#include <type_traits> |
| 19 |
|
| 20 |
#include <cstddef> |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
namespace mpt { |
| 25 |
inline namespace MPT_INLINE_NS { |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
namespace IO { |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
struct FileOperationsStdIos { |
| 34 |
|
| 35 |
private: |
| 36 |
std::ios & f; |
| 37 |
|
| 38 |
public: |
| 39 |
inline FileOperationsStdIos(std::ios & f_) |
| 40 |
: f(f_) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
public: |
| 45 |
inline bool IsValid() { |
| 46 |
return !f.fail(); |
| 47 |
} |
| 48 |
}; |
| 49 |
|
| 50 |
struct FileOperationsStdIstream |
| 51 |
: public FileOperationsStdIos { |
| 52 |
|
| 53 |
private: |
| 54 |
std::istream & f; |
| 55 |
|
| 56 |
public: |
| 57 |
inline FileOperationsStdIstream(std::istream & f_) |
| 58 |
: FileOperationsStdIos(f_) |
| 59 |
, f(f_) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
public: |
| 64 |
inline bool IsReadSeekable() { |
| 65 |
f.clear(); |
| 66 |
std::streampos oldpos = f.tellg(); |
| 67 |
if (f.fail() || oldpos == std::streampos(std::streamoff(-1))) { |
| 68 |
f.clear(); |
| 69 |
return false; |
| 70 |
} |
| 71 |
f.seekg(0, std::ios::beg); |
| 72 |
if (f.fail()) { |
| 73 |
f.clear(); |
| 74 |
f.seekg(oldpos); |
| 75 |
f.clear(); |
| 76 |
return false; |
| 77 |
} |
| 78 |
f.seekg(0, std::ios::end); |
| 79 |
if (f.fail()) { |
| 80 |
f.clear(); |
| 81 |
f.seekg(oldpos); |
| 82 |
f.clear(); |
| 83 |
return false; |
| 84 |
} |
| 85 |
std::streampos length = f.tellg(); |
| 86 |
if (f.fail() || length == std::streampos(std::streamoff(-1))) { |
| 87 |
f.clear(); |
| 88 |
f.seekg(oldpos); |
| 89 |
f.clear(); |
| 90 |
return false; |
| 91 |
} |
| 92 |
f.seekg(oldpos); |
| 93 |
f.clear(); |
| 94 |
return true; |
| 95 |
} |
| 96 |
|
| 97 |
inline IO::Offset TellRead() { |
| 98 |
return static_cast<std::streamoff>(f.tellg()); |
| 99 |
} |
| 100 |
|
| 101 |
inline bool SeekBegin() { |
| 102 |
f.seekg(0, std::ios::beg); |
| 103 |
return !f.fail(); |
| 104 |
} |
| 105 |
|
| 106 |
inline bool SeekEnd() { |
| 107 |
f.seekg(0, std::ios::end); |
| 108 |
return !f.fail(); |
| 109 |
} |
| 110 |
|
| 111 |
inline bool SeekAbsolute(IO::Offset pos) { |
| 112 |
if (!mpt::in_range<std::streamoff>(pos)) { |
| 113 |
return false; |
| 114 |
} |
| 115 |
f.seekg(static_cast<std::streamoff>(pos), std::ios::beg); |
| 116 |
return !f.fail(); |
| 117 |
} |
| 118 |
|
| 119 |
inline bool SeekRelative(IO::Offset off) { |
| 120 |
if (!mpt::in_range<std::streamoff>(off)) { |
| 121 |
return false; |
| 122 |
} |
| 123 |
f.seekg(static_cast<std::streamoff>(off), std::ios::cur); |
| 124 |
return !f.fail(); |
| 125 |
} |
| 126 |
|
| 127 |
inline mpt::byte_span ReadRawImpl(mpt::byte_span data) { |
| 128 |
std::size_t bytesToRead = data.size(); |
| 129 |
std::size_t bytesRead = 0; |
| 130 |
while (bytesToRead > 0) { |
| 131 |
std::streamsize bytesChunkToRead = mpt::saturate_cast<std::streamsize>(bytesToRead); |
| 132 |
f.read(mpt::byte_cast<char *>(data.data()) + bytesRead, bytesChunkToRead); |
| 133 |
std::streamsize bytesChunkRead = f.gcount(); |
| 134 |
bytesRead += static_cast<std::size_t>(bytesChunkRead); |
| 135 |
bytesToRead -= static_cast<std::size_t>(bytesChunkRead); |
| 136 |
if (bytesChunkRead != bytesChunkToRead) { |
| 137 |
break; |
| 138 |
} |
| 139 |
} |
| 140 |
return data.first(bytesRead); |
| 141 |
} |
| 142 |
|
| 143 |
inline bool IsEof() { |
| 144 |
return f.eof(); |
| 145 |
} |
| 146 |
}; |
| 147 |
|
| 148 |
struct FileOperationsStdOstream |
| 149 |
: public FileOperationsStdIos { |
| 150 |
|
| 151 |
private: |
| 152 |
std::ostream & f; |
| 153 |
|
| 154 |
public: |
| 155 |
inline FileOperationsStdOstream(std::ostream & f_) |
| 156 |
: FileOperationsStdIos(f_) |
| 157 |
, f(f_) { |
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
public: |
| 162 |
inline bool IsWriteSeekable() { |
| 163 |
f.clear(); |
| 164 |
std::streampos oldpos = f.tellp(); |
| 165 |
if (f.fail() || oldpos == std::streampos(std::streamoff(-1))) { |
| 166 |
f.clear(); |
| 167 |
return false; |
| 168 |
} |
| 169 |
f.seekp(0, std::ios::beg); |
| 170 |
if (f.fail()) { |
| 171 |
f.clear(); |
| 172 |
f.seekp(oldpos); |
| 173 |
f.clear(); |
| 174 |
return false; |
| 175 |
} |
| 176 |
f.seekp(0, std::ios::end); |
| 177 |
if (f.fail()) { |
| 178 |
f.clear(); |
| 179 |
f.seekp(oldpos); |
| 180 |
f.clear(); |
| 181 |
return false; |
| 182 |
} |
| 183 |
std::streampos length = f.tellp(); |
| 184 |
if (f.fail() || length == std::streampos(std::streamoff(-1))) { |
| 185 |
f.clear(); |
| 186 |
f.seekp(oldpos); |
| 187 |
f.clear(); |
| 188 |
return false; |
| 189 |
} |
| 190 |
f.seekp(oldpos); |
| 191 |
f.clear(); |
| 192 |
return true; |
| 193 |
} |
| 194 |
|
| 195 |
inline IO::Offset TellWrite() { |
| 196 |
return static_cast<std::streamoff>(f.tellp()); |
| 197 |
} |
| 198 |
|
| 199 |
inline bool SeekBegin() { |
| 200 |
f.seekp(0, std::ios::beg); |
| 201 |
return !f.fail(); |
| 202 |
} |
| 203 |
|
| 204 |
inline bool SeekEnd() { |
| 205 |
f.seekp(0, std::ios::end); |
| 206 |
return !f.fail(); |
| 207 |
} |
| 208 |
|
| 209 |
inline bool SeekAbsolute(IO::Offset pos) { |
| 210 |
if (!mpt::in_range<std::streamoff>(pos)) { |
| 211 |
return false; |
| 212 |
} |
| 213 |
f.seekp(static_cast<std::streamoff>(pos), std::ios::beg); |
| 214 |
return !f.fail(); |
| 215 |
} |
| 216 |
|
| 217 |
inline bool SeekRelative(IO::Offset off) { |
| 218 |
if (!mpt::in_range<std::streamoff>(off)) { |
| 219 |
return false; |
| 220 |
} |
| 221 |
f.seekp(static_cast<std::streamoff>(off), std::ios::cur); |
| 222 |
return !f.fail(); |
| 223 |
} |
| 224 |
|
| 225 |
inline bool WriteRawImpl(mpt::const_byte_span data) { |
| 226 |
std::size_t bytesToWrite = data.size(); |
| 227 |
std::size_t bytesWritten = 0; |
| 228 |
while (bytesToWrite > 0) { |
| 229 |
std::streamsize bytesChunk = mpt::saturate_cast<std::streamsize>(bytesToWrite); |
| 230 |
f.write(mpt::byte_cast<const char *>(data.data()) + bytesWritten, bytesChunk); |
| 231 |
if (f.fail()) { |
| 232 |
break; |
| 233 |
} |
| 234 |
bytesWritten += static_cast<std::size_t>(bytesChunk); |
| 235 |
bytesToWrite -= static_cast<std::size_t>(bytesChunk); |
| 236 |
} |
| 237 |
return !f.fail(); |
| 238 |
} |
| 239 |
|
| 240 |
inline bool Flush() { |
| 241 |
f.flush(); |
| 242 |
return !f.fail(); |
| 243 |
} |
| 244 |
}; |
| 245 |
|
| 246 |
struct FileOperationsStdIOstream |
| 247 |
: public FileOperationsStdIstream |
| 248 |
, public FileOperationsStdOstream { |
| 249 |
|
| 250 |
private: |
| 251 |
std::iostream & f; |
| 252 |
|
| 253 |
public: |
| 254 |
inline FileOperationsStdIOstream(std::iostream & f_) |
| 255 |
: FileOperationsStdIstream(f_) |
| 256 |
, FileOperationsStdOstream(f_) |
| 257 |
, f(f_) { |
| 258 |
return; |
| 259 |
} |
| 260 |
|
| 261 |
public: |
| 262 |
// cppcheck-suppress duplInheritedMember |
| 263 |
inline bool SeekBegin() { |
| 264 |
FileOperationsStdIstream::SeekBegin(); |
| 265 |
FileOperationsStdOstream::SeekBegin(); |
| 266 |
return !f.fail(); |
| 267 |
} |
| 268 |
|
| 269 |
// cppcheck-suppress duplInheritedMember |
| 270 |
inline bool SeekEnd() { |
| 271 |
FileOperationsStdIstream::SeekEnd(); |
| 272 |
FileOperationsStdOstream::SeekEnd(); |
| 273 |
return !f.fail(); |
| 274 |
} |
| 275 |
|
| 276 |
// cppcheck-suppress duplInheritedMember |
| 277 |
inline bool SeekAbsolute(IO::Offset pos) { |
| 278 |
if (!mpt::in_range<std::streamoff>(pos)) { |
| 279 |
return false; |
| 280 |
} |
| 281 |
FileOperationsStdIstream::SeekAbsolute(pos); |
| 282 |
FileOperationsStdOstream::SeekAbsolute(pos); |
| 283 |
return !f.fail(); |
| 284 |
} |
| 285 |
|
| 286 |
// cppcheck-suppress duplInheritedMember |
| 287 |
inline bool SeekRelative(IO::Offset off) { |
| 288 |
if (!mpt::in_range<std::streamoff>(off)) { |
| 289 |
return false; |
| 290 |
} |
| 291 |
FileOperationsStdIstream::SeekRelative(off); |
| 292 |
FileOperationsStdOstream::SeekRelative(off); |
| 293 |
return !f.fail(); |
| 294 |
} |
| 295 |
}; |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
template <typename Tstream> |
| 300 |
struct FileOperations<Tstream, typename std::enable_if_t<std::is_base_of<std::iostream, Tstream>::value>> |
| 301 |
: public FileOperationsStdIOstream { |
| 302 |
public: |
| 303 |
inline FileOperations(Tstream & f) |
| 304 |
: FileOperationsStdIOstream(f) { |
| 305 |
return; |
| 306 |
} |
| 307 |
}; |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
template <typename Tstream> |
| 312 |
struct FileOperations<Tstream, typename std::enable_if_t<std::is_base_of<std::istream, Tstream>::value && !std::is_base_of<std::iostream, Tstream>::value>> |
| 313 |
: public FileOperationsStdIstream { |
| 314 |
public: |
| 315 |
inline FileOperations(Tstream & f) |
| 316 |
: FileOperationsStdIstream(f) { |
| 317 |
return; |
| 318 |
} |
| 319 |
}; |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
template <typename Tstream> |
| 324 |
struct FileOperations<Tstream, typename std::enable_if_t<std::is_base_of<std::ostream, Tstream>::value && !std::is_base_of<std::iostream, Tstream>::value>> |
| 325 |
: public FileOperationsStdOstream { |
| 326 |
public: |
| 327 |
inline FileOperations(Tstream & f) |
| 328 |
: FileOperationsStdOstream(f) { |
| 329 |
return; |
| 330 |
} |
| 331 |
}; |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
} // namespace IO |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
} // namespace MPT_INLINE_NS |
| 340 |
} // namespace mpt |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
#endif // MPT_IO_IO_STDSTREAM_HPP |