A toolkit for working with phylogenetic data.
v0.18.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
fs.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_CORE_FS_H_
2 #define GENESIS_UTILS_CORE_FS_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2017 Lucas Czech
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lucas.czech@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
25 */
26 
34 #include <string>
35 #include <unordered_map>
36 #include <vector>
37 
38 namespace genesis {
39 namespace utils {
40 
41 // =================================================================================================
42 // File Access
43 // =================================================================================================
44 
45 bool is_file ( std::string const& path );
46 bool file_exists ( std::string const& filename );
47 std::string file_read ( std::string const& filename );
48 void file_write ( std::string const& content, std::string const& filename );
49 void file_append ( std::string const& content, std::string const& filename );
50 
51 bool is_dir ( std::string const& path );
52 bool dir_exists ( std::string const& dir );
53 void dir_create ( std::string const& path );
54 
55 std::string dir_normalize_path( std::string const& path );
56 
57 
66 std::vector<std::string> dir_list_contents(
67  std::string const& dir,
68  bool full_path = false,
69  std::string const& regex = ""
70 );
71 
78 std::vector<std::string> dir_list_files(
79  std::string const& dir,
80  bool full_path = false,
81  std::string const& regex = ""
82 );
83 
90 std::vector<std::string> dir_list_directories(
91  std::string const& dir,
92  bool full_path = false,
93  std::string const& regex = ""
94 );
95 
96 // =================================================================================================
97 // File Information
98 // =================================================================================================
99 
100 std::unordered_map<std::string, std::string> file_info ( std::string const& filename );
101 
102 size_t file_size ( std::string const& filename );
103 std::string file_path ( std::string const& filename );
104 std::string file_basename ( std::string const& filename );
105 std::string file_filename ( std::string const& filename );
106 std::string file_extension ( std::string const& filename );
107 
108 // =================================================================================================
109 // File Names
110 // =================================================================================================
111 
112 bool is_valid_filname( std::string const& filename );
113 std::string sanitize_filname( std::string const& filename );
114 
115 } // namespace utils
116 } // namespace genesis
117 
118 #endif // include guard
bool is_file(std::string const &path)
Return true iff the provided path is a file.
Definition: fs.cpp:61
utils::Range< IteratorPath< TreeLink const, TreeNode const, TreeEdge const > > path(ElementType const &start, ElementType const &finish)
Definition: path.hpp:325
std::string dir_normalize_path(std::string const &path)
Normalize a dir name, i.e., make sure that the given path ends with exaclty one slash.
Definition: fs.cpp:192
std::string file_filename(std::string const &filename)
Remove extension if present.
Definition: fs.cpp:335
bool file_exists(std::string const &filename)
Return true iff the file exists.
Definition: fs.cpp:69
bool dir_exists(std::string const &dir)
Return true iff the directory exists.
Definition: fs.cpp:150
std::vector< std::string > dir_list_contents(std::string const &dir, bool full_path, std::string const &regex)
Get a list of files and directories in a directory.
Definition: fs.cpp:238
std::vector< std::string > dir_list_directories(std::string const &dir, bool full_path, std::string const &regex)
Get a list of directories in a directory.
Definition: fs.cpp:259
std::unordered_map< std::string, std::string > file_info(std::string const &filename)
Return information about a file.
Definition: fs.cpp:276
std::vector< std::string > dir_list_files(std::string const &dir, bool full_path, std::string const &regex)
Get a list of files in a directory.
Definition: fs.cpp:249
bool is_valid_filname(std::string const &filename)
Check whether a file name is valid.
Definition: fs.cpp:386
void dir_create(std::string const &path)
Create a directory.
Definition: fs.cpp:175
size_t file_size(std::string const &filename)
Return the size of a file.
Definition: fs.cpp:292
std::string file_path(std::string const &filename)
Return the path leading to a file.
Definition: fs.cpp:304
std::string sanitize_filname(std::string const &filename)
Remove or replace all invalid parts of a filename.
Definition: fs.cpp:435
std::string file_basename(std::string const &filename)
Remove directory name from file name if present.
Definition: fs.cpp:318
std::string file_read(std::string const &filename)
Return the contents of a file as a string.
Definition: fs.cpp:80
bool is_dir(std::string const &path)
Return true iff the provided path is a directory.
Definition: fs.cpp:142
void file_append(std::string const &content, std::string const &filename)
Append the content of a string to a file.
Definition: fs.cpp:120
std::string file_extension(std::string const &filename)
Return the extension name of a file.
Definition: fs.cpp:351
void file_write(std::string const &content, std::string const &filename)
Write the content of a string to a file.
Definition: fs.cpp:106