examples/core/filesystem/main.cpp
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 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/examples/core/filesystem/main.cpp
23 
24  \brief Examples for the Terralib File System Module
25 
26  \author Matheus Cavassan Zaglia
27 */
28 
29 // STL
30 #include <string>
31 #include <fstream>
32 #include <cstdlib>
33 #include <iostream>
34 
35 // TerraLib
38 
39 int main(int argc, char *argv[])
40 {
41 
42  /* Initialize an utf8 string with a path */
43  std::string utf8_path =
44  te::core::CharEncoding::toUTF8("../example/filesystem");
45 
46  /* Create a directory and make some verifications about the path */
48  std::cout << "Directory created!" << std::endl;
49 
50  if (te::core::FileSystem::exists(utf8_path))
51  std::cout << "Path exists!" << std::endl;
52 
54  std::cout << "Path is a directory!" << std::endl;
55 
56  if(te::core::FileSystem::isEmpty(utf8_path))
57  std::cout << "Path is empty!" << std::endl;
58 
59  /* Creating files to test some functions */
60  std::string file1_str = utf8_path + "/file1.txt",
61  file2_str = utf8_path + "/file2.sh",
62  file3_str = utf8_path + "/file3.txt";
63  std::ofstream file1(file1_str);
64  std::ofstream file2(file2_str);
65  file1.close();
66  file2.close();
67 
68  /* Return the file extension */
69  std::string stringTxt = te::core::FileSystem::extension(file1_str);
70  std::cout << "File1 has extension: " << stringTxt << std::endl;
71  std::string stringSh = te::core::FileSystem::extension(file2_str);
72  std::cout << "File1 has extension: " << stringSh << std::endl;
73 
74  /* Rename and copy files */
75  te::core::FileSystem::rename(file1_str, file3_str);
76  te::core::FileSystem::copyFile(file3_str, file1_str);
77 
78  /* Check if is a regular file */
80  std::cout << "File3 is a regular file!" << std::endl;
81 
82  /* Create a list with the files in directory, check the list size and remove
83  * the files */
84  std::vector<std::string> files =
86 
87  std::cout << "Files size is " << files.size() << std::endl;
88 
89  for(size_t i = 0; i < files.size(); ++i)
91 
92  if (te::core::FileSystem::remove(utf8_path))
93  std::cout << "utf8 path was removed!" << std::endl;
94 
95 }
int main(int argc, char *argv[])
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 isEmpty(const std::string &path)
Checks if a given path in UTF-8 is an empty directory.
Definition: FileSystem.cpp:93
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 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.
A class for handling character enconding/decoding.
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 toUTF8(const std::string &src)
Convert a string from a current locale encoding to UTF-8.
static bool isRegularFile(const std::string &path)
Checks if a given path in UTF-8 is a regular file.
Definition: FileSystem.cpp:98