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