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