Documentation for OpenFLUID 2.2.2
Loading...
Searching...
No Matches
Filesystem.hpp
Go to the documentation of this file.
1/*
2
3 This file is part of OpenFLUID software
4 Copyright(c) 2021-2026, INRAE
5
6
7 == GNU General Public License Usage ==
8
9 OpenFLUID is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 OpenFLUID is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
21
22
23 == Other Usage ==
24
25 Other Usage means a use of OpenFLUID that is inconsistent with the GPL
26 license, and requires a written agreement between You and INRA.
27 Licensees for Other Usage of OpenFLUID may use this file in accordance
28 with the terms contained in the written agreement between You and INRA.
29
30*/
31
32
33/**
34 @file Filesystem.hpp
35
36 @author Jean-Christophe FABRE <jean-christophe.fabre@inra.fr>
37*/
38
39
40#ifndef __OPENFLUID_TOOLS_FILESYSTEM_HPP__
41#define __OPENFLUID_TOOLS_FILESYSTEM_HPP__
42
43
44#include <string>
45#include <vector>
46
49
50
51namespace openfluid { namespace tools {
52
53
54// TODO Refactor for consistent usage of FilesystemPath and std::string
55
56
58{
59
60 public:
61
62 Filesystem() = delete;
63
64 /**
65 @deprecated Since version 2.2.0. Use openfluid::tools::FilesystemPath::listSeparator() instead
66 */
67 [[deprecated]] constexpr static inline char pathsListSeparator() noexcept
68 {
69#if defined(OPENFLUID_OS_UNIX)
70 return ':';
71#elif defined(OPENFLUID_OS_WINDOWS)
72 return ';';
73#endif
74 }
75
76 /**
77 @deprecated Since version 2.2.0. Use openfluid::tools::FilesystemPath::separator() instead
78 */
79 [[deprecated]] constexpr static inline char pathSeparator() noexcept
80 {
81#if defined(OPENFLUID_OS_UNIX)
82 return '/';
83#elif defined(OPENFLUID_OS_WINDOWS)
84 return '\\';
85#endif
86 }
87
88 /**
89 Returns a joined path string from a vector of path parts, using the '/' separator
90 @param[in] PathParts a vector of path parts
91 @return the joined path
92 @snippet misc/filesystem.cpp joinpath
93 */
94 static std::string joinPath(const std::vector<std::string>& PathParts);
95
96 /**
97 Returns the user home path (e.g. '/home/username' on Linux, 'C:/Users/username' on Windows, ...)
98 @return the user home path
99 */
100 static std::string homePath();
101
102 /**
103 Returns the system path for temporary files (e.g. '/tmp' on Linux, ...)
104 @return the path for temporary files
105 */
106 static std::string tempPath();
107
108 /**
109 @deprecated Since version 2.2.0. Use openfluid::tools::FilesystemPath::filename() instead
110 */
111 [[deprecated]] static std::string filename(const std::string& Path);
112
113 /**
114 @deprecated Since version 2.2.0. Use openfluid::tools::FilesystemPath::dirname() instead
115 */
116 [[deprecated]] static std::string dirname(const std::string& Path);
117
118 /**
119 @deprecated Since version 2.2.0. Use openfluid::tools::FilesystemPath::basename() instead
120 */
121 [[deprecated]] static std::string basename(const std::string& Path);
122
123 /**
124 @deprecated Since version 2.2.0. Use openfluid::tools::FilesystemPath::minimalBasename() instead
125 */
126 [[deprecated]] static std::string minimalBasename(const std::string& Path);
127
128 /**
129 @deprecated Since version 2.2.0. Use openfluid::tools::FilesystemPath::extension() instead
130 */
131 [[deprecated]] static std::string extension(const std::string& Path);
132
133 /**
134 @deprecated Since version 2.2.0. Use openfluid::tools::FilesystemPath::completeExtension() instead
135 */
136 [[deprecated]] static std::string completeExtension(const std::string& Path);
137
138 /**
139 Returns the current path
140 @return the current path
141 */
142 static std::string currentPath();
143
144 /**
145 Returns the absolute path of the given path.
146 If the given path is already an absolute path, it is returned as-is.
147 @param[in] Path the path to make absolute
148 @return the absolute path
149 */
150 static std::string absolutePath(const std::string& Path);
151
152 /**
153 Creates a unique subdirectory in the given path, using the given subdirectory name as a prefix.
154 If the subdirectory already exists, it adds an incremental suffix to the subdirectory name.
155 It creates all parent directories necessary to create the subdirectory.
156 @param[in] Path the given path
157 @param[in] SubdirName the given path
158 @return the full path of the created unique subdirectory
159 */
160 static std::string makeUniqueSubdirectory(const std::string& Path, const std::string& SubdirName);
161
162 /**
163 Creates a unique file in the given path, using the given File name and extension as prefix and suffix.
164 If the file already exists, it adds an incremental part to the file name.
165 If the Path does not exists, it creates all needed parent directories.
166 @param[in] Path the given path for the file
167 @param[in] FileName the file name used to make a unique one
168 @return the full path of the created unique file
169 */
170 static std::string makeUniqueFile(const std::string& Path, const std::string& FileName);
171
172 /**
173 Reads the content of a file
174 @param[in] FileObj the given path for the file
175 @return the content of the file
176 */
177 static std::string readFile(const openfluid::tools::FilesystemPath& FileObj);
178
179 /**
180 Writes content into a file
181 @param[in] Content the content to write
182 @param[in] FileObj the given path for the file
183 */
184 static void writeFile(const std::string& Content, const openfluid::tools::FilesystemPath& FileObj);
185
186 /**
187 Appends content on a new line into a file
188 @param[in] Content the content to append
189 @param[in] FileObj the given path for the file
190 */
191 static void appendToFile(const std::string& Content, const openfluid::tools::FilesystemPath& FileObj);
192
193 /**
194 Copies a file from source to destination.
195 @param[in] SrcPath the source path
196 @param[in] DestPath the destination path
197 @return true if the file was successfully copied, false otherwise
198 */
199 static bool copyFile(const std::string& SrcPath, const std::string& DestPath);
200
201 /**
202 Rename a file.
203 @param[in] OriginalPath the original file path
204 @param[in] NewPath the new file path
205 @return true if the file was successfully renamed, false otherwise
206 */
207 static bool renameFile(const std::string& OriginalPath, const std::string& NewPath);
208
209 /**
210 Recursively copies a directory from source to destination.
211 @param[in] SrcPath the source path
212 @param[in] DestPath the destination path
213 @param[in] WithBaseDir if true, the source directory is created in the destination dir
214 as an intermdiate directory (e.g. if the content of /my/path/first is copied
215 into /my/path/second, the content will actually be copied into /my/path/second/first)
216 @param[in] RemoveExisting if true, the destination directory is deleted if it already exists
217 @return true if the directory was successfully copied, false otherwise
218 */
219 static bool copyDirectory(const std::string& SrcPath, const std::string& DestPath,
220 bool WithBaseDir = false, bool RemoveExisting = false);
221
222
223 /**
224 Recursively copies content of a directory from source to destination, handling windows bug with regular dir copy.
225 @param[in] SrcPath the source path
226 @param[in] DestPath the destination path
227 @return true if the directory was successfully copied, false otherwise
228 */
229 static bool copyDirectoryContent(const std::filesystem::path& SrcPath, const std::filesystem::path& DestPath);
230
231 /**
232 Recursively removes all files and directories contained in the given directory.
233 It deletes the directory and recreates it.
234 @param[in] Path the directory to empty
235 @param[in] PathsToExlude Paths that are not deleted.
236 @return true if successful
237 */
238 static bool emptyDirectory(const std::string& Path, const std::vector<std::string>& PathsToExlude = {});
239
240 /**
241 Gets the list of files found in the specified directory
242 @param[in] Path the directory to explore
243 @param[in] WithPath return full path if true, directory name only otherwise
244 @param[in] Pattern an optional pattern to filter the files to find
245 */
246 static std::vector<std::string> findFiles(const std::string& Path,
247 bool WithPath = false, const std::string& Pattern = "");
248
249 /**
250 Gets the list of directories found in the specified directory
251 @param[in] Path the directory to explore
252 @param[in] WithPath return full path if true, directory name only otherwise
253 @param[in] Pattern an optional pattern to filter the directories to find
254 */
255 static std::vector<std::string> findDirectories(const std::string& Path,
256 bool WithPath = false, const std::string& Pattern = "");
257
258 /**
259 Gets the list of files with specified extension contained in the specified directory
260 @param[in] Path the directory to explore
261 @param[in] Ext the file extension
262 @param[in] WithPath return full path with file name if true, file name only otherwise
263 @param[in] ExtIncludeDot if true, the given extension through Ext parameter is suffixed by a dot
264 */
265 static std::vector<std::string> findFilesByExtension(const std::string& Path,
266 const std::string& Ext,
267 bool WithPath = false,
268 bool ExtIncludeDot = false);
269
270 /**
271 Get list of files with specified extension contained in the specified dir
272 @param[in] Path the directory to explore
273 @param[in] Ext the file extension
274 @param[in] Suffix the file suffix
275 @param[in] WithPath return full path with file name if true, file name only otherwise
276 @param[in] ExtIncludeDot if true, the given extension through Ext parameter is suffixed by a dot
277 */
278 static std::vector<std::string> findFilesBySuffixAndExtension(const std::string& Path,
279 const std::string& Suffix,
280 const std::string& Ext,
281 bool WithPath = false,
282 bool ExtIncludeDot = false);
283
284};
285
286} } // namespaces
287
288
289#endif /* __OPENFLUID_TOOLS_FILESYSTEM_HPP__ */
Definition FilesystemPath.hpp:62
Definition Filesystem.hpp:58
static void writeFile(const std::string &Content, const openfluid::tools::FilesystemPath &FileObj)
static std::string makeUniqueFile(const std::string &Path, const std::string &FileName)
static std::string extension(const std::string &Path)
static std::vector< std::string > findFilesBySuffixAndExtension(const std::string &Path, const std::string &Suffix, const std::string &Ext, bool WithPath=false, bool ExtIncludeDot=false)
static std::string completeExtension(const std::string &Path)
static std::string minimalBasename(const std::string &Path)
static void appendToFile(const std::string &Content, const openfluid::tools::FilesystemPath &FileObj)
static std::string absolutePath(const std::string &Path)
static bool copyDirectoryContent(const std::filesystem::path &SrcPath, const std::filesystem::path &DestPath)
static bool copyDirectory(const std::string &SrcPath, const std::string &DestPath, bool WithBaseDir=false, bool RemoveExisting=false)
static std::string joinPath(const std::vector< std::string > &PathParts)
static std::string basename(const std::string &Path)
static std::string makeUniqueSubdirectory(const std::string &Path, const std::string &SubdirName)
static std::vector< std::string > findDirectories(const std::string &Path, bool WithPath=false, const std::string &Pattern="")
static bool renameFile(const std::string &OriginalPath, const std::string &NewPath)
static std::string tempPath()
static std::string dirname(const std::string &Path)
static std::string readFile(const openfluid::tools::FilesystemPath &FileObj)
static constexpr char pathsListSeparator() noexcept
Definition Filesystem.hpp:67
static std::vector< std::string > findFilesByExtension(const std::string &Path, const std::string &Ext, bool WithPath=false, bool ExtIncludeDot=false)
static constexpr char pathSeparator() noexcept
Definition Filesystem.hpp:79
static bool copyFile(const std::string &SrcPath, const std::string &DestPath)
static std::vector< std::string > findFiles(const std::string &Path, bool WithPath=false, const std::string &Pattern="")
static std::string currentPath()
static std::string homePath()
static bool emptyDirectory(const std::string &Path, const std::vector< std::string > &PathsToExlude={})
static std::string filename(const std::string &Path)
#define OPENFLUID_API
Definition dllexport.hpp:86
Definition ApplicationException.hpp:47