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