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
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
31
33struct SC_COMPILER_EXPORT FileDescriptorDefinition
34{
35 using Handle = int; // fd
36 static Result releaseHandle(Handle& handle);
37
38 static constexpr Handle Invalid = -1; // invalid fd
39};
41#endif
42} // namespace detail
43
46
48struct SC_COMPILER_EXPORT FileOpen
49{
60
61 FileOpen(Mode mode = Read) : mode(mode) {}
62
64 bool inheritable = false;
65 bool blocking = true;
66 bool sync = false;
67 bool exclusive = false;
68
69#if !SC_PLATFORM_WINDOWS
70 int toPosixFlags() const;
71 int toPosixAccess() const;
72#endif
73};
75
77struct SC_COMPILER_EXPORT FileDescriptor : public UniqueHandle<detail::FileDescriptorDefinition>
78{
79 using UniqueHandle::UniqueHandle;
80
83
87
93
97 Result setBlocking(bool blocking);
98
102 Result setInheritable(bool inheritable);
103
107 Result isInheritable(bool& hasValue) const;
108
114 Result read(Span<char> data, Span<char>& actuallyRead, uint64_t offset);
115
121 Result read(Span<uint8_t> data, Span<uint8_t>& actuallyRead, uint64_t offset);
122
127 Result read(Span<char> data, Span<char>& actuallyRead);
128
134
140 template <typename T>
141 Result readUntilEOF(T& destination)
142 {
143 return readUntilEOF(GrowableBuffer<T>{destination});
144 }
145
150 Result readUntilEOF(IGrowableBuffer&& buffer);
151
157
163
168
173
181
186 Result seek(SeekMode seekMode, uint64_t offset);
187
191 Result currentPosition(size_t& position) const;
192
196 Result sizeInBytes(size_t& sizeInBytes) const;
197
198 private:
199 friend struct File;
200 struct Internal;
201};
202
204struct SC_COMPILER_EXPORT PipeDescriptor
205{
208 {
210 ReadNonInheritable
211 };
212
215 {
217 WriteNonInheritable
218 };
221
226 Result createPipe(InheritableReadFlag readFlag = ReadNonInheritable,
227 InheritableWriteFlag writeFlag = WriteNonInheritable);
228
232};
234} // namespace SC
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
[UniqueHandleDeclaration2Snippet]
Definition File.h:78
SeekMode
How the offset to FileDescriptor::seek is defined.
Definition File.h:176
@ SeekCurrent
Offset to FileDescriptor::seek is to be applied from current descriptor position.
Definition File.h:179
@ SeekEnd
Offset to FileDescriptor::seek is to be applied (backwards) from end of descriptor.
Definition File.h:178
@ SeekStart
Offset to FileDescriptor::seek is to be applied from start of descriptor.
Definition File.h:177
Result read(Span< uint8_t > data, Span< uint8_t > &actuallyRead)
Reads bytes from current position (FileDescriptor::seek) into user supplied Span.
Result openForWriteToDevNull()
... [UniqueHandleDeclaration2Snippet]
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:141
Result readUntilEOF(IGrowableBuffer &&buffer)
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)
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:49
Mode
Indicates the mode in which the file should be opened (read, write, append, etc.)
Definition File.h:52
@ Append
a Open for appending. The file is created if it does not exist.
Definition File.h:55
@ WriteRead
w+ Open for reading and writing. The file is created (if it does not exist) or truncated.
Definition File.h:58
@ ReadWrite
r+ Open for reading and writing. An error occurs if the file does not exist.
Definition File.h:54
@ Write
w Open for writing. The file is created (if it does not exist) or truncated (if it exists).
Definition File.h:57
@ AppendRead
a+ Open for reading and appending. The file is created if it does not exist.
Definition File.h:56
Mode mode
Open mode (read, write, append, etc.). See FileOpen::Mode for more details.
Definition File.h:63
Read / Write pipe (Process stdin/stdout and IPC communication)
Definition File.h:205
Result createPipe(InheritableReadFlag readFlag=ReadNonInheritable, InheritableWriteFlag writeFlag=WriteNonInheritable)
Creates a Pipe.
InheritableReadFlag
Specifies a flag for read side of the pipe.
Definition File.h:208
@ ReadInheritable
Requests read side of the pipe to be inheritable from child processes.
Definition File.h:209
FileDescriptor writePipe
The write side of the pipe.
Definition File.h:220
Result close()
Closes the pipe.
FileDescriptor readPipe
The read side of the pipe.
Definition File.h:219
InheritableWriteFlag
Specifies a flag for write side of the pipe.
Definition File.h:215
@ WriteInheritable
Requests write side of the pipe to be inheritable from child processes.
Definition File.h:216
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:37
Move only handle that has a special tag value flagging its invalid state.
Definition UniqueHandle.h:27