Sane C++ Libraries
C++ Platform Abstraction Libraries
File.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Strings/StringView.h"
5#include "FileDescriptor.h"
6
7namespace SC
8{
9struct String;
10template <typename T>
11struct Vector;
12} // namespace SC
15
20{
22
23 File(FileDescriptor& descriptor) : fd(descriptor) {}
24
27 {
32 };
33
36 {
37 bool inheritable = false;
38 bool blocking = true;
39 };
40
45 [[nodiscard]] Result open(StringView path, OpenMode mode);
46
52 [[nodiscard]] Result open(StringView path, OpenMode mode, OpenOptions options);
53
58 [[nodiscard]] Result readUntilEOF(Vector<char>& destination);
59
64 [[nodiscard]] Result readUntilEOF(Vector<uint8_t>& destination);
65
70 [[nodiscard]] Result readUntilEOF(String& destination);
71
72 private:
73 struct Internal;
74 struct ReadResult;
75 template <typename T>
76 Result readUntilEOFTemplate(Vector<T>& destination);
77};
78
Additional flags to be set when opening files.
Definition: File.h:36
bool blocking
Set to false if file will be used for Async I/O (see Async)
Definition: File.h:38
bool inheritable
Set to true to make the file visible to child processes.
Definition: File.h:37
File Descriptor (use SC::File to open and use it with strings and containers).
Definition: FileDescriptor.h:52
Wraps a SC::FileDescriptor to open it and use strings / containers.
Definition: File.h:20
Result readUntilEOF(String &destination)
Reads into a given string until End of File (EOF) is signaled It works also for non-seekable file des...
Result open(StringView path, OpenMode mode, OpenOptions options)
Opens file at path with a given mode
OpenMode
Define mode for opening the file (read, write etc.)
Definition: File.h:27
@ ReadAndWrite
Opens file for read / write mode.
Definition: File.h:31
@ WriteAppend
Opens write mode, appending to existing file that must exist at the same location.
Definition: File.h:30
@ WriteCreateTruncate
Opens in write mode, creating or truncating it if another file exists at same location.
Definition: File.h:29
@ ReadOnly
Opens in read-only mode.
Definition: File.h:28
Result readUntilEOF(Vector< uint8_t > &destination)
Reads into a given dynamic buffer until End of File (EOF) is signaled.
Result open(StringView path, OpenMode mode)
Opens file at path with a given mode
Result readUntilEOF(Vector< char > &destination)
Reads into a given dynamic buffer until End of File (EOF) is signaled.
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition: Result.h:12
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
A contiguous sequence of heap allocated elements.
Definition: Vector.h:51