| 1 |
#include "MemoryBufferFile.hh" |
| 2 |
#include "File.hh" |
| 3 |
#include "FileException.hh" |
| 4 |
#include "ranges.hh" |
| 5 |
#include <memory> |
| 6 |
|
| 7 |
namespace openmsx { |
| 8 |
|
| 9 |
void MemoryBufferFile::read(std::span<uint8_t> dst) |
| 10 |
{ |
| 11 |
if (getSize() < (getPos() + dst.size())) { |
| 12 |
throw FileException("Read beyond end of file"); |
| 13 |
} |
| 14 |
copy_to_range(buffer.subspan(pos, dst.size()), dst); |
| 15 |
pos += dst.size(); |
| 16 |
} |
| 17 |
|
| 18 |
void MemoryBufferFile::write(std::span<const uint8_t> ) |
| 19 |
{ |
| 20 |
throw FileException("Writing to MemoryBufferFile not supported"); |
| 21 |
} |
| 22 |
|
| 23 |
MappedFileImpl MemoryBufferFile::mmap(size_t extra, bool is_const) |
| 24 |
{ |
| 25 |
return {buffer, extra, is_const}; |
| 26 |
} |
| 27 |
|
| 28 |
size_t MemoryBufferFile::getSize() |
| 29 |
{ |
| 30 |
return buffer.size(); |
| 31 |
} |
| 32 |
|
| 33 |
void MemoryBufferFile::seek(size_t newPos) |
| 34 |
{ |
| 35 |
pos = newPos; |
| 36 |
} |
| 37 |
|
| 38 |
size_t MemoryBufferFile::getPos() |
| 39 |
{ |
| 40 |
return pos; |
| 41 |
} |
| 42 |
|
| 43 |
void MemoryBufferFile::flush() |
| 44 |
{ |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
bool MemoryBufferFile::isReadOnly() const |
| 49 |
{ |
| 50 |
return true; |
| 51 |
} |
| 52 |
|
| 53 |
time_t MemoryBufferFile::getModificationDate() |
| 54 |
{ |
| 55 |
return 0; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
File memory_buffer_file(std::span<const uint8_t> buffer) |
| 60 |
{ |
| 61 |
return File(std::make_unique<MemoryBufferFile>(buffer)); |
| 62 |
} |
| 63 |
|
| 64 |
} |