WPILibC++ 2023.4.3
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 <string_view>
17#include <system_error>
18
19#if defined(__APPLE__)
20#include <Availability.h>
21#endif
22#if ((defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) \
23 || (defined(__cplusplus) && __cplusplus >= 201703L)) \
24 && defined(__has_include)
25#if __has_include(<filesystem>) \
26 && (!defined(__MAC_OS_X_VERSION_MIN_REQUIRED) \
27 || __MAC_OS_X_VERSION_MIN_REQUIRED >= 101500) \
28 && (defined(__clang__) || !defined(__GNUC__) || __GNUC__ >= 10 \
29 || (__GNUC__ >= 9 && __GNUC_MINOR__ >= 1))
30#define GHC_USE_STD_FS
31#include <filesystem>
32namespace fs {
33using namespace std::filesystem;
36using fstream = std::fstream;
37} // namespace fs
38#endif
39#endif
40#ifndef GHC_USE_STD_FS
41// #define GHC_WIN_DISABLE_WSTRING_STORAGE_TYPE
42#define GHC_FILESYSTEM_FWD
44namespace fs {
45using namespace ghc::filesystem;
49} // namespace fs
50#endif
51
52namespace fs {
53
54#if defined(_WIN32)
55// A Win32 HANDLE is a typedef of void*
56using file_t = void*;
57#else
58using file_t = int;
59#endif
60
61extern const file_t kInvalidFile;
62
63enum CreationDisposition : unsigned {
64 /// CD_CreateAlways - When opening a file:
65 /// * If it already exists, truncate it.
66 /// * If it does not already exist, create a new file.
68
69 /// CD_CreateNew - When opening a file:
70 /// * If it already exists, fail.
71 /// * If it does not already exist, create a new file.
73
74 /// CD_OpenExisting - When opening a file:
75 /// * If it already exists, open the file with the offset set to 0.
76 /// * If it does not already exist, fail.
78
79 /// CD_OpenAlways - When opening a file:
80 /// * If it already exists, open the file with the offset set to 0.
81 /// * If it does not already exist, create a new file.
83};
84
85enum FileAccess : unsigned {
88};
89
90enum OpenFlags : unsigned {
92 F_None = 0, // For compatibility
93
94 /// The file should be opened in text mode on platforms that make this
95 /// distinction.
97 F_Text = 1, // For compatibility
98
99 /// The file should be opened in append mode.
101 F_Append = 2, // For compatibility
102
103 /// Delete the file on close. Only makes a difference on windows.
105
106 /// When a child process is launched, this file should remain open in the
107 /// child process.
109
110 /// Force files Atime to be updated on access. Only makes a difference on
111 /// windows.
113};
114
116 return OpenFlags(unsigned(A) | unsigned(B));
117}
118
120 A = A | B;
121 return A;
122}
123
125 return FileAccess(unsigned(A) | unsigned(B));
126}
127
129 A = A | B;
130 return A;
131}
132
133/**
134 * Opens a file with the specified creation disposition, access mode,
135 * and flags and returns a platform-specific file object.
136 *
137 * The caller is responsible for closing the file object once they are
138 * finished with it.
139 *
140 * @param Path The path of the file to open, relative or absolute.
141 * @param EC Error code output, set to non-zero on error
142 * @param Disp Value specifying the existing-file behavior
143 * @param Access Value specifying whether to open the file in read, write, or
144 * read-write mode.
145 * @param Flags Additional flags.
146 * @param Mode The access permissions of the file, represented in octal.
147 * @returns errc::success if \a Name has been opened, otherwise a
148 * platform-specific error_code.
149 */
150file_t OpenFile(const path& Path, std::error_code& EC, CreationDisposition Disp,
151 FileAccess Access, OpenFlags Flags, unsigned Mode = 0666);
152
153/**
154 * @brief Opens the file with the given name in a write-only or read-write
155 * mode, returning its open file descriptor. If the file does not exist, it
156 * is created.
157 *
158 * The caller is responsible for closing the freeing the file once they are
159 * finished with it.
160 *
161 * @param Path The path of the file to open, relative or absolute.
162 * @param EC Error code output, set to non-zero on error
163 * @param Disp Value specifying the existing-file behavior
164 * @param Flags Additional flags used to determine whether the file should be
165 * opened in, for example, read-write or in write-only mode.
166 * @param Mode The access permissions of the file, represented in octal.
167 * @returns a platform-specific file descriptor if \a Name has been opened,
168 * otherwise kInvalidFile.
169 */
170inline file_t OpenFileForWrite(const path& Path, std::error_code& EC,
171 CreationDisposition Disp, OpenFlags Flags,
172 unsigned Mode = 0666) {
173 return OpenFile(Path, EC, Disp, FA_Write, Flags, Mode);
174}
175
176/**
177 * @brief Opens the file with the given name in a write-only or read-write
178 * mode, returning its open file descriptor. If the file does not exist, it
179 * is created.
180 *
181 * The caller is responsible for closing the freeing the file once they are
182 * finished with it.
183 *
184 * @param Path The path of the file to open, relative or absolute.
185 * @param EC Error code output, set to non-zero on error
186 * @param Disp Value specifying the existing-file behavior
187 * @param Flags Additional flags used to determine whether the file should be
188 * opened in, for example, read-write or in write-only mode.
189 * @param Mode The access permissions of the file, represented in octal.
190 * @return a platform-specific file descriptor if \a Name has been opened,
191 * otherwise kInvalidFile.
192 */
193inline file_t OpenFileForReadWrite(const path& Path, std::error_code& EC,
194 CreationDisposition Disp, OpenFlags Flags,
195 unsigned Mode = 0666) {
196 return OpenFile(Path, EC, Disp, FA_Write | FA_Read, Flags, Mode);
197}
198
199/**
200 * Opens the file with the given name in a read-only mode, returning
201 * its open file descriptor.
202 *
203 * The caller is responsible for closing the freeing the file once they are
204 * finished with it.
205 *
206 * @param Path The path of the file to open, relative or absolute.
207 * @param EC Error code output, set to non-zero on error
208 * @param Flags Additional flags
209 * @return a platform-specific file descriptor if \a Name has been opened,
210 * otherwise kInvalidFile.
211 */
212file_t OpenFileForRead(const path& Path, std::error_code& EC,
213 OpenFlags Flags = OF_None);
214
215/**
216 * Converts a file object to a file descriptor. The returned file descriptor
217 * must be closed with ::close() instead of CloseFile().
218 *
219 * @param F On input, this is the file to convert to a file descriptor.
220 * On output, the file is set to kInvalidFile.
221 * @param EC Error code output, set to non-zero on error
222 * @param Flags Flags passed to the OpenFile function that created file_t
223 * @return file descriptor, or -1 on error
224 */
225int FileToFd(file_t& F, std::error_code& EC, OpenFlags Flags);
226
227/**
228 * Closes the file object.
229 *
230 * @param F On input, this is the file to close. On output, the file is
231 * set to kInvalidFile.
232 */
234
235} // namespace fs
Definition: filesystem.hpp:1219
Definition: filesystem.hpp:1173
Definition: filesystem.hpp:1196
Definition: filesystem.hpp:374
Definition: fs.h:44
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:193
OpenFlags
Definition: fs.h:90
@ F_Append
Definition: fs.h:101
@ OF_Append
The file should be opened in append mode.
Definition: fs.h:100
@ OF_ChildInherit
When a child process is launched, this file should remain open in the child process.
Definition: fs.h:108
@ OF_None
Definition: fs.h:91
@ OF_Text
The file should be opened in text mode on platforms that make this distinction.
Definition: fs.h:96
@ F_Text
Definition: fs.h:97
@ OF_Delete
Delete the file on close. Only makes a difference on windows.
Definition: fs.h:104
@ F_None
Definition: fs.h:92
@ OF_UpdateAtime
Force files Atime to be updated on access.
Definition: fs.h:112
FileAccess
Definition: fs.h:85
@ FA_Write
Definition: fs.h:87
@ FA_Read
Definition: fs.h:86
int FileToFd(file_t &F, std::error_code &EC, OpenFlags Flags)
Converts a file object to a file descriptor.
ghc::filesystem::ofstream ofstream
Definition: fs.h:47
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:170
CreationDisposition
Definition: fs.h:63
@ CD_OpenExisting
CD_OpenExisting - When opening a file:
Definition: fs.h:77
@ CD_CreateNew
CD_CreateNew - When opening a file:
Definition: fs.h:72
@ CD_OpenAlways
CD_OpenAlways - When opening a file:
Definition: fs.h:82
@ CD_CreateAlways
CD_CreateAlways - When opening a file:
Definition: fs.h:67
int file_t
Definition: fs.h:58
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:115
const file_t kInvalidFile
ghc::filesystem::fstream fstream
Definition: fs.h:48
ghc::filesystem::ifstream ifstream
Definition: fs.h:46
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:119
Definition: filesystem.hpp:314
basic_ofstream< char > ofstream
Definition: filesystem.hpp:1244
basic_fstream< char > fstream
Definition: filesystem.hpp:1246
basic_ifstream< char > ifstream
Definition: filesystem.hpp:1242
static constexpr const unit_t< compound_unit< charge::coulomb, inverse< substance::mol > > > F(N_A *e)
Faraday constant.