get-accessible-paths.cc
Go to the documentation of this file.
1 #include "boost/filesystem.hpp"
2 #include "cetlib/getenv.h"
3 #include "cetlib/split.h"
4 
5 #include <iostream>
6 #include <string>
7 #include <vector>
8 
9 using namespace std::string_literals;
10 namespace bfs = boost::filesystem;
11 
12 namespace {
13 
14  void
15  print_paths(std::string const& path_name,
16  std::string pattern_to_match,
17  bool const match_full_path)
18  {
19  bfs::path path{path_name};
20  if (exists(path) && is_directory(path)) {
21  std::vector<std::string> v;
22  for (auto const& x : bfs::directory_iterator(path)) {
23  if (match_full_path) {
24  auto const str = canonical(x.path()).string();
25  if (str.find(pattern_to_match) != 0) {
26  continue;
27  }
28  std::cout << str << ' ';
29  } else {
30  auto const str = canonical(x.path()).filename().string();
31  if (str.find(pattern_to_match) != 0) {
32  continue;
33  }
34  std::cout << str << ' ';
35  }
36  }
37  }
38  }
39 }
40 
41 int
42 main(int argc, char** argv)
43 {
44  if (argc != 3) {
45  return 1;
46  }
47 
48  std::string const envname{argv[1]};
49  std::string spec{argv[2]};
50  std::string to_match{spec};
51  auto const last_slash = to_match.find("/");
52  bool const slash_found{last_slash != std::string::npos};
53  std::string const prefix = slash_found ? to_match.substr(0, last_slash) : ""s;
54  if (slash_found) {
55  to_match.erase(0, last_slash + 1);
56  }
57 
58  if (spec.empty() || spec[0] == '/') {
59  print_paths("/"s + prefix, "/"s + to_match, true);
60  }
61 
62  if (spec[0] != '/') {
63  print_paths("./"s + prefix, to_match, false);
64  }
65 
66  if (spec[0] != '/' && spec.find("./") != 0) {
67  // Find paths on environment variable
68  auto const env = cet::getenv(envname);
69  std::vector<std::string> paths;
70  cet::split(env, ':', back_inserter(paths));
71  for (auto const& path : paths) {
72  if (path == "."s)
73  continue;
74  print_paths(path + prefix, to_match, false);
75  }
76  }
77 }
int main(int argc, char **argv)
std::string string
Definition: nybbler.cc:12
int find(char c, int index=0, bool cs=TRUE) const
Definition: qcstring.cpp:41
string filename
Definition: train.py:213
bool exists(std::string path)
std::string getenv(std::string const &name)
Definition: getenv.cc:15
list x
Definition: train.py:276
void split(std::string const &s, char c, OutIter dest)
Definition: split.h:35
static QCString * s
Definition: config.cpp:1042
static QCString str