Documentation for OpenFLUID 2.2.0
FilesystemPath.hpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of OpenFLUID software
4  Copyright(c) 2007, INRA - Montpellier SupAgro
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 FilesystemPath.hpp
35 
36  @author Jean-Christophe FABRE <jean-christophe.fabre@inra.fr>
37  @author Armel THÖNI <armel.thoni@inrae.fr>
38 */
39 
40 
41 #ifndef __OPENFLUID_TOOLS_FILESYSTEMPATH_HPP__
42 #define __OPENFLUID_TOOLS_FILESYSTEMPATH_HPP__
43 
44 
45 #include <string>
46 #include <vector>
47 #include <filesystem>
48 
49 
51 #include <openfluid/global.hpp>
52 #include <openfluid/dllexport.hpp>
53 
54 
55 namespace openfluid { namespace tools {
56 
57 
58 static std::error_code DefaultErrorCode = std::error_code();
59 
60 
62 {
63  private:
64 
65  std::filesystem::path m_Path;
66 
67 
68  public:
69 
70  /**
71  Returns the separator used in paths lists according to current operating system
72  @return the list separator character
73  */
74  constexpr static inline char listSeparator() noexcept
75  {
76 #if defined(OPENFLUID_OS_UNIX)
77  return ':';
78 #elif defined(OPENFLUID_OS_WINDOWS)
79  return ';';
80 #endif
81  }
82 
83  /**
84  Returns the path separator according to current operating system
85  @return the path separator character
86  */
87  constexpr static inline char separator() noexcept
88  {
89 #if defined(OPENFLUID_OS_UNIX)
90  return '/';
91 #elif defined(OPENFLUID_OS_WINDOWS)
92  return '\\';
93 #endif
94  }
95 
97 
98  /**
99  Constructs a FilesystemPath object using a string
100  @param[in] PathStr the path as a string
101  */
102  FilesystemPath(const std::string& PathStr);
103 
104  /**
105  Constructs a FilesystemPath object using a C-style string
106  @param[in] PathStr the path as a char*
107  */
108  FilesystemPath(const char* PathStr);
109 
110  /**
111  Constructs a FilesystemPath object using a vector of strings that will be joined using the path separator
112  @param[in] PathParts the path as a string vector
113  */
114  FilesystemPath(const std::vector<std::string>& PathParts);
115 
116  /**
117  Creates a FilesystemPath object from a std::filesystem::path object
118  @param[in] StdPath the std::filesystem::path object
119  @return The newly created FilesystemPath object
120  */
121  static FilesystemPath fromStdPath(const std::filesystem::path& StdPath);
122 
123  /**
124  Returns a const reference to the internally stored path (std::filesystem::path)
125  */
126  const std::filesystem::path& stdPath() const;
127 
128  /**
129  Creates a new FilesystemPath object from the current FilesystemPath object.
130  @param[in] PathStr a relative path added to the to the current stored path (optional, none by default)
131  @return The newly created FilesystemPath object
132  */
133  FilesystemPath fromThis(const std::string& PathStr = "") const;
134 
135  /**
136  Creates a new FilesystemPath object from the current FilesystemPath object.
137  @param[in] PathParts a relative joined path added to the to the current stored path (optional, none by default)
138  @return The newly created FilesystemPath object
139  */
140  FilesystemPath fromThis(const std::vector<std::string>& PathParts) const;
141 
142  /**
143  Returns true if the current strored path is empty
144  @return true if the current strored path is empty, false otherwise
145  */
146  bool empty() const;
147 
148  /**
149  Removes trailing separators in the given path, if any.
150  @param[in] Path the path
151  @param[in] Sep the separator character (default is '/')
152  @return the path without any trailing separator
153  */
154  static std::string removeTrailingSeparators(const std::string& Path, char Sep = '/') noexcept;
155 
156  /**
157  Returns the path as a path with native paths separators
158  (e.g. '/' on Unix systems, '\' on Windows systems)
159  @return the cleaned path with native separators
160  */
161  std::string toNative() const;
162 
163  /**
164  Returns the path as a path with generic paths separators (always '/')
165  @return the cleaned path with generic separators
166  */
167  std::string toGeneric() const;
168 
169  /**
170  Returns the path as a cleaned path with generic paths separators (always '/')
171  @return the cleaned path with generic separators
172  */
173  std::string toGenericClean() const;
174 
175  /**
176  Returns parts of the path, splitted using the path separator
177  @return the path parts
178  */
179  std::vector<std::string> split() const;
180 
181  /**
182  Returns the filename of the path
183  @snippet misc/filesystempath.cpp filename
184  @return the filename
185  */
186  std::string filename() const;
187 
188  /**
189  Returns the directory name (parent path)
190  @snippet misc/filesystempath.cpp dirname
191  @return the directory name
192  */
193  std::string dirname() const;
194 
195 
196  /**
197  Returns the complete base name of the path
198  (without latest extension part, without trailing dot if any)
199  @snippet misc/filesystempath.cpp basename
200  @return the base name of the file
201  */
202  std::string basename() const;
203 
204  /**
205  Returns the minimal base name of the path
206  (e.g. without any extension part, without trailing dot if any)
207  @snippet misc/filesystempath.cpp minimalbasename
208  @return the minimal base name of the file
209  */
210  std::string minimalBasename() const;
211 
212  /**
213  Returns the extension (without dot character) of the path
214  @snippet misc/filesystempath.cpp extension
215  @return the extension
216  */
217  std::string extension() const;
218 
219  /**
220  Returns the complete extension (without first dot character) of the path
221  @snippet misc/filesystempath.cpp completeextension
222  @return the complete extension
223  */
224  std::string completeExtension() const;
225 
226  /**
227  Returns true if a the path contains a child path (e.g. /path/to contains /path/to/child/path)
228  @param[in] Path the child path
229  @return true or false
230  */
231  bool contains(const std::string& Path) const;
232 
233  /**
234  Returns the resulting path relative to a given path
235  (e.g with path /path/to/this/place relatively to given path /path/to, resulting path is this/place)
236  @param[in] Path the given path
237  @return the resulting relative path
238  */
239  std::string relativeTo(const std::string& Path) const;
240 
241  /**
242  Returns true if the path is a directory
243  @param[in] Path a relative path added to the to the base path (optional, none by default)
244  @return true or false
245  */
246  bool isDirectory(const std::string& Path = "") const;
247 
248  /**
249  Returns true if the path is a file or a valid symbolic link
250  @param[in] Path a relative path added to the to the base path (optional, none by default)
251  @return true or false
252  */
253  bool isFile(const std::string& Path = "") const;
254 
255  /**
256  Returns true if the path exists (a file, a directory or a valid symbolic link)
257  @param[in] Path a relative path added to the to the base path (optional, none by default)
258  @return true or false
259  */
260  bool exists(const std::string& Path = "") const;
261 
262  /**
263  Creates the directory of the path. It creates all parent directories necessary to create the directory.
264  If the directory already exists, it does nothing.
265  @param[in] Path a relative path added to the to the base path (optional, none by default)
266  @param[in] ErrorCode the storage variable for eventual Error code returned by operation
267  @return true if the directory has been successfully created or if it already exists, false otherwise
268  */
269  bool makeDirectory(const std::string& Path = "", std::error_code& ErrorCode=DefaultErrorCode) const;
270 
271  /**
272  Removes the directory of the path. It recursively deletes all contents of the directory.
273  @param[in] Path a relative path added to the to the base path (optional, none by default)
274  @param[in] ErrorCode the storage variable for eventual Error code returned by operation
275  @return true if the directory has been successfully deleted, false otherwise
276  */
277  bool removeDirectory(const std::string& Path = "", std::error_code& ErrorCode=DefaultErrorCode) const;
278 
279  /**
280  Creates an empty file at the path. It creates all parent directories necessary to create the file.
281  If the file already exists, it does nothing.
282  @param[in] Path a relative path added to the to the base path (optional, none by default)
283  @return true if the file has been successfully created or if it already exists, false otherwise
284  */
285  bool makeFile(const std::string& Path = "") const;
286 
287  /**
288  Removes the file of the path.
289  @param[in] Path a relative path added to the to the base path (optional, none by default)
290  @param[in] ErrorCode the storage variable for eventual Error code returned by operation
291  @return true if the file was successfully deleted, false otherwise
292  */
293  bool removeFile(const std::string& Path = "", std::error_code& ErrorCode=DefaultErrorCode) const;
294 
295  /**
296  Removes the file or directory of the path.
297  @param[in] Path a relative path added to the to the base path (optional, none by default)
298  @param[in] ErrorCode the storage variable for eventual Error code returned by operation
299  @return true if the file was successfully deleted, false otherwise
300  */
301  bool remove(const std::string& Path = "", std::error_code& ErrorCode=DefaultErrorCode) const;
302 };
303 
304 /**
305  Alias for the openfluid::tools::FilesystemPath class
306  @see openfluid::tools::FilesystemPath
307 */
309 
310 
311 } } // namespaces
312 
313 
314 #endif /* __OPENFLUID_TOOLS_FILESYSTEMPATH_HPP__ */
#define CPPCLASS_DEFAULT_FIVE(classname)
Definition: CppLangHelpers.hpp:57
Definition: FilesystemPath.hpp:62
constexpr static char listSeparator() noexcept
Definition: FilesystemPath.hpp:74
constexpr static char separator() noexcept
Definition: FilesystemPath.hpp:87
#define OPENFLUID_API
Definition: dllexport.hpp:86
bool OPENFLUID_API contains(const std::string &Str, const std::string &SubStr, bool CaseSensitive=true)
std::vector< std::string > OPENFLUID_API split(const std::string &Str, const char Sep, bool KeepEmpty=false)
static std::error_code DefaultErrorCode
Definition: FilesystemPath.hpp:58
Definition: ApplicationException.hpp:47