openMSX
FileBase.cc
Go to the documentation of this file.
1#include "FileBase.hh"
2#include "FileOperations.hh"
3#include "ranges.hh"
4#include <algorithm>
5#include <array>
6
7namespace openmsx {
8
9std::span<const uint8_t> FileBase::mmap()
10{
11 auto size = getSize();
12 if (mmapBuf.empty()) {
13 auto pos = getPos();
14 seek(0);
15
16 MemBuffer<uint8_t> tmpBuf(size);
17 read(std::span{tmpBuf});
18 std::swap(mmapBuf, tmpBuf);
19
20 seek(pos);
21 }
22 return std::span{mmapBuf};
23}
24
26{
27 mmapBuf.clear();
28}
29
30void FileBase::truncate(size_t newSize)
31{
32 auto oldSize = getSize();
33 if (newSize < oldSize) {
34 // default truncate() can't shrink file
35 return;
36 }
37 auto remaining = newSize - oldSize;
38 seek(oldSize);
39
40 std::array<uint8_t, 4096> buf = {}; // zero-initialized
41 while (remaining) {
42 auto chunkSize = std::min(buf.size(), remaining);
43 write(subspan(buf, 0, chunkSize));
44 remaining -= chunkSize;
45 }
46}
47
49{
50 // default implementation, file is not backed (uncompressed) on
51 // the local file system
52 return {};
53}
54
55std::string_view FileBase::getOriginalName()
56{
57 // default implementation just returns filename portion of URL
59}
60
61} // namespace openmsx
virtual void write(std::span< const uint8_t > buffer)=0
virtual size_t getSize()=0
virtual const std::string & getURL() const =0
virtual void read(std::span< uint8_t > buffer)=0
virtual std::string_view getOriginalName()
Definition FileBase.cc:55
virtual std::string getLocalReference()
Definition FileBase.cc:48
virtual void seek(size_t pos)=0
virtual std::span< const uint8_t > mmap()
Definition FileBase.cc:9
virtual size_t getPos()=0
virtual void truncate(size_t size)
Definition FileBase.cc:30
virtual void munmap()
Definition FileBase.cc:25
This class manages the lifetime of a block of memory.
Definition MemBuffer.hh:32
bool empty() const
Definition MemBuffer.hh:100
void clear()
Free the allocated memory block and set the current size to 0.
Definition MemBuffer.hh:168
string_view getFilename(string_view path)
Returns the file portion of a path name.
This file implemented 3 utility functions:
Definition Autofire.cc:11
constexpr auto subspan(Range &&range, size_t offset, size_t count=std::dynamic_extent)
Definition ranges.hh:481