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 Checks if a given path in UTF-8 is a directory.
108 
109  \param path Path in UTF-8.
110 
111  \return true if is a directory, otherwise false.
112  */
113  static bool isDirectory(const std::string& path);
114 
115  /*!
116  \brief Checks if a given path in UTF-8 is an empty directory.
117 
118  \param path Path in UTF-8.
119 
120  \return true if is a empty directory, otherwise false.
121  */
122  static bool isEmpty(const std::string& path);
123  /*!
124  \brief Checks if a given path in UTF-8 is a regular file.
125 
126  \param path File path in UTF-8.
127 
128  \return true if is a regular file, otherwise false.
129  */
130  static bool isRegularFile(const std::string& path);
131 
132  /*
133  \brief Returns the full directory to the currently running executable,
134  or an empty string in case of failure.
135 
136  \return String in UTF-8 of the running executable
137  */
138  static std::string executableDirectory();
139 
140  /*!
141  \brief Checks if a given path in UTF-8 exists.
142 
143  \param path Path in UTF-8.
144 
145  \return true if exists, otherwise false.
146  */
147  static bool exists(const std::string& path);
148 
149  /*!
150  \brief Creates a directory from a given path in UTF-8.
151 
152  \param path Path in UTF-8.
153 
154  \return true if a new directory was created, otherwise false.
155  */
156  static bool createDirectory(const std::string& path);
157 
158  /*!
159  \brief Creates a directory for any element of path that does not
160  exist.
161  \param path Path in UTF-8.
162 
163  \return true if a new directory was created, otherwise false.
164  */
165  static bool createDirectories(const std::string& path);
166 
167  /*!
168  \brief Copies a file
169 
170  \param from Path in UTF-8 for the file to be copied.
171  \param to Path in UTF-8 for the copy output.
172 
173  */
174  static void copyFile(const std::string& from, const std::string& to);
175 
176  /*!
177  \brief Removes a file or directory from a given path in UTF-8.
178 
179  \note This function will not remove if the directory is not empty.
180 
181  \param path Path in UTF-8.
182 
183  \return false if path did not exist in the first place, otherwise
184  true.
185  */
186  static bool remove(const std::string& path);
187 
188  /*!
189  \brief Renames a file or directory from a given path in UTF-8.
190 
191  \param old_p Old name in UTF-8.
192  \param new_p New name in UTF-8.
193  */
194  static void rename(const std::string& old_p, const std::string& new_p);
195 
196  /*!
197  \brief Lists a directory from a given path in UTF-8.
198 
199  \param path Path in UTF-8.
200 
201  \return Vector of strings in UTF-8 for directory content.
202  */
203  static std::vector<std::string> listDirectory(const std::string& path);
204 
205  /*!
206  \brief Returns the file size of a FILE in UTF-8.
207 
208  \param path Path in UTF-8.
209 
210  \return File size.
211 
212  \exception te::core::Exception if the given path doesn't exists or its
213  used in a directory.
214  */
215  static uintmax_t fileSize(const std::string& path);
216 
217  private:
218  // Not instantiable
219  FileSystem();
220  ~FileSystem();
221 // No copy allowed
222  FileSystem(FileSystem const&);
223  FileSystem& operator=(FileSystem const&);
224  };
225  } // end namespace core
226 } // end namespace te
227 
228 #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
URI C++ Library.