fennec
Loading...
Searching...
No Matches
file.h
1// =====================================================================================================================
2// fennec, a free and open source game engine
3// Copyright © 2025 Medusa Slockbower
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <https://www.gnu.org/licenses/>.
17// =====================================================================================================================
18
19#ifndef FENNEC_FILESYSTEM_FILE_H
20#define FENNEC_FILESYSTEM_FILE_H
21
22#include <fennec/filesystem/path.h>
24
25#include <fennec/string/cstring.h>
26#include <fennec/string/string.h>
27#include <fennec/string/wstring.h>
28
29namespace fennec
30{
31
57enum fmode_ : uint8_t
58{
59 fmode_read = 0b00000001
60, fmode_write = 0b00000010
61, fmode_trunc = 0b00000100
62, fmode_exclusive = 0b00001000
63, fmode_binary = 0b00010000
64, fmode_wide = 0b00100000
65};
66
74class file
75{
76public:
78 static constexpr size_t npos = -1;
79
84 static constexpr bool is_valid(uint8_t mode) {
85 const bool t = mode & fmode_trunc;
86 const bool x = mode & fmode_exclusive;
87 const bool w = mode & fmode_write;
88
89 // when x is true, t must be true
90 // when t is true, w must be true
91 return (t && x && w)
92 || (t && w)
93 || !(t || x);
94 }
95
98 static file& cout();
99
102 static file& cin();
103
106 static file& cerr();
107
110 file();
111
112 file(const cstring& path, uint8_t mode)
113 : file() {
114 open(path, mode);
115 }
116
117 file(const string& path, uint8_t mode)
118 : file() {
119 open(path, mode);
120 }
121
122 file(const path& path, uint8_t mode)
123 : file() {
124 open(path, mode);
125 }
126
129 ~file();
130
134 file(file&& file) noexcept;
135
136 file& operator=(file&& file) noexcept;
137
138 // don't allow copying streams
139 file(const file&) = delete;
140
141
142// Properties ==========================================================================================================
143
146 const path& get_path() const {
147 return _path;
148 }
149
152 uint8_t mode() const {
153 return _mode;
154 }
155
158 bool is_open() const {
159 return _handle != nullptr;
160 }
161
162
163// File Access =========================================================================================================
164
170 bool open(const cstring& path, uint8_t mode);
171
177 bool open(const string& path, uint8_t mode);
178
184 bool open(const path& path, uint8_t mode);
185
189 bool close();
190
194 bool commit();
195
196
197// File Operations =====================================================================================================
198
202 bool erase();
203
214 bool rename(const cstring& path);
215
226 bool rename(const string& path);
227
238 bool rename(const path& path);
239
244 file copy(const cstring& path);
245
250 file copy(const string& path);
251
256 file copy(const path& path);
257
258
259// File Positioning ====================================================================================================
260
261 size_t get_pos() const;
262 bool set_pos(size_t i);
263 bool rewind();
264 bool eof() const;
265
266
267// Binary Read Operations ==============================================================================================
268
269 size_t read(void* data, size_t size, size_t n);
270
271 template<typename T>
272 size_t read(T* data, size_t n) {
273 return read(static_cast<void*>(data), sizeof(T), n);
274 }
275
276 template<typename T, size_t n>
277 size_t read(T (&data)[n]) {
278 return read(static_cast<void*>(data), sizeof(T), n);
279 }
280
281
282// Binary Write Operations =============================================================================================
283
284 bool putc(char c);
285 bool putwc(wchar_t c);
286
287 size_t write(const void* data, size_t size, size_t n);
288
289 template<size_t n>
290 size_t write(const char (&data)[n]) {
291 return write(data, sizeof(char), n - 1);
292 }
293
294 template<size_t n>
295 size_t write(const wchar_t (&data)[n]) {
296 return write(data, sizeof(wchar_t), n - 1);
297 }
298
299 template<typename T>
300 size_t write(const T* data, size_t n) {
301 return write(static_cast<const void*>(data), sizeof(T), n);
302 }
303
304 template<typename T, size_t n>
305 size_t write(const T (&data)[n]) {
306 return write(static_cast<const void*>(data), sizeof(T), n);
307 }
308
309
310// Read Operations =====================================================================================================
311
312 char getc();
313 wchar_t getwc();
314
315 string getline();
316 wstring getwline();
317
318
319// Printing Operations =================================================================================================
320
321 void print(const cstring& str);
322 void print(const string& str);
323
324 void println(const cstring& str);
325 void println(const string& str);
326
327 template<typename...ArgsT>
328 void printf(const cstring& str, ArgsT&&...args) {
329 string fmt = fennec::format(str, fennec::forward<ArgsT>(args)...);
330 this->print(cstring(fmt.cstr(), fmt.length()));
331 }
332
333
334
335
336// Error Handling ======================================================================================================
337
338 const char* get_error() const { return _error; }
339 void clear_error() { _error = nullptr; }
340
341private:
342 FILE* _handle;
343 path _path;
344 uint8_t _mode;
345 char* _error;
346};
347
348}
349
350#endif // FENNEC_FILESYSTEM_FILE_H
Structure for handling streams of data.
Definition file.h:75
bool rename(const cstring &path)
rebinds the stream, copying contents to path, and erasing the old file
Definition file.cpp:324
static file & cout()
Definition file.cpp:78
file()
default constructor, initializes an empty stream
Definition file.cpp:114
bool open(const cstring &path, uint8_t mode)
open a file
Definition file.cpp:150
bool close()
close a stream
Definition file.cpp:264
uint8_t mode() const
Definition file.h:152
bool commit()
commit the streams buffer to the file
Definition file.cpp:287
static file & cerr()
Definition file.cpp:102
static constexpr bool is_valid(uint8_t mode)
Check if the provided mode bitflags are a valid combination.
Definition file.h:84
bool erase()
closes the stream and erases the file
Definition file.cpp:304
static file & cin()
Definition file.cpp:90
const path & get_path() const
Definition file.h:146
static constexpr size_t npos
value of an invalid position
Definition file.h:78
~file()
default destructor, cleans up an open stream
Definition file.cpp:122
bool is_open() const
Definition file.h:158
file copy(const cstring &path)
copies the contents of this file to path.
Definition file.cpp:597
This struct wraps c-style strings.
Definition cstring.h:64
struct for handling file paths
Definition path.h:34
::uint8_t uint8_t
Unsigned 8-bit integer.
Definition types.h:272