Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
File.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Foundation/Internal/IGrowableBuffer.h"
5#include "../Foundation/Result.h"
6#include "../Foundation/Span.h"
7#include "../Foundation/StringSpan.h"
8#include "../Foundation/UniqueHandle.h"
9
12
13namespace SC
14{
15namespace detail
16{
17#if SC_PLATFORM_WINDOWS
18
19struct SC_COMPILER_EXPORT FileDescriptorDefinition
20{
21 using Handle = void*; // HANDLE
22 static Result releaseHandle(Handle& handle);
23#ifdef __clang__
24 static constexpr void* Invalid = __builtin_constant_p(-1) ? (void*)-1 : (void*)-1; // INVALID_HANDLE_VALUE
25#else
26 static constexpr void* Invalid = (void*)-1; // INVALID_HANDLE_VALUE
27#endif
28};
29
30#else
32struct SC_COMPILER_EXPORT FileDescriptorDefinition
33{
34 using Handle = int; // fd
35 static Result releaseHandle(Handle& handle);
36
37 static constexpr Handle Invalid = -1; // invalid fd
38};
39
40#endif
41} // namespace detail
42
45
48{
59
60 FileOpen(Mode mode = Read) : mode(mode) {}
61
63 bool inheritable = false;
64 bool blocking = true;
65 bool sync = false;
66 bool exclusive = false;
67
68#if !SC_PLATFORM_WINDOWS
69 int toPosixFlags() const;
70 int toPosixAccess() const;
71#endif
72};
73
75struct SC_COMPILER_EXPORT FileDescriptor : public UniqueHandle<detail::FileDescriptorDefinition>
76{
77 using UniqueHandle::UniqueHandle;
78
82
88
92 Result setBlocking(bool blocking);
93
97 Result setInheritable(bool inheritable);
98
102 Result isInheritable(bool& hasValue) const;
103
109 Result read(Span<char> data, Span<char>& actuallyRead, uint64_t offset);
110
116 Result read(Span<uint8_t> data, Span<uint8_t>& actuallyRead, uint64_t offset);
117
122 Result read(Span<char> data, Span<char>& actuallyRead);
123
129
135 template <typename T>
136 Result readUntilEOF(T& destination)
137 {
138 return readUntilEOFGrowable(GrowableBuffer<T>{destination});
139 }
140
146
152
157
162
170
175 Result seek(SeekMode seekMode, uint64_t offset);
176
180 Result currentPosition(size_t& position) const;
181
185 Result sizeInBytes(size_t& sizeInBytes) const;
186
187 private:
188 friend struct File;
189 struct Internal;
190 Result readUntilEOFGrowable(IGrowableBuffer&& buffer);
191};
192
195{
198 {
200 ReadNonInheritable
201 };
202
205 {
207 WriteNonInheritable
208 };
211
216 Result createPipe(InheritableReadFlag readFlag = ReadNonInheritable,
217 InheritableWriteFlag writeFlag = WriteNonInheritable);
218
222};
224} // 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
Open, read and write to/from a file descriptor (like a file or pipe).
Definition File.h:76
SeekMode
How the offset to FileDescriptor::seek is defined.
Definition File.h:165
@ SeekCurrent
Offset to FileDescriptor::seek is to be applied from current descriptor position.
Definition File.h:168
@ SeekEnd
Offset to FileDescriptor::seek is to be applied (backwards) from end of descriptor.
Definition File.h:167
@ SeekStart
Offset to FileDescriptor::seek is to be applied from start of descriptor.
Definition File.h:166
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 readUntilEOF(T &destination)
Reads into a given dynamic buffer until End of File (EOF) is signaled.
Definition File.h:136
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 open(StringSpan path, FileOpen mode)
Opens a file descriptor handle from a file system path.
Result read(Span< uint8_t > data, Span< uint8_t > &actuallyRead, uint64_t offset)
Reads bytes at offset into user supplied span.
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 File.h:48
Mode
Indicates the mode in which the file should be opened (read, write, append, etc.)
Definition File.h:51
@ Append
a Open for appending. The file is created if it does not exist.
Definition File.h:54
@ WriteRead
w+ Open for reading and writing. The file is created (if it does not exist) or truncated.
Definition File.h:57
@ ReadWrite
r+ Open for reading and writing. An error occurs if the file does not exist.
Definition File.h:53
@ Write
w Open for writing. The file is created (if it does not exist) or truncated (if it exists).
Definition File.h:56
@ AppendRead
a+ Open for reading and appending. The file is created if it does not exist.
Definition File.h:55
Mode mode
Open mode (read, write, append, etc.). See FileOpen::Mode for more details.
Definition File.h:62
Read / Write pipe (Process stdin/stdout and IPC communication)
Definition File.h:195
Result createPipe(InheritableReadFlag readFlag=ReadNonInheritable, InheritableWriteFlag writeFlag=WriteNonInheritable)
Creates a Pipe.
InheritableReadFlag
Specifies a flag for read side of the pipe.
Definition File.h:198
@ ReadInheritable
Requests read side of the pipe to be inheritable from child processes.
Definition File.h:199
FileDescriptor writePipe
The write side of the pipe.
Definition File.h:210
Result close()
Closes the pipe.
FileDescriptor readPipe
The read side of the pipe.
Definition File.h:209
InheritableWriteFlag
Specifies a flag for write side of the pipe.
Definition File.h:205
@ WriteInheritable
Requests write side of the pipe to be inheritable from child processes.
Definition File.h:206
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 when parsing is not needed).
Definition StringSpan.h:31
Move only handle that has a special tag value flagging its invalid state.
Definition UniqueHandle.h:67