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_view>
10
11namespace openmsx {
12
13class Filename;
14class FileBase;
15
16class File
17{
18public:
19 enum class OpenMode {
20 NORMAL,
22 CREATE,
26 };
27
33
40 explicit File(std::string filename, OpenMode mode = OpenMode::NORMAL);
41 explicit File(const Filename& filename, OpenMode mode = OpenMode::NORMAL);
42 explicit File(Filename&& filename, OpenMode mode = OpenMode::NORMAL);
43
51 File(std::string filename, const char* mode);
52 File(const Filename& filename, const char* mode);
53 File(Filename&& filename, const char* mode);
54 File(File&& other) noexcept;
55
56 /* Used by MemoryBufferFile. */
57 explicit File(std::unique_ptr<FileBase> file_);
58
60
61 File& operator=(File&& other) noexcept;
62
64 [[nodiscard]] bool is_open() const { return file != nullptr; }
65
69 void close();
70
75 void read(std::span<uint8_t> buffer);
76
77 template<typename T>
78 void read(std::span<T> buffer) {
79 read(std::span<uint8_t>{std::bit_cast<uint8_t*>(buffer.data()), buffer.size_bytes()});
80 }
81
86 void write(std::span<const uint8_t> buffer);
87
88 template<typename T>
89 void write(std::span<T> buffer) {
90 write(std::span<const uint8_t>{std::bit_cast<const uint8_t*>(buffer.data()), buffer.size_bytes()});
91 }
92
97 [[nodiscard]] std::span<const uint8_t> mmap();
98
101 void munmap();
102
107 [[nodiscard]] size_t getSize();
108
113 void seek(size_t pos);
114
119 [[nodiscard]] size_t getPos();
120
125 void truncate(size_t size);
126
130 void flush();
131
135 [[nodiscard]] const std::string& getURL() const;
136
143 [[nodiscard]] std::string_view getOriginalName();
144
149 [[nodiscard]] bool isReadOnly() const;
150
154 [[nodiscard]] time_t getModificationDate();
155
156private:
157 friend class LocalFileReference;
162 [[nodiscard]] std::string getLocalReference() const;
163
164 std::unique_ptr<FileBase> file;
165};
166
167} // namespace openmsx
168
169#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:78
void truncate(size_t size)
Truncate file size.
Definition File.cc:127
void write(std::span< T > buffer)
Definition File.hh:89
bool is_open() const
Return true iff this file handle refers to an open file.
Definition File.hh:64
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