search_path.h
Go to the documentation of this file.
1 #ifndef cetlib_search_path_h
2 #define cetlib_search_path_h
3 
4 // ======================================================================
5 //
6 // search_path: Seek filename or pattern in a given list of pathnames
7 //
8 // ======================================================================
9 
11 
12 #include <cstdlib>
13 #include <new>
14 #include <ostream>
15 #include <string>
16 #include <vector>
17 
18 namespace cet {
19  // A search_path is used to manage the use of a PATH-like
20  // environment variable. It is created from the name of an
21  // environment variable; the value associated with this variable is
22  // expected to be a colon-separated list of directories. The
23  // search_path provides facilities for finding files in these
24  // directories.
25  class search_path;
26 
27  struct path_tag_t {}; // Distinguish calling signatures.
28 
29  extern path_tag_t const path_tag;
30 
31  std::ostream& operator<<(std::ostream& os, search_path const& p);
32 }
33 
34 // ----------------------------------------------------------------------
35 
37 public:
38  // Autodetect environment variables (no ':').
39  explicit search_path(std::string const& env_name_or_path);
40 
41  // Specify environent variable (no throw on missing).
42  search_path(std::string const& env_name, std::nothrow_t);
43 
44  // Specify path.
46 
47  // If an environment variable was used to create the search_path
48  // object, return it. Otherwise, it will be empty.
49  std::string const&
50  showenv() const
51  {
52  return env_;
53  }
54 
55  // Return true if there are no directories in the path.
56  bool empty() const;
57 
58  // Return the number of directories in the path.
59  std::size_t size() const;
60 
61  // Return the k'th entry in the path.
62  std::string const& operator[](std::size_t k) const;
63 
64  // Return the full pathname of the file named filename, found
65  // somewhere along the path. If no such file is found, an exception
66  // is thrown.
67  std::string find_file(std::string const& filename) const;
68 
69  // Look for a file named 'filename' in the path. If filename is
70  // empty, or if no file is found, return false, without modifying
71  // result. If one is found, return true and fill result with the
72  // full pathname for the file.
73  bool find_file(std::string const& filename, std::string& result) const;
74 
75  // Find all the files with names maching 'filename_pattern' in the
76  // search path. filename_pattern is used to construct a std::regex
77  // that is used for the matching. The path to each matching file is
78  // appended to out, and the total number of matching paths is the
79  // return value of the function.
80  std::size_t find_files(std::string const& filename_pattern,
81  std::vector<std::string>& result) const;
82 
83  // Find all the files with names maching 'filename_pattern' in the
84  // search path. filename_pattern is used to construct a std::regex
85  // that is used for the matching. The path to each matching file is
86  // written to 'dest', and the total number of matching paths is the
87  // return value of the function.
88  template <class OutIter>
89  std::size_t find_files(std::string const& filename_pattern,
90  OutIter dest) const;
91 
92  // Return the string format (colon-delimited) of the search path.
93  std::string to_string() const;
94 
95 private:
97  std::vector<std::string> dirs_{};
98 }; // search_path
99 
100 template <class OutIter>
101 std::size_t
103 {
104  std::vector<std::string> results;
105  size_t const nfound{find_files(pattern, results)};
106  cet::copy_all(results, dest);
107  return nfound;
108 }
109 
110 #endif /* cetlib_search_path_h */
111 
112 // Local Variables:
113 // mode: c++
114 // End:
static QCString result
std::ostream & operator<<(std::ostream &, map_vector_key const &)
Definition: map_vector.h:342
std::string string
Definition: nybbler.cc:12
std::string const & showenv() const
Definition: search_path.h:50
path_tag_t const path_tag
Definition: search_path.cc:60
std::string env_
Definition: search_path.h:96
string filename
Definition: train.py:213
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:92
std::size_t find_files(std::string const &filename_pattern, std::vector< std::string > &result) const
Definition: search_path.cc:126
p
Definition: test.py:223
auto copy_all(FwdCont &, FwdIter)
std::string pattern
Definition: regex_t.cc:35
std::string to_string(ModuleType const mt)
Definition: ModuleType.h:34
decltype(auto) constexpr empty(T &&obj)
ADL-aware version of std::empty.
Definition: StdUtils.h:97