LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
FileNameComponents.cc
Go to the documentation of this file.
2 #include "boost/regex.hpp"
3 
4 #include <cassert>
5 #include <iomanip>
6 #include <sstream>
7 
8 namespace art {
9  namespace detail {
10 
11  void
12  FileNameComponents::add(std::string const& prefix,
13  std::string const& digitFormat)
14  {
15  components_.emplace_back(prefix, digitFormat);
16  }
17 
18  void
19  FileNameComponents::setSuffix(std::string suffix)
20  {
21  suffix_ = move(suffix);
22  }
23 
24  bool
26  {
27  if (components_.size() < fnc.components_.size()) {
28  return true;
29  }
30  if (components_.size() > fnc.components_.size()) {
31  return false;
32  }
33 
34  assert(components_.size() == fnc.components_.size());
35  auto b1 = cbegin(components_), e1 = cend(components_);
36  auto b2 = cbegin(fnc.components_);
37  for (; b1 != e1; ++b1, ++b2) {
38  if (b1->first < b2->first)
39  return true;
40  }
41  assert(b2 == cend(fnc.components_));
42  return false;
43  }
44 
45  std::string
46  FileNameComponents::fileNameWithIndex(std::size_t const index) const
47  {
48  std::ostringstream num_str;
49  for (auto const& pr : components_) {
50  auto const& prefix = pr.first;
51  auto const& specifier = pr.second;
52  num_str << prefix;
53  if (!specifier.empty()) { // Zero-filling.
54  num_str << std::setfill('0') << std::setw(std::stoul(specifier));
55  }
56  num_str << index;
57  }
58  num_str << suffix_;
59  return num_str.str();
60  }
61 
63  componentsFromPattern(std::string const& pattern)
64  {
65  FileNameComponents result;
66 
67  boost::smatch match;
68  auto si = cbegin(pattern), se = cend(pattern);
69  while (boost::regex_search(
70  si,
71  se,
72  match,
73  boost::regex{"%(\\d+)?#", boost::regex_constants::ECMAScript})) {
74  assert(match.size() == 2);
75  // Subexpressions:
76  // 0. Entire matched expression
77  // 1. Possible fill format digits for numeric substitution.
78  result.add(match.prefix(), match[1].str());
79  si = match[0].second;
80  }
81  // Get remaining characters of filename
82  result.setSuffix(std::string(si, se));
83  return result;
84  }
85  }
86 }
bool operator<(FileNameComponents const &fnc) const
void setSuffix(std::string suffix)
FileNameComponents componentsFromPattern(std::string const &pattern)
std::string fileNameWithIndex(std::size_t index) const
void add(std::string const &prefix, std::string const &digitFormat)
HLT enums.
std::vector< std::pair< std::string, std::string > > components_