openMSX
FileBase.cc
Go to the documentation of this file.
1 #include "FileBase.hh"
2 #include "FileOperations.hh"
3 #include <algorithm>
4 #include <cstring>
5 
6 using std::string;
7 
8 namespace openmsx {
9 
10 const byte* FileBase::mmap(size_t& size)
11 {
12  if (mmapBuf.empty()) {
13  size = getSize();
14  MemBuffer<byte> tmpBuf(size);
15  read(tmpBuf.data(), size);
16  std::swap(mmapBuf, tmpBuf);
17  }
18  return mmapBuf.data();
19 }
20 
22 {
23  mmapBuf.clear();
24 }
25 
26 void FileBase::truncate(size_t newSize)
27 {
28  auto oldSize = getSize();
29  if (newSize < oldSize) {
30  // default truncate() can't shrink file
31  return;
32  }
33  auto remaining = newSize - oldSize;
34  seek(oldSize);
35 
36  static const size_t BUF_SIZE = 4096;
37  byte buf[BUF_SIZE];
38  memset(buf, 0, sizeof(buf));
39  while (remaining) {
40  auto chunkSize = std::min(BUF_SIZE, remaining);
41  write(buf, chunkSize);
42  remaining -= chunkSize;
43  }
44 }
45 
47 {
48  // default implementation, file is not backed (uncompressed) on
49  // the local file system
50  return {};
51 }
52 
54 {
55  // default implementation just returns filename portion of URL
57 }
58 
59 } // namespace openmsx
string_view getFilename(string_view path)
Returns the file portion of a path name.
vecN< N, T > min(const vecN< N, T > &x, const vecN< N, T > &y)
Definition: gl_vec.hh:268
virtual const std::string getOriginalName()
Definition: FileBase.cc:53
uint8_t byte
8 bit unsigned integer
Definition: openmsx.hh:26
virtual const std::string getURL() const =0
virtual void read(void *buffer, size_t num)=0
virtual void write(const void *buffer, size_t num)=0
virtual const byte * mmap(size_t &size)
Definition: FileBase.cc:10
virtual void munmap()
Definition: FileBase.cc:21
virtual size_t getSize()=0
void clear()
Free the allocated memory block and set the current size to 0.
Definition: MemBuffer.hh:134
const T * data() const
Returns pointer to the start of the memory buffer.
Definition: MemBuffer.hh:90
virtual const std::string getLocalReference()
Definition: FileBase.cc:46
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
size_t size() const
virtual void truncate(size_t size)
Definition: FileBase.cc:26
std::string str() const
Definition: string_view.cc:12
bool empty() const
No memory allocated?
Definition: MemBuffer.hh:112
virtual void seek(size_t pos)=0