2 * Distributed under the Boost Software License, Version 1.0.(See accompanying
3 * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
5 * See http://www.boost.org/libs/iostreams for documentation.
7 * File: boost/iostreams/filter/grep.hpp
8 * Date: Mon May 26 17:48:45 MDT 2008
9 * Copyright: 2008 CodeRage, LLC
10 * Author: Jonathan Turkanis
11 * Contact: turkanis at coderage dot com
13 * Defines the class template basic_grep_filter and its specializations
14 * grep_filter and wgrep_filter.
17 #ifndef BOOST_IOSTREAMS_GREP_FILTER_HPP_INCLUDED
18 #define BOOST_IOSTREAMS_GREP_FILTER_HPP_INCLUDED
26 #include <memory> // allocator.
27 #include <boost/iostreams/char_traits.hpp>
28 #include <boost/iostreams/filter/line.hpp>
29 #include <boost/iostreams/pipeline.hpp>
30 #include <boost/regex.hpp>
32 namespace boost { namespace iostreams {
37 const int whole_line = invert << 1;
39 } // End namespace grep.
41 template< typename Ch,
42 typename Tr = regex_traits<Ch>,
43 typename Alloc = std::allocator<Ch> >
44 class basic_grep_filter : public basic_line_filter<Ch, Alloc> {
46 typedef basic_line_filter<Ch, Alloc> base_type;
48 typedef typename base_type::char_type char_type;
49 typedef typename base_type::category category;
50 typedef char_traits<char_type> traits_type;
51 typedef typename base_type::string_type string_type;
52 typedef basic_regex<Ch, Tr> regex_type;
53 typedef regex_constants::match_flag_type match_flag_type;
54 basic_grep_filter( const regex_type& re,
55 match_flag_type match_flags =
56 regex_constants::match_default,
58 int count() const { return count_; }
60 template<typename Sink>
61 void close(Sink& snk, BOOST_IOS::openmode which)
63 base_type::close(snk, which);
64 options_ &= ~f_initialized;
67 virtual string_type do_filter(const string_type& line)
69 if ((options_ & f_initialized) == 0) {
70 options_ |= f_initialized;
73 bool matches = (options_ & grep::whole_line) ?
74 regex_match(line, re_, match_flags_) :
75 regex_search(line, re_, match_flags_);
76 if (options_ & grep::invert)
80 return matches ? line + traits_type::newline() : string_type();
83 // Private flags bitwise OR'd with constants from namespace grep
89 match_flag_type match_flags_;
93 BOOST_IOSTREAMS_PIPABLE(basic_grep_filter, 3)
95 typedef basic_grep_filter<char> grep_filter;
96 typedef basic_grep_filter<wchar_t> wgrep_filter;
98 //------------------Implementation of basic_grep_filter-----------------------//
100 template<typename Ch, typename Tr, typename Alloc>
101 basic_grep_filter<Ch, Tr, Alloc>::basic_grep_filter
102 (const regex_type& re, match_flag_type match_flags, int options)
103 : base_type(true), re_(re), match_flags_(match_flags),
104 options_(options), count_(0)
107 } } // End namespaces iostreams, boost.
109 #endif // #ifndef BOOST_IOSTREAMS_REGEX_FILTER_HPP_INCLUDED