Sane C++ Libraries
C++ Platform Abstraction Libraries
FileDescriptor.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Foundation/Result.h"
5#include "../Foundation/Span.h"
6#include "../Foundation/UniqueHandle.h"
7#include "../Strings/StringView.h"
10
11namespace SC
12{
13struct String;
14template <typename T>
15struct Vector;
16
17struct SC_COMPILER_EXPORT FileDescriptor;
18struct SC_COMPILER_EXPORT PipeDescriptor;
19namespace detail
20{
21struct SC_COMPILER_EXPORT FileDescriptorDefinition;
22}
23} // namespace SC
24
25#if SC_PLATFORM_WINDOWS
26
27struct SC::detail::FileDescriptorDefinition
28{
29 using Handle = void*; // HANDLE
30 static Result releaseHandle(Handle& handle);
31#ifdef __clang__
32 static constexpr void* Invalid = __builtin_constant_p(-1) ? (void*)-1 : (void*)-1; // INVALID_HANDLE_VALUE
33#else
34 static constexpr void* Invalid = (void*)-1; // INVALID_HANDLE_VALUE
35#endif
36};
37
38#else
40struct SC::detail::FileDescriptorDefinition
41{
42 using Handle = int; // fd
43 static Result releaseHandle(Handle& handle);
44
45 static constexpr Handle Invalid = -1; // invalid fd
46};
47
48#endif
49
52
56struct SC::FileDescriptor : public SC::UniqueHandle<SC::detail::FileDescriptorDefinition>
57{
58 using UniqueHandle::UniqueHandle;
61 {
66 };
67
70 {
71 bool inheritable = false;
72 bool blocking = true;
73 };
74
79 [[nodiscard]] Result open(StringView path, OpenMode mode);
80
86 [[nodiscard]] Result open(StringView path, OpenMode mode, OpenOptions options);
87
91 [[nodiscard]] Result setBlocking(bool blocking);
92
96 [[nodiscard]] Result setInheritable(bool inheritable);
97
101 [[nodiscard]] Result isInheritable(bool& hasValue) const;
102
108 [[nodiscard]] Result read(Span<char> data, Span<char>& actuallyRead, uint64_t offset);
109
115 [[nodiscard]] Result read(Span<uint8_t> data, Span<uint8_t>& actuallyRead, uint64_t offset);
116
121 [[nodiscard]] Result read(Span<char> data, Span<char>& actuallyRead);
122
127 [[nodiscard]] Result read(Span<uint8_t> data, Span<uint8_t>& actuallyRead);
128
133 [[nodiscard]] Result write(Span<const char> data, uint64_t offset);
134
139 [[nodiscard]] Result write(Span<const uint8_t> data, uint64_t offset);
140
144 [[nodiscard]] Result write(Span<const char> data);
145
149 [[nodiscard]] Result write(Span<const uint8_t> data);
150
153 {
157 };
158
163 [[nodiscard]] Result seek(SeekMode seekMode, uint64_t offset);
164
168 [[nodiscard]] Result currentPosition(size_t& position) const;
169
173 [[nodiscard]] Result sizeInBytes(size_t& sizeInBytes) const;
174
179 [[nodiscard]] Result readUntilEOF(Vector<char>& destination);
180
185 [[nodiscard]] Result readUntilEOF(Vector<uint8_t>& destination);
186
191 [[nodiscard]] Result readUntilEOF(String& destination);
192
193 private:
194 struct Internal;
195 struct ReadResult;
196 template <typename T>
197 Result readUntilEOFTemplate(Vector<T>& destination);
198};
199
202{
205 {
208 };
209
212 {
215 };
218
225
228 [[nodiscard]] Result close();
229};
#define SC_COMPILER_EXPORT
Macro for symbol visibility in non-MSVC compilers.
Definition: Compiler.h:78
unsigned long long uint64_t
Platform independent (8) bytes unsigned int.
Definition: PrimitiveTypes.h:42
Additional flags to be set when opening files.
Definition: FileDescriptor.h:70
bool blocking
Set to false if file will be used for Async I/O (see Async)
Definition: FileDescriptor.h:72
bool inheritable
Set to true to make the file visible to child processes.
Definition: FileDescriptor.h:71
Wraps an OS File descriptor to read and write to and from it.
Definition: FileDescriptor.h:57
SeekMode
How the offset to FileDescriptor::seek is defined.
Definition: FileDescriptor.h:153
@ SeekCurrent
Offset to FileDescriptor::seek is to be applied from current descriptor position.
Definition: FileDescriptor.h:156
@ SeekEnd
Offset to FileDescriptor::seek is to be applied (backwards) from end of descriptor.
Definition: FileDescriptor.h:155
@ SeekStart
Offset to FileDescriptor::seek is to be applied from start of descriptor.
Definition: FileDescriptor.h:154
Result read(Span< uint8_t > data, Span< uint8_t > &actuallyRead)
Reads bytes from current position (FileDescriptor::seek) into user supplied Span.
Result open(StringView path, OpenMode mode)
Opens file at path with a given mode
Result write(Span< const uint8_t > data, uint64_t offset)
Writes bytes at offset from start of the file descriptor.
Result readUntilEOF(Vector< uint8_t > &destination)
Reads into a given dynamic buffer until End of File (EOF) is signaled.
Result setInheritable(bool inheritable)
Set inheritable flag (visibility to child processes).
Result read(Span< char > data, Span< char > &actuallyRead)
Reads bytes from current position (FileDescriptor::seek) into user supplied Span.
Result currentPosition(size_t &position) const
Gets current descriptor position (if seekable)
Result write(Span< const uint8_t > data)
Writes bytes from current position (FileDescriptor::seek) of the file descriptor.
Result write(Span< const char > data, uint64_t offset)
Writes bytes at offset from start of the file descriptor.
Result sizeInBytes(size_t &sizeInBytes) const
Gets total file size in bytes (if seekable)
OpenMode
Define mode for opening the file (read, write etc.)
Definition: FileDescriptor.h:61
@ WriteAppend
Opens write mode, appending to existing file that must exist at the same location.
Definition: FileDescriptor.h:64
@ ReadOnly
Opens in read-only mode.
Definition: FileDescriptor.h:62
@ WriteCreateTruncate
Opens in write mode, creating or truncating it if another file exists at same location.
Definition: FileDescriptor.h:63
@ ReadAndWrite
Opens file for read / write mode.
Definition: FileDescriptor.h:65
Result read(Span< char > data, Span< char > &actuallyRead, uint64_t offset)
Reads bytes at offset into user supplied span.
Result open(StringView path, OpenMode mode, OpenOptions options)
Opens file at path with a given mode
Result setBlocking(bool blocking)
Set blocking mode (read / write waiting for I/O).
Result readUntilEOF(Vector< char > &destination)
Reads into a given dynamic buffer until End of File (EOF) is signaled.
Result isInheritable(bool &hasValue) const
Queries the inheritable state of this descriptor.
Result read(Span< uint8_t > data, Span< uint8_t > &actuallyRead, uint64_t offset)
Reads bytes at offset into user supplied span.
Result readUntilEOF(String &destination)
Reads into a given string until End of File (EOF) is signaled Works also for non-seekable file descri...
Result write(Span< const char > data)
Writes bytes from current position (FileDescriptor::seek) of the file descriptor.
Result seek(SeekMode seekMode, uint64_t offset)
Changes the current position in the file descriptor, if seekable.
Descriptor representing a Pipe used for InterProcess Communication (IPC)
Definition: FileDescriptor.h:202
Result createPipe(InheritableReadFlag readFlag=ReadNonInheritable, InheritableWriteFlag writeFlag=WriteNonInheritable)
Creates a Pipe.
InheritableReadFlag
Specifies a flag for read side of the pipe.
Definition: FileDescriptor.h:205
@ ReadNonInheritable
Requests read side of the pipe not to be inheritable from child processes.
Definition: FileDescriptor.h:207
@ ReadInheritable
Requests read side of the pipe to be inheritable from child processes.
Definition: FileDescriptor.h:206
FileDescriptor writePipe
The write side of the pipe.
Definition: FileDescriptor.h:217
Result close()
Closes the pipe.
FileDescriptor readPipe
The read side of the pipe.
Definition: FileDescriptor.h:216
InheritableWriteFlag
Specifies a flag for write side of the pipe.
Definition: FileDescriptor.h:212
@ WriteInheritable
Requests write side of the pipe to be inheritable from child processes.
Definition: FileDescriptor.h:213
@ WriteNonInheritable
Requests write side of the pipe to be inheritable from child processes.
Definition: FileDescriptor.h:214
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition: Result.h:11
View over a contiguous sequence of items (pointer + size in elements).
Definition: Span.h:20
A non-modifiable owning string with associated encoding.
Definition: String.h:30
Non-owning view over a range of characters with UTF Encoding.
Definition: StringView.h:47
Move only handle that has a special tag value flagging its invalid state.
Definition: UniqueHandle.h:67
A contiguous sequence of heap allocated elements.
Definition: Vector.h:51