Wraps an OS File descriptor to read and write to and from it. More...
#include <FileDescriptor.h>
Classes | |
struct | OpenOptions |
Additional flags to be set when opening files. More... | |
Public Types | |
enum | OpenMode { ReadOnly , WriteCreateTruncate , WriteAppend , ReadAndWrite } |
Define mode for opening the file (read, write etc.) More... | |
enum | SeekMode { SeekStart , SeekEnd , SeekCurrent } |
How the offset to FileDescriptor::seek is defined. More... | |
Public Member Functions | |
Result | open (StringView path, OpenMode mode) |
Opens file at path with a given mode More... | |
Result | open (StringView path, OpenMode mode, OpenOptions options) |
Opens file at path with a given mode More... | |
Result | setBlocking (bool blocking) |
Set blocking mode (read / write waiting for I/O). More... | |
Result | setInheritable (bool inheritable) |
Set inheritable flag (visibility to child processes). More... | |
Result | isInheritable (bool &hasValue) const |
Queries the inheritable state of this descriptor. More... | |
Result | read (Span< char > data, Span< char > &actuallyRead, uint64_t offset) |
Reads bytes at offset into user supplied span. More... | |
Result | read (Span< uint8_t > data, Span< uint8_t > &actuallyRead, uint64_t offset) |
Reads bytes at offset into user supplied span. More... | |
Result | read (Span< char > data, Span< char > &actuallyRead) |
Reads bytes from current position (FileDescriptor::seek) into user supplied Span. More... | |
Result | read (Span< uint8_t > data, Span< uint8_t > &actuallyRead) |
Reads bytes from current position (FileDescriptor::seek) into user supplied Span. More... | |
Result | write (Span< const char > data, uint64_t offset) |
Writes bytes at offset from start of the file descriptor. More... | |
Result | write (Span< const uint8_t > data, uint64_t offset) |
Writes bytes at offset from start of the file descriptor. More... | |
Result | write (Span< const char > data) |
Writes bytes from current position (FileDescriptor::seek) of the file descriptor. More... | |
Result | write (Span< const uint8_t > data) |
Writes bytes from current position (FileDescriptor::seek) of the file descriptor. More... | |
Result | seek (SeekMode seekMode, uint64_t offset) |
Changes the current position in the file descriptor, if seekable. More... | |
Result | currentPosition (size_t &position) const |
Gets current descriptor position (if seekable) More... | |
Result | sizeInBytes (size_t &sizeInBytes) const |
Gets total file size in bytes (if seekable) More... | |
Result | readUntilEOF (Vector< char > &destination) |
Reads into a given dynamic buffer until End of File (EOF) is signaled. More... | |
Result | readUntilEOF (Vector< uint8_t > &destination) |
Reads into a given dynamic buffer until End of File (EOF) is signaled. More... | |
Result | readUntilEOF (String &destination) |
Reads into a given string until End of File (EOF) is signaled Works also for non-seekable file descriptors (stdout / in / err). More... | |
UniqueHandle ()=default | |
UniqueHandle (const UniqueHandle &v)=delete | |
UniqueHandle (UniqueHandle &&v) | |
UniqueHandle (const Handle &externalHandle) | |
Wraps an OS File descriptor to read and write to and from it.
Example usage:
Define mode for opening the file (read, write etc.)
How the offset to FileDescriptor::seek is defined.
Enumerator | |
---|---|
SeekStart | Offset to FileDescriptor::seek is to be applied from start of descriptor. |
SeekEnd | Offset to FileDescriptor::seek is to be applied (backwards) from end of descriptor. |
SeekCurrent | Offset to FileDescriptor::seek is to be applied from current descriptor position. |
Gets current descriptor position (if seekable)
position | (output) current position of file descriptor |
Result SC::FileDescriptor::isInheritable | ( | bool & | hasValue | ) | const |
Queries the inheritable state of this descriptor.
hasValue | will be set to true if the file descriptor has inheritable file set |
Result SC::FileDescriptor::open | ( | StringView | path, |
OpenMode | mode | ||
) |
Opens file at path
with a given mode
path | The path to file |
mode | The mode used to open file (read-only, write-append etc.) |
Result SC::FileDescriptor::open | ( | StringView | path, |
OpenMode | mode, | ||
OpenOptions | options | ||
) |
Opens file at path
with a given mode
path | The path to file |
mode | The mode used to open file |
options | Options that can be applied when opening the file (inheritable, blocking etc.) |
Reads bytes from current position (FileDescriptor::seek) into user supplied Span.
data | Span of bytes where data should be written to |
actuallyRead | A sub-span of data of the actually read bytes. A zero sized span means EOF. |
Reads bytes at offset into user supplied span.
data | Span of bytes where data should be written to |
actuallyRead | A sub-span of data of the actually read bytes. A zero sized span means EOF. |
offset | Offset from begin of the file descriptor where read should be started |
Reads bytes from current position (FileDescriptor::seek) into user supplied Span.
data | Span of bytes where data should be written to |
actuallyRead | A sub-span of data of the actually read bytes. A zero sized span means EOF. |
Result SC::FileDescriptor::read | ( | Span< uint8_t > | data, |
Span< uint8_t > & | actuallyRead, | ||
uint64_t | offset | ||
) |
Reads bytes at offset into user supplied span.
data | Span of bytes where data should be written to |
actuallyRead | A sub-span of data of the actually read bytes. A zero sized span means EOF. |
offset | Offset from begin of the file descriptor where read should be started |
Reads into a given string until End of File (EOF) is signaled Works also for non-seekable file descriptors (stdout / in / err).
destination | A destination string to write to (it will be sized as needed) |
Reads into a given dynamic buffer until End of File (EOF) is signaled.
Works also for non-seekable file descriptors (stdout / in / err).
destination | A destination buffer to write to (it will be resized as needed) |
Reads into a given dynamic buffer until End of File (EOF) is signaled.
Works also for non-seekable file descriptors (stdout / in / err).
destination | A destination buffer to write to (it will be resized as needed) |
Changes the current position in the file descriptor, if seekable.
seekMode | How the offset is defined (from start, end, current) |
offset | An offset to be applied according to seekMode to this descriptor |
Result SC::FileDescriptor::setBlocking | ( | bool | blocking | ) |
Set blocking mode (read / write waiting for I/O).
Can be set also during open with OpenOptions.
blocking | true to set file to blocking mode |
true
if blocking mode has been changed successfully Result SC::FileDescriptor::setInheritable | ( | bool | inheritable | ) |
Set inheritable flag (visibility to child processes).
Can be set also during open with OpenOptions.
inheritable | true to set file to blocking mode |
true
if blocking mode has been changed successfully Gets total file size in bytes (if seekable)
sizeInBytes | (output) total size of file |
Writes bytes from current position (FileDescriptor::seek) of the file descriptor.
data | Span of bytes containing the data to write |
Writes bytes at offset from start of the file descriptor.
data | Span of bytes containing the data to write |
offset | Offset from begin of file descriptor to start writing |
Writes bytes from current position (FileDescriptor::seek) of the file descriptor.
data | Span of bytes containing the data to write |
Writes bytes at offset from start of the file descriptor.
data | Span of bytes containing the data to write |
offset | Offset from begin of file descriptor to start writing |