1 | /* Copyright © 2006-2007 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 <boost/regex.hpp> |
---|
27 | |
---|
28 | namespace sbuild |
---|
29 | { |
---|
30 | |
---|
31 | /** |
---|
32 | * POSIX extended regular expression. |
---|
33 | */ |
---|
34 | class regex : public boost::regex |
---|
35 | { |
---|
36 | public: |
---|
37 | /// The constructor |
---|
38 | regex (): |
---|
39 | boost::regex() |
---|
40 | {} |
---|
41 | |
---|
42 | /** |
---|
43 | * The constructor. |
---|
44 | * |
---|
45 | * May throw if the regex is invalid. |
---|
46 | * |
---|
47 | * @param pattern a regex |
---|
48 | */ |
---|
49 | regex (std::string const& pattern): |
---|
50 | boost::regex(pattern, boost::regex::extended) |
---|
51 | {} |
---|
52 | |
---|
53 | /** |
---|
54 | * The constructor. |
---|
55 | * |
---|
56 | * May throw if the regex is invalid. |
---|
57 | * |
---|
58 | * @param pattern a regex |
---|
59 | */ |
---|
60 | regex (const char *pattern): |
---|
61 | boost::regex(pattern, boost::regex::extended) |
---|
62 | {} |
---|
63 | |
---|
64 | ///* The destructor. |
---|
65 | ~regex () |
---|
66 | {} |
---|
67 | |
---|
68 | /** |
---|
69 | * Get the regex name from a stream. |
---|
70 | * |
---|
71 | * May throw if the regex is invalid. |
---|
72 | * |
---|
73 | * @param stream the stream to get input from. |
---|
74 | * @param rhs the regex to set. |
---|
75 | * @returns the stream. |
---|
76 | */ |
---|
77 | template <class charT, class traits> |
---|
78 | friend |
---|
79 | std::basic_istream<charT,traits>& |
---|
80 | operator >> (std::basic_istream<charT,traits>& stream, |
---|
81 | regex& rhs) |
---|
82 | { |
---|
83 | std::string regex; |
---|
84 | |
---|
85 | if (std::getline(stream, regex)) |
---|
86 | { |
---|
87 | rhs.assign(regex, boost::regex::extended); |
---|
88 | } |
---|
89 | |
---|
90 | return stream; |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * Print the regex name to a stream. |
---|
95 | * |
---|
96 | * @param stream the stream to output to. |
---|
97 | * @param rhs the regex to output. |
---|
98 | * @returns the stream. |
---|
99 | */ |
---|
100 | template <class charT, class traits> |
---|
101 | friend |
---|
102 | std::basic_ostream<charT,traits>& |
---|
103 | operator << (std::basic_ostream<charT,traits>& stream, |
---|
104 | regex const& rhs) |
---|
105 | { |
---|
106 | return stream << rhs.str(); |
---|
107 | } |
---|
108 | }; |
---|
109 | |
---|
110 | } |
---|
111 | |
---|
112 | #endif /* SBUILD_REGEX_H */ |
---|
113 | |
---|
114 | /* |
---|
115 | * Local Variables: |
---|
116 | * mode:C++ |
---|
117 | * End: |
---|
118 | */ |
---|