• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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.)
4  *
5  * See http://www.boost.org/libs/iostreams for documentation.
6 
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
12  *
13  * Defines the class template basic_grep_filter and its specializations
14  * grep_filter and wgrep_filter.
15  */
16 
17 #ifndef BOOST_IOSTREAMS_GREP_FILTER_HPP_INCLUDED
18 #define BOOST_IOSTREAMS_GREP_FILTER_HPP_INCLUDED
19 
20 #if defined(_MSC_VER)
21 # pragma once
22 #endif
23 
24 #include <iostream>
25 
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>
31 
32 namespace boost { namespace iostreams {
33 
34 namespace grep {
35 
36 const int invert      = 1;
37 const int whole_line  = invert << 1;
38 
39 } // End namespace grep.
40 
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> {
45 private:
46     typedef basic_line_filter<Ch, Alloc>               base_type;
47 public:
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,
57                        int options = 0 );
count() const58     int count() const { return count_; }
59 
60     template<typename Sink>
close(Sink & snk,BOOST_IOS::openmode which)61     void close(Sink& snk, BOOST_IOS::openmode which)
62     {
63         base_type::close(snk, which);
64         options_ &= ~f_initialized;
65     }
66 private:
do_filter(const string_type & line)67     virtual string_type do_filter(const string_type& line)
68     {
69         if ((options_ & f_initialized) == 0) {
70             options_ |= f_initialized;
71             count_ = 0;
72         }
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)
77             matches = !matches;
78         if (matches)
79             ++count_;
80         return matches ? line + traits_type::newline() : string_type();
81     }
82 
83     // Private flags bitwise OR'd with constants from namespace grep
84     enum flags_ {
85         f_initialized = 65536
86     };
87 
88     regex_type       re_;
89     match_flag_type  match_flags_;
90     int              options_;
91     int              count_;
92 };
93 BOOST_IOSTREAMS_PIPABLE(basic_grep_filter, 3)
94 
95 typedef basic_grep_filter<char>     grep_filter;
96 typedef basic_grep_filter<wchar_t>  wgrep_filter;
97 
98 //------------------Implementation of basic_grep_filter-----------------------//
99 
100 template<typename Ch, typename Tr, typename Alloc>
basic_grep_filter(const regex_type & re,match_flag_type match_flags,int options)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)
105     { }
106 
107 } } // End namespaces iostreams, boost.
108 
109 #endif      // #ifndef BOOST_IOSTREAMS_REGEX_FILTER_HPP_INCLUDED
110