artdaq_core  v1_07_08
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
SimpleLookupPolicy.cc
1 #include "artdaq-core/Utilities/SimpleLookupPolicy.hh"
2 #include "cetlib/filesystem.h"
3 
5 SimpleLookupPolicy(std::string const& paths, ArgType argType)
6 {
7  // the cetlib search_path constructor expects either the name of
8  // an environmental variable that contains the search path *or* a
9  // colon-delimited list of paths. So, a constructor argument of
10  // a single path string is doomed to fail because it gets interpreted
11  // as an env var. So, in this class, we will always pass either
12  // an env var name or a list of paths. If/when a single path is
13  // specified, we'll simply duplicate it so that search_path will
14  // do the right thing.
15  cwdPath_.reset(new cet::search_path(".:."));
16 
17  // if no fallback path was specified, simply use the current directory
18  if (paths.empty())
19  {
20  fallbackPaths_.reset(new cet::search_path(".:."));
21  return;
22  }
23 
24  if (argType == ArgType::PATH_STRING)
25  {
26  auto workString(paths);
27  if (workString.find(':') == std::string::npos)
28  {
29  workString.append(":");
30  workString.append(paths);
31  }
32  fallbackPaths_.reset(new cet::search_path(workString));
33  }
34 
35  else
36  { // argType == ENV_VAR
37  fallbackPaths_.reset(new cet::search_path(paths));
38  }
39 }
40 
41 std::string artdaq::SimpleLookupPolicy::operator()(std::string const& filename)
42 {
43  if (cet::is_absolute_filepath(filename))
44  {
45  return filename;
46  }
47 
48  try
49  {
50  return cwdPath_->find_file(filename);
51  }
52  catch (...) {}
53 
54  return fallbackPaths_->find_file(filename);
55 }
56 
SimpleLookupPolicy(std::string const &paths, ArgType argType=ArgType::ENV_VAR)
Constructor.
Constructor argument is a list of directories.
virtual ~SimpleLookupPolicy() noexcept
Default destructor.
ArgType
Flag if the constructor argument is a list of paths or the name of an environment variable...
std::string operator()(std::string const &filename) override
Perform the file lookup.