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