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:
18 enum OpenMode {
25 };
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
75 void read(void* buffer, size_t num);
76
82 void write(const void* buffer, size_t num);
83
88 [[nodiscard]] std::span<const uint8_t> mmap();
89
92 void munmap();
93
98 [[nodiscard]] size_t getSize();
99
104 void seek(size_t pos);
105
110 [[nodiscard]] size_t getPos();
111
116 void truncate(size_t size);
117
121 void flush();
122
126 [[nodiscard]] const std::string& getURL() const;
127
134 [[nodiscard]] std::string_view getOriginalName();
135
140 [[nodiscard]] bool isReadOnly() const;
141
145 [[nodiscard]] time_t getModificationDate();
146
147private:
148 friend class LocalFileReference;
153 [[nodiscard]] std::string getLocalReference() const;
154
155 std::unique_ptr<FileBase> file;
156};
157
158} // namespace openmsx
159
160#endif
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
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: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
@ SAVE_PERSISTENT
Definition: File.hh:23
@ LOAD_PERSISTENT
Definition: File.hh:22
@ TRUNCATE
Definition: File.hh:20
@ 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
Helper class to use files in APIs other than openmsx::File.
This file implemented 3 utility functions:
Definition: Autofire.cc:9
constexpr const char *const filename
size_t size(std::string_view utf8)