Manual for OpenFLUID 2.1.11

Filesystem.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 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 
47 #include <openfluid/dllexport.hpp>
48 
49 
50 namespace openfluid { namespace tools {
51 
52 
54 {
55 
56  public:
57 
58  Filesystem() = delete;
59 
60  constexpr static inline char pathsListSeparator() noexcept
61  {
62 #if defined(OPENFLUID_OS_UNIX)
63  return ':';
64 #elif defined(OPENFLUID_OS_WINDOWS)
65  return ';';
66 #endif
67  }
68 
69  constexpr static inline char pathSeparator() noexcept
70  {
71 #if defined(OPENFLUID_OS_UNIX)
72  return '/';
73 #elif defined(OPENFLUID_OS_WINDOWS)
74  return '\\';
75 #endif
76  }
77 
78  /**
79  Returns a joined path string from a vector of path parts, using the '/' separator
80  @param[in] PathParts a vector of path parts
81  @return the joined path
82  @snippet misc/filesystem.cpp joinpath
83  */
84  static std::string joinPath(const std::vector<std::string>& PathParts);
85 
86  /**
87  Returns the name of the file in the given path
88  @snippet misc/filesystem.cpp filename
89  @param[in] Path the given path
90  @return the filename
91  */
92  static std::string filename(const std::string& Path);
93 
94  /**
95  Returns the complete base name of the file in the given path
96  @snippet misc/filesystem.cpp basename
97  @param[in] Path the given path
98  @return the base name of the file
99  */
100  static std::string basename(const std::string& Path);
101 
102  /**
103  Returns the directory name (parent path) of the given path
104  @snippet misc/filesystem.cpp dirname
105  @param[in] Path the given path
106  @return the directory name
107  */
108  static std::string dirname(const std::string& Path);
109 
110  /**
111  Returns the extension of the file of the given path
112  @snippet misc/filesystem.cpp extension
113  @param[in] Path the given path
114  @return the extension
115  */
116  static std::string extension(const std::string& Path);
117 
118  /**
119  Returns the current path
120  @return the current path
121  */
122  static std::string currentPath();
123 
124  /**
125  Returns the absolute path of the given path.
126  If the given path is already an absolute path, it is returned as-is.
127  @param[in] Path the path to make absolute
128  @return the absolute path
129  */
130  static std::string absolutePath(const std::string& Path);
131 
132  /**
133  Cleans the given path : removes trailing and redundant slashes, resolves '.' and '..' as far as possible.
134  @param[in] Path the path to clean
135  @return the cleaned path
136  */
137  static std::string cleanPath(const std::string& Path);
138 
139  /**
140  Returns true if the given path is a directory
141  @param[in] Path the given path
142  @return true or false
143  */
144  static bool isDirectory(const std::string& Path);
145 
146  /**
147  Returns true if the given path is a file or a valid symbolic link
148  @param[in] Path the given path
149  @return true or false
150  */
151  static bool isFile(const std::string& Path);
152 
153  /**
154  Creates the directory of the given path. It creates all parent directories necessary to create the directory.
155  If the directory already exists, it does nothing.
156  @param[in] Path the given path
157  @return true if the directory has been successfully created or if it already exists, false otherwise
158  */
159  static bool makeDirectory(const std::string& Path);
160 
161  /**
162  Removes the directory of the given path. It recursively deletes all contents of the directory.
163  @param[in] Path the given path
164  @return true if the directory has been successfully deleted, false otherwise
165  */
166  static bool removeDirectory(const std::string& Path);
167 
168  /**
169  Creates a unique subdirectory in the given path, using the given subdirectory name as a prefix.
170  If the subdirectory already exists, it adds an incremental suffix to the subdirectory name.
171  It creates all parent directories necessary to create the subdirectory.
172  @param[in] Path the given path
173  @param[in] SubdirName the given path
174  @return the full path of the created unique subdirectory
175  */
176  static std::string makeUniqueSubdirectory(const std::string& Path, const std::string& SubdirName);
177 
178  /**
179  Creates a unique file in the given path, using the given File name and extension as prefix and suffix.
180  If the file already exists, it adds an incremental part to the file name.
181  If the Path does not exists, it creates all needed parent directories.
182  @param[in] Path the given path for the file
183  @param[in] FileName the file name used to make a unique one
184  @return the full path of the created unique file
185  */
186  static std::string makeUniqueFile(const std::string& Path, const std::string& FileName);
187 
188 
189  /**
190  Removes the file of the given path.
191  @param[in] Path the given path
192  @return true if the file was successfully deleted, false otherwise
193  */
194  static bool removeFile(const std::string& Path);
195 
196  /**
197  Copies a file from source to destination.
198  @param[in] SrcPath the source path
199  @param[in] DestPath the destination path
200  @return true if the file was successfully copied, false otherwise
201  */
202  static bool copyFile(const std::string& SrcPath, const std::string& DestPath);
203 
204  /**
205  Rename a file.
206  @param[in] OriginalPath the original file path
207  @param[in] NewPath the new file path
208  @return true if the file was successfully renamed, false otherwise
209  */
210  static bool renameFile(const std::string& OriginalPath, const std::string& NewPath);
211 
212  /**
213  Recursively copies a directory from source to destination.
214  @param[in] SrcPath the source path
215  @param[in] DestPath the destination path
216  @param[in] DontCopyDotDirs if set to true, it ignores the directories beginning with a dot ('.').
217  Default is false
218  @return true if the directory was successfully copied, false otherwise
219  */
220  static bool copyDirectory(const std::string& SrcPath, const std::string& DestPath,
221  const bool DontCopyDotDirs = false);
222 
223 };
224 
225 
226 } } // namespaces
227 
228 
229 #endif /* __OPENFLUID_TOOLS_FILESYSTEM_HPP__ */
openfluid::tools::Filesystem::pathSeparator
constexpr static char pathSeparator() noexcept
Definition: Filesystem.hpp:69
OPENFLUID_API
#define OPENFLUID_API
Definition: dllexport.hpp:86
openfluid::tools::Filesystem
Definition: Filesystem.hpp:53
openfluid
Definition: ApplicationException.hpp:47
dllexport.hpp
openfluid::tools::Filesystem::pathsListSeparator
constexpr static char pathsListSeparator() noexcept
Definition: Filesystem.hpp:60