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