19#ifndef FENNEC_FILESYSTEM_FILE_H
20#define FENNEC_FILESYSTEM_FILE_H
22#include <fennec/filesystem/path.h>
25#include <fennec/string/cstring.h>
26#include <fennec/string/string.h>
27#include <fennec/string/wstring.h>
59 fmode_read = 0b00000001
60, fmode_write = 0b00000010
61, fmode_trunc = 0b00000100
62, fmode_exclusive = 0b00001000
63, fmode_binary = 0b00010000
64, fmode_wide = 0b00100000
78 static constexpr size_t npos = -1;
85 const bool t =
mode & fmode_trunc;
86 const bool x =
mode & fmode_exclusive;
87 const bool w =
mode & fmode_write;
117 file(
const string& path, uint8_t
mode)
122 file(
const path& path, uint8_t
mode)
159 return _handle !=
nullptr;
261 size_t get_pos()
const;
262 bool set_pos(
size_t i);
269 size_t read(
void* data,
size_t size,
size_t n);
272 size_t read(T* data,
size_t n) {
273 return read(
static_cast<void*
>(data),
sizeof(T), n);
276 template<
typename T,
size_t n>
277 size_t read(T (&data)[n]) {
278 return read(
static_cast<void*
>(data),
sizeof(T), n);
285 bool putwc(
wchar_t c);
287 size_t write(
const void* data,
size_t size,
size_t n);
290 size_t write(
const char (&data)[n]) {
291 return write(data,
sizeof(
char), n - 1);
295 size_t write(
const wchar_t (&data)[n]) {
296 return write(data,
sizeof(
wchar_t), n - 1);
300 size_t write(
const T* data,
size_t n) {
301 return write(
static_cast<const void*
>(data),
sizeof(T), n);
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);
321 void print(
const cstring& str);
322 void print(
const string& str);
324 void println(
const cstring& str);
325 void println(
const string& str);
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()));
338 const char* get_error()
const {
return _error; }
339 void clear_error() { _error =
nullptr; }
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