Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
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/StringViewData.h"
7#include "../Foundation/UniqueHandle.h"
8
11
12namespace SC
13{
14namespace detail
15{
16#if SC_PLATFORM_WINDOWS
17
18struct SC_COMPILER_EXPORT FileDescriptorDefinition
19{
20 using Handle = void*; // HANDLE
21 static Result releaseHandle(Handle& handle);
22#ifdef __clang__
23 static constexpr void* Invalid = __builtin_constant_p(-1) ? (void*)-1 : (void*)-1; // INVALID_HANDLE_VALUE
24#else
25 static constexpr void* Invalid = (void*)-1; // INVALID_HANDLE_VALUE
26#endif
27};
28
29#else
31struct SC_COMPILER_EXPORT FileDescriptorDefinition
32{
33 using Handle = int; // fd
34 static Result releaseHandle(Handle& handle);
35
36 static constexpr Handle Invalid = -1; // invalid fd
37};
38
39#endif
40} // namespace detail
41
44
47{
58
59 FileOpen(Mode mode = Read) : mode(mode) {}
60
62 bool inheritable = false;
63 bool blocking = true;
64 bool sync = false;
65 bool exclusive = false;
66
67#if !SC_PLATFORM_WINDOWS
68 int toPosixFlags() const;
69 int toPosixAccess() const;
70#endif
71};
72
74struct SC_COMPILER_EXPORT FileDescriptor : public UniqueHandle<detail::FileDescriptorDefinition>
75{
76 using UniqueHandle::UniqueHandle;
77
81
88
92 [[nodiscard]] Result setBlocking(bool blocking);
93
97 [[nodiscard]] Result setInheritable(bool inheritable);
98
102 [[nodiscard]] Result isInheritable(bool& hasValue) const;
103
109 [[nodiscard]] Result read(Span<char> data, Span<char>& actuallyRead, uint64_t offset);
110
116 [[nodiscard]] Result read(Span<uint8_t> data, Span<uint8_t>& actuallyRead, uint64_t offset);
117
122 [[nodiscard]] Result read(Span<char> data, Span<char>& actuallyRead);
123
128 [[nodiscard]] Result read(Span<uint8_t> data, Span<uint8_t>& actuallyRead);
129
134 [[nodiscard]] Result write(Span<const char> data, uint64_t offset);
135
140 [[nodiscard]] Result write(Span<const uint8_t> data, uint64_t offset);
141
145 [[nodiscard]] Result write(Span<const char> data);
146
150 [[nodiscard]] Result write(Span<const uint8_t> data);
151
159
164 [[nodiscard]] Result seek(SeekMode seekMode, uint64_t offset);
165
169 [[nodiscard]] Result currentPosition(size_t& position) const;
170
174 [[nodiscard]] Result sizeInBytes(size_t& sizeInBytes) const;
175
176 private:
177 friend struct File;
178 struct Internal;
179};
180
183{
186 {
188 ReadNonInheritable
189 };
190
193 {
195 WriteNonInheritable
196 };
199
204 [[nodiscard]] Result createPipe(InheritableReadFlag readFlag = ReadNonInheritable,
205 InheritableWriteFlag writeFlag = WriteNonInheritable);
206
209 [[nodiscard]] Result close();
210};
212} // namespace SC
#define SC_COMPILER_EXPORT
Macro for symbol visibility in non-MSVC compilers.
Definition Compiler.h:78
unsigned char uint8_t
Platform independent (1) byte unsigned int.
Definition PrimitiveTypes.h:36
unsigned long long uint64_t
Platform independent (8) bytes unsigned int.
Definition PrimitiveTypes.h:42
File Descriptor (use File to open and use it with strings and buffers).
Definition FileDescriptor.h:75
SeekMode
How the offset to FileDescriptor::seek is defined.
Definition FileDescriptor.h:154
@ SeekCurrent
Offset to FileDescriptor::seek is to be applied from current descriptor position.
Definition FileDescriptor.h:157
@ SeekEnd
Offset to FileDescriptor::seek is to be applied (backwards) from end of descriptor.
Definition FileDescriptor.h:156
@ SeekStart
Offset to FileDescriptor::seek is to be applied from start of descriptor.
Definition FileDescriptor.h:155
Result read(Span< uint8_t > data, Span< uint8_t > &actuallyRead)
Reads bytes from current position (FileDescriptor::seek) into user supplied Span.
Result openForWriteToDevNull()
Opens a file descriptor handle for writing to /dev/null or equivalent on current OS.
Result write(Span< const uint8_t > data, uint64_t offset)
Writes bytes at offset from start of the file descriptor.
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)
Result read(Span< char > data, Span< char > &actuallyRead, uint64_t offset)
Reads bytes at offset into user supplied span.
Result setBlocking(bool blocking)
Set blocking mode (read / write waiting for I/O).
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 openNativeEncoding(StringViewData path, FileOpen mode)
Opens a file descriptor handle for a file path encoded in the native encoding of the current OS.
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.
Options used to open a file descriptor.
Definition FileDescriptor.h:47
Mode
Indicates the mode in which the file should be opened (read, write, append, etc.)
Definition FileDescriptor.h:50
@ Append
a Open for appending. The file is created if it does not exist.
Definition FileDescriptor.h:53
@ WriteRead
w+ Open for reading and writing. The file is created (if it does not exist) or truncated.
Definition FileDescriptor.h:56
@ ReadWrite
r+ Open for reading and writing. An error occurs if the file does not exist.
Definition FileDescriptor.h:52
@ Write
w Open for writing. The file is created (if it does not exist) or truncated (if it exists).
Definition FileDescriptor.h:55
@ AppendRead
a+ Open for reading and appending. The file is created if it does not exist.
Definition FileDescriptor.h:54
Mode mode
Open mode (read, write, append, etc.). See FileOpen::Mode for more details.
Definition FileDescriptor.h:61
Wraps a SC::FileDescriptor to open it and use strings / buffers.
Definition File.h:18
Read / Write pipe (Process stdin/stdout and IPC communication)
Definition FileDescriptor.h:183
Result createPipe(InheritableReadFlag readFlag=ReadNonInheritable, InheritableWriteFlag writeFlag=WriteNonInheritable)
Creates a Pipe.
InheritableReadFlag
Specifies a flag for read side of the pipe.
Definition FileDescriptor.h:186
@ ReadInheritable
Requests read side of the pipe to be inheritable from child processes.
Definition FileDescriptor.h:187
FileDescriptor writePipe
The write side of the pipe.
Definition FileDescriptor.h:198
Result close()
Closes the pipe.
FileDescriptor readPipe
The read side of the pipe.
Definition FileDescriptor.h:197
InheritableWriteFlag
Specifies a flag for write side of the pipe.
Definition FileDescriptor.h:193
@ WriteInheritable
Requests write side of the pipe to be inheritable from child processes.
Definition FileDescriptor.h:194
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition Result.h:12
View over a contiguous sequence of items (pointer + size in elements).
Definition Span.h:29
An read-only view over a string (to avoid including Strings library)
Definition StringViewData.h:31
Move only handle that has a special tag value flagging its invalid state.
Definition UniqueHandle.h:67