A library for working with phylogenetic and population genetic data.
v0.32.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-2022 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 <lczech@carnegiescience.edu>
23  Department of Plant Biology, Carnegie Institution For Science
24  260 Panama Street, Stanford, CA 94305, USA
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 
57 bool is_file( std::string const& path );
58 
64 bool file_exists( std::string const& filename );
65 
75 bool file_is_readable( std::string const& filename );
76 
83 bool file_is_readable( std::string const& filename, std::string& err_str );
84 
93 std::string file_read(
94  std::string const& filename,
95  bool detect_compression = true
96 );
97 
106 std::vector<std::string> file_read_lines(
107  std::string const& filename,
108  bool detect_compression = true
109 );
110 
119 void file_write(
120  std::string const& content,
121  std::string const& filename,
122  bool create_dirs = true
123 );
124 
130 void file_append(
131  std::string const& content,
132  std::string const& filename,
133  bool create_dirs = true
134 );
135 
136 // =================================================================================================
137 // Directory Access
138 // =================================================================================================
139 
146 bool is_dir( std::string const& path );
147 
151 bool dir_exists( std::string const& dir );
152 
160 void dir_create( std::string const& path, bool with_parents = true );
161 
165 std::string dir_normalize_path( std::string const& path );
166 
175 std::vector<std::string> dir_list_contents(
176  std::string const& dir,
177  bool full_path = false,
178  std::string const& regex = ""
179 );
180 
187 std::vector<std::string> dir_list_files(
188  std::string const& dir,
189  bool full_path = false,
190  std::string const& regex = ""
191 );
192 
199 std::vector<std::string> dir_list_directories(
200  std::string const& dir,
201  bool full_path = false,
202  std::string const& regex = ""
203 );
204 
205 // =================================================================================================
206 // Path Information
207 // =================================================================================================
208 
217 std::string current_path();
218 
227 std::string real_path( std::string const& path, bool resolve_link = true );
228 
229 // =================================================================================================
230 // File Information
231 // =================================================================================================
232 
236 std::unordered_map<std::string, std::string> file_info ( std::string const& filename );
237 
241 size_t file_size( std::string const& filename );
242 
249 std::string file_path( std::string const& filename );
250 
254 std::string file_basename( std::string const& filename );
255 
273 std::string file_basename(
274  std::string const& filename,
275  std::vector<std::string> const& remove_extensions
276 );
277 
284 std::string file_filename( std::string const& filename );
285 
291 std::string file_extension( std::string const& filename );
292 
293 // =================================================================================================
294 // File Names
295 // =================================================================================================
296 
317 bool is_valid_filename( std::string const& filename );
318 
340 std::string sanitize_filename( std::string const& filename );
341 
342 } // namespace utils
343 } // namespace genesis
344 
345 #endif // include guard
genesis::utils::current_path
std::string current_path()
Return the current (working) directory, simiar to getcwd().
Definition: fs.cpp:502
genesis::utils::dir_create
void dir_create(std::string const &path, bool with_parents)
Create a directory.
Definition: fs.cpp:236
genesis::utils::file_size
size_t file_size(std::string const &filename)
Return the size of a file.
Definition: fs.cpp:762
genesis::utils::dir_list_directories
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:347
genesis::utils::file_info
std::unordered_map< std::string, std::string > file_info(std::string const &filename)
Return information about a file.
Definition: fs.cpp:749
genesis::utils::file_path
std::string file_path(std::string const &filename)
Return the path leading to a file.
Definition: fs.cpp:776
genesis::utils::dir_exists
bool dir_exists(std::string const &dir)
Return true iff the directory exists.
Definition: fs.cpp:218
genesis::utils::sanitize_filename
std::string sanitize_filename(std::string const &filename)
Remove or replace all invalid parts of a filename.
Definition: fs.cpp:863
genesis::utils::file_filename
std::string file_filename(std::string const &filename)
Remove extension if present.
Definition: fs.cpp:811
genesis::tree::path
utils::Range< IteratorPath< true > > path(ElementType const &start, ElementType const &finish)
Definition: path.hpp:337
genesis::utils::file_read
std::string file_read(std::string const &filename, bool detect_compression)
Return the contents of a file as a string.
Definition: fs.cpp:126
genesis::utils::real_path
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:731
genesis::utils::is_dir
bool is_dir(std::string const &path)
Return true iff the provided path is a directory.
Definition: fs.cpp:213
genesis::utils::file_read_lines
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:171
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
genesis::utils::path_exists
bool path_exists(std::string const &path)
Return whether a path exists, i.e., is a file or directory.
Definition: fs.cpp:74
genesis::utils::file_is_readable
bool file_is_readable(std::string const &filename)
Return whether a file is readable.
Definition: fs.cpp:108
genesis::utils::is_file
bool is_file(std::string const &path)
Return true iff the provided path is a (readable) file.
Definition: fs.cpp:90
genesis::utils::file_exists
bool file_exists(std::string const &filename)
Return true iff the file exists (and is in fact a file, and not, e.g., a directory).
Definition: fs.cpp:95
genesis::utils::dir_list_contents
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:318
genesis::utils::file_basename
std::string file_basename(std::string const &filename)
Remove directory name from file name if present.
Definition: fs.cpp:788
genesis::utils::is_valid_filename
bool is_valid_filename(std::string const &filename)
Check whether a file name is valid.
Definition: fs.cpp:835
genesis::utils::dir_normalize_path
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:269
genesis::utils::file_write
void file_write(std::string const &content, std::string const &filename, bool create_dirs)
Write the content of a string to a file.
Definition: fs.cpp:183
genesis::utils::file_append
void file_append(std::string const &content, std::string const &filename, bool create_dirs)
Append the content of a string to a file.
Definition: fs.cpp:190
genesis::utils::file_extension
std::string file_extension(std::string const &filename)
Return the extension name of a file.
Definition: fs.cpp:821
genesis::utils::dir_list_files
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:329