sbuild  1.6.9
sbuild-regex.h
1 /* Copyright © 2006-2007,2012 Roger Leigh <rleigh@debian.org>
2  *
3  * schroot is free software: you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * schroot is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  *********************************************************************/
18 
19 #ifndef SBUILD_REGEX_H
20 #define SBUILD_REGEX_H
21 
22 #include <istream>
23 #include <ostream>
24 #include <string>
25 
26 #include <sbuild/sbuild-config.h>
27 #ifdef HAVE_REGEX_REGEX
28 # include <regex>
29 #else
30 # include <boost/regex.hpp>
31 namespace std {
32  using boost::regex;
33  using boost::regex_error;
34  using boost::regex_search;
35 }
36 #endif
37 
38 namespace sbuild
39 {
40 
55  class regex
56  {
57  public:
59  regex ():
60  comp(),
61  rstr()
62  {}
63 
71  regex (std::string const& pattern):
72  comp(pattern, std::regex::extended),
73  rstr(pattern)
74  {}
75 
83  regex (const char *pattern):
84  comp(pattern, std::regex::extended),
85  rstr(pattern)
86  {}
87 
89  ~regex ()
90  {}
91 
99  regex (const regex& rhs):
100  comp(rhs.comp),
101  rstr(rhs.rstr)
102  {}
103 
104  std::string const&
105  str() const
106  {
107  return rstr;
108  }
109 
110  bool
111  compare (regex const& rhs) const
112  {
113  return this->rstr != rhs.rstr;
114  }
115 
116  bool
117  search (std::string const& str) const
118  {
119  return std::regex_search(str, this->comp);
120  }
121 
131  template <class charT, class traits>
132  friend
133  std::basic_istream<charT,traits>&
134  operator >> (std::basic_istream<charT,traits>& stream,
135  regex& rhs)
136  {
137  std::string regex;
138 
139  if (std::getline(stream, regex))
140  {
141  rhs.comp.assign(regex, std::regex::extended);
142  rhs.rstr = regex;
143  }
144 
145  return stream;
146  }
147 
155  template <class charT, class traits>
156  friend
157  std::basic_ostream<charT,traits>&
158  operator << (std::basic_ostream<charT,traits>& stream,
159  regex const& rhs)
160  {
161  return stream << rhs.str();
162  }
163 
164  private:
166  std::regex comp;
168  std::string rstr;
169  };
170 
174  inline bool
175  regex_search (const std::string& str,
176  regex const& regex)
177  {
178  return regex.search(str);
179  }
180 
181 }
182 
183 #endif /* SBUILD_REGEX_H */
184 
185 /*
186  * Local Variables:
187  * mode:C++
188  * End:
189  */
POSIX extended regular expression.
Definition: sbuild-regex.h:55
~regex()
Definition: sbuild-regex.h:89
regex()
The constructor.
Definition: sbuild-regex.h:59
friend std::basic_istream< charT, traits > & operator>>(std::basic_istream< charT, traits > &stream, regex &rhs)
Get the regex name from a stream.
Definition: sbuild-regex.h:134
std::string rstr
String containing the regex.
Definition: sbuild-regex.h:168
bool regex_search(const std::string &str, regex const &regex)
Search using the regular expression.
Definition: sbuild-regex.h:175
regex(const char *pattern)
The constructor.
Definition: sbuild-regex.h:83
std::regex comp
Compiled regular expression.
Definition: sbuild-regex.h:166
regex(const regex &rhs)
The copy constructor.
Definition: sbuild-regex.h:99
regex(std::string const &pattern)
The constructor.
Definition: sbuild-regex.h:71