Scarab  v3.9.3
Project 8 C++ Utility Library
path.cc
Go to the documentation of this file.
1 /*
2  * path.cc
3  *
4  * Created on: Jan 22, 2016
5  * Author: nsoblath
6  */
7 
8 #define SCARAB_API_EXPORTS
9 
10 #include "path.hh"
11 
12 //#include <iostream>
13 #include <regex>
14 
15 using std::string;
16 using boost::filesystem::absolute;
17 
18 namespace scarab
19 {
20  path SCARAB_API expand_path( const string& a_path )
21  {
22  string t_exp_path( a_path );
23 #ifndef _WIN32
24  // POSIX-shell-like expansion
25  wordexp_t t_expansion_result;
26  wordexp( a_path.c_str(), &t_expansion_result, 0 );
27  if( t_expansion_result.we_wordc > 0 )
28  {
29  t_exp_path = string( t_expansion_result.we_wordv[0] );
30  //std::cout << "#### expanded path: " << t_exp_path << std::endl;
31  }
32  wordfree( &t_expansion_result );
33 #endif
34  // do canonical expansion; return the absolute path
35  return absolute( t_exp_path );
36  }
37 
38  std::vector< path > SCARAB_API glob( const string& a_path, const string& a_pattern )
39  {
40  std::vector< path > t_file_paths;
41  path t_abs_path = expand_path( a_path );
42  for( fs::directory_entry& dir_entry: fs::recursive_directory_iterator(t_abs_path) )
43  {
44  path t_file_path=dir_entry.path();
45  if ( ! std::regex_search( t_file_path.string(), std::regex(a_pattern) ) ) continue;
46  t_file_paths.push_back( t_file_path );
47  }
48  return t_file_paths;
49  }
50 }
fs::path path
Definition: path.hh:26
path expand_path(const string &a_path)
Definition: path.cc:20
#define SCARAB_API
Definition: scarab_api.hh:24
std::vector< path > glob(const string &a_path, const string &a_pattern)
Definition: path.cc:38