A toolkit for working with phylogenetic data.
v0.24.0
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-2020 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 
71 std::string file_read( std::string const& filename, bool detect_compression = true );
72 
81 std::vector<std::string> file_read_lines( std::string const& filename, bool detect_compression = true );
82 
91 void file_write( std::string const& content, std::string const& filename );
92 
98 void file_append( std::string const& content, std::string const& filename );
99 
100 // =================================================================================================
101 // Directory Access
102 // =================================================================================================
103 
110 bool is_dir( std::string const& path );
111 
115 bool dir_exists( std::string const& dir );
116 
124 void dir_create( std::string const& path, bool with_parents = true );
125 
129 std::string dir_normalize_path( std::string const& path );
130 
139 std::vector<std::string> dir_list_contents(
140  std::string const& dir,
141  bool full_path = false,
142  std::string const& regex = ""
143 );
144 
151 std::vector<std::string> dir_list_files(
152  std::string const& dir,
153  bool full_path = false,
154  std::string const& regex = ""
155 );
156 
163 std::vector<std::string> dir_list_directories(
164  std::string const& dir,
165  bool full_path = false,
166  std::string const& regex = ""
167 );
168 
169 // =================================================================================================
170 // Path Information
171 // =================================================================================================
172 
181 std::string current_path();
182 
191 std::string real_path( std::string const& path, bool resolve_link = true );
192 
193 // =================================================================================================
194 // File Information
195 // =================================================================================================
196 
200 std::unordered_map<std::string, std::string> file_info ( std::string const& filename );
201 
205 size_t file_size ( std::string const& filename );
206 
213 std::string file_path ( std::string const& filename );
214 
218 std::string file_basename ( std::string const& filename );
219 
226 std::string file_filename ( std::string const& filename );
227 
233 std::string file_extension ( std::string const& filename );
234 
235 // =================================================================================================
236 // File Names
237 // =================================================================================================
238 
259 bool is_valid_filename( std::string const& filename );
260 
282 std::string sanitize_filename( std::string const& filename );
283 
284 } // namespace utils
285 } // namespace genesis
286 
287 #endif // include guard
bool is_file(std::string const &path)
Return true iff the provided path is a file.
Definition: fs.cpp:73
void file_write(std::string const &content, std::string const &filename)
Write the content of a string to a file.
Definition: fs.cpp:142
std::unordered_map< std::string, std::string > file_info(std::string const &filename)
Return information about a file.
Definition: fs.cpp:653
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:275
std::string file_extension(std::string const &filename)
Return the extension name of a file.
Definition: fs.cpp:707
std::string file_path(std::string const &filename)
Return the path leading to a file.
Definition: fs.cpp:673
std::string current_path()
Return the current (working) directory, simiar to getcwd().
Definition: fs.cpp:421
void dir_create(std::string const &path, bool with_parents)
Create a directory.
Definition: fs.cpp:190
std::string file_read(std::string const &filename, bool detect_compression)
Return the contents of a file as a string.
Definition: fs.cpp:90
bool dir_exists(std::string const &dir)
Return true iff the directory exists.
Definition: fs.cpp:172
std::string file_filename(std::string const &filename)
Remove extension if present.
Definition: fs.cpp:696
Container namespace for all symbols of genesis in order to keep them separate when used as a library...
bool file_exists(std::string const &filename)
Return true iff the file exists.
Definition: fs.cpp:78
bool is_dir(std::string const &path)
Return true iff the provided path is a directory.
Definition: fs.cpp:167
size_t file_size(std::string const &filename)
Return the size of a file.
Definition: fs.cpp:666
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:285
std::string file_basename(std::string const &filename)
Remove directory name from file name if present.
Definition: fs.cpp:685
utils::Range< IteratorPath< true > > path(ElementType const &start, ElementType const &finish)
Definition: path.hpp:337
std::string real_path(std::string const &path, bool resolve_link)
Return the real path of a file or directory, similar to realpath().
Definition: fs.cpp:635
bool is_valid_filename(std::string const &filename)
Check whether a file name is valid.
Definition: fs.cpp:722
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:264
std::vector< std::string > file_read_lines(std::string const &filename, bool detect_compression)
Return the contents of a file as a vector of strings, one entry for each line.
Definition: fs.cpp:130
void file_append(std::string const &content, std::string const &filename)
Append the content of a string to a file.
Definition: fs.cpp:151
std::string sanitize_filename(std::string const &filename)
Remove or replace all invalid parts of a filename.
Definition: fs.cpp:750
bool path_exists(std::string const &path)
Return whether a path exists, i.e., is a file or directory.
Definition: fs.cpp:67
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:218