openMSX
File.hh
Go to the documentation of this file.
1#ifndef FILE_HH
2#define FILE_HH
3
4#include <cstdint>
5#include <ctime>
6#include <memory>
7#include <span>
8#include <string_view>
9
10namespace openmsx {
11
12class Filename;
13class FileBase;
14
15class File
16{
17public:
26
32
39 explicit File(std::string filename, OpenMode mode = NORMAL);
40 explicit File(const Filename& filename, OpenMode mode = NORMAL);
41 explicit File(Filename&& filename, OpenMode mode = NORMAL);
42
50 File(std::string filename, const char* mode);
51 File(const Filename& filename, const char* mode);
52 File(Filename&& filename, const char* mode);
53 File(File&& other) noexcept;
54
55 /* Used by MemoryBufferFile. */
56 File(std::unique_ptr<FileBase> file_);
57
59
60 File& operator=(File&& other) noexcept;
61
63 [[nodiscard]] bool is_open() const { return file != nullptr; }
64
68 void close();
69
74 void read(std::span<uint8_t> buffer);
75
76 template<typename T>
77 void read(std::span<T> buffer) {
78 read(std::span<uint8_t>{reinterpret_cast<uint8_t*>(buffer.data()), buffer.size_bytes()});
79 }
80
85 void write(std::span<const uint8_t> buffer);
86
87 template<typename T>
88 void write(std::span<T> buffer) {
89 write(std::span<const uint8_t>{reinterpret_cast<const uint8_t*>(buffer.data()), buffer.size_bytes()});
90 }
91
96 [[nodiscard]] std::span<const uint8_t> mmap();
97
100 void munmap();
101
106 [[nodiscard]] size_t getSize();
107
112 void seek(size_t pos);
113
118 [[nodiscard]] size_t getPos();
119
124 void truncate(size_t size);
125
129 void flush();
130
134 [[nodiscard]] const std::string& getURL() const;
135
142 [[nodiscard]] std::string_view getOriginalName();
143
148 [[nodiscard]] bool isReadOnly() const;
149
153 [[nodiscard]] time_t getModificationDate();
154
155private:
156 friend class LocalFileReference;
161 [[nodiscard]] std::string getLocalReference() const;
162
163 std::unique_ptr<FileBase> file;
164};
165
166} // namespace openmsx
167
168#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:77
void truncate(size_t size)
Truncate file size.
Definition File.cc:127
void write(std::span< T > buffer)
Definition File.hh:88
bool is_open() const
Return true iff this file handle refers to an open file.
Definition File.hh:63
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
@ SAVE_PERSISTENT
Definition File.hh:23
@ LOAD_PERSISTENT
Definition File.hh:22
void flush()
Force a write of all buffered data to disk.
Definition File.cc:132
This class represents a filename.
Definition Filename.hh:18
Helper class to use files in APIs other than openmsx::File.
This file implemented 3 utility functions:
Definition Autofire.cc:9