GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
Filesystem.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_CORE_SYSTEM_FILESYSTEM_HPP
5#define GREM_CORE_SYSTEM_FILESYSTEM_HPP
6
8
17
18#include <type_traits> // std::is_void_v
19
20namespace grem {
21
26public:
28 constexpr Filesystem() noexcept = default;
29
31 virtual ~Filesystem() = default;
32
34 constexpr Filesystem(const Filesystem&) = default;
35
37 constexpr Filesystem(Filesystem&&) noexcept = default;
38
40 Filesystem& operator=(const Filesystem&) = default;
41
43 Filesystem& operator=(Filesystem&&) noexcept = default;
44
51 [[nodiscard]] virtual CStringView getOutputDirectory() const noexcept = 0;
52
64 [[nodiscard]] virtual CStringView findInputArchiveOfFile(CStringView filepath) const noexcept = 0;
65
77 virtual void createOutputDirectory(CStringView filepath) = 0;
78
90 virtual void createParentOutputDirectories(CStringView filepath) = 0;
91
113 virtual void deleteOutputFile(CStringView filepath) = 0;
114
125 [[nodiscard]] virtual bool inputFileExists(CStringView filepath) const = 0;
126
138 [[nodiscard]] virtual File::Metadata getFileMetadata(CStringView filepath) const = 0;
139
170 virtual void forEachInputFilenameInDirectory(CStringView filepath, FunctionView<bool(CStringView filename)> callback) const = 0; // NOLINT(modernize-use-nodiscard)
171
199 void forEachInputFilenameInDirectory(CStringView filepath, auto callback) const requires(std::is_void_v<decltype(callback(CStringView{}))>) {
200 forEachInputFilenameInDirectory(filepath, [&](CStringView filename) -> bool {
201 callback(filename);
202 return false;
203 });
204 }
205
221 [[nodiscard]] virtual InputFileHandle openInputFile(CStringView filepath) const = 0;
222
238 [[nodiscard]] virtual InputFileHandle tryOpenInputFile(CStringView filepath) const = 0;
239
256 [[nodiscard]] Allocation<byte> readInputFile(CStringView filepath) const {
257 return openInputFile(filepath).readBytesIntoAllocation();
258 }
259
276 if (InputFileHandle file = tryOpenInputFile(filepath)) {
277 return file.readBytesIntoAllocation();
278 }
279 return {};
280 }
281
298 [[nodiscard]] ArrayList<byte> readInputFileArrayList(CStringView filepath) const {
299 return openInputFile(filepath).readBytesIntoArrayList();
300 }
301
318 if (InputFileHandle file = tryOpenInputFile(filepath)) {
319 return file.readBytesIntoArrayList();
320 }
321 return {};
322 }
323
340 [[nodiscard]] Buffer<byte> readInputFileBuffer(CStringView filepath) const {
341 return openInputFile(filepath).readBytesIntoBuffer();
342 }
343
360 if (InputFileHandle file = tryOpenInputFile(filepath)) {
361 return file.readBytesIntoBuffer();
362 }
363 return {};
364 }
365
382 [[nodiscard]] String readInputFileString(CStringView filepath) const {
383 return openInputFile(filepath).readBytesIntoString();
384 }
385
402 if (InputFileHandle file = tryOpenInputFile(filepath)) {
403 return file.readBytesIntoString();
404 }
405 return {};
406 }
407
425 [[nodiscard]] Allocation<char> readInputFileCString(CStringView filepath) const {
426 return openInputFile(filepath).readBytesIntoCString();
427 }
428
445 if (InputFileHandle file = tryOpenInputFile(filepath)) {
446 return file.readBytesIntoCString();
447 }
448 return {};
449 }
450
476 [[nodiscard]] virtual OutputFileHandle openEmptyOutputFile(CStringView filepath) = 0;
477
498 virtual void createEmptyOutputFile(CStringView filepath) = 0;
499
524 [[nodiscard]] virtual OutputFileHandle openOutputFileForAppending(CStringView filepath) = 0;
525};
526
527} // namespace grem
528
529#endif
Definition Allocation.hpp:25
Definition ArrayList.hpp:27
Definition Buffer.hpp:28
Base interface for a file in a Filesystem.
Definition File.hpp:31
constexpr Filesystem() noexcept=default
Default constructor.
Allocation< char > tryReadInputFileCString(CStringView filepath) const
Try to open an input file in the virtual filesystem for reading and read its full contents into an al...
Definition Filesystem.hpp:444
virtual void deleteOutputFile(CStringView filepath)=0
Delete a host file or directory relative to the current output directory.
virtual void createOutputDirectory(CStringView filepath)=0
Create a new host directory relative to the current output directory, while also creating any missing...
virtual CStringView findInputArchiveOfFile(CStringView filepath) const noexcept=0
Get the host filepath of the mounted input archive on the host filesystem that contains a given virtu...
Allocation< char > readInputFileCString(CStringView filepath) const
Open an input file in the virtual filesystem for reading and read its full contents into an allocated...
Definition Filesystem.hpp:425
String readInputFileString(CStringView filepath) const
Open an input file in the virtual filesystem for reading and read its full contents into a string of ...
Definition Filesystem.hpp:382
Buffer< byte > readInputFileBuffer(CStringView filepath) const
Open an input file in the virtual filesystem for reading and read its full contents into a buffer of ...
Definition Filesystem.hpp:340
virtual InputFileHandle tryOpenInputFile(CStringView filepath) const =0
Try to open an input file in the virtual filesystem for reading.
virtual void forEachInputFilenameInDirectory(CStringView filepath, FunctionView< bool(CStringView filename)> callback) const =0
Iterate the filenames of all readable virtual filepaths that are direct children of a given directory...
virtual void createParentOutputDirectories(CStringView filepath)=0
Create any missing parent directories above a host directory relative to the current output directory...
virtual File::Metadata getFileMetadata(CStringView filepath) const =0
Get the metadata of a file that is mounted at a given virtual filepath.
ArrayList< byte > readInputFileArrayList(CStringView filepath) const
Open an input file in the virtual filesystem for reading and read its full contents into an array lis...
Definition Filesystem.hpp:298
virtual InputFileHandle openInputFile(CStringView filepath) const =0
Open an input file in the virtual filesystem for reading.
Optional< String > tryReadInputFileString(CStringView filepath) const
Try to open an input file in the virtual filesystem for reading and read its full contents into a str...
Definition Filesystem.hpp:401
Allocation< byte > readInputFile(CStringView filepath) const
Open an input file in the virtual filesystem for reading and read its full contents into an array of ...
Definition Filesystem.hpp:256
virtual void createEmptyOutputFile(CStringView filepath)=0
Create an empty file relative to the current output directory, overwriting any existing file at the s...
Optional< Allocation< byte > > tryReadInputFile(CStringView filepath) const
Try to open an input file in the virtual filesystem for reading and read its full contents into an ar...
Definition Filesystem.hpp:275
Optional< ArrayList< byte > > tryReadInputFileArrayList(CStringView filepath) const
Try to open an input file in the virtual filesystem for reading and read its full contents into an ar...
Definition Filesystem.hpp:317
Optional< Buffer< byte > > tryReadInputFileBuffer(CStringView filepath) const
Try to open an input file in the virtual filesystem for reading and read its full contents into a buf...
Definition Filesystem.hpp:359
virtual bool inputFileExists(CStringView filepath) const =0
Check if a given virtual filepath has a corresponding host file mounted.
virtual OutputFileHandle openEmptyOutputFile(CStringView filepath)=0
Create a file relative to the current output directory and open it for writing, overwriting any exist...
virtual OutputFileHandle openOutputFileForAppending(CStringView filepath)=0
Open a file relative to the current output directory for appended writing, or create an empty file an...
virtual CStringView getOutputDirectory() const noexcept=0
Get the current output directory of the filesystem.
Definition FunctionView.hpp:29
Generic boxed unique handle to a readable file in any filesystem.
Definition File.hpp:363
GREM_ALWAYS_INLINE Allocation< char > readBytesIntoCString(size_t maxLength=Limits< size_t >::MAX)
Read from an open file into an allocated null-terminated string of bytes.
Definition File.hpp:520
GREM_ALWAYS_INLINE String readBytesIntoString(size_t maxLength=Limits< size_t >::MAX)
Read from an open file into a string of bytes.
Definition File.hpp:512
GREM_ALWAYS_INLINE Allocation< byte > readBytesIntoAllocation(size_t maxLength=Limits< size_t >::MAX)
Read from an open file into an allocation of bytes.
Definition File.hpp:488
GREM_ALWAYS_INLINE Buffer< byte > readBytesIntoBuffer(size_t maxLength=Limits< size_t >::MAX)
Read from an open file into a buffer of bytes.
Definition File.hpp:504
GREM_ALWAYS_INLINE ArrayList< byte > readBytesIntoArrayList(size_t maxLength=Limits< size_t >::MAX)
Read from an open file into an array list of bytes.
Definition File.hpp:496
Optional value type that holds either a value or no value.
Definition Optional.hpp:58
Generic boxed unique handle to a writable file in any filesystem.
Definition File.hpp:686
Definition aliases.hpp:9
StringBase< char, std::char_traits< char >, std::allocator< char > > String
Definition String.hpp:17
CStringViewBase< char, std::char_traits< char > > CStringView
Definition CStringView.hpp:635