LArSoft  v09_90_00
Liquid Argon Software toolkit - https://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::detail {
9 
10  void
11  FileNameComponents::add(std::string const& prefix,
12  std::string const& digitFormat)
13  {
14  components_.emplace_back(prefix, digitFormat);
15  }
16 
17  void
18  FileNameComponents::setSuffix(std::string suffix)
19  {
20  suffix_ = std::move(suffix);
21  }
22 
23  bool
25  {
26  if (components_.size() < fnc.components_.size()) {
27  return true;
28  }
29  if (components_.size() > fnc.components_.size()) {
30  return false;
31  }
32 
33  assert(components_.size() == fnc.components_.size());
34  auto b1 = cbegin(components_), e1 = cend(components_);
35  auto b2 = cbegin(fnc.components_);
36  for (; b1 != e1; ++b1, ++b2) {
37  if (b1->first < b2->first)
38  return true;
39  }
40  assert(b2 == cend(fnc.components_));
41  return false;
42  }
43 
44  std::string
45  FileNameComponents::fileNameWithIndex(std::size_t const index) const
46  {
47  std::ostringstream num_str;
48  for (auto const& pr : components_) {
49  auto const& prefix = pr.first;
50  auto const& specifier = pr.second;
51  num_str << prefix;
52  if (!specifier.empty()) { // Zero-filling.
53  num_str << std::setfill('0') << std::setw(std::stoul(specifier));
54  }
55  num_str << index;
56  }
57  num_str << suffix_;
58  return num_str.str();
59  }
60 
62  componentsFromPattern(std::string const& pattern)
63  {
64  FileNameComponents result;
65 
66  boost::smatch match;
67  auto sid = cbegin(pattern), se = cend(pattern);
68  while (boost::regex_search(
69  sid,
70  se,
71  match,
72  boost::regex{"%(\\d+)?#", boost::regex_constants::ECMAScript})) {
73  assert(match.size() == 2);
74  // Subexpressions:
75  // 0. Entire matched expression
76  // 1. Possible fill format digits for numeric substitution.
77  result.add(match.prefix(), match[1].str());
78  sid = match[0].second;
79  }
80  // Get remaining characters of filename
81  result.setSuffix(std::string(sid, se));
82  return result;
83  }
84 }
decltype(auto) constexpr cend(T &&obj)
ADL-aware version of std::cend.
Definition: StdUtils.h:93
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)
decltype(auto) constexpr cbegin(T &&obj)
ADL-aware version of std::cbegin.
Definition: StdUtils.h:85
std::vector< std::pair< std::string, std::string > > components_