openMSX
File.hh
Go to the documentation of this file.
1#ifndef FILE_HH
2#define FILE_HH
3
4#include <bit>
5#include <cstdint>
6#include <ctime>
7#include <memory>
8#include <span>
9#include <string>
10#include <string_view>
11
12namespace openmsx {
13
14class Filename;
15class FileBase;
16
17class File
18{
19public:
20 enum class OpenMode {
21 NORMAL,
23 CREATE,
27 };
28
34
41 explicit File(std::string filename, OpenMode mode = OpenMode::NORMAL);
42 explicit File(const Filename& filename, OpenMode mode = OpenMode::NORMAL);
43 explicit File(Filename&& filename, OpenMode mode = OpenMode::NORMAL);
44
52 File(std::string filename, const char* mode);
53 File(const Filename& filename, const char* mode);
54 File(Filename&& filename, const char* mode);
55 File(File&& other) noexcept;
56
57 /* Used by MemoryBufferFile. */
58 explicit File(std::unique_ptr<FileBase> file_);
59
61
62 File& operator=(File&& other) noexcept;
63
65 [[nodiscard]] bool is_open() const { return file != nullptr; }
66
70 void close();
71
76 void read(std::span<uint8_t> buffer);
77
78 template<typename T>
79 void read(std::span<T> buffer) {
80 read(std::span<uint8_t>{std::bit_cast<uint8_t*>(buffer.data()), buffer.size_bytes()});
81 }
82
87 void write(std::span<const uint8_t> buffer);
88
89 template<typename T>
90 void write(std::span<T> buffer) {
91 write(std::span<const uint8_t>{std::bit_cast<const uint8_t*>(buffer.data()), buffer.size_bytes()});
92 }
93
98 [[nodiscard]] std::span<const uint8_t> mmap();
99
102 void munmap();
103
108 [[nodiscard]] size_t getSize();
109
114 void seek(size_t pos);
115
120 [[nodiscard]] size_t getPos();
121
126 void truncate(size_t size);
127
131 void flush();
132
136 [[nodiscard]] const std::string& getURL() const;
137
144 [[nodiscard]] std::string_view getOriginalName();
145
150 [[nodiscard]] bool isReadOnly() const;
151
155 [[nodiscard]] time_t getModificationDate();
156
157private:
158 friend class LocalFileReference;
163 [[nodiscard]] std::string getLocalReference() const;
164
165 std::unique_ptr<FileBase> file;
166};
167
168} // namespace openmsx
169
170#endif
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 read(std::span< T > buffer)
Definition File.hh:79
void truncate(size_t size)
Truncate file size.
Definition File.cc:127
void write(std::span< T > buffer)
Definition File.hh:90
bool is_open() const
Return true iff this file handle refers to an open file.
Definition File.hh:65
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
Helper class to use files in APIs other than openmsx::File.
This file implemented 3 utility functions:
Definition Autofire.cc:11