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 #include "cetlib/split.h"
12 #include <algorithm>
13 #include <cstdlib>
14 #include <new>
15 #include <ostream>
16 #include <string>
17 #include <vector>
18 
19 namespace cet {
20  // A search_path is used to manage the use of a PATH-like
21  // environment variable. It is created from the name of an
22  // environment variable; the value associated with this variable is
23  // expected to be a colon-separated list of directories. The
24  // search_path provides facilities for finding files in these
25  // directories.
26  class search_path;
27 
28  struct path_tag_t {}; // Distinguish calling signatures.
29 
30  extern path_tag_t const path_tag;
31 
32  std::ostream& operator<<(std::ostream& os, search_path const& p);
33 }
34 
35 // ----------------------------------------------------------------------
36 
38 public:
39  // Autodetect environment variables (no ':').
40  explicit search_path(std::string const& env_name_or_path);
41 
42  // Specify environent variable (no throw on missing).
43  search_path(std::string const& env_name, std::nothrow_t);
44 
45  // Specify path.
47 
48  // If an environment variable was used to create the search_path
49  // object, return it. Otherwise, it will be empty.
50  std::string const&
51  showenv() const
52  {
53  return env_;
54  }
55 
56  // Return true if there are no directories in the path.
57  bool empty() const;
58 
59  // Return the number of directories in the path.
60  std::size_t size() const;
61 
62  // Return the k'th entry in the path.
63  std::string const& operator[](std::size_t k) const;
64 
65  // Return the full pathname of the file named filename, found
66  // somewhere along the path. If no such file is found, an exception
67  // is thrown.
69 
70  // Look for a file named 'filename' in the path. If filename is
71  // empty, or if no file is found, return false, without modifying
72  // result. If one is found, return true and fill result with the
73  // full pathname for the file.
74  bool find_file(std::string const& filename, std::string& result) const;
75 
76  // Find all the files with names maching 'filename_pattern' in the
77  // search path. filename_pattern is used to construct a std::regex
78  // that is used for the matching. The path to each matching file is
79  // appended to out, and the total number of matching paths is the
80  // return value of the function.
81  std::size_t find_files(std::string const& filename_pattern,
82  std::vector<std::string>& result) const;
83 
84  // Find all the files with names maching 'filename_pattern' in the
85  // search path. filename_pattern is used to construct a std::regex
86  // that is used for the matching. The path to each matching file is
87  // written to 'dest', and the total number of matching paths is the
88  // return value of the function.
89  template <class OutIter>
90  std::size_t find_files(std::string const& filename_pattern,
91  OutIter dest) const;
92 
93  // Return the string format (colon-delimited) of the search path.
94  std::string to_string() const;
95 
96 private:
98  std::vector<std::string> dirs_{};
99 }; // search_path
100 
101 template <class OutIter>
102 std::size_t
104 {
105  std::vector<std::string> results;
106  size_t const nfound{find_files(pattern, results)};
107  cet::copy_all(results, dest);
108  return nfound;
109 }
110 
111 #endif /* cetlib_search_path_h */
112 
113 // Local Variables:
114 // mode: c++
115 // End:
def find_files(d, t, l, f_name)
std::ostream & operator<<(std::ostream &, map_vector_key const &)
Definition: map_vector.h:330
std::string const & showenv() const
Definition: search_path.h:51
const char * p
Definition: xmltok.h:285
def find_file(paths, filename)
string filename
Definition: shutoffs.py:106
std::string env_
Definition: search_path.h:97
std::size_t find_files(std::string const &filename_pattern, std::vector< std::string > &result) const
path_tag_t const path_tag
const std::string path
Definition: plot_BEN.C:43
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
auto copy_all(FwdCont &, FwdIter)
std::string to_string(ModuleType mt)
Definition: ModuleType.h:32