OmniSciDB  91042dcc5b
 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 274 of file TableFunctionsCommon.cpp.

References glob_to_regex().

274  {
275  const std::filesystem::path file_or_directory_path(file_or_directory);
276  const auto file_status = std::filesystem::status(file_or_directory_path);
277 
278  std::vector<std::filesystem::path> fs_paths;
279  if (std::filesystem::is_regular_file(file_status)) {
280  fs_paths.emplace_back(file_or_directory_path);
281  return fs_paths;
282  } else if (std::filesystem::is_directory(file_status)) {
283  for (std::filesystem::directory_entry const& entry :
284  std::filesystem::directory_iterator(file_or_directory_path)) {
285  if (std::filesystem::is_regular_file(std::filesystem::status(entry))) {
286  fs_paths.emplace_back(entry.path());
287  }
288  }
289  return fs_paths;
290  } else {
291  const auto parent_path = file_or_directory_path.parent_path();
292  const auto parent_status = std::filesystem::status(parent_path);
293  if (std::filesystem::is_directory(parent_status)) {
294  const auto file_glob = file_or_directory_path.filename();
295  const std::regex glob_regex{glob_to_regex(file_glob.string(), false)};
296 
297  for (std::filesystem::directory_entry const& entry :
298  std::filesystem::directory_iterator(parent_path)) {
299  if (std::filesystem::is_regular_file(std::filesystem::status(entry))) {
300  const auto entry_filename = entry.path().filename().string();
301  if (std::regex_match(entry_filename, glob_regex)) {
302  fs_paths.emplace_back(entry.path());
303  }
304  }
305  }
306  return fs_paths;
307  }
308  }
309  return fs_paths;
310 }
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 247 of file TableFunctionsCommon.cpp.

Referenced by get_fs_paths().

247  {
248  // Note It is possible to automate checking if filesystem is case sensitive or not (e.g.
249  // by performing a test first time this function is ran)
250  std::string regex_string{glob};
251  // Escape all regex special chars:
252  regex_string = std::regex_replace(regex_string, std::regex("\\\\"), "\\\\");
253  regex_string = std::regex_replace(regex_string, std::regex("\\^"), "\\^");
254  regex_string = std::regex_replace(regex_string, std::regex("\\."), "\\.");
255  regex_string = std::regex_replace(regex_string, std::regex("\\$"), "\\$");
256  regex_string = std::regex_replace(regex_string, std::regex("\\|"), "\\|");
257  regex_string = std::regex_replace(regex_string, std::regex("\\("), "\\(");
258  regex_string = std::regex_replace(regex_string, std::regex("\\)"), "\\)");
259  regex_string = std::regex_replace(regex_string, std::regex("\\{"), "\\{");
260  regex_string = std::regex_replace(regex_string, std::regex("\\{"), "\\}");
261  regex_string = std::regex_replace(regex_string, std::regex("\\["), "\\[");
262  regex_string = std::regex_replace(regex_string, std::regex("\\]"), "\\]");
263  regex_string = std::regex_replace(regex_string, std::regex("\\+"), "\\+");
264  regex_string = std::regex_replace(regex_string, std::regex("\\/"), "\\/");
265  // Convert wildcard specific chars '*?' to their regex equivalents:
266  regex_string = std::regex_replace(regex_string, std::regex("\\?"), ".");
267  regex_string = std::regex_replace(regex_string, std::regex("\\*"), ".*");
268 
269  return std::regex(
270  regex_string,
271  case_sensitive ? std::regex_constants::ECMAScript : std::regex_constants::icase);
272 }
std::vector< std::string > glob(const std::string &pattern)

+ Here is the caller graph for this function: