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
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 application path
120  @return the current path
121  */
122  static std::string currentPath();
123 
124  /**
125  Returns true if the given path is a directory
126  @param[in] Path the given path
127  @return true or false
128  */
129  static bool isDirectory(const std::string& Path);
130 
131  /**
132  Returns true if the given path is a file or a valid symbolic link
133  @param[in] Path the given path
134  @return true or false
135  */
136  static bool isFile(const std::string& Path);
137 
138  /**
139  Creates the directory of the given path. It creates all parent directories necessary to create the directory.
140  If the directory already exists, it does nothing.
141  @param[in] Path the given path
142  @return true if the directory was successfully created, false otherwise
143  */
144  static bool makeDirectory(const std::string& Path);
145 
146  /**
147  Removes the directory of the given path. It recursively deletes all contents of the directory.
148  @param[in] Path the given path
149  @return true if the directory was successfully deleted, false otherwise
150  */
151  static bool removeDirectory(const std::string& Path);
152 
153  /**
154  Creates a unique subdirectory in the given path, using the given subdirectory name as a prefix.
155  If the subdirectory already exists, it adds an incremental suffix to the subdirectory name.
156  It creates all parent directories necessary to create the subdirectory.
157  @param[in] Path the given path
158  @param[in] SubdirName the given path
159  @return the full path of the created unique subdirectory
160  */
161  static std::string makeUniqueSubdirectory(const std::string& Path, const std::string& SubdirName);
162 
163  /**
164  Creates a unique file in the given path, using the given File name and extension as prefix and suffix.
165  If the file already exists, it adds an incremental part to the file name.
166  If the Path does not exists, it creates all needed parent directories.
167  @param[in] Path the given path for the file
168  @param[in] FileName the file name used to make a unique one
169  @return the full path of the created unique file
170  */
171  static std::string makeUniqueFile(const std::string& Path, const std::string& FileName);
172 
173 
174  /**
175  Removes the file of the given path.
176  @param[in] Path the given path
177  @return true if the file was successfully deleted, false otherwise
178  */
179  static bool removeFile(const std::string& Path);
180 
181  /**
182  Copies a file from source to destination.
183  @param[in] SrcPath the source path
184  @param[in] DestPath the destination path
185  @return true if the file was successfully copied, false otherwise
186  */
187  static bool copyFile(const std::string& SrcPath, const std::string& DestPath);
188 
189  /**
190  Recursively copies a directory from source to destination.
191  @param[in] SrcPath the source path
192  @param[in] DestPath the destination path
193  @param[in] DontCopyDotDirs if set to true, it ignores the directories beginning with a dot ('.').
194  Default is false
195  @return true if the directory was successfully copied, false otherwise
196  */
197  static bool copyDirectory(const std::string& SrcPath, const std::string& DestPath,
198  const bool DontCopyDotDirs = false);
199 
200 };
201 
202 
203 } } // namespaces
204 
205 
206 #endif /* __OPENFLUID_TOOLS_FILESYSTEM_HPP__ */
static constexpr char pathSeparator() noexcept
Definition: Filesystem.hpp:69
static constexpr char pathsListSeparator() noexcept
Definition: Filesystem.hpp:60
Definition: Filesystem.hpp:53
Definition: ApplicationException.hpp:47
#define OPENFLUID_API
Definition: dllexport.hpp:86