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 namespace openmsx {
7 
9 {
10  auto size = getSize();
11  if (mmapBuf.empty()) {
12  auto pos = getPos();
13  seek(0);
14 
15  MemBuffer<uint8_t> tmpBuf(size);
16  read(tmpBuf.data(), size);
17  std::swap(mmapBuf, tmpBuf);
18 
19  seek(pos);
20  }
21  return {mmapBuf.data(), size};
22 }
23 
25 {
26  mmapBuf.clear();
27 }
28 
29 void FileBase::truncate(size_t newSize)
30 {
31  auto oldSize = getSize();
32  if (newSize < oldSize) {
33  // default truncate() can't shrink file
34  return;
35  }
36  auto remaining = newSize - oldSize;
37  seek(oldSize);
38 
39  constexpr size_t BUF_SIZE = 4096;
40  uint8_t buf[BUF_SIZE];
41  memset(buf, 0, sizeof(buf));
42  while (remaining) {
43  auto chunkSize = std::min(BUF_SIZE, remaining);
44  write(buf, chunkSize);
45  remaining -= chunkSize;
46  }
47 }
48 
50 {
51  // default implementation, file is not backed (uncompressed) on
52  // the local file system
53  return {};
54 }
55 
56 std::string_view FileBase::getOriginalName()
57 {
58  // default implementation just returns filename portion of URL
60 }
61 
62 } // namespace openmsx
virtual size_t getSize()=0
virtual void read(void *buffer, size_t num)=0
virtual void write(const void *buffer, size_t num)=0
virtual std::string_view getOriginalName()
Definition: FileBase.cc:56
virtual std::string getLocalReference()
Definition: FileBase.cc:49
virtual void seek(size_t pos)=0
virtual const std::string & getURL() const =0
virtual size_t getPos()=0
virtual void truncate(size_t size)
Definition: FileBase.cc:29
virtual span< const uint8_t > mmap()
Definition: FileBase.cc:8
virtual void munmap()
Definition: FileBase.cc:24
const T * data() const
Returns pointer to the start of the memory buffer.
Definition: MemBuffer.hh:81
bool empty() const
No memory allocated?
Definition: MemBuffer.hh:103
void clear()
Free the allocated memory block and set the current size to 0.
Definition: MemBuffer.hh:125
constexpr vecN< N, T > min(const vecN< N, T > &x, const vecN< N, T > &y)
Definition: gl_vec.hh:269
string_view getFilename(string_view path)
Returns the file portion of a path name.
This file implemented 3 utility functions:
Definition: Autofire.cc:9
constexpr int BUF_SIZE
size_t size(std::string_view utf8)