OmniSciDB  cde582ebc3
 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 587 of file TableFunctionsCommon.cpp.

References glob_to_regex().

587  {
588  const std::filesystem::path file_or_directory_path(file_or_directory);
589  const auto file_status = std::filesystem::status(file_or_directory_path);
590 
591  std::vector<std::filesystem::path> fs_paths;
592  if (std::filesystem::is_regular_file(file_status)) {
593  fs_paths.emplace_back(file_or_directory_path);
594  return fs_paths;
595  } else if (std::filesystem::is_directory(file_status)) {
596  for (std::filesystem::directory_entry const& entry :
597  std::filesystem::directory_iterator(file_or_directory_path)) {
598  if (std::filesystem::is_regular_file(std::filesystem::status(entry))) {
599  fs_paths.emplace_back(entry.path());
600  }
601  }
602  return fs_paths;
603  } else {
604  const auto parent_path = file_or_directory_path.parent_path();
605  const auto parent_status = std::filesystem::status(parent_path);
606  if (std::filesystem::is_directory(parent_status)) {
607  const auto file_glob = file_or_directory_path.filename();
608  const std::regex glob_regex{glob_to_regex(file_glob.string(), false)};
609 
610  for (std::filesystem::directory_entry const& entry :
611  std::filesystem::directory_iterator(parent_path)) {
612  if (std::filesystem::is_regular_file(std::filesystem::status(entry))) {
613  const auto entry_filename = entry.path().filename().string();
614  if (std::regex_match(entry_filename, glob_regex)) {
615  fs_paths.emplace_back(entry.path());
616  }
617  }
618  }
619  return fs_paths;
620  }
621  }
622  return fs_paths;
623 }
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 560 of file TableFunctionsCommon.cpp.

Referenced by get_fs_paths().

560  {
561  // Note It is possible to automate checking if filesystem is case sensitive or not (e.g.
562  // by performing a test first time this function is ran)
563  std::string regex_string{glob};
564  // Escape all regex special chars:
565  regex_string = std::regex_replace(regex_string, std::regex("\\\\"), "\\\\");
566  regex_string = std::regex_replace(regex_string, std::regex("\\^"), "\\^");
567  regex_string = std::regex_replace(regex_string, std::regex("\\."), "\\.");
568  regex_string = std::regex_replace(regex_string, std::regex("\\$"), "\\$");
569  regex_string = std::regex_replace(regex_string, std::regex("\\|"), "\\|");
570  regex_string = std::regex_replace(regex_string, std::regex("\\("), "\\(");
571  regex_string = std::regex_replace(regex_string, std::regex("\\)"), "\\)");
572  regex_string = std::regex_replace(regex_string, std::regex("\\{"), "\\{");
573  regex_string = std::regex_replace(regex_string, std::regex("\\{"), "\\}");
574  regex_string = std::regex_replace(regex_string, std::regex("\\["), "\\[");
575  regex_string = std::regex_replace(regex_string, std::regex("\\]"), "\\]");
576  regex_string = std::regex_replace(regex_string, std::regex("\\+"), "\\+");
577  regex_string = std::regex_replace(regex_string, std::regex("\\/"), "\\/");
578  // Convert wildcard specific chars '*?' to their regex equivalents:
579  regex_string = std::regex_replace(regex_string, std::regex("\\?"), ".");
580  regex_string = std::regex_replace(regex_string, std::regex("\\*"), ".*");
581 
582  return std::regex(
583  regex_string,
584  case_sensitive ? std::regex_constants::ECMAScript : std::regex_constants::icase);
585 }
std::vector< std::string > glob(const std::string &pattern)

+ Here is the caller graph for this function: