sbuild  1.7.1
regex.h
1 /* Copyright © 2006-2013 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/config.h>
27 #ifdef HAVE_REGEX_REGEX
28 # include <regex>
29 #elif HAVE_TR1_REGEX_REGEX
30 # include <tr1/regex>
31 namespace std {
32  using std::tr1::regex;
33  using std::tr1::regex_error;
34  using std::tr1::regex_search;
35 }
36 #else
37 # include <boost/regex.hpp>
38 namespace std {
39  using boost::regex;
40  using boost::regex_error;
41  using boost::regex_search;
42 }
43 #endif
44 
45 namespace sbuild
46 {
47 
62  class regex
63  {
64  public:
66  regex ():
67  comp(),
68  rstr()
69  {}
70 
78  regex (const std::string& pattern):
79  comp(pattern, std::regex::extended),
80  rstr(pattern)
81  {}
82 
90  regex (const char *pattern):
91  comp(pattern, std::regex::extended),
92  rstr(pattern)
93  {}
94 
96  ~regex ()
97  {}
98 
106  regex (const regex& rhs):
107  comp(rhs.comp),
108  rstr(rhs.rstr)
109  {}
110 
111  std::string const&
112  str() const
113  {
114  return rstr;
115  }
116 
117  bool
118  compare (const regex& rhs) const
119  {
120  return this->rstr != rhs.rstr;
121  }
122 
123  bool
124  search (const std::string& str) const
125  {
126  return std::regex_search(str, this->comp);
127  }
128 
138  template <class charT, class traits>
139  friend
140  std::basic_istream<charT,traits>&
141  operator >> (std::basic_istream<charT,traits>& stream,
142  regex& rhs)
143  {
144  std::string regex;
145 
146  if (std::getline(stream, regex))
147  {
148  rhs.comp.assign(regex, std::regex::extended);
149  rhs.rstr = regex;
150  }
151 
152  return stream;
153  }
154 
162  template <class charT, class traits>
163  friend
164  std::basic_ostream<charT,traits>&
165  operator << (std::basic_ostream<charT,traits>& stream,
166  const regex& rhs)
167  {
168  return stream << rhs.str();
169  }
170 
171  private:
173  std::regex comp;
175  std::string rstr;
176  };
177 
181  inline bool
182  regex_search (const std::string& str,
183  const regex& regex)
184  {
185  return regex.search(str);
186  }
187 
188 }
189 
190 #endif /* SBUILD_REGEX_H */
191 
192 /*
193  * Local Variables:
194  * mode:C++
195  * End:
196  */