A toolkit for working with phylogenetic data.
v0.20.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-2018 Lucas Czech and HITS gGmbH
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 
48 bool path_exists( std::string const& path );
49 
56 bool is_file( std::string const& path );
57 
61 bool file_exists( std::string const& filename );
62 
68 std::string file_read( std::string const& filename );
69 
78 void file_write( std::string const& content, std::string const& filename );
79 
85 void file_append( std::string const& content, std::string const& filename );
86 
93 bool is_dir( std::string const& path );
94 
98 bool dir_exists( std::string const& dir );
99 
107 void dir_create( std::string const& path, bool with_parents = true );
108 
112 std::string dir_normalize_path( std::string const& path );
113 
122 std::vector<std::string> dir_list_contents(
123  std::string const& dir,
124  bool full_path = false,
125  std::string const& regex = ""
126 );
127 
134 std::vector<std::string> dir_list_files(
135  std::string const& dir,
136  bool full_path = false,
137  std::string const& regex = ""
138 );
139 
146 std::vector<std::string> dir_list_directories(
147  std::string const& dir,
148  bool full_path = false,
149  std::string const& regex = ""
150 );
151 
152 // =================================================================================================
153 // File Information
154 // =================================================================================================
155 
159 std::unordered_map<std::string, std::string> file_info ( std::string const& filename );
160 
164 size_t file_size ( std::string const& filename );
165 
172 std::string file_path ( std::string const& filename );
173 
177 std::string file_basename ( std::string const& filename );
178 
185 std::string file_filename ( std::string const& filename );
186 
192 std::string file_extension ( std::string const& filename );
193 
194 // =================================================================================================
195 // File Names
196 // =================================================================================================
197 
218 bool is_valid_filname( std::string const& filename );
219 
241 std::string sanitize_filname( std::string const& filename );
242 
243 } // namespace utils
244 } // namespace genesis
245 
246 #endif // include guard
bool is_file(std::string const &path)
Return true iff the provided path is a file.
Definition: fs.cpp:61
bool path_exists(std::string const &path)
Return whether a path exists, i.e., is a file or directory.
Definition: fs.cpp:55
utils::Range< IteratorPath< TreeLink const, TreeNode const, TreeEdge const > > path(ElementType const &start, ElementType const &finish)
Definition: path.hpp:326
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:172
std::string file_filename(std::string const &filename)
Remove extension if present.
Definition: fs.cpp:296
bool file_exists(std::string const &filename)
Return true iff the file exists.
Definition: fs.cpp:66
bool dir_exists(std::string const &dir)
Return true iff the directory exists.
Definition: fs.cpp:126
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:218
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:239
std::unordered_map< std::string, std::string > file_info(std::string const &filename)
Return information about a file.
Definition: fs.cpp:253
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:229
bool is_valid_filname(std::string const &filename)
Check whether a file name is valid.
Definition: fs.cpp:322
size_t file_size(std::string const &filename)
Return the size of a file.
Definition: fs.cpp:266
std::string file_path(std::string const &filename)
Return the path leading to a file.
Definition: fs.cpp:273
void dir_create(std::string const &path, bool with_parents)
Create a directory.
Definition: fs.cpp:144
std::string sanitize_filname(std::string const &filename)
Remove or replace all invalid parts of a filename.
Definition: fs.cpp:350
std::string file_basename(std::string const &filename)
Remove directory name from file name if present.
Definition: fs.cpp:285
std::string file_read(std::string const &filename)
Return the contents of a file as a string.
Definition: fs.cpp:78
bool is_dir(std::string const &path)
Return true iff the provided path is a directory.
Definition: fs.cpp:121
void file_append(std::string const &content, std::string const &filename)
Append the content of a string to a file.
Definition: fs.cpp:105
std::string file_extension(std::string const &filename)
Return the extension name of a file.
Definition: fs.cpp:307
void file_write(std::string const &content, std::string const &filename)
Write the content of a string to a file.
Definition: fs.cpp:96