openMSX
File.hh
Go to the documentation of this file.
1 #ifndef FILE_HH
2 #define FILE_HH
3 
4 #include "span.hh"
5 #include <cstdint>
6 #include <ctime>
7 #include <memory>
8 #include <string_view>
9 
10 namespace openmsx {
11 
12 class Filename;
13 class FileBase;
14 
15 class File
16 {
17 public:
18  enum OpenMode {
25  };
26 
31  File();
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 
58  ~File();
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]] 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 
147 private:
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:88
span< const uint8_t > mmap()
Map file in memory.
Definition: File.cc:103
void seek(size_t pos)
Move read/write pointer to the specified position.
Definition: File.cc:118
bool isReadOnly() const
Check if this file is readonly.
Definition: File.cc:154
File()
Create a closed file handle.
File & operator=(File &&other) noexcept
Definition: File.cc:82
std::string_view getOriginalName()
Get Original filename for this object.
Definition: File.cc:148
void read(void *buffer, size_t num)
Read from file.
Definition: File.cc:93
time_t getModificationDate()
Get the date/time of last modification.
Definition: File.cc:159
void truncate(size_t size)
Truncate file size.
Definition: File.cc:128
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:113
const std::string & getURL() const
Returns the URL of this file object.
Definition: File.cc:138
size_t getPos()
Get the current position of the read/write pointer.
Definition: File.cc:123
void munmap()
Unmap file from memory.
Definition: File.cc:108
void write(const void *buffer, size_t num)
Write to file.
Definition: File.cc:98
@ 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:133
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:5
constexpr const char *const filename
size_t size(std::string_view utf8)