AttribStringParser.h
Go to the documentation of this file.
1 #ifndef _ATTRIB_STRING_PARSER_H_
2 #define _ATTRIB_STRING_PARSER_H_
3 
4 #include "GaudiKernel/Kernel.h"
5 #include "GaudiKernel/System.h"
6 #include <iterator>
7 #include <boost/regex.hpp>
8 
9 namespace Gaudi {
10  namespace Utils {
21  class AttribStringParser {
22  public:
24  struct Attrib {
25  std::string tag;
26  std::string value;
27  };
28 
30  // This class is essentially a wrapper around boost::sregex_iterator.
31  class Iterator: public std::iterator<std::input_iterator_tag, Attrib> {
32  public:
33  Iterator() {}
34  Iterator(const boost::sregex_iterator& it, bool expand_vars):
35  m_it(it),
36  m_expandVars(expand_vars)
37  {
38  //i_setAttrib();
39  }
40  Iterator(const Iterator& other): Iterator(other.m_it, other.m_expandVars)
41  {
42  }
43  Iterator(Iterator&& other):
44  m_it(std::move(other.m_it)),
45  m_expandVars(other.m_expandVars)
46  {
47  }
48  Iterator operator++(int) {
49  ++m_it;
50  return *this;
51  }
52  Iterator operator++() {
53  auto old = *this;
54  ++m_it;
55  return std::move(old);
56  }
57  reference operator*() {
58  i_setAttrib();
59  return m_attrib;
60  }
61  bool operator==(const Iterator& other) {
62  return m_it == other.m_it;
63  }
64  bool operator!=(const Iterator& other) {
65  return m_it != other.m_it;
66  }
67  private:
69  boost::sregex_iterator m_it;
70  bool m_expandVars = false;
72  Attrib m_attrib;
75  void i_setAttrib() {
76  static const boost::sregex_iterator endmark;
77  if (m_it != endmark) {
78  // if we have a match, we cache the values
79  m_attrib = Attrib{(*m_it)[1], (*m_it)[2]};
80  if (m_expandVars && m_attrib.value.find("${") != std::string::npos) {
81  static const boost::regex varexp{"\\$\\{([^}]+)\\}"};
82  auto i = 1;
83  while (i) {
84  i = 0;
85  m_attrib.value = boost::regex_replace(m_attrib.value,
86  varexp, [&i] (const boost::smatch& m) -> std::string {
87  const std::string name = m[1];
88  const char* cname = name.c_str();
89  if (System::isEnvSet(cname)) {
90  ++i;
91  return System::getEnv(cname);
92  }
93  return m[0]; // do not expand unknown vars
94  },
95  boost::match_default | boost::format_all);
96  }
97  }
98  } else {
99  // otherwise we clean the cache
100  m_attrib = Attrib();
101  }
102  }
103  };
104 
110  AttribStringParser(std::string data, bool expand_vars=true):
111  m_data(data),
112  m_expandVars(expand_vars) {
113  }
114 
115  private:
116  std::string m_data;
117  bool m_expandVars;
118 
119  boost::sregex_iterator parse() const {
120  static const boost::regex exp{"[[:space:]]*([^[:space:]]+)[[:space:]]*=[[:space:]]*'(.*?)'"};
121  return boost::sregex_iterator(begin(m_data), end(m_data), exp);
122  }
123  friend Iterator begin(const AttribStringParser&);
124  };
125  inline AttribStringParser::Iterator begin(const AttribStringParser& parser) {
126  return AttribStringParser::Iterator(parser.parse(), parser.m_expandVars);
127  }
128  inline AttribStringParser::Iterator end(const AttribStringParser& /*parser*/) {
130  }
131  }
132 }
133 
134 #endif
tuple parser
Definition: gaudirun.py:98
bool operator==(const Gaudi::StringKey &key1, const char(&key2)[N])
equality operator with C-arrays
Definition: StringKey.h:151
STL namespace.
StatusCode parse(GaudiUtils::HashMap< K, V > &result, const std::string &input)
Basic parser for the types of HashMap used in DODBasicMapper.
bool operator!=(const Gaudi::StringKey &key1, const char(&key2)[N])
non-equality operator with C-arrays
Definition: StringKey.h:165
tuple end
Definition: IOTest.py:101
boost::spirit::classic::position_iterator2< ForwardIterator > Iterator
Definition: Iterator.h:19
list i
Definition: ana.py:128
Helper functions to set/get the application return code.
Definition: __init__.py:1
TO * reference(FROM *from)
Definition: KeyedObject.cpp:21