libhatchet
Loading...
Searching...
No Matches
hxfile.hpp
Go to the documentation of this file.
1#pragma once
2// SPDX-FileCopyrightText: © 2017-2026 Adrian Johnston.
3// SPDX-License-Identifier: MIT
4// This file is licensed under the MIT license found in the LICENSE.md file.
5
9
10#include "libhatchet.h"
11
12// HX_USE_MACROS_WITH_MODULE allows including macros alongside the module.
13#if HX_USE_MACROS_WITH_MODULE
14#error Header does not provide macros alone.
15#endif
16
17#if HX_USE_FILE_IO
18
19#if HX_CPLUSPLUS >= 202302L
20#include "hxexpected.hpp"
21#endif
22
23HX_NS_BEGIN_
24
25class hxfile;
26
28extern hxfile hxin;
29
31extern hxfile hxout;
32
34extern hxfile hxerr;
35
38extern hxfile hxdev_null;
39
43
70class hxfile {
71public:
88
90 hxfile(void);
91
99 hxfile(uint8_t mode_, const char* filename_, ...) hxattr_printf(3, 4);
100
106 hxfile(uint8_t mode_, intptr_t file_);
107
110 hxfile(const char* file_, uint8_t mode_=0) = delete;
111
113 hxfile(hxfile&& file_) noexcept;
114
118
120 void operator=(hxfile&& file_) noexcept;
121
124 operator bool(void) const;
125
132 bool open(uint8_t mode_, const char* filename_, ...) hxattr_printf(3, 4);
133
135 void close(void);
136
138 hxattr_nodiscard bool is_open(void) const;
139
141 hxattr_nodiscard hxinline bool fail(void) const { return m_fail_; }
142
145 void set_fail(void) { m_fail_ = true; }
146
148 hxattr_nodiscard hxinline bool eof(void) const { return m_eof_; }
149
152 void clear(void);
153
155 hxattr_nodiscard hxinline uint8_t mode(void) const { return m_open_mode_; }
156
159 hxattr_nodiscard size_t get_pos(void) const;
160
164 bool set_pos(size_t position_);
165
173 size_t read(void* bytes_, size_t buffer_size_, size_t count_) hxattr_nonnull(2) hxattr_hot;
174
181 size_t write(const void* bytes_, size_t count_) hxattr_nonnull(2) hxattr_hot;
182
185 bool flush(void) hxattr_hot;
186
192 bool print(const char* format_, ...) hxattr_printf(2, 3) hxattr_hot;
193
196 template<typename T_>
197 hxinline bool read1(T_& t_) { return this->read(&t_, sizeof t_, sizeof t_) == sizeof t_; }
198
199#if HX_CPLUSPLUS >= 202302L
202 template<typename T_>
204 T_ t_;
205 const bool error_ = this->read(&t_, sizeof(T_), sizeof(T_)) != sizeof(T_);
206 return hxexpected<T_>(error_, t_);
207 }
208#endif
209
212 template<typename T_>
213 hxinline bool write1(const T_& t_) { return this->write(&t_, sizeof t_) == sizeof t_; }
214
217 template<typename T_>
219 this->read(&t_, sizeof t_, sizeof t_);
220 return *this;
221 }
222
225 template<typename T_>
226 hxinline hxfile& operator<<(const T_& t_) {
227 this->write(&t_, sizeof t_);
228 return *this;
229 }
230
234 template<size_t string_length_>
235 hxinline hxfile& operator<<(const char(&str_)[string_length_]) {
236 this->write(str_, string_length_-1);
237 return *this;
238 }
239
240private:
241 hxfile(const hxfile&) = delete;
242 void operator=(const hxfile&) = delete;
243 template<typename T_> hxfile& operator>>(const T_* t_) = delete;
244
245 bool openv_(uint8_t mode_, const char* format_, va_list args_);
246
247 intptr_t m_file_pimpl_;
248 uint8_t m_open_mode_;
249 bool m_owns_;
250 bool m_fail_;
251 bool m_eof_;
252};
253
254HX_NS_END_
255#endif // HX_USE_FILE_IO
hxexpected<T, E=bool> - Holds either a value of type T or an error of type E.
Definition hxexpected.hpp:67
hxfile - Single-ownership C++ RAII abstraction for file I/O.
Definition hxfile.hpp:70
hxexpected< T > expect(void)
Reads a single unformatted native-endian object from the file, returning it or an error if the read f...
Definition hxfile.hpp:203
bool fail(void) const
Checks if an error has been encountered, EOF set or set_fail called.
Definition hxfile.hpp:141
void operator=(hxfile &&file_) noexcept
Move assignment. No copy assignment operator is provided.
bool flush(void)
Flushes buffered output to the underlying file.
void close(void)
Closes the currently open file.
bool read1(T &t_)
Reads a single unformatted native-endian object from the file.
Definition hxfile.hpp:197
bool set_pos(size_t position_)
Sets the current position in the file.
hxfile(uint8_t mode_, const char *filename_,...)
Constructs and opens a file with a formatted filename.
hxfile & operator<<(const char(&str_)[string_length_])
Writes a string literal to the file.
Definition hxfile.hpp:235
hxfile & operator>>(T &t_)
Reads a single unformatted native-endian object from a stream.
Definition hxfile.hpp:218
size_t get_pos(void) const
Returns the current position in the file if open, 0 otherwise.
bool is_open(void) const
Checks if the file is open.
bool eof(void) const
Checks if EOF has been reached.
Definition hxfile.hpp:148
size_t read(void *bytes_, size_t buffer_size_, size_t count_)
Reads a specified number of bytes from the file into the provided buffer.
hxfile & operator<<(const T &t_)
Writes a single unformatted native-endian object to a stream.
Definition hxfile.hpp:226
hxfile(void)
Default-constructs as a closed file.
bool print(const char *format_,...)
Writes a formatted UTF-8 string to the file.
open_mode
open_mode - Flags indicating how the file is to be used.
Definition hxfile.hpp:74
@ open_mode_in
Open for binary reading. e.g., "rb".
Definition hxfile.hpp:78
@ open_mode_out
Open for binary writing.
Definition hxfile.hpp:81
@ open_mode_asserts
By default, any unexpected failure is reported only through the failure flag.
Definition hxfile.hpp:86
@ open_mode_none
No flags.
Definition hxfile.hpp:76
uint8_t mode(void) const
Returns the current open mode of the file.
Definition hxfile.hpp:155
void clear(void)
Resets the failure and EOF flags.
bool write1(const T &t_)
Writes a single unformatted native-endian object to the file.
Definition hxfile.hpp:213
size_t write(const void *bytes_, size_t count_)
Writes a specified number of bytes from the provided buffer to the file.
bool open(uint8_t mode_, const char *filename_,...)
Opens a file with the specified mode and formatted filename.
void set_fail(void)
Marks the file as having encountered a failure.
Definition hxfile.hpp:145
An expected value type.
constexpr char hxendl[]
hxendl - Equivalent to std::endl without the flush.
Definition hxfile.hpp:42
hxfile hxin
hxin - Global reference to stdin or equivalent.
hxfile hxerr
hxerr - Global reference to stderr or equivalent.
hxfile hxout
hxout - Global reference to stdout or equivalent.
hxfile hxdev_null
hxdev_null - Global equivalent to /dev/null.
#define hxinline
hxinline - Force a function to be inlined into its callers.
Definition hxsettings.h:125
#define hxinline_constexpr
hxinline_constexpr - Enables C++17 compatable "inline constexpr" usage for variables so they can be e...
Definition hxsettings.h:275
#define hxattr_printf(...)
hxattr_printf - Indicates to gcc that a function uses printf-style formatting so it can type-check th...
Definition hxsettings.h:117
#define hxattr_nonnull(...)
hxattr_nonnull - Indicates that a function has args that should not be null.
Definition hxsettings.h:106
#define hxattr_nodiscard
hxattr_nodiscard - Indicates the caller should not discard the return value.
Definition hxsettings.h:98
#define hxattr_hot
hxattr_hot - Optimize a function more aggressively.
Definition hxsettings.h:94
Provides core macros, memory management and feature detection.