OmniSciDB  a987f07e93
 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 463 of file TableFunctionsCommon.cpp.

References glob_to_regex().

463  {
464  const std::filesystem::path file_or_directory_path(file_or_directory);
465  const auto file_status = std::filesystem::status(file_or_directory_path);
466 
467  std::vector<std::filesystem::path> fs_paths;
468  if (std::filesystem::is_regular_file(file_status)) {
469  fs_paths.emplace_back(file_or_directory_path);
470  return fs_paths;
471  } else if (std::filesystem::is_directory(file_status)) {
472  for (std::filesystem::directory_entry const& entry :
473  std::filesystem::directory_iterator(file_or_directory_path)) {
474  if (std::filesystem::is_regular_file(std::filesystem::status(entry))) {
475  fs_paths.emplace_back(entry.path());
476  }
477  }
478  return fs_paths;
479  } else {
480  const auto parent_path = file_or_directory_path.parent_path();
481  const auto parent_status = std::filesystem::status(parent_path);
482  if (std::filesystem::is_directory(parent_status)) {
483  const auto file_glob = file_or_directory_path.filename();
484  const std::regex glob_regex{glob_to_regex(file_glob.string(), false)};
485 
486  for (std::filesystem::directory_entry const& entry :
487  std::filesystem::directory_iterator(parent_path)) {
488  if (std::filesystem::is_regular_file(std::filesystem::status(entry))) {
489  const auto entry_filename = entry.path().filename().string();
490  if (std::regex_match(entry_filename, glob_regex)) {
491  fs_paths.emplace_back(entry.path());
492  }
493  }
494  }
495  return fs_paths;
496  }
497  }
498  return fs_paths;
499 }
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 436 of file TableFunctionsCommon.cpp.

Referenced by get_fs_paths().

436  {
437  // Note It is possible to automate checking if filesystem is case sensitive or not (e.g.
438  // by performing a test first time this function is ran)
439  std::string regex_string{glob};
440  // Escape all regex special chars:
441  regex_string = std::regex_replace(regex_string, std::regex("\\\\"), "\\\\");
442  regex_string = std::regex_replace(regex_string, std::regex("\\^"), "\\^");
443  regex_string = std::regex_replace(regex_string, std::regex("\\."), "\\.");
444  regex_string = std::regex_replace(regex_string, std::regex("\\$"), "\\$");
445  regex_string = std::regex_replace(regex_string, std::regex("\\|"), "\\|");
446  regex_string = std::regex_replace(regex_string, std::regex("\\("), "\\(");
447  regex_string = std::regex_replace(regex_string, std::regex("\\)"), "\\)");
448  regex_string = std::regex_replace(regex_string, std::regex("\\{"), "\\{");
449  regex_string = std::regex_replace(regex_string, std::regex("\\{"), "\\}");
450  regex_string = std::regex_replace(regex_string, std::regex("\\["), "\\[");
451  regex_string = std::regex_replace(regex_string, std::regex("\\]"), "\\]");
452  regex_string = std::regex_replace(regex_string, std::regex("\\+"), "\\+");
453  regex_string = std::regex_replace(regex_string, std::regex("\\/"), "\\/");
454  // Convert wildcard specific chars '*?' to their regex equivalents:
455  regex_string = std::regex_replace(regex_string, std::regex("\\?"), ".");
456  regex_string = std::regex_replace(regex_string, std::regex("\\*"), ".*");
457 
458  return std::regex(
459  regex_string,
460  case_sensitive ? std::regex_constants::ECMAScript : std::regex_constants::icase);
461 }
std::vector< std::string > glob(const std::string &pattern)

+ Here is the caller graph for this function: