libdonut  2.3.2
Application framework for cross-platform game development in C++20
File.hpp
Go to the documentation of this file.
1 #ifndef DONUT_FILE_HPP
2 #define DONUT_FILE_HPP
3 
4 #include <donut/UniqueHandle.hpp>
5 
6 #include <cstddef> // std::size_t, std::ptrdiff_t, std::byte
7 #include <cstdint> // std::int64_t, std::uint8_t
8 #include <span> // std::span
9 #include <stdexcept> // std::runtime_error
10 #include <string> // std::string
11 #include <vector> // std::vector
12 
13 namespace donut {
14 
15 class Filesystem; // Forward declaration, to avoid a circular include of Filesystem.hpp.
16 
25 class File {
26 public:
31  struct Error : std::runtime_error {
32  explicit Error(const std::string& message)
33  : std::runtime_error(message) {}
34 
35  explicit Error(const char* message)
36  : std::runtime_error(message) {}
37  };
38 
42  enum class Kind : std::uint8_t {
43  REGULAR,
44  DIRECTORY,
45  SYMLINK,
46  OTHER,
47  };
48 
52  struct Metadata {
53  std::size_t size;
54  std::int64_t creationTime;
55  std::int64_t lastAccessTime;
56  std::int64_t lastModificationTime;
58  bool readOnly;
59  };
60 
64  static constexpr std::size_t NPOS = static_cast<std::size_t>(-1);
65 
69  constexpr File() noexcept = default;
70 
76  constexpr explicit operator bool() const noexcept {
77  return isOpen();
78  }
79 
93  void close();
94 
100  [[nodiscard]] constexpr bool isOpen() const noexcept {
101  return static_cast<bool>(file);
102  }
103 
109  [[nodiscard]] bool eof() const noexcept;
110 
117  [[nodiscard]] std::size_t size() const noexcept;
118 
125  [[nodiscard]] std::size_t tellg() const noexcept;
126 
133  [[nodiscard]] std::size_t tellp() const noexcept;
134 
146  void seekg(std::size_t position);
147 
159  void seekp(std::size_t position);
160 
172  void skipg(std::ptrdiff_t offset);
173 
185  void skipp(std::ptrdiff_t offset);
186 
196  [[nodiscard]] std::vector<std::byte> readAll() &&;
197 
206  [[nodiscard]] std::string readAllIntoString() &&;
207 
228  [[nodiscard]] std::size_t read(std::span<std::byte> data);
229 
245  std::size_t write(std::span<const std::byte> data);
246 
253  void flush();
254 
255 private:
256  friend Filesystem;
257 
258  constexpr explicit File(void* handle) noexcept
259  : file(handle) {}
260 
261  struct FileDeleter {
262  void operator()(void* handle) const noexcept;
263  };
264 
265  UniqueHandle<void*, FileDeleter> file{};
266 };
267 
268 } // namespace donut
269 
270 #endif
Unique handle to a file in the virtual Filesystem.
Definition: File.hpp:25
std::size_t tellp() const noexcept
Get the current writing position of the file.
std::size_t write(std::span< const std::byte > data)
Write data to the end of an open file from a buffer.
std::size_t read(std::span< std::byte > data)
Read data from an open file into a buffer, starting at the current reading position.
std::vector< std::byte > readAll() &&
Read the full contents of an open file into an array of bytes.
bool eof() const noexcept
Check if the end of the file has been reached.
std::size_t tellg() const noexcept
Get the current reading position of the file.
constexpr File() noexcept=default
Construct a closed virtual file handle without an associated file.
static constexpr std::size_t NPOS
Invalid value for a file offset, used as an end-of-file marker.
Definition: File.hpp:64
std::size_t size() const noexcept
Get the readable length of the full file contents, in bytes.
void seekg(std::size_t position)
Set the file reading position to an absolute offset from the beginning of the file.
void skipg(std::ptrdiff_t offset)
Advance the file reading position by a relative offset, which may be negative in order to go backward...
void seekp(std::size_t position)
Set the file writing position to an absolute offset from the beginning of the file.
std::string readAllIntoString() &&
Read the full contents of an open file into a string of bytes.
void flush()
Synchronize with the underlying file to make sure that all buffered data that has been written so far...
void close()
Close the associated file so that it can no longer be accessed through this handle,...
Kind
Virtual file entry type.
Definition: File.hpp:42
@ OTHER
Something else, such as a network socket or a device.
@ SYMLINK
Symbolic link.
@ DIRECTORY
Directory/folder.
@ REGULAR
Regular file.
void skipp(std::ptrdiff_t offset)
Advance the file writing position by a relative offset, which may be negative in order to go backward...
constexpr bool isOpen() const noexcept
Check if the file handle has an open file associated with it.
Definition: File.hpp:100
Persistent system for managing the virtual filesystem.
Definition: Filesystem.hpp:185
Definition: Application.hpp:9
Exception type for errors that may arise when attempting to access files through the virtual File API...
Definition: File.hpp:31
Error(const char *message)
Definition: File.hpp:35
Error(const std::string &message)
Definition: File.hpp:32
Record of metadata for a specific virtual file.
Definition: File.hpp:52
Kind kind
Kind of file, such as regular file or directory.
Definition: File.hpp:57
std::int64_t lastModificationTime
Last time when the file was modified, in seconds since the Unix epoch (1970-01-01 00:00),...
Definition: File.hpp:56
std::int64_t lastAccessTime
Last time when the file was accessed, in seconds since the Unix epoch (1970-01-01 00:00),...
Definition: File.hpp:55
bool readOnly
True if the file may only be opened for reading, false if it may also be opened for writing.
Definition: File.hpp:58
std::int64_t creationTime
Time when the file was created, in seconds since the Unix epoch (1970-01-01 00:00),...
Definition: File.hpp:54
std::size_t size
File size, in bytes, or NPOS if unavailable.
Definition: File.hpp:53