OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FileUtilities Namespace Reference

Functions

std::regex glob_to_regex (const std::string &glob, bool case_sensitive=false)
 
std::vector
< std::filesystem::path > 
get_fs_paths (const std::string &file_or_directory)
 

Function Documentation

std::vector< std::filesystem::path > FileUtilities::get_fs_paths ( const std::string &  file_or_directory)

Definition at line 515 of file TableFunctionsCommon.cpp.

References glob_to_regex().

515  {
516  const std::filesystem::path file_or_directory_path(file_or_directory);
517  const auto file_status = std::filesystem::status(file_or_directory_path);
518 
519  std::vector<std::filesystem::path> fs_paths;
520  if (std::filesystem::is_regular_file(file_status)) {
521  fs_paths.emplace_back(file_or_directory_path);
522  return fs_paths;
523  } else if (std::filesystem::is_directory(file_status)) {
524  for (std::filesystem::directory_entry const& entry :
525  std::filesystem::directory_iterator(file_or_directory_path)) {
526  if (std::filesystem::is_regular_file(std::filesystem::status(entry))) {
527  fs_paths.emplace_back(entry.path());
528  }
529  }
530  return fs_paths;
531  } else {
532  const auto parent_path = file_or_directory_path.parent_path();
533  const auto parent_status = std::filesystem::status(parent_path);
534  if (std::filesystem::is_directory(parent_status)) {
535  const auto file_glob = file_or_directory_path.filename();
536  const std::regex glob_regex{glob_to_regex(file_glob.string(), false)};
537 
538  for (std::filesystem::directory_entry const& entry :
539  std::filesystem::directory_iterator(parent_path)) {
540  if (std::filesystem::is_regular_file(std::filesystem::status(entry))) {
541  const auto entry_filename = entry.path().filename().string();
542  if (std::regex_match(entry_filename, glob_regex)) {
543  fs_paths.emplace_back(entry.path());
544  }
545  }
546  }
547  return fs_paths;
548  }
549  }
550  return fs_paths;
551 }
std::regex glob_to_regex(const std::string &glob, bool case_sensitive=false)

+ Here is the call graph for this function:

std::regex FileUtilities::glob_to_regex ( const std::string &  glob,
bool  case_sensitive = false 
)

Definition at line 488 of file TableFunctionsCommon.cpp.

Referenced by get_fs_paths().

488  {
489  // Note It is possible to automate checking if filesystem is case sensitive or not (e.g.
490  // by performing a test first time this function is ran)
491  std::string regex_string{glob};
492  // Escape all regex special chars:
493  regex_string = std::regex_replace(regex_string, std::regex("\\\\"), "\\\\");
494  regex_string = std::regex_replace(regex_string, std::regex("\\^"), "\\^");
495  regex_string = std::regex_replace(regex_string, std::regex("\\."), "\\.");
496  regex_string = std::regex_replace(regex_string, std::regex("\\$"), "\\$");
497  regex_string = std::regex_replace(regex_string, std::regex("\\|"), "\\|");
498  regex_string = std::regex_replace(regex_string, std::regex("\\("), "\\(");
499  regex_string = std::regex_replace(regex_string, std::regex("\\)"), "\\)");
500  regex_string = std::regex_replace(regex_string, std::regex("\\{"), "\\{");
501  regex_string = std::regex_replace(regex_string, std::regex("\\{"), "\\}");
502  regex_string = std::regex_replace(regex_string, std::regex("\\["), "\\[");
503  regex_string = std::regex_replace(regex_string, std::regex("\\]"), "\\]");
504  regex_string = std::regex_replace(regex_string, std::regex("\\+"), "\\+");
505  regex_string = std::regex_replace(regex_string, std::regex("\\/"), "\\/");
506  // Convert wildcard specific chars '*?' to their regex equivalents:
507  regex_string = std::regex_replace(regex_string, std::regex("\\?"), ".");
508  regex_string = std::regex_replace(regex_string, std::regex("\\*"), ".*");
509 
510  return std::regex(
511  regex_string,
512  case_sensitive ? std::regex_constants::ECMAScript : std::regex_constants::icase);
513 }
std::vector< std::string > glob(const std::string &pattern)

+ Here is the caller graph for this function: