openMSX
File.cc
Go to the documentation of this file.
1 #include "File.hh"
2 #include "Filename.hh"
3 #include "LocalFile.hh"
4 #include "GZFileAdapter.hh"
5 #include "ZipFileAdapter.hh"
6 #include "checked_cast.hh"
7 #include <cstring>
8 #include <memory>
9 
10 using std::string;
11 
12 namespace openmsx {
13 
14 File::File() = default;
15 
16 static std::unique_ptr<FileBase> init(string_view filename, File::OpenMode mode)
17 {
18  static const uint8_t GZ_HEADER[3] = { 0x1F, 0x8B, 0x08 };
19  static const uint8_t ZIP_HEADER[4] = { 0x50, 0x4B, 0x03, 0x04 };
20 
21  std::unique_ptr<FileBase> file = std::make_unique<LocalFile>(filename, mode);
22  if (file->getSize() >= 4) {
23  uint8_t buf[4];
24  file->read(buf, 4);
25  file->seek(0);
26  if (memcmp(buf, GZ_HEADER, 3) == 0) {
27  file = std::make_unique<GZFileAdapter>(std::move(file));
28  } else if (memcmp(buf, ZIP_HEADER, 4) == 0) {
29  file = std::make_unique<ZipFileAdapter>(std::move(file));
30  } else {
31  // only pre-cache non-compressed files
32  if (mode == File::PRE_CACHE) {
33  checked_cast<LocalFile*>(file.get())->preCacheFile();
34  }
35  }
36  }
37  return file;
38 }
39 
41  : file(init(filename, mode))
42 {
43 }
44 
45 File::File(const Filename& filename, OpenMode mode)
46  : File(filename.getResolved(), mode)
47 {
48 }
49 
50 File::File(string_view filename, const char* mode)
51  : file(std::make_unique<LocalFile>(filename, mode))
52 {
53 }
54 
55 File::File(const Filename& filename, const char* mode)
56  : File(filename.getResolved(), mode)
57 {
58 }
59 
60 File::File(File&& other) noexcept
61  : file(std::move(other.file))
62 {
63 }
64 
65 File::File(std::unique_ptr<FileBase> file_)
66  : file(std::move(file_))
67 {
68 }
69 
70 File::~File() = default;
71 
72 File& File::operator=(File&& other) noexcept
73 {
74  file = std::move(other.file);
75  return *this;
76 }
77 
79 {
80  file.reset();
81 }
82 
83 void File::read(void* buffer, size_t num)
84 {
85  file->read(buffer, num);
86 }
87 
88 void File::write(const void* buffer, size_t num)
89 {
90  file->write(buffer, num);
91 }
92 
94 {
95  return file->mmap();
96 }
97 
99 {
100  file->munmap();
101 }
102 
104 {
105  return file->getSize();
106 }
107 
108 void File::seek(size_t pos)
109 {
110  file->seek(pos);
111 }
112 
113 size_t File::getPos()
114 {
115  return file->getPos();
116 }
117 
118 void File::truncate(size_t size)
119 {
120  return file->truncate(size);
121 }
122 
124 {
125  file->flush();
126 }
127 
128 string File::getURL() const
129 {
130  return file->getURL();
131 }
132 
133 string File::getLocalReference() const
134 {
135  return file->getLocalReference();
136 }
137 
139 {
140  string orig = file->getOriginalName();
141  return !orig.empty() ? orig : getURL();
142 }
143 
144 bool File::isReadOnly() const
145 {
146  return file->isReadOnly();
147 }
148 
150 {
151  return file->getModificationDate();
152 }
153 
154 } // namespace openmsx
bool isReadOnly() const
Check if this file is readonly.
Definition: File.cc:144
std::string getOriginalName()
Get Original filename for this object.
Definition: File.cc:138
File()
Create a closed file handle.
size_t getSize()
Returns the size of this file.
Definition: File.cc:103
Definition: span.hh:34
STL namespace.
std::string getURL() const
Returns the URL of this file object.
Definition: File.cc:128
virtual void read(void *buffer, size_t num)=0
File & operator=(File &&other) noexcept
Definition: File.cc:72
virtual size_t getSize()=0
void flush()
Force a write of all buffered data to disk.
Definition: File.cc:123
time_t getModificationDate()
Get the date/time of last modification.
Definition: File.cc:149
This class represents a filename.
Definition: Filename.hh:17
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
void munmap()
Unmap file from memory.
Definition: File.cc:98
size_t getPos()
Get the current position of the read/write pointer.
Definition: File.cc:113
This class implements a (close approximation) of the std::string_view class.
Definition: string_view.hh:16
void write(const void *buffer, size_t num)
Write to file.
Definition: File.cc:88
void close()
Close the current file.
Definition: File.cc:78
void seek(size_t pos)
Move read/write pointer to the specified position.
Definition: File.cc:108
void read(void *buffer, size_t num)
Read from file.
Definition: File.cc:83
span< uint8_t > mmap()
Map file in memory.
Definition: File.cc:93
constexpr auto size(const C &c) -> decltype(c.size())
Definition: span.hh:62
virtual void seek(size_t pos)=0
void truncate(size_t size)
Truncate file size.
Definition: File.cc:118