Public Member Functions | Private Attributes | List of all members
cet::search_path Class Reference

#include <search_path.h>

Public Member Functions

 search_path (std::string const &env_name_or_path)
 
 search_path (std::string const &env_name, std::nothrow_t)
 
 search_path (std::string const &path, cet::path_tag_t)
 
std::string const & showenv () const
 
bool empty () const
 
std::size_t size () const
 
std::string const & operator[] (std::size_t k) const
 
std::string find_file (std::string const &filename) const
 
bool find_file (std::string const &filename, std::string &result) const
 
std::size_t find_files (std::string const &filename_pattern, std::vector< std::string > &result) const
 
template<class OutIter >
std::size_t find_files (std::string const &filename_pattern, OutIter dest) const
 
std::string to_string () const
 

Private Attributes

std::string env_
 
std::vector< std::stringdirs_ {}
 

Detailed Description

Definition at line 36 of file search_path.h.

Constructor & Destructor Documentation

search_path::search_path ( std::string const &  env_name_or_path)
explicit

Definition at line 62 of file search_path.cc.

63  : env_{get_env_if_colon_present(env_name_or_path)}
64  , dirs_{get_dirs(env_, env_name_or_path)}
65 {}
std::vector< std::string > dirs_
Definition: search_path.h:97
std::string env_
Definition: search_path.h:96
search_path::search_path ( std::string const &  env_name,
std::nothrow_t   
)

Definition at line 67 of file search_path.cc.

68  : env_{env_name}, dirs_{get_dirs(env_, std::nothrow)}
69 {}
std::vector< std::string > dirs_
Definition: search_path.h:97
std::string env_
Definition: search_path.h:96
search_path::search_path ( std::string const &  path,
cet::path_tag_t   
)

Definition at line 71 of file search_path.cc.

72  : env_{}, dirs_{get_dirs(path)}
73 {}
std::vector< std::string > dirs_
Definition: search_path.h:97
std::string env_
Definition: search_path.h:96

Member Function Documentation

bool search_path::empty ( ) const

Definition at line 76 of file search_path.cc.

77 {
78  return dirs_.empty();
79 }
std::vector< std::string > dirs_
Definition: search_path.h:97
string search_path::find_file ( std::string const &  filename) const

Definition at line 96 of file search_path.cc.

97 {
98  string result;
99  if (find_file(filename, result))
100  return result;
101  throw cet::exception(exception_category)
102  << "Can't find file \"" << filename << '"';
103 }
static QCString result
string filename
Definition: train.py:213
std::string find_file(std::string const &filename) const
Definition: search_path.cc:96
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
bool search_path::find_file ( std::string const &  filename,
std::string result 
) const

Definition at line 106 of file search_path.cc.

107 {
108  if (filename.empty())
109  return false;
110 
111  for (auto const& dir : dirs_) {
112  string fullpath = dir + '/' + filename;
113  for (size_t k; (k = fullpath.find("//")) != string::npos;) {
114  fullpath.erase(k, 1);
115  }
116  if (cet::file_exists(fullpath)) {
117  result = fullpath;
118  return true;
119  }
120  }
121  return false;
122 }
std::vector< std::string > dirs_
Definition: search_path.h:97
static QCString result
string dir
string filename
Definition: train.py:213
bool file_exists(std::string const &qualified_filename)
Definition: filesystem.cc:14
size_t search_path::find_files ( std::string const &  filename_pattern,
std::vector< std::string > &  result 
) const

Definition at line 126 of file search_path.cc.

127 {
128  regex const re{pat};
129  size_t count{};
130  for (auto const& dir : dirs_) {
131  unique_ptr<DIR, function<int(DIR*)>> dd(opendir(dir.c_str()), closedir);
132  if (dd.get() == nullptr) {
133  // The opendir() failed, we do not care why, skip it.
134  continue;
135  }
136  while (1) {
137  // Note: errno is a thread-local!
138  errno = 0;
139  // Note: This is thread-safe so long as each thread
140  // has their own dd, which is the case here.
141  auto entry = readdir(dd.get());
142  if (errno != 0) {
143  throw cet::exception(exception_category)
144  << "Failed to read directory \"" << dir
145  << "\"; error num = " << errno;
146  }
147  if (entry == nullptr) {
148  // We have reached the end of this directory stream.
149  break;
150  }
151  if (regex_match(entry->d_name, re)) {
152  out.push_back(dir + '/' + entry->d_name);
153  ++count;
154  }
155  }
156  }
157  return count;
158 }
QList< Entry > entry
std::vector< std::string > dirs_
Definition: search_path.h:97
string dir
int errno
Contains the last error code.
Definition: structcmd.h:53
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
template<class OutIter >
std::size_t cet::search_path::find_files ( std::string const &  filename_pattern,
OutIter  dest 
) const

Definition at line 102 of file search_path.h.

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 }
std::size_t find_files(std::string const &filename_pattern, std::vector< std::string > &result) const
Definition: search_path.cc:126
auto copy_all(FwdCont &, FwdIter)
std::string pattern
Definition: regex_t.cc:35
string const & search_path::operator[] ( std::size_t  k) const

Definition at line 87 of file search_path.cc.

88 {
89  return dirs_.at(k);
90 }
std::vector< std::string > dirs_
Definition: search_path.h:97
std::string const& cet::search_path::showenv ( ) const
inline

Definition at line 50 of file search_path.h.

51  {
52  return env_;
53  }
std::string env_
Definition: search_path.h:96
size_t search_path::size ( ) const

Definition at line 82 of file search_path.cc.

83 {
84  return dirs_.size();
85 }
std::vector< std::string > dirs_
Definition: search_path.h:97
std::string search_path::to_string ( ) const

Definition at line 161 of file search_path.cc.

162 {
163  std::ostringstream oss;
164  oss << *this;
165  return oss.str();
166 }

Member Data Documentation

std::vector<std::string> cet::search_path::dirs_ {}
private

Definition at line 97 of file search_path.h.

std::string cet::search_path::env_
private

Definition at line 96 of file search_path.h.


The documentation for this class was generated from the following files: