Loading...
Searching...
No Matches
FileSystem.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
3
4 This file is part of the TerraLib - a Framework for building GIS enabled
5 applications.
6
7 TerraLib is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation, either version 3 of the License,
10 or (at your option) any later version.
11
12 TerraLib is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with TerraLib. See COPYING. If not, write to
19 TerraLib Team at <terralib-team@terralib.org>.
20 */
21
22/*!
23 \file terralib/core/filesystem/FileSystem.h
24
25 \brief A class for handling system files and paths.
26
27 \author Matheus Cavassan Zaglia
28*/
29
30#ifndef __TERRALIB_CORE_FILESYSTEM_FILESYSTEM_H__
31#define __TERRALIB_CORE_FILESYSTEM_FILESYSTEM_H__
32
33// TerraLib
34#include "../Config.h"
35
36// STL
37#include <string>
38#include <vector>
39#include <cstdint>
40
41// boost
42#include <boost/thread/mutex.hpp>
43
44namespace te
45{
46 namespace core
47 {
48 /*!
49 \class FileSystem
50
51 \brief A class for handling system files and paths using UTF-8 strings.
52 */
54 {
55 public:
56
57 /*!
58 \brief Retrives the current working directory path in UTF-8.
59
60 \return String in UTF-8 for the current working directory path.
61 */
62 static std::string currentPath();
63
64 /*!
65 \brief Composes an absolute path for the given path in UTF-8.
66
67 \param path Path in UTF-8.
68
69 \return String in UTF-8 for the composed path.
70 */
71 static std::string systemCompletePath(const std::string& path);
72
73 /*!
74 \brief Retrives the temp directory in UTF-8.
75
76 \return String in UTF-8 for temp directory.
77 */
78 static std::string tempDirectoryPath();
79
80 /*!
81 \brief Retrives an unique path generated by a given format.
82
83 \param format The format used to create a unique path.
84 e.g.: "%%%%-%%%%-%%%%-%%%%"
85
86 \return String in UTF-8 for the generated unique path.
87 */
88 static std::string uniquePath(
89 const std::string& format = "%%%%-%%%%-%%%%-%%%%");
90
91 /*!
92 \brief Retrives the absolute path for the given path in UTF-8.
93
94 \param path Path in UTF-8.
95
96 \return String in UTF-8 for the absolute path.
97 */
98 static std::string absolutePath(const std::string& path);
99
100 /*!
101 \brief Retrives the extension of a given file path in UTF-8.
102
103 \param path File path in UTF-8.
104
105 \return String in UTF-8 for the extension.
106 */
107 static std::string extension(const std::string& path);
108
109 /*!
110 \brief Retrives the filename of a given file path in UTF-8, including extension.
111
112 \param path File path in UTF-8.
113
114 \return String in UTF-8 for the name of the file, including the extension.
115 */
116 static std::string fileName(const std::string& path);
117
118 /*!
119 \brief Retrives the filename of a given file path in UTF-8, excluding extension.
120
121 \param path File path in UTF-8.
122
123 \return String in UTF-8 for the name of the file, excluding the extension.
124 */
125 static std::string stem(const std::string& path);
126
127 /*!
128 \brief Retrives the parent path of a given file path in UTF-8. For /home/files/fileName.shp, it will return /home/files
129
130 \param path File path in UTF-8.
131
132 \return String in UTF-8 for the parent path of the give file path.
133 */
134 static std::string parentPath(const std::string& path);
135
136 /*!
137 \brief Checks if a given path in UTF-8 is a directory.
138
139 \param path Path in UTF-8.
140
141 \return true if is a directory, otherwise false.
142 */
143 static bool isDirectory(const std::string& path);
144
145 /*!
146 \brief Checks if a given path in UTF-8 is an empty directory.
147
148 \param path Path in UTF-8.
149
150 \return true if is a empty directory, otherwise false.
151 */
152 static bool isEmpty(const std::string& path);
153 /*!
154 \brief Checks if a given path in UTF-8 is a regular file.
155
156 \param path File path in UTF-8.
157
158 \return true if is a regular file, otherwise false.
159 */
160 static bool isRegularFile(const std::string& path);
161
162 /*
163 \brief Returns the full directory to the currently running executable,
164 or an empty string in case of failure.
165
166 \return String in UTF-8 of the running executable
167 */
168 static std::string executableDirectory();
169
170 /*!
171 \brief Checks if a given path in UTF-8 exists.
172
173 \param path Path in UTF-8.
174
175 \return true if exists, otherwise false.
176 */
177 static bool exists(const std::string& path);
178
179 /*!
180 \brief Creates a directory from a given path in UTF-8.
181
182 \param path Path in UTF-8.
183
184 \return true if a new directory was created, otherwise false.
185 */
186 static bool createDirectory(const std::string& path);
187
188 /*!
189 \brief Creates a directory for any element of path that does not
190 exist.
191 \param path Path in UTF-8.
192
193 \return true if a new directory was created, otherwise false.
194 */
195 static bool createDirectories(const std::string& path);
196
197 /*!
198 \brief Copies a file
199
200 \param from Path in UTF-8 for the file to be copied.
201 \param to Path in UTF-8 for the copy output.
202
203 */
204 static void copyFile(const std::string& from, const std::string& to);
205
206 /*!
207 \brief Removes a file or directory from a given path in UTF-8.
208
209 \note This function will not remove if the directory is not empty.
210
211 \param path Path in UTF-8.
212
213 \return false if path did not exist in the first place, otherwise
214 true.
215 */
216 static bool remove(const std::string& path);
217
218 /*!
219 \brief Renames a file or directory from a given path in UTF-8.
220
221 \param old_p Old name in UTF-8.
222 \param new_p New name in UTF-8.
223 */
224 static void rename(const std::string& old_p, const std::string& new_p);
225
226 /*!
227 \brief Lists a directory from a given path in UTF-8.
228
229 \param path Path in UTF-8.
230
231 \return Vector of strings in UTF-8 for directory content.
232 */
233 static std::vector<std::string> listDirectory(const std::string& path);
234
235 /*!
236 \brief Returns the file size of a FILE in UTF-8.
237
238 \param path Path in UTF-8.
239
240 \return File size.
241
242 \exception te::core::Exception if the given path doesn't exists or its
243 used in a directory.
244 */
245 static uintmax_t fileSize(const std::string& path);
246
247 /*!
248 \brief Adjusts the given path to native form
249
250 \description Documentation from Boost:
251 In general, Boost.Filesystem differentiates between native paths and generic paths.
252 Native paths are operating system specific and must be used when calling operating system functions.
253 Generic paths are portable and independent of the operating system.
254 */
255 static std::string adjustPathToNativeForm(const std::string& filePath);
256
257 /*!
258 \brief Adjusts the given path to generic form
259
260 \description Documentation from Boost:
261 In general, Boost.Filesystem differentiates between native paths and generic paths.
262 Native paths are operating system specific and must be used when calling operating system functions.
263 Generic paths are portable and independent of the operating system.
264 */
265 static std::string adjustPathToGenericForm(const std::string& filePath);
266
267 /*!
268 \brief Returns the native separator based on the Operational System
269 */
270 static std::string getNativeSeparator();
271
272 /*!
273 \brief Read file and returns the contents of the file.
274
275 \description Read file, in case of corruption use the copy. Returns the contents of the file.
276 */
277 static std::string readFromFile(const std::string& filePath);
278
279 /*!
280 \brief will create a copy of the file before saving.
281
282 \description In the write to file, this method will create a copy of the file before saving.
283 If it gets corrupted in the process, when using the read from file method will replace
284 the corrupted file with the copy.
285 */
286 static void writeToFile(const std::string& filePath, const std::string& content);
287
288 /*!
289 \brief Create and allocate a new temporary disk file.
290
291 \param size The file size.
292
293 \param fileptr The created file pointer.
294
295 \param fullFileName The full file name of the created file name.
296
297 \return true if OK. false on errors.
298
299 \note The caller of this method is responsable for closing and deleting the created file.
300
301 \note A first attempty will try to create the file inside the user diretory, if it fails a second attempt will try the system temporary directory.
302 */
303 static bool allocateTmpDiskFile( unsigned long int size, FILE** fileptr,
304 std::string& fullFileName );
305
306 private:
307 // Not instantiable
310// No copy allowed
313
314 static std::string createTempFilePathString(const std::string& filePath);
315
317
318 /*!
319 \brief Get file content.
320
321 \param file path
322 */
323 static std::string getFileContent(const std::string& filePath);
324
325 /*!
326 \brief Checks whether the file contains full non-printable character content.
327
328 \param file path
329 */
330 static bool isBlank(const std::string& filePath);
331
332 /*!
333 \brief Checks whether the contents contains full non-printable character content.
334
335 \param file path
336 */
337 static bool isInvalid(const std::string& content);
338
339 private:
340
341 static boost::mutex mutexFileSystem; //!< Used to perform the tasks: write to file or read from file.
342
343 };
344 } // end namespace core
345} // end namespace te
346
347#endif //__TERRALIB_CORE_FILESYSTEM_FILESYSTEM_H__
A class for handling system files and paths using UTF-8 strings.
Definition: FileSystem.h:54
static std::string executableDirectory()
static bool createDirectory(const std::string &path)
Creates a directory from a given path in UTF-8.
static std::string stem(const std::string &path)
Retrives the filename of a given file path in UTF-8, excluding extension.
static void copyFile(const std::string &from, const std::string &to)
Copies a file.
static bool isDirectory(const std::string &path)
Checks if a given path in UTF-8 is a directory.
static std::string getFileContent(const std::string &filePath)
Get file content.
static std::string absolutePath(const std::string &path)
Retrives the absolute path for the given path in UTF-8.
static std::string readFromFile(const std::string &filePath)
Read file and returns the contents of the file.
static std::vector< std::string > listDirectory(const std::string &path)
Lists a directory from a given path in UTF-8.
static bool allocateTmpDiskFile(unsigned long int size, FILE **fileptr, std::string &fullFileName)
Create and allocate a new temporary disk file.
static bool isInvalid(const std::string &content)
Checks whether the contents contains full non-printable character content.
FileSystem(FileSystem const &)
static boost::mutex mutexFileSystem
Used to perform the tasks: write to file or read from file.
Definition: FileSystem.h:341
static bool exists(const std::string &path)
Checks if a given path in UTF-8 exists.
static std::string createTempFileDirectoryPathString()
static std::string currentPath()
Retrives the current working directory path in UTF-8.
static std::string adjustPathToNativeForm(const std::string &filePath)
Adjusts the given path to native form.
static std::string uniquePath(const std::string &format="%%%%-%%%%-%%%%-%%%%")
Retrives an unique path generated by a given format.
static std::string createTempFilePathString(const std::string &filePath)
static std::string adjustPathToGenericForm(const std::string &filePath)
Adjusts the given path to generic form.
static std::string fileName(const std::string &path)
Retrives the filename of a given file path in UTF-8, including extension.
static bool isEmpty(const std::string &path)
Checks if a given path in UTF-8 is an empty directory.
static bool remove(const std::string &path)
Removes a file or directory from a given path in UTF-8.
static void writeToFile(const std::string &filePath, const std::string &content)
will create a copy of the file before saving.
static uintmax_t fileSize(const std::string &path)
Returns the file size of a FILE in UTF-8.
FileSystem & operator=(FileSystem const &)
static bool createDirectories(const std::string &path)
Creates a directory for any element of path that does not exist.
static std::string extension(const std::string &path)
Retrives the extension of a given file path in UTF-8.
static std::string systemCompletePath(const std::string &path)
Composes an absolute path for the given path in UTF-8.
static std::string parentPath(const std::string &path)
Retrives the parent path of a given file path in UTF-8. For /home/files/fileName.shp,...
static std::string getNativeSeparator()
Returns the native separator based on the Operational System.
static void rename(const std::string &old_p, const std::string &new_p)
Renames a file or directory from a given path in UTF-8.
static std::string tempDirectoryPath()
Retrives the temp directory in UTF-8.
static bool isRegularFile(const std::string &path)
Checks if a given path in UTF-8 is a regular file.
static bool isBlank(const std::string &filePath)
Checks whether the file contains full non-printable character content.
TerraLib.
#define TECOREEXPORT
Definition: Config.h:52