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_view filename, OpenMode mode = NORMAL);
40  explicit File(const Filename& filename, OpenMode mode = NORMAL);
41 
49  File(std::string_view filename, const char* mode);
50  File(const Filename& filename, const char* mode);
51  File(File&& other) noexcept;
52 
53  /* Used by MemoryBufferFile. */
54  File(std::unique_ptr<FileBase> file_);
55 
56  ~File();
57 
58  File& operator=(File&& other) noexcept;
59 
61  bool is_open() const { return file != nullptr; }
62 
66  void close();
67 
73  void read(void* buffer, size_t num);
74 
80  void write(const void* buffer, size_t num);
81 
87 
90  void munmap();
91 
96  size_t getSize();
97 
102  void seek(size_t pos);
103 
108  size_t getPos();
109 
114  void truncate(size_t size);
115 
119  void flush();
120 
124  std::string getURL() const;
125 
132  std::string getOriginalName();
133 
138  bool isReadOnly() const;
139 
143  time_t getModificationDate();
144 
145 private:
146  friend class LocalFileReference;
151  std::string getLocalReference() const;
152 
153  std::unique_ptr<FileBase> file;
154 };
155 
156 } // namespace openmsx
157 
158 #endif
bool isReadOnly() const
Check if this file is readonly.
Definition: File.cc:144
std::string getOriginalName()
Get Original filename for this object.
Definition: File.cc:138
File()
Create a closed file handle.
size_t getSize()
Returns the size of this file.
Definition: File.cc:103
Definition: span.hh:34
size_t size(std::string_view utf8)
std::string getURL() const
Returns the URL of this file object.
Definition: File.cc:128
File & operator=(File &&other) noexcept
Definition: File.cc:72
constexpr const char *const filename
bool is_open() const
Return true iff this file handle refers to an open file.
Definition: File.hh:61
void flush()
Force a write of all buffered data to disk.
Definition: File.cc:123
time_t getModificationDate()
Get the date/time of last modification.
Definition: File.cc:149
This class represents a filename.
Definition: Filename.hh:17
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
void munmap()
Unmap file from memory.
Definition: File.cc:98
size_t getPos()
Get the current position of the read/write pointer.
Definition: File.cc:113
void write(const void *buffer, size_t num)
Write to file.
Definition: File.cc:88
void close()
Close the current file.
Definition: File.cc:78
void seek(size_t pos)
Move read/write pointer to the specified position.
Definition: File.cc:108
void read(void *buffer, size_t num)
Read from file.
Definition: File.cc:83
span< uint8_t > mmap()
Map file in memory.
Definition: File.cc:93
Helper class to use files in APIs other than openmsx::File.
void truncate(size_t size)
Truncate file size.
Definition: File.cc:118