MemoryBufferFile.cc 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include "MemoryBufferFile.hh"
  2. #include "File.hh"
  3. #include "FileException.hh"
  4. #include <cstring>
  5. #include <memory>
  6. namespace openmsx {
  7. void MemoryBufferFile::read(void* dst, size_t num)
  8. {
  9. if (getSize() < (getPos() + num)) {
  10. throw FileException("Read beyond end of file");
  11. }
  12. memcpy(dst, buffer.data() + pos, num);
  13. pos += num;
  14. }
  15. void MemoryBufferFile::write(const void* /*src*/, size_t /*num*/)
  16. {
  17. throw FileException("Writing to MemoryBufferFile not supported");
  18. }
  19. size_t MemoryBufferFile::getSize()
  20. {
  21. return buffer.size();
  22. }
  23. void MemoryBufferFile::seek(size_t newPos)
  24. {
  25. pos = newPos;
  26. }
  27. size_t MemoryBufferFile::getPos()
  28. {
  29. return pos;
  30. }
  31. void MemoryBufferFile::flush()
  32. {
  33. // nothing
  34. }
  35. std::string MemoryBufferFile::getURL() const
  36. {
  37. return "";
  38. }
  39. bool MemoryBufferFile::isReadOnly() const
  40. {
  41. return true;
  42. }
  43. time_t MemoryBufferFile::getModificationDate()
  44. {
  45. return 0;
  46. }
  47. File memory_buffer_file(span<const uint8_t> buffer)
  48. {
  49. return File(std::make_unique<MemoryBufferFile>(buffer));
  50. }
  51. } // namespace openmsx