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 [[nodiscard]] static std::unique_ptr<FileBase> init(std::string filename, File::OpenMode mode)
17 {
18  static constexpr uint8_t GZ_HEADER[3] = { 0x1F, 0x8B, 0x08 };
19  static constexpr uint8_t ZIP_HEADER[4] = { 0x50, 0x4B, 0x03, 0x04 };
20 
21  std::unique_ptr<FileBase> file = std::make_unique<LocalFile>(std::move(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 
40 File::File(std::string filename, OpenMode mode)
41  : file(init(std::move(filename), mode))
42 {
43 }
44 
46  : File(filename.getResolved(), mode)
47 {
48 }
49 
51  : File(std::move(filename).getResolved(), mode)
52 {
53 }
54 
55 File::File(std::string filename, const char* mode)
56  : file(std::make_unique<LocalFile>(std::move(filename), mode))
57 {
58 }
59 
60 File::File(const Filename& filename, const char* mode)
61  : File(filename.getResolved(), mode)
62 {
63 }
64 
65 File::File(Filename&& filename, const char* mode)
66  : File(std::move(filename).getResolved(), mode)
67 {
68 }
69 
70 File::File(File&& other) noexcept
71  : file(std::move(other.file))
72 {
73 }
74 
75 File::File(std::unique_ptr<FileBase> file_)
76  : file(std::move(file_))
77 {
78 }
79 
80 File::~File() = default;
81 
82 File& File::operator=(File&& other) noexcept
83 {
84  file = std::move(other.file);
85  return *this;
86 }
87 
89 {
90  file.reset();
91 }
92 
93 void File::read(void* buffer, size_t num)
94 {
95  file->read(buffer, num);
96 }
97 
98 void File::write(const void* buffer, size_t num)
99 {
100  file->write(buffer, num);
101 }
102 
104 {
105  return file->mmap();
106 }
107 
109 {
110  file->munmap();
111 }
112 
114 {
115  return file->getSize();
116 }
117 
118 void File::seek(size_t pos)
119 {
120  file->seek(pos);
121 }
122 
123 size_t File::getPos()
124 {
125  return file->getPos();
126 }
127 
128 void File::truncate(size_t size)
129 {
130  return file->truncate(size);
131 }
132 
134 {
135  file->flush();
136 }
137 
138 const string& File::getURL() const
139 {
140  return file->getURL();
141 }
142 
143 string File::getLocalReference() const
144 {
145  return file->getLocalReference();
146 }
147 
148 std::string_view File::getOriginalName()
149 {
150  std::string_view orig = file->getOriginalName();
151  return !orig.empty() ? orig : getURL();
152 }
153 
154 bool File::isReadOnly() const
155 {
156  return file->isReadOnly();
157 }
158 
160 {
161  return file->getModificationDate();
162 }
163 
164 } // namespace openmsx
openmsx::File::getURL
const std::string & getURL() const
Returns the URL of this file object.
Definition: File.cc:138
openmsx::File::mmap
span< uint8_t > mmap()
Map file in memory.
Definition: File.cc:103
openmsx::File::getModificationDate
time_t getModificationDate()
Get the date/time of last modification.
Definition: File.cc:159
openmsx::File::operator=
File & operator=(File &&other) noexcept
Definition: File.cc:82
openmsx::File::getSize
size_t getSize()
Returns the size of this file.
Definition: File.cc:113
openmsx::FileBase::seek
virtual void seek(size_t pos)=0
openmsx::File::close
void close()
Close the current file.
Definition: File.cc:88
utf8::unchecked::size
size_t size(std::string_view utf8)
Definition: utf8_unchecked.hh:233
openmsx::File::getOriginalName
std::string_view getOriginalName()
Get Original filename for this object.
Definition: File.cc:148
openmsx::FileBase::getSize
virtual size_t getSize()=0
openmsx::File::~File
~File()
openmsx::File::getPos
size_t getPos()
Get the current position of the read/write pointer.
Definition: File.cc:123
Filename.hh
span
Definition: span.hh:126
ZipFileAdapter.hh
File.hh
openmsx::File::truncate
void truncate(size_t size)
Truncate file size.
Definition: File.cc:128
openmsx::LocalFile
Definition: LocalFile.hh:19
openmsx::File::write
void write(const void *buffer, size_t num)
Write to file.
Definition: File.cc:98
openmsx::File::File
File()
Create a closed file handle.
openmsx::filename
constexpr const char *const filename
Definition: FirmwareSwitch.cc:10
openmsx::File::flush
void flush()
Force a write of all buffered data to disk.
Definition: File.cc:133
LocalFile.hh
checked_cast.hh
openmsx::File::seek
void seek(size_t pos)
Move read/write pointer to the specified position.
Definition: File.cc:118
openmsx::File
Definition: File.hh:16
openmsx::File::read
void read(void *buffer, size_t num)
Read from file.
Definition: File.cc:93
openmsx::File::OpenMode
OpenMode
Definition: File.hh:18
openmsx::Filename
This class represents a filename.
Definition: Filename.hh:18
openmsx::FileBase::read
virtual void read(void *buffer, size_t num)=0
openmsx::File::munmap
void munmap()
Unmap file from memory.
Definition: File.cc:108
openmsx
This file implemented 3 utility functions:
Definition: Autofire.cc:5
openmsx::File::PRE_CACHE
@ PRE_CACHE
Definition: File.hh:24
openmsx::File::isReadOnly
bool isReadOnly() const
Check if this file is readonly.
Definition: File.cc:154
GZFileAdapter.hh