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 namespace te
42 {
43  namespace core
44  {
45  /*!
46  \class FileSystem
47 
48  \brief A class for handling system files and paths using UTF-8 strings.
49  */
51  {
52  public:
53 
54  /*!
55  \brief Retrives the current working directory path in UTF-8.
56 
57  \return String in UTF-8 for the current working directory path.
58  */
59  static std::string currentPath();
60 
61  /*!
62  \brief Composes an absolute path for the given path in UTF-8.
63 
64  \param path Path in UTF-8.
65 
66  \return String in UTF-8 for the composed path.
67  */
68  static std::string systemCompletePath(const std::string& path);
69 
70  /*!
71  \brief Retrives the temp directory in UTF-8.
72 
73  \return String in UTF-8 for temp directory.
74  */
75  static std::string tempDirectoryPath();
76 
77  /*!
78  \brief Retrives an unique path generated by a given format.
79 
80  \param format The format used to create a unique path.
81  e.g.: "%%%%-%%%%-%%%%-%%%%"
82 
83  \return String in UTF-8 for the generated unique path.
84  */
85  static std::string uniquePath(
86  const std::string& format = "%%%%-%%%%-%%%%-%%%%");
87 
88  /*!
89  \brief Retrives the absolute path for the given path in UTF-8.
90 
91  \param path Path in UTF-8.
92 
93  \return String in UTF-8 for the absolute path.
94  */
95  static std::string absolutePath(const std::string& path);
96 
97  /*!
98  \brief Retrives the extension of a given file path in UTF-8.
99 
100  \param path File path in UTF-8.
101 
102  \return String in UTF-8 for the extension.
103  */
104  static std::string extension(const std::string& path);
105 
106  /*!
107  \brief Retrives the filename of a given file path in UTF-8, including extension.
108 
109  \param path File path in UTF-8.
110 
111  \return String in UTF-8 for the name of the file, including the extension.
112  */
113  static std::string fileName(const std::string& path);
114 
115  /*!
116  \brief Retrives the filename of a given file path in UTF-8, excluding extension.
117 
118  \param path File path in UTF-8.
119 
120  \return String in UTF-8 for the name of the file, excluding the extension.
121  */
122  static std::string stem(const std::string& path);
123 
124  /*!
125  \brief Retrives the parent path of a given file path in UTF-8. For /home/files/fileName.shp, it will return /home/files
126 
127  \param path File path in UTF-8.
128 
129  \return String in UTF-8 for the parent path of the give file path.
130  */
131  static std::string parentPath(const std::string& path);
132 
133  /*!
134  \brief Checks if a given path in UTF-8 is a directory.
135 
136  \param path Path in UTF-8.
137 
138  \return true if is a directory, otherwise false.
139  */
140  static bool isDirectory(const std::string& path);
141 
142  /*!
143  \brief Checks if a given path in UTF-8 is an empty directory.
144 
145  \param path Path in UTF-8.
146 
147  \return true if is a empty directory, otherwise false.
148  */
149  static bool isEmpty(const std::string& path);
150  /*!
151  \brief Checks if a given path in UTF-8 is a regular file.
152 
153  \param path File path in UTF-8.
154 
155  \return true if is a regular file, otherwise false.
156  */
157  static bool isRegularFile(const std::string& path);
158 
159  /*
160  \brief Returns the full directory to the currently running executable,
161  or an empty string in case of failure.
162 
163  \return String in UTF-8 of the running executable
164  */
165  static std::string executableDirectory();
166 
167  /*!
168  \brief Checks if a given path in UTF-8 exists.
169 
170  \param path Path in UTF-8.
171 
172  \return true if exists, otherwise false.
173  */
174  static bool exists(const std::string& path);
175 
176  /*!
177  \brief Creates a directory from a given path in UTF-8.
178 
179  \param path Path in UTF-8.
180 
181  \return true if a new directory was created, otherwise false.
182  */
183  static bool createDirectory(const std::string& path);
184 
185  /*!
186  \brief Creates a directory for any element of path that does not
187  exist.
188  \param path Path in UTF-8.
189 
190  \return true if a new directory was created, otherwise false.
191  */
192  static bool createDirectories(const std::string& path);
193 
194  /*!
195  \brief Copies a file
196 
197  \param from Path in UTF-8 for the file to be copied.
198  \param to Path in UTF-8 for the copy output.
199 
200  */
201  static void copyFile(const std::string& from, const std::string& to);
202 
203  /*!
204  \brief Removes a file or directory from a given path in UTF-8.
205 
206  \note This function will not remove if the directory is not empty.
207 
208  \param path Path in UTF-8.
209 
210  \return false if path did not exist in the first place, otherwise
211  true.
212  */
213  static bool remove(const std::string& path);
214 
215  /*!
216  \brief Renames a file or directory from a given path in UTF-8.
217 
218  \param old_p Old name in UTF-8.
219  \param new_p New name in UTF-8.
220  */
221  static void rename(const std::string& old_p, const std::string& new_p);
222 
223  /*!
224  \brief Lists a directory from a given path in UTF-8.
225 
226  \param path Path in UTF-8.
227 
228  \return Vector of strings in UTF-8 for directory content.
229  */
230  static std::vector<std::string> listDirectory(const std::string& path);
231 
232  /*!
233  \brief Returns the file size of a FILE in UTF-8.
234 
235  \param path Path in UTF-8.
236 
237  \return File size.
238 
239  \exception te::core::Exception if the given path doesn't exists or its
240  used in a directory.
241  */
242  static uintmax_t fileSize(const std::string& path);
243 
244  private:
245  // Not instantiable
246  FileSystem();
247  ~FileSystem();
248 // No copy allowed
249  FileSystem(FileSystem const&);
250  FileSystem& operator=(FileSystem const&);
251  };
252  } // end namespace core
253 } // end namespace te
254 
255 #endif //__TERRALIB_CORE_FILESYSTEM_FILESYSTEM_H__
A class for handling system files and paths using UTF-8 strings.
Definition: FileSystem.h:50
#define TECOREEXPORT
Definition: Config.h:52
TerraLib.