|
A library for working with phylogenetic and population genetic data.
v0.27.0
|
|
Go to the documentation of this file.
70 return ( stat(
path.c_str(), &info ) == 0 );
85 std::ifstream instream(filename);
86 instream.seekg( 0, std::ios::end);
87 return instream.good() && !
is_dir( filename );
90 std::string
file_read( std::string
const& filename,
bool detect_compression )
99 if( !instream.good() ) {
100 throw std::runtime_error(
"Cannot read from file '" + filename +
"'." );
105 std::istreambuf_iterator<char>(instream),
106 std::istreambuf_iterator<char>()
112 std::ifstream instream(filename);
113 if( !instream.good() ) {
114 throw std::runtime_error(
"Cannot read from file '" + filename +
"'." );
118 instream.seekg(0, std::ios::end);
119 str.reserve(instream.tellg());
120 instream.seekg(0, std::ios::beg);
124 std::istreambuf_iterator<char>(instream),
125 std::istreambuf_iterator<char>()
130 std::vector<std::string>
file_read_lines( std::string
const& filename,
bool detect_compression )
132 std::vector<std::string> result;
136 assert( !it || *it ==
'\n' );
142 void file_write( std::string
const& content, std::string
const& filename,
bool create_dirs )
149 void file_append( std::string
const& content, std::string
const& filename,
bool create_dirs )
158 std::ofstream out_stream( filename, std::ofstream::app );
159 if( out_stream.fail() ) {
160 throw std::runtime_error(
"Cannot append to file '" + filename +
"'." );
162 out_stream << content;
177 if (stat (dir.c_str(), &info) != 0) {
180 return static_cast<bool>( info.st_mode & S_IFDIR );
199 if( path_no_bs.empty() ) {
205 if( !
dir_exists( path_no_bs ) && path_no_bs.size() > 0 ) {
211 if( stat (
path.c_str(), &info) != 0 ) {
212 if( mkdir(
path.c_str(), mode ) != 0 && errno != EEXIST ) {
213 throw std::runtime_error(
"Cannot create directory: " +
path );
215 }
else if( !S_ISDIR(info.st_mode) ) {
216 throw std::runtime_error(
"Path exists, but is not a directory: " +
path );
226 std::string
const& dir,
228 std::string
const& regex,
229 std::function<
bool( std::string
const& )> condition
231 std::vector<std::string> list;
233 std::regex pattern( regex );
238 if( ( dp = opendir( dir.c_str() )) ==
nullptr) {
239 throw std::runtime_error(
"Cannot open directory '" + dir +
"'." );
241 while ((dirp = readdir(dp)) !=
nullptr) {
242 auto const fn = std::string( dirp->d_name );
244 if (fn ==
"." || fn ==
"..") {
247 if( ! regex.empty() && ! regex_match( fn, pattern ) ) {
250 if( ! condition( dir_path + fn ) ) {
255 list.push_back( dir_path + fn );
257 list.push_back( fn );
267 std::string
const& dir,
269 std::string
const& regex
272 dir, full_path, regex,
273 []( std::string
const& ){
return true; }
278 std::string
const& dir,
280 std::string
const& regex
288 std::string
const& dir,
290 std::string
const& regex
293 dir, full_path, regex,
is_dir
307 char temp[ PATH_MAX ];
309 if( getcwd( temp, PATH_MAX ) !=
nullptr ) {
316 throw std::runtime_error(
"Cannot read current directory. Access denied." );
319 throw std::runtime_error(
"Cannot read current directory. Insufficient storage." );
322 std::ostringstream str;
323 str <<
"Cannot read current directory. Unrecognised error: " << error;
324 throw std::runtime_error( str.str() );
338 typedef std::pair<dev_t, ino_t> file_id;
341 int start_fd = open(
".", O_RDONLY);
342 if( start_fd == -1 ) {
343 throw std::runtime_error(
"Cannot read current directory." );
347 if( fstat(start_fd, &sb) ) {
348 throw std::runtime_error(
"Cannot read current directory." );
352 file_id current_id(sb.st_dev, sb.st_ino);
353 if( stat(
"/", &sb )) {
354 throw std::runtime_error(
"Cannot read current directory." );
357 std::vector<std::string> path_components;
358 file_id root_id(sb.st_dev, sb.st_ino);
361 while( current_id != root_id ) {
365 if( ! chdir(
"..") ) {
366 DIR *dir = opendir(
".");
371 while(( entry = readdir(dir) )) {
373 strcmp(entry->d_name,
".")
374 && strcmp(entry->d_name,
"..")
375 && !lstat(entry->d_name, &sb)
377 file_id child_id(sb.st_dev, sb.st_ino);
380 if( child_id == current_id ) {
381 path_components.push_back(entry->d_name);
390 if( pushed && !stat(
".", &sb) ) {
391 current_id = file_id(sb.st_dev, sb.st_ino);
404 if( current_id == root_id ) {
408 for(
auto i = path_components.rbegin(); i != path_components.rend(); ++i ) {
412 throw std::runtime_error(
"Cannot read current directory." );
415 if( fchdir(start_fd) ) {
416 throw std::runtime_error(
"Cannot change directory." );
426 #if defined( _WIN32 ) || defined( _WIN64 )
444 auto slash_pos =
path.rfind(
"/");
447 if (slash_pos != std::string::npos) {
451 dir =
path.substr(0, slash_pos);
454 base =
path.substr(slash_pos);
465 int start_fd = open(
".", O_RDONLY);
466 if (start_fd == -1) {
467 throw std::runtime_error(
"Cannot open current directory." );
471 if( chdir(dir.c_str()) ) {
472 throw std::runtime_error(
"Cannot change directory." );
479 if( fchdir(start_fd) ) {
480 throw std::runtime_error(
"Cannot change directory." );
495 return resolved_path + base;
500 bool success =
false;
503 char *buf =
new(std::nothrow)
char[
length+1];
507 ssize_t amount = ::readlink(
path.c_str(), buf,
length+1);
510 if ((amount > 0) && (amount <=
length)) {
529 path = dir + newbase;
537 typedef std::pair<dev_t, ino_t> file_id;
539 bool success =
false;
542 std::string
path = start;
544 std::set<file_id> seen_links;
549 resolved_link =
false;
550 if (!lstat(
path.c_str(), &sb)) {
551 file_id current_id(sb.st_dev, sb.st_ino);
553 if (seen_links.find(current_id) == seen_links.end()) {
555 seen_links.insert(current_id);
558 if (S_ISLNK(sb.st_mode)) {
562 if (newpath[0] ==
'/') {
568 resolved_link =
true;
581 }
while (resolved_link);
598 if( stat(
path.c_str(), &sb) ) {
599 throw std::runtime_error(
"Cannot read path." );
602 if( S_ISDIR( sb.st_mode )) {
608 throw std::runtime_error(
"Cannot determine real path." );
629 char resolved_path[PATH_MAX];
630 auto ptr = realpath(
path.c_str(), resolved_path );
632 throw std::runtime_error(
"Cannot determine real path." );
634 return std::string( ptr );
640 #if defined( _WIN32 ) || defined( _WIN64 )
655 std::unordered_map<std::string, std::string>
file_info( std::string
const& filename )
658 std::unordered_map<std::string, std::string> res;
661 res[
"basename"] = basename;
670 auto result = filename;
671 std::ifstream in(result, std::ifstream::ate | std::ifstream::binary);
672 return static_cast<size_t>(in.tellg());
677 auto result = filename;
678 const size_t idx = result.find_last_of(
"\\/");
679 if( idx == std::string::npos ) {
689 auto result = filename;
690 const size_t idx = result.find_last_of(
"\\/");
691 if( idx != std::string::npos ) {
692 result.erase(0, idx + 1);
698 std::string
const& filename,
699 std::vector<std::string>
const& remove_extensions
702 for(
auto const& ext : remove_extensions ) {
704 bn.erase( bn.size() - ext.size() );
712 auto result = filename;
713 const size_t idx = result.rfind(
'.');
714 if( idx != 0 && idx != std::string::npos ) {
722 auto result = filename;
723 const size_t idx = result.rfind(
'.');
724 if( idx != 0 && idx != std::string::npos ) {
725 result.erase(0, idx + 1);
737 if(
trim( filename ) ==
"" ) {
747 if( filename.find_first_of(
"<>:\"\\/|?*" ) != std::string::npos ) {
753 for(
auto c : filename ) {
754 if( ! isprint( c ) ) {
765 std::string result =
"";
766 result.reserve( filename.size() );
769 for(
auto c : filename ) {
776 result =
trim( result );
780 throw std::runtime_error(
"Invalid filename." );
std::string current_path()
Return the current (working) directory, simiar to getcwd().
static bool readlink_internal_(const std::string &path, std::string &buffer, ssize_t length)
Provides some valuable algorithms that are not part of the C++ 11 STL.
static std::string chdir_getcwd_(std::string const &dir)
std::string trim_right(std::string const &s, std::string const &delimiters)
Return a copy of the input string, with left trimmed white spaces.
static bool symlink_resolve_(const std::string &start, std::string &end)
Provides functions for accessing the file system.
void dir_create(std::string const &path, bool with_parents)
Create a directory.
size_t file_size(std::string const &filename)
Return the size of a file.
std::shared_ptr< BaseInputSource > from_file(std::string const &file_name, bool detect_compression=true)
Obtain an input source for reading from a file.
std::vector< std::string > dir_list_directories(std::string const &dir, bool full_path, std::string const ®ex)
Get a list of directories in a directory.
static void relative_dir_base_split_(std::string const &path, std::string &dir, std::string &base)
std::unordered_map< std::string, std::string > file_info(std::string const &filename)
Return information about a file.
static std::string current_path_getcwd_()
double length(Tree const &tree)
Get the length of the tree, i.e., the sum of all branch lengths.
std::string trim(std::string const &s, std::string const &delimiters)
Return a copy of the input string, with trimmed white spaces.
Input file stream that offers on-the-fly gzip-decompression if needed.
std::string file_path(std::string const &filename)
Return the path leading to a file.
std::string replace_all_chars(std::string const &text, std::string const &search_chars, char replace)
Replace all occurrences of the search_chars in text by the replace char.
static std::string current_path_unix_()
bool dir_exists(std::string const &dir)
Return true iff the directory exists.
std::string sanitize_filename(std::string const &filename)
Remove or replace all invalid parts of a filename.
std::string file_filename(std::string const &filename)
Remove extension if present.
static void build_path_base_swap_(std::string &path, const std::string &newbase)
utils::Range< IteratorPath< true > > path(ElementType const &start, ElementType const &finish)
Provides some commonly used string utility functions.
std::string file_read(std::string const &filename, bool detect_compression)
Return the contents of a file as a string.
std::string real_path(std::string const &path, bool resolve_link)
Return the real path of a file or directory, similar to realpath().
static std::string realpath_file_(const std::string &path)
bool is_gzip_compressed_file(std::string const &file_name)
Return whether a given file is gzip-compressed.
bool starts_with(std::string const &text, std::string const &start)
Return whether a string starts with another string.
bool is_dir(std::string const &path)
Return true iff the provided path is a directory.
static std::string real_path_unix_(std::string const &path, bool resolve_link)
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.
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
bool path_exists(std::string const &path)
Return whether a path exists, i.e., is a file or directory.
bool ends_with(std::string const &text, std::string const &ending)
Return whether a string ends with another string.
static std::string real_path_realpath_(std::string const &path, bool resolve_link)
bool is_file(std::string const &path)
Return true iff the provided path is a file.
bool file_exists(std::string const &filename)
Return true iff the file exists.
std::vector< std::string > dir_list_contents(std::string const &dir, bool full_path, std::string const ®ex)
Get a list of files and directories in a directory.
static std::vector< std::string > dir_list_contents_(std::string const &dir, bool full_path, std::string const ®ex, std::function< bool(std::string const &)> condition)
std::string file_basename(std::string const &filename)
Remove directory name from file name if present.
bool is_valid_filename(std::string const &filename)
Check whether a file name is valid.
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.
void file_write(std::string const &content, std::string const &filename, bool create_dirs)
Write the content of a string to a file.
void file_append(std::string const &content, std::string const &filename, bool create_dirs)
Append the content of a string to a file.
std::string file_extension(std::string const &filename)
Return the extension name of a file.
std::string read_to_end_of_line(InputStream &source)
Lexing function that reads until the end of the line (i.e., to the new line char),...
std::vector< std::string > dir_list_files(std::string const &dir, bool full_path, std::string const ®ex)
Get a list of files in a directory.
void file_output_stream(std::string const &filename, std::ofstream &out_stream, std::ios_base::openmode mode=std::ios_base::out, bool create_dirs=true)
Helper function to obtain an output stream to a file.