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  /**
61  Returns a joined path string from a vector of path parts
62  @code{.cpp}
63  path = openfluid::tools::Filesystem::joinPath({"/my/joined","path/myfile.txt"});
64  // path = /my/joined/path/myfile.txt
65  @endcode
66  @param[in] PathParts a vector of path parts
67  @return the joined path
68  */
69  static std::string joinPath(const std::vector<std::string>& PathParts);
70 
71  /**
72  Returns the name of the file in the given path
73  @code{.cpp}
74  name = openfluid::tools::Filesystem::filename("/tmp/archive.tar.gz")
75  // name = archive.tar.gz
76  @endcode
77  @param[in] Path the given path
78  @return the filename
79  */
80  static std::string filename(const std::string& Path);
81 
82  /**
83  Returns the complete base name of the file in the given path
84  @code{.cpp}
85  name = openfluid::tools::Filesystem::basename("/tmp/archive.tar.gz")
86  // name = archive.tar
87  @endcode
88  @param[in] Path the given path
89  @return the base name of the file
90  */
91  static std::string basename(const std::string& Path);
92 
93  /**
94  Returns the directory name (parent path) of the given path
95  @code{.cpp}
96  name = openfluid::tools::Filesystem::dirname("/tmp/archive.tar.gz")
97  // name = /tmp
98  @endcode
99  @param[in] Path the given path
100  @return the directory name
101  */
102  static std::string dirname(const std::string& Path);
103 
104  /**
105  Returns the extension of the file of the given path
106  @code{.cpp}
107  name = openfluid::tools::Filesystem::extension("/tmp/archive.tar.gz")
108  // name = gz
109  @endcode
110  @param[in] Path the given path
111  @return the extension
112  */
113  static std::string extension(const std::string& Path);
114 
115  /**
116  Returns the current application path
117  @return the current path
118  */
119  static std::string currentPath();
120 
121  /**
122  Returns true if the given path is a directory
123  @param[in] Path the given path
124  @return true or false
125  */
126  static bool isDirectory(const std::string& Path);
127 
128  /**
129  Returns true if the given path is a file or a valid symbolic link
130  @param[in] Path the given path
131  @return true or false
132  */
133  static bool isFile(const std::string& Path);
134 
135  /**
136  Creates the directory of the given path. It creates all parent directories necessary to create the directory.
137  If the directory already exists, it does nothing.
138  @param[in] Path the given path
139  @return true if the directory was successfully created, false otherwise
140  */
141  static bool makeDirectory(const std::string& Path);
142 
143  /**
144  Removes the directory of the given path. It recursively deletes all contents of the directory.
145  @param[in] Path the given path
146  @return true if the directory was successfully deleted, false otherwise
147  */
148  static bool removeDirectory(const std::string& Path);
149 
150  /**
151  Creates a unique subdirectory in the given path, using the given subdirectory name as a prefix.
152  If the subdirectory already exists, it adds an incremental suffix to the subdirectory name.
153  It creates all parent directories necessary to create the subdirectory.
154  @param[in] Path the given path
155  @param[in] SubdirName the given path
156  @return the full path of the created unique subdirectory
157  */
158  static std::string makeUniqueSubdirectory(const std::string& Path, const std::string& SubdirName);
159 
160  /**
161  Creates a unique file in the given path, using the given File name and extension as prefix and suffix.
162  If the file already exists, it adds an incremental part to the file name.
163  If the Path does not exists, it creates all needed parent directories.
164  @param[in] Path the given path for the file
165  @param[in] FileName the file name used to make a unique one
166  @return the full path of the created unique file
167  */
168  static std::string makeUniqueFile(const std::string& Path, const std::string& FileName);
169 
170 
171  /**
172  Removes the file of the given path.
173  @param[in] Path the given path
174  @return true if the file was successfully deleted, false otherwise
175  */
176  static bool removeFile(const std::string& Path);
177 
178  /**
179  Copies a file from source to destination.
180  @param[in] SrcPath the source path
181  @param[in] DestPath the destination path
182  @return true if the file was successfully copied, false otherwise
183  */
184  static bool copyFile(const std::string& SrcPath, const std::string& DestPath);
185 
186  /**
187  Recursively copies a directory from source to destination.
188  @param[in] SrcPath the source path
189  @param[in] DestPath the destination path
190  @param[in] DontCopyDotDirs if set to true, it ignores the directories beginning with a dot ('.').
191  Default is false
192  @return true if the directory was successfully copied, false otherwise
193  */
194  static bool copyDirectory(const std::string& SrcPath, const std::string& DestPath,
195  const bool DontCopyDotDirs = false);
196 
197 };
198 
199 
200 } } // namespaces
201 
202 
203 #endif /* __OPENFLUID_TOOLS_FILESYSTEM_HPP__ */
Definition: Filesystem.hpp:53
Definition: ApplicationException.hpp:47
#define OPENFLUID_API
Definition: dllexport.hpp:86