|
libhatchet
|
#include <hxfile.hpp>
Public Types | |
| enum | open_mode : uint8_t { open_mode_none = 0u , open_mode_in = 1u , open_mode_out = 2u , open_mode_asserts = 4u } |
Public Member Functions | |
| hxfile (void) | |
| hxfile (uint8_t mode_, const char *filename_,...) | |
| hxfile (uint8_t mode_, intptr_t file_) | |
| hxfile (const char *file_, uint8_t mode_=0)=delete | |
| hxfile (hxfile &&file_) noexcept | |
| ~hxfile () | |
| void | operator= (hxfile &&file_) noexcept |
| operator bool (void) const | |
| bool | open (uint8_t mode_, const char *filename_,...) |
| void | close (void) |
| bool | is_open (void) const |
| bool | fail (void) const |
| void | set_fail (void) |
| bool | eof (void) const |
| void | clear (void) |
| uint8_t | mode (void) const |
| size_t | get_pos (void) const |
| bool | set_pos (size_t position_) |
| size_t | read (void *bytes_, size_t buffer_size_, size_t count_) |
| size_t | write (const void *bytes_, size_t count_) |
| bool | flush (void) |
| template<size_t buffer_size_> | |
| bool | getline (char(&buffer_)[buffer_size_]) |
| bool | getline (char *buffer_, int buffer_size_) |
| bool | print (const char *format_,...) |
| int | scan (const char *format_,...) |
| template<typename T> | |
| bool | read1 (T &t_) |
| template<typename T> | |
| bool | write1 (const T &t_) |
| template<typename T> | |
| hxfile & | operator>> (T &t_) |
| template<typename T> | |
| hxfile & | operator<< (const T &t_) |
| template<size_t string_length_> | |
| hxfile & | operator<< (const char(&str_)[string_length_]) |
hxfile - Single-ownership C++ RAII abstraction for file I/O.
Provides a mixture of unformatted binary stream operations and formatted printf/scanf style I/O, along with optional error handling. gcc is useful for validating printf/scanf style arguments. However, memory-imaged data structures are still recommended. hxfile uses binary I/O only for portability. It implements the equivalent of the standard eofbit and failbit but not the badbit. The failbit should always be set when the eofbit is set.
Here is the syntax to make a block of code conditional on opening a file. The filename is also formatted printf style. This is equivalent to Python's with open(filename, mode) as f:.
To switch to a different implementation, use an alternate .cpp file for your target. Allows hxerr to be a serial port while file I/O uses a DMA controller.
WARNING: get_position/set_position over 4 GiB is not supported on Windows. As well, size_t is limited to 4 GiB on all 32-bit platforms.
| enum hxfile::open_mode : uint8_t |
open_mode - Flags indicating how the file is to be used.
Modifying or appending to an existing file is not implemented.
| Enumerator | |
|---|---|
| open_mode_none | No flags. |
| open_mode_in | Open for binary reading. e.g., "rb". |
| open_mode_out | Open for binary writing. Replaces any existing file with an empty one even if open_mode_in is used at the same time. e.g., "wb". |
| open_mode_asserts | By default, any unexpected failure is reported only through the failure flag. To assert on reasonably unforeseen failures, set open_mode_asserts. Bad parameters (e.g., providing a null buffer) will still result in assertions regardless of this flag. |
| hxfile::hxfile | ( | void | ) |
Default-constructs as a closed file.
| hxfile::hxfile | ( | uint8_t | mode_, |
| const char * | filename_, | ||
| ... ) |
| hxfile::hxfile | ( | uint8_t | mode_, |
| intptr_t | file_ ) |
|
delete |
Disallow usage where the filename comes first, like with fopen.
This is done because hxfile::hxfile() uses variadic arguments.
|
noexcept |
Move constructor. No copy constructor is provided.
| hxfile::~hxfile | ( | ) |
Destroys the file and ensures it is closed when the object goes out of scope.
| void hxfile::clear | ( | void | ) |
Resets the failure and EOF flags.
This is required to clear EOF after EOF is encountered.
| void hxfile::close | ( | void | ) |
Closes the currently open file.
|
inline |
Checks if EOF has been reached.
|
inline |
Checks if an error has been encountered, EOF set or set_fail called.
| bool hxfile::flush | ( | void | ) |
Flushes buffered output to the underlying file.
Safe to call on hxdev_null. Does not reset the failure flag to false on success.
| size_t hxfile::get_pos | ( | void | ) | const |
Returns the current position in the file if open, 0 otherwise.
FILE* implementation requires a 64-bit long to support 64-bit files.
| bool hxfile::getline | ( | char * | buffer_, |
| int | buffer_size_ ) |
Reads a \n or EOF terminated character sequence.
Allowed to fail on EOF without needing hxfile::open_mode_asserts to be unset. Encountering EOF also sets the failure flag. NOTE: Not available in the POSIX version.
|
inline |
Reads a \n or EOF terminated character sequence.
Allowed to fail on EOF without needing hxfile::open_mode_asserts to be unset. Encountering EOF also sets the failure flag. Automatically determines the size of the provided char array. NOTE: Not available in the POSIX version.
| bool hxfile::is_open | ( | void | ) | const |
Checks if the file is open.
|
inline |
Returns the current open mode of the file.
| bool hxfile::open | ( | uint8_t | mode_, |
| const char * | filename_, | ||
| ... ) |
| hxfile::operator bool | ( | void | ) | const |
Checks if the file is open, EOF has not been reached, and no error has been encountered.
See usage example in the class documentation.
|
inline |
Writes a string literal to the file.
Supports Google Test style diagnostic messages in hxtest.
|
inline |
Writes a single unformatted native-endian object to a stream.
|
noexcept |
Move assignment. No copy assignment operator is provided.
|
inline |
Reads a single unformatted native-endian object from a stream.
| bool hxfile::print | ( | const char * | format_, |
| ... ) |
Writes a formatted UTF-8 string to the file.
Uses printf conventions. Formatting and writing will be skipped when using hxdev_null. Does not modify the failure flag because it is not clear from vfprintf.
| size_t hxfile::read | ( | void * | bytes_, |
| size_t | buffer_size_, | ||
| size_t | count_ ) |
Reads a specified number of bytes from the file into the provided buffer.
Does not reset the failure flag to false on success.
|
inline |
Reads a single unformatted native-endian object from the file.
| int hxfile::scan | ( | const char * | format_, |
| ... ) |
Reads a formatted UTF-8 string from the file.
Uses scanf conventions. Returns the same value as scanf. Omit hxfile::open_mode_asserts to read until EOF. Parse errors will set fail to true. Will set the failure flag and check EOF on a return value of EOF from vfscanf. Returns a negative value on EOF.
|
inline |
Marks the file as having encountered a failure.
Allows the user to report additional errors without having to track them. Non-standard.
| bool hxfile::set_pos | ( | size_t | position_ | ) |
Sets the current position in the file.
Returns true on success. FILE* implementation requires a 64-bit long to support 64-bit files. Resets the failure flag to false on success.
| size_t hxfile::write | ( | const void * | bytes_, |
| size_t | count_ ) |
Writes a specified number of bytes from the provided buffer to the file.
Writing will be skipped when using hxdev_null. Resets the failure flag to false on success.
|
inline |
Writes a single unformatted native-endian object to the file.