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
19HX_NS_BEGIN_
20
21class hxfile;
22
24extern hxfile hxin;
25
27extern hxfile hxout;
28
30extern hxfile hxerr;
31
34extern hxfile hxdev_null;
35
39
66class hxfile {
67public:
84
86 hxfile(void);
87
93 hxfile(uint8_t mode_, const char* filename_, ...) hxattr_printf(3, 4);
94
100 hxfile(uint8_t mode_, intptr_t file_);
101
104 hxfile(const char* file_, uint8_t mode_=0) = delete;
105
107 hxfile(hxfile&& file_) noexcept;
108
112
114 void operator=(hxfile&& file_) noexcept;
115
118 operator bool(void) const;
119
124 bool open(uint8_t mode_, const char* filename_, ...) hxattr_printf(3, 4);
125
127 void close(void);
128
130 hxattr_nodiscard bool is_open(void) const;
131
133 hxattr_nodiscard bool fail(void) const { return m_fail_; }
134
137 void set_fail(void) { m_fail_ = true; }
138
140 hxattr_nodiscard bool eof(void) const { return m_eof_; }
141
144 void clear(void);
145
147 hxattr_nodiscard uint8_t mode(void) const { return m_open_mode_; }
148
151 hxattr_nodiscard size_t get_pos(void) const;
152
156 bool set_pos(size_t position_);
157
163 size_t read(void* bytes_, size_t buffer_size_, size_t count_) hxattr_nonnull(2) hxattr_hot ;
164
171 size_t write(const void* bytes_, size_t count_) hxattr_nonnull(2) hxattr_hot ;
172
175 bool flush(void) hxattr_hot;
176
177#if (HX_USE_FILE_IO) != 2
184 template<size_t buffer_size_>
185 bool getline(char(&buffer_)[buffer_size_]) {
186 return this->getline(buffer_, buffer_size_);
187 }
188
196 bool getline(char* buffer_, int buffer_size_) hxattr_nonnull(2) hxattr_hot ;
197#endif // (HX_USE_FILE_IO) != 2
198
204 bool print(const char* format_, ...) hxattr_printf(2, 3) hxattr_hot ;
205
213 int scan(const char* format_, ...) hxattr_scanf(2, 3) hxattr_hot ;
214
217 template<typename T_>
218 bool read1(T_& t_) { return this->read(&t_, sizeof t_, sizeof t_) == sizeof t_; }
219
222 template<typename T_>
223 bool write1(const T_& t_) { return this->write(&t_, sizeof t_) == sizeof t_; }
224
227 template<typename T_>
229 this->read(&t_, sizeof t_, sizeof t_);
230 return *this;
231 }
232
235 template<typename T_>
236 hxfile& operator<<(const T_& t_) {
237 this->write(&t_, sizeof t_);
238 return *this;
239 }
240
244 template<size_t string_length_>
245 hxfile& operator<<(const char(&str_)[string_length_]) {
246 this->write(str_, string_length_-1);
247 return *this;
248 }
249
250private:
251 hxfile(const hxfile&) = delete;
252 void operator=(const hxfile&) = delete;
253 template<typename T_> hxfile& operator>>(const T_* t_) = delete;
254
255 bool openv_(uint8_t mode_, const char* format_, va_list args_);
256
257 intptr_t m_file_pimpl_;
258 uint8_t m_open_mode_;
259 bool m_owns_;
260 bool m_fail_;
261 bool m_eof_;
262};
263
264HX_NS_END_
265#endif // HX_USE_FILE_IO
hxfile - Single-ownership C++ RAII abstraction for file I/O.
Definition hxfile.hpp:66
bool fail(void) const
Checks if an error has been encountered, EOF set or set_fail called.
Definition hxfile.hpp:133
void operator=(hxfile &&file_) noexcept
Move assignment. No copy assignment operator is provided.
bool getline(char(&buffer_)[buffer_size_])
Reads a \n or EOF terminated character sequence.
Definition hxfile.hpp:185
int scan(const char *format_,...)
Reads a formatted UTF-8 string from the file.
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:218
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:245
hxfile & operator>>(T &t_)
Reads a single unformatted native-endian object from a stream.
Definition hxfile.hpp:228
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:140
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:236
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:70
@ open_mode_in
Open for binary reading. e.g., "rb".
Definition hxfile.hpp:74
@ open_mode_out
Open for binary writing.
Definition hxfile.hpp:77
@ open_mode_asserts
By default, any unexpected failure is reported only through the failure flag.
Definition hxfile.hpp:82
@ open_mode_none
No flags.
Definition hxfile.hpp:72
bool getline(char *buffer_, int buffer_size_)
Reads a \n or EOF terminated character sequence.
uint8_t mode(void) const
Returns the current open mode of the file.
Definition hxfile.hpp:147
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:223
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:137
constexpr char hxendl[]
hxendl - Equivalent to std::endl without the flush.
Definition hxfile.hpp:38
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_constexpr
hxinline_constexpr - Enables C++17 compatable "inline constexpr" usage for variables so they can be e...
Definition hxsettings.h:272
#define hxattr_printf(...)
hxattr_printf - Indicates to gcc that a function uses printf-style formatting so it can type-check th...
Definition hxsettings.h:108
#define hxattr_scanf(...)
hxattr_scanf - Indicates to gcc that a function uses scanf-style formatting so it can type-check the ...
Definition hxsettings.h:116
#define hxattr_nonnull(...)
hxattr_nonnull - Indicates that a function has args that should not be null.
Definition hxsettings.h:97
#define hxattr_nodiscard
hxattr_nodiscard - Indicates the caller should not discard the return value.
Definition hxsettings.h:89
#define hxattr_hot
hxattr_hot - Optimize a function more aggressively.
Definition hxsettings.h:86
Provides core macros, memory management and feature detection.