WPILibC++ 2023.4.3-108-ge5452e3
fs.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5//===----------------------------------------------------------------------===//
6//
7// The LLVM Compiler Infrastructure
8//
9// This file is distributed under the University of Illinois Open Source
10// License. See LICENSE.TXT for details.
11//
12//===----------------------------------------------------------------------===//
13
14#pragma once
15
16#include <filesystem>
17#include <fstream>
18#include <string_view>
19#include <system_error>
20
21namespace fs {
22
23using namespace std::filesystem;
27
28#if defined(_WIN32)
29// A Win32 HANDLE is a typedef of void*
30using file_t = void*;
31#else
32using file_t = int;
33#endif
34
35extern const file_t kInvalidFile;
36
37enum CreationDisposition : unsigned {
38 /// CD_CreateAlways - When opening a file:
39 /// * If it already exists, truncate it.
40 /// * If it does not already exist, create a new file.
42
43 /// CD_CreateNew - When opening a file:
44 /// * If it already exists, fail.
45 /// * If it does not already exist, create a new file.
47
48 /// CD_OpenExisting - When opening a file:
49 /// * If it already exists, open the file with the offset set to 0.
50 /// * If it does not already exist, fail.
52
53 /// CD_OpenAlways - When opening a file:
54 /// * If it already exists, open the file with the offset set to 0.
55 /// * If it does not already exist, create a new file.
57};
58
59enum FileAccess : unsigned {
62};
63
64enum OpenFlags : unsigned {
66 F_None = 0, // For compatibility
67
68 /// The file should be opened in text mode on platforms that make this
69 /// distinction.
71 F_Text = 1, // For compatibility
72
73 /// The file should be opened in append mode.
75 F_Append = 2, // For compatibility
76
77 /// Delete the file on close. Only makes a difference on windows.
79
80 /// When a child process is launched, this file should remain open in the
81 /// child process.
83
84 /// Force files Atime to be updated on access. Only makes a difference on
85 /// windows.
87};
88
90 return OpenFlags(unsigned(A) | unsigned(B));
91}
92
94 A = A | B;
95 return A;
96}
97
99 return FileAccess(unsigned(A) | unsigned(B));
100}
101
103 A = A | B;
104 return A;
105}
106
107/**
108 * Opens a file with the specified creation disposition, access mode,
109 * and flags and returns a platform-specific file object.
110 *
111 * The caller is responsible for closing the file object once they are
112 * finished with it.
113 *
114 * @param Path The path of the file to open, relative or absolute.
115 * @param EC Error code output, set to non-zero on error
116 * @param Disp Value specifying the existing-file behavior
117 * @param Access Value specifying whether to open the file in read, write, or
118 * read-write mode.
119 * @param Flags Additional flags.
120 * @param Mode The access permissions of the file, represented in octal.
121 * @returns errc::success if \a Name has been opened, otherwise a
122 * platform-specific error_code.
123 */
124file_t OpenFile(const path& Path, std::error_code& EC, CreationDisposition Disp,
125 FileAccess Access, OpenFlags Flags, unsigned Mode = 0666);
126
127/**
128 * @brief Opens the file with the given name in a write-only or read-write
129 * mode, returning its open file descriptor. If the file does not exist, it
130 * is created.
131 *
132 * The caller is responsible for closing the freeing the file once they are
133 * finished with it.
134 *
135 * @param Path The path of the file to open, relative or absolute.
136 * @param EC Error code output, set to non-zero on error
137 * @param Disp Value specifying the existing-file behavior
138 * @param Flags Additional flags used to determine whether the file should be
139 * opened in, for example, read-write or in write-only mode.
140 * @param Mode The access permissions of the file, represented in octal.
141 * @returns a platform-specific file descriptor if \a Name has been opened,
142 * otherwise kInvalidFile.
143 */
144inline file_t OpenFileForWrite(const path& Path, std::error_code& EC,
145 CreationDisposition Disp, OpenFlags Flags,
146 unsigned Mode = 0666) {
147 return OpenFile(Path, EC, Disp, FA_Write, Flags, Mode);
148}
149
150/**
151 * @brief Opens the file with the given name in a write-only or read-write
152 * mode, returning its open file descriptor. If the file does not exist, it
153 * is created.
154 *
155 * The caller is responsible for closing the freeing the file once they are
156 * finished with it.
157 *
158 * @param Path The path of the file to open, relative or absolute.
159 * @param EC Error code output, set to non-zero on error
160 * @param Disp Value specifying the existing-file behavior
161 * @param Flags Additional flags used to determine whether the file should be
162 * opened in, for example, read-write or in write-only mode.
163 * @param Mode The access permissions of the file, represented in octal.
164 * @return a platform-specific file descriptor if \a Name has been opened,
165 * otherwise kInvalidFile.
166 */
167inline file_t OpenFileForReadWrite(const path& Path, std::error_code& EC,
168 CreationDisposition Disp, OpenFlags Flags,
169 unsigned Mode = 0666) {
170 return OpenFile(Path, EC, Disp, FA_Write | FA_Read, Flags, Mode);
171}
172
173/**
174 * Opens the file with the given name in a read-only mode, returning
175 * its open file descriptor.
176 *
177 * The caller is responsible for closing the freeing the file once they are
178 * finished with it.
179 *
180 * @param Path The path of the file to open, relative or absolute.
181 * @param EC Error code output, set to non-zero on error
182 * @param Flags Additional flags
183 * @return a platform-specific file descriptor if \a Name has been opened,
184 * otherwise kInvalidFile.
185 */
186file_t OpenFileForRead(const path& Path, std::error_code& EC,
187 OpenFlags Flags = OF_None);
188
189/**
190 * Converts a file object to a file descriptor. The returned file descriptor
191 * must be closed with ::close() instead of CloseFile().
192 *
193 * @param F On input, this is the file to convert to a file descriptor.
194 * On output, the file is set to kInvalidFile.
195 * @param EC Error code output, set to non-zero on error
196 * @param Flags Flags passed to the OpenFile function that created file_t
197 * @return file descriptor, or -1 on error
198 */
199int FileToFd(file_t& F, std::error_code& EC, OpenFlags Flags);
200
201/**
202 * Closes the file object.
203 *
204 * @param F On input, this is the file to close. On output, the file is
205 * set to kInvalidFile.
206 */
208
209} // namespace fs
Definition: MappedFileRegion.h:12
file_t OpenFileForReadWrite(const path &Path, std::error_code &EC, CreationDisposition Disp, OpenFlags Flags, unsigned Mode=0666)
Opens the file with the given name in a write-only or read-write mode, returning its open file descri...
Definition: fs.h:167
OpenFlags
Definition: fs.h:64
@ F_Append
Definition: fs.h:75
@ OF_Append
The file should be opened in append mode.
Definition: fs.h:74
@ OF_ChildInherit
When a child process is launched, this file should remain open in the child process.
Definition: fs.h:82
@ OF_None
Definition: fs.h:65
@ OF_Text
The file should be opened in text mode on platforms that make this distinction.
Definition: fs.h:70
@ F_Text
Definition: fs.h:71
@ OF_Delete
Delete the file on close. Only makes a difference on windows.
Definition: fs.h:78
@ F_None
Definition: fs.h:66
@ OF_UpdateAtime
Force files Atime to be updated on access.
Definition: fs.h:86
FileAccess
Definition: fs.h:59
@ FA_Write
Definition: fs.h:61
@ FA_Read
Definition: fs.h:60
int FileToFd(file_t &F, std::error_code &EC, OpenFlags Flags)
Converts a file object to a file descriptor.
file_t OpenFileForWrite(const path &Path, std::error_code &EC, CreationDisposition Disp, OpenFlags Flags, unsigned Mode=0666)
Opens the file with the given name in a write-only or read-write mode, returning its open file descri...
Definition: fs.h:144
CreationDisposition
Definition: fs.h:37
@ CD_OpenExisting
CD_OpenExisting - When opening a file:
Definition: fs.h:51
@ CD_CreateNew
CD_CreateNew - When opening a file:
Definition: fs.h:46
@ CD_OpenAlways
CD_OpenAlways - When opening a file:
Definition: fs.h:56
@ CD_CreateAlways
CD_CreateAlways - When opening a file:
Definition: fs.h:41
int file_t
Definition: MappedFileRegion.h:17
std::ifstream ifstream
Definition: fs.h:24
std::fstream fstream
Definition: fs.h:26
std::ofstream ofstream
Definition: fs.h:25
void CloseFile(file_t &F)
Closes the file object.
file_t OpenFileForRead(const path &Path, std::error_code &EC, OpenFlags Flags=OF_None)
Opens the file with the given name in a read-only mode, returning its open file descriptor.
OpenFlags operator|(OpenFlags A, OpenFlags B)
Definition: fs.h:89
const file_t kInvalidFile
file_t OpenFile(const path &Path, std::error_code &EC, CreationDisposition Disp, FileAccess Access, OpenFlags Flags, unsigned Mode=0666)
Opens a file with the specified creation disposition, access mode, and flags and returns a platform-s...
OpenFlags & operator|=(OpenFlags &A, OpenFlags B)
Definition: fs.h:93
static constexpr const unit_t< compound_unit< charge::coulomb, inverse< substance::mol > > > F(N_A *e)
Faraday constant.