A library for working with phylogenetic and population genetic data.
v0.27.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-2021 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 
56 bool is_file( std::string const& path );
57 
61 bool file_exists( std::string const& filename );
62 
71 std::string file_read(
72  std::string const& filename,
73  bool detect_compression = true
74 );
75 
84 std::vector<std::string> file_read_lines(
85  std::string const& filename,
86  bool detect_compression = true
87 );
88 
97 void file_write(
98  std::string const& content,
99  std::string const& filename,
100  bool create_dirs = true
101 );
102 
108 void file_append(
109  std::string const& content,
110  std::string const& filename,
111  bool create_dirs = true
112 );
113 
114 // =================================================================================================
115 // Directory Access
116 // =================================================================================================
117 
124 bool is_dir( std::string const& path );
125 
129 bool dir_exists( std::string const& dir );
130 
138 void dir_create( std::string const& path, bool with_parents = true );
139 
143 std::string dir_normalize_path( std::string const& path );
144 
153 std::vector<std::string> dir_list_contents(
154  std::string const& dir,
155  bool full_path = false,
156  std::string const& regex = ""
157 );
158 
165 std::vector<std::string> dir_list_files(
166  std::string const& dir,
167  bool full_path = false,
168  std::string const& regex = ""
169 );
170 
177 std::vector<std::string> dir_list_directories(
178  std::string const& dir,
179  bool full_path = false,
180  std::string const& regex = ""
181 );
182 
183 // =================================================================================================
184 // Path Information
185 // =================================================================================================
186 
195 std::string current_path();
196 
205 std::string real_path( std::string const& path, bool resolve_link = true );
206 
207 // =================================================================================================
208 // File Information
209 // =================================================================================================
210 
214 std::unordered_map<std::string, std::string> file_info ( std::string const& filename );
215 
219 size_t file_size( std::string const& filename );
220 
227 std::string file_path( std::string const& filename );
228 
232 std::string file_basename( std::string const& filename );
233 
251 std::string file_basename(
252  std::string const& filename,
253  std::vector<std::string> const& remove_extensions
254 );
255 
262 std::string file_filename( std::string const& filename );
263 
269 std::string file_extension( std::string const& filename );
270 
271 // =================================================================================================
272 // File Names
273 // =================================================================================================
274 
295 bool is_valid_filename( std::string const& filename );
296 
318 std::string sanitize_filename( std::string const& filename );
319 
320 } // namespace utils
321 } // namespace genesis
322 
323 #endif // include guard
genesis::utils::current_path
std::string current_path()
Return the current (working) directory, simiar to getcwd().
Definition: fs.cpp:423
genesis::utils::dir_create
void dir_create(std::string const &path, bool with_parents)
Create a directory.
Definition: fs.cpp:192
genesis::utils::file_size
size_t file_size(std::string const &filename)
Return the size of a file.
Definition: fs.cpp:668
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:287
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:655
genesis::utils::file_path
std::string file_path(std::string const &filename)
Return the path leading to a file.
Definition: fs.cpp:675
genesis::utils::dir_exists
bool dir_exists(std::string const &dir)
Return true iff the directory exists.
Definition: fs.cpp:174
genesis::utils::sanitize_filename
std::string sanitize_filename(std::string const &filename)
Remove or replace all invalid parts of a filename.
Definition: fs.cpp:762
genesis::utils::file_filename
std::string file_filename(std::string const &filename)
Remove extension if present.
Definition: fs.cpp:710
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:90
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:637
genesis::utils::is_dir
bool is_dir(std::string const &path)
Return true iff the provided path is a directory.
Definition: fs.cpp:169
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:130
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:67
genesis::utils::is_file
bool is_file(std::string const &path)
Return true iff the provided path is a file.
Definition: fs.cpp:73
genesis::utils::file_exists
bool file_exists(std::string const &filename)
Return true iff the file exists.
Definition: fs.cpp:78
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:266
genesis::utils::file_basename
std::string file_basename(std::string const &filename)
Remove directory name from file name if present.
Definition: fs.cpp:687
genesis::utils::is_valid_filename
bool is_valid_filename(std::string const &filename)
Check whether a file name is valid.
Definition: fs.cpp:734
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:220
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:142
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:149
genesis::utils::file_extension
std::string file_extension(std::string const &filename)
Return the extension name of a file.
Definition: fs.cpp:720
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:277