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