FileSystem.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled
4  applications.
5 
6  TerraLib is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published by
8  the Free Software Foundation, either version 3 of the License,
9  or (at your option) any later version.
10 
11  TerraLib is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with TerraLib. See COPYING. If not, write to
18  TerraLib Team at <terralib-team@terralib.org>.
19  */
20 
21 /*!
22  \file terralib/core/filesystem/FileSystem.cpp
23 
24  \brief A class for handling system files and paths.
25 
26  \author Matheus Cavassan Zaglia
27 */
28 
29 // TerraLib
30 #include "FileSystem.h"
31 #include "../Exception.h"
32 #include "../encoding/CharEncoding.h"
33 
34 // Boost
35 #include <boost/filesystem.hpp>
36 
37 //STL
38 #include <vector>
39 
40 // System dependent includes
41 #if TE_PLATFORMCODE_MSWINDOWS == TE_PLATFORM
42 #include <windows.h>
43 #elif TE_PLATFORMCODE_LINUX == TE_PLATFORM
44 #include <limits.h>
45 #include <unistd.h>
46 #elif TE_PLATFORMCODE_APPLE == TE_PLATFORM
47 #include <mach-o/dyld.h>
48 #endif
49 
51 {
53  boost::filesystem::current_path().string());
54 }
55 
56 std::string te::core::FileSystem::systemCompletePath(const std::string& path)
57 {
58  std::string complete =
59  boost::filesystem::system_complete(te::core::CharEncoding::fromUTF8(path))
60  .string();
61  return te::core::CharEncoding::toUTF8(complete);
62 }
63 
65 {
67  boost::filesystem::temp_directory_path().string());
68 }
69 
70 std::string te::core::FileSystem::uniquePath(const std::string& format)
71 {
73  boost::filesystem::unique_path(format).string());
74 }
75 
76 std::string te::core::FileSystem::absolutePath(const std::string& path)
77 {
79  boost::filesystem::absolute(path).string());
80 }
81 
82 std::string te::core::FileSystem::extension(const std::string& path)
83 {
84  return boost::filesystem::extension(path);
85 }
86 
87 bool te::core::FileSystem::isDirectory(const std::string& path)
88 {
89  return boost::filesystem::is_directory(
91 }
92 
93 bool te::core::FileSystem::isEmpty(const std::string& path)
94 {
95  return boost::filesystem::is_empty(te::core::CharEncoding::fromUTF8(path));
96 }
97 
98 bool te::core::FileSystem::isRegularFile(const std::string& path)
99 {
100  return boost::filesystem::is_regular_file(
102 }
103 
105 {
106 #if TE_PLATFORMCODE_MSWINDOWS == TE_PLATFORM
107  char exePath[1024];
108  if(GetModuleFileName(NULL, exePath, sizeof(exePath)) == 0)
109  exePath[0] = '\0';
110 #elif TE_PLATFORMCODE_SOLARIS == TE_PLATFORM
111  char exePath[PATH_MAX];
112  if(realpath(getexecname(), exePath) == NULL)
113  exePath[0] = '\0';
114 #elif TE_PLATFORMCODE_LINUX == TE_PLATFORM
115  char exePath[PATH_MAX];
116  ssize_t len = ::readlink("/proc/self/exe", exePath, sizeof(exePath));
117  if(len == -1 || len == sizeof(exePath))
118  len = 0;
119  exePath[len] = '\0';
120 #elif TE_PLATFORMCODE_APPLE == TE_PLATFORM
121  char exePath[PATH_MAX];
122  uint32_t len = sizeof(exePath);
123  if(_NSGetExecutablePath(exePath, &len) != 0)
124  {
125  exePath[0] = '\0'; // buffer too small (!)
126  }
127  else
128  {
129  char* canonicalPath = realpath(exePath, NULL);
130  if(canonicalPath != NULL)
131  {
132  strncpy(exePath, canonicalPath, len);
133  free(canonicalPath);
134  }
135  }
136 #endif
137 
138  boost::filesystem::path p(exePath);
139  return te::core::CharEncoding::toUTF8(p.parent_path().string());
140 }
141 
142 bool te::core::FileSystem::exists(const std::string& path)
143 {
144  return boost::filesystem::exists(te::core::CharEncoding::fromUTF8(path));
145 }
146 
147 bool te::core::FileSystem::createDirectory(const std::string& path)
148 {
149  return boost::filesystem::create_directory(
151 }
152 
153 bool te::core::FileSystem::createDirectories(const std::string& path)
154 {
155  return boost::filesystem::create_directories(
157 }
158 
159 void te::core::FileSystem::copyFile(const std::string& from,
160  const std::string& to)
161 {
162  boost::filesystem::copy_file(te::core::CharEncoding::fromUTF8(from),
164 }
165 
166 bool te::core::FileSystem::remove(const std::string& path)
167 {
168  return boost::filesystem::remove(te::core::CharEncoding::fromUTF8(path));
169 }
170 
171 void te::core::FileSystem::rename(const std::string& old_p,
172  const std::string& new_p)
173 {
174  boost::filesystem::rename(te::core::CharEncoding::fromUTF8(old_p),
176 }
177 
178 std::vector<std::string> te::core::FileSystem::listDirectory(
179  const std::string& path)
180 {
181  if(!isDirectory(path))
183  << ErrorDescription("The given path is not a directory.");
184 
185  std::vector<std::string> result;
186 
187  boost::filesystem::path p(te::core::CharEncoding::fromUTF8(path));
188 
189  for(boost::filesystem::directory_iterator it(p), it_end; it != it_end; ++it)
190  result.push_back(te::core::CharEncoding::toUTF8(it->path().string()));
191 
192  return result;
193 }
194 
195 uintmax_t te::core::FileSystem::fileSize(const std::string &path)
196 {
197  return boost::filesystem::file_size(te::core::CharEncoding::fromUTF8(path));
198 }
static bool exists(const std::string &path)
Checks if a given path in UTF-8 exists.
Definition: FileSystem.cpp:142
static bool isDirectory(const std::string &path)
Checks if a given path in UTF-8 is a directory.
Definition: FileSystem.cpp:87
static std::vector< std::string > listDirectory(const std::string &path)
Lists a directory from a given path in UTF-8.
Definition: FileSystem.cpp:178
static bool createDirectories(const std::string &path)
Creates a directory for any element of path that does not exist.
Definition: FileSystem.cpp:153
static bool isEmpty(const std::string &path)
Checks if a given path in UTF-8 is an empty directory.
Definition: FileSystem.cpp:93
static std::string fromUTF8(const std::string &src)
Convert a string in UTF-8 to the current locale encoding.
boost::error_info< struct tag_error_description, std::string > ErrorDescription
The base type for error report messages.
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.
Definition: FileSystem.cpp:171
static std::string extension(const std::string &path)
Retrives the extension of a given file path in UTF-8.
Definition: FileSystem.cpp:82
static std::string absolutePath(const std::string &path)
Retrives the absolute path for the given path in UTF-8.
Definition: FileSystem.cpp:76
static bool remove(const std::string &path)
Removes a file or directory from a given path in UTF-8.
Definition: FileSystem.cpp:166
A class for handling system files and paths.
static std::string uniquePath(const std::string &format="%%%%-%%%%-%%%%-%%%%")
Retrives an unique path generated by a given format.
Definition: FileSystem.cpp:70
te::gm::Polygon * p
static void copyFile(const std::string &from, const std::string &to)
Copies a file.
Definition: FileSystem.cpp:159
static bool createDirectory(const std::string &path)
Creates a directory from a given path in UTF-8.
Definition: FileSystem.cpp:147
static std::string systemCompletePath(const std::string &path)
Composes an absolute path for the given path in UTF-8.
Definition: FileSystem.cpp:56
static std::string executableDirectory()
Definition: FileSystem.cpp:104
static std::string toUTF8(const std::string &src)
Convert a string from a current locale encoding to UTF-8.
static uintmax_t fileSize(const std::string &path)
Returns the file size of a FILE in UTF-8.
Definition: FileSystem.cpp:195
static std::string tempDirectoryPath()
Retrives the temp directory in UTF-8.
Definition: FileSystem.cpp:64
static std::string currentPath()
Retrives the current working directory path in UTF-8.
Definition: FileSystem.cpp:50
An exception indicating that a given argument is not valid, for instance if a given item already exis...
static bool isRegularFile(const std::string &path)
Checks if a given path in UTF-8 is a regular file.
Definition: FileSystem.cpp:98