GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
NativeInputFile.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_NATIVE_INPUT_FILE_HPP
5#define GREM_CORE_SYSTEM_NATIVE_INPUT_FILE_HPP
6
8
19
20#include <fstream> // std::ifstream, std::streamoff, std::streamsize
21#include <utility> // std::move, std::exchange
22
23namespace grem {
24
25class NativeFilesystem; // Forward declaration, to avoid a circular include of NativeFilesystem.hpp.
26
30class NativeInputFile final
31 : public InputFile
32 , public std::ifstream {
33public:
37 NativeInputFile() noexcept = default;
38
40 ~NativeInputFile() override = default;
41
44
46 NativeInputFile(NativeInputFile&& other) // NOLINT(cppcoreguidelines-noexcept-move-operations, performance-noexcept-move-constructor)
47 : InputFile(std::move(static_cast<InputFile&>(other)))
48 , std::ifstream(std::move(static_cast<std::ifstream&>(other)))
49 , fileSize(std::exchange(other.fileSize, size_t{0})) {}
50
53
55 NativeInputFile& operator=(NativeInputFile&&) = default; // NOLINT(cppcoreguidelines-noexcept-move-operations, performance-noexcept-move-constructor)
56
57 void close() override {
58 std::ifstream::close();
59 }
60
61 [[nodiscard]] bool isOpen() const noexcept override {
62 return std::ifstream::is_open();
63 }
64
65 [[nodiscard]] bool eof() const noexcept override {
66 return !isOpen() || std::ifstream::eof();
67 }
68
69 [[nodiscard]] size_t size() const noexcept override {
70 return fileSize;
71 }
72
73 [[nodiscard]] size_t tellg() const noexcept override {
74 return static_cast<size_t>(const_cast<std::ifstream*>(static_cast<const std::ifstream*>(this))->tellg());
75 }
76
77 void seekg(size_t position) override {
78 if (std::ifstream::seekg(static_cast<std::ifstream::pos_type>(static_cast<std::streamoff>(position)), std::ifstream::beg).fail()) {
79 throw File::Error{"Failed to seek in file."};
80 }
81 }
82
83 void skipg(ptrdiff_t offset) override {
84 if (std::ifstream::seekg(static_cast<std::ifstream::pos_type>(static_cast<std::streamoff>(offset)), std::ifstream::cur).fail()) {
85 throw File::Error{"Failed to seek in file."};
86 }
87 }
88
89 [[nodiscard]] Allocation<byte> readBytesIntoAllocation(size_t maxLength = Limits<size_t>::MAX) override {
90 return readInto<Allocation<byte>>(maxLength);
91 }
92
93 [[nodiscard]] ArrayList<byte> readBytesIntoArrayList(size_t maxLength = Limits<size_t>::MAX) override {
94 return readInto<ArrayList<byte>>(maxLength);
95 }
96
97 [[nodiscard]] Buffer<byte> readBytesIntoBuffer(size_t maxLength = Limits<size_t>::MAX) override {
98 return readInto<Buffer<byte>>(maxLength);
99 }
100
101 [[nodiscard]] String readBytesIntoString(size_t maxLength = Limits<size_t>::MAX) override {
102 return readInto<String>(maxLength);
103 }
104
105 [[nodiscard]] Allocation<char> readBytesIntoCString(size_t maxLength = Limits<size_t>::MAX) override {
106 Allocation<char> result{};
107 const size_t fileOffset = tellg();
108 if (fileOffset < fileSize) {
109 const size_t length = min(fileSize - fileOffset, maxLength);
110 result.resize(length + 1);
111 read(asWritableBytes(Span{result.data(), length}));
112 result[length] = '\0';
113 } else {
114 result.resize(1);
115 result[0] = '\0';
116 }
117 return result;
118 }
119
120 [[nodiscard]] size_t readSome(Span<byte> data) override {
122 }
123
124 [[nodiscard]] size_t readUntilEOF(Span<byte> data) override {
125 const size_t size = min(data.size(), size_t{Limits<std::streamsize>::MAX});
126 if (std::ifstream::read(reinterpret_cast<char*>(data.data()), static_cast<std::streamsize>(size)).bad()) {
127 throw File::Error{"Failed to read from file."};
128 }
129 const std::streamsize bytesRead = std::ifstream::gcount();
130 if (bytesRead <= 0) {
131 if (bytesRead < 0 || !std::ifstream::eof()) {
132 throw File::Error{"Failed to read from file."};
133 }
134 return 0;
135 }
136 return static_cast<size_t>(bytesRead);
137 }
138
139 using InputFile::read;
140
141private:
142 friend NativeFilesystem;
143
144 NativeInputFile(std::ifstream&& stream, size_t fileSize) noexcept
145 : std::ifstream(std::move(stream))
146 , fileSize(fileSize) {}
147
148 template <typename Container>
149 [[nodiscard]] Container readInto(size_t maxLength) {
150 Container result{};
151 const size_t fileOffset = tellg();
152 if (fileOffset < fileSize) {
153 result.resize(min(fileSize - fileOffset, maxLength));
154 read(asWritableBytes(Span{result}));
155 }
156 return result;
157 }
158
159 size_t fileSize{};
160};
161
162} // namespace grem
163
164#endif
Definition Allocation.hpp:25
constexpr void resize(size_type count)
Definition Allocation.hpp:273
constexpr pointer data() noexcept
Definition Allocation.hpp:425
Definition ArrayList.hpp:27
Definition Buffer.hpp:28
void read(Span< byte > data)
Read an exact amount of data from an open file, starting at the current reading position.
Definition File.hpp:331
InputFile() noexcept=default
Default constructor.
Wrapper over the native host filesystem.
Definition NativeFilesystem.hpp:21
size_t readUntilEOF(Span< byte > data) override
Read data from an open file into a buffer, starting at the current reading position.
Definition NativeInputFile.hpp:124
size_t size() const noexcept override
Get the readable length of the full file contents, in bytes.
Definition NativeInputFile.hpp:69
void read(Span< byte > data)
Read an exact amount of data from an open file, starting at the current reading position.
Definition File.hpp:331
void skipg(ptrdiff_t offset) override
Advance the file reading position by a relative offset, which may be negative in order to go backward...
Definition NativeInputFile.hpp:83
NativeInputFile & operator=(NativeInputFile &&)=default
Move assignment.
NativeInputFile & operator=(const NativeInputFile &)=delete
Copying a file handle is not allowed.
Allocation< char > readBytesIntoCString(size_t maxLength=Limits< size_t >::MAX) override
Read from an open file into an allocated null-terminated string of bytes.
Definition NativeInputFile.hpp:105
Allocation< byte > readBytesIntoAllocation(size_t maxLength=Limits< size_t >::MAX) override
Read from an open file into an allocation of bytes.
Definition NativeInputFile.hpp:89
size_t tellg() const noexcept override
Get the current reading position of the file.
Definition NativeInputFile.hpp:73
ArrayList< byte > readBytesIntoArrayList(size_t maxLength=Limits< size_t >::MAX) override
Read from an open file into an array list of bytes.
Definition NativeInputFile.hpp:93
Buffer< byte > readBytesIntoBuffer(size_t maxLength=Limits< size_t >::MAX) override
Read from an open file into a buffer of bytes.
Definition NativeInputFile.hpp:97
void seekg(size_t position) override
Set the file reading position to an absolute offset from the beginning of the file.
Definition NativeInputFile.hpp:77
bool eof() const noexcept override
Check if the end of the file has been reached.
Definition NativeInputFile.hpp:65
String readBytesIntoString(size_t maxLength=Limits< size_t >::MAX) override
Read from an open file into a string of bytes.
Definition NativeInputFile.hpp:101
NativeInputFile() noexcept=default
Construct a closed file handle without an associated file.
void close() override
Close the associated file so that it can no longer be accessed through this handle,...
Definition NativeInputFile.hpp:57
bool isOpen() const noexcept override
Check if the file handle has an open file associated with it.
Definition NativeInputFile.hpp:61
size_t readSome(Span< byte > data) override
Read some amount of data from an open file, starting at the current reading position.
Definition NativeInputFile.hpp:120
Definition Span.hpp:146
constexpr size_type size() const noexcept
Definition Span.hpp:207
constexpr pointer data() const noexcept
Definition Span.hpp:203
Definition aliases.hpp:9
auto asWritableBytes(Span< T, N > s) noexcept
Definition Span.hpp:368
constexpr auto move(input_range auto &&range, auto outputIterator)
Definition algorithms.hpp:669
Span(R &&range) -> Span< std::remove_reference_t< decltype(*std::data(range))> >
StringBase< char, std::char_traits< char >, std::allocator< char > > String
Definition String.hpp:17
GREM_ALWAYS_INLINE constexpr decltype(auto) min(A &&a, B &&b)
Get the smaller of two values, or the first value if they are equivalent.
Definition fundamentals.hpp:585
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL length(T a)
Definition math.hpp:2001
Exception type for errors that may arise when attempting to access files through the File API.
Definition File.hpp:37
static constexpr T MAX
Definition fundamentals.hpp:29