File indexing completed on 2025-01-30 09:44:30
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #ifndef BOOST_IOSTREAMS_GREP_FILTER_HPP_INCLUDED
0018 #define BOOST_IOSTREAMS_GREP_FILTER_HPP_INCLUDED
0019
0020 #if defined(_MSC_VER)
0021 # pragma once
0022 #endif
0023
0024 #include <iostream>
0025
0026 #include <memory> // allocator.
0027 #include <boost/iostreams/char_traits.hpp>
0028 #include <boost/iostreams/filter/line.hpp>
0029 #include <boost/iostreams/pipeline.hpp>
0030 #include <boost/regex.hpp>
0031
0032 namespace boost { namespace iostreams {
0033
0034 namespace grep {
0035
0036 const int invert = 1;
0037 const int whole_line = invert << 1;
0038
0039 }
0040
0041 template< typename Ch,
0042 typename Tr = regex_traits<Ch>,
0043 typename Alloc = std::allocator<Ch> >
0044 class basic_grep_filter : public basic_line_filter<Ch, Alloc> {
0045 private:
0046 typedef basic_line_filter<Ch, Alloc> base_type;
0047 public:
0048 typedef typename base_type::char_type char_type;
0049 typedef typename base_type::category category;
0050 typedef char_traits<char_type> traits_type;
0051 typedef typename base_type::string_type string_type;
0052 typedef basic_regex<Ch, Tr> regex_type;
0053 typedef regex_constants::match_flag_type match_flag_type;
0054 basic_grep_filter( const regex_type& re,
0055 match_flag_type match_flags =
0056 regex_constants::match_default,
0057 int options = 0 );
0058 int count() const { return count_; }
0059
0060 template<typename Sink>
0061 void close(Sink& snk, BOOST_IOS::openmode which)
0062 {
0063 base_type::close(snk, which);
0064 options_ &= ~f_initialized;
0065 }
0066 private:
0067 virtual string_type do_filter(const string_type& line)
0068 {
0069 if ((options_ & f_initialized) == 0) {
0070 options_ |= f_initialized;
0071 count_ = 0;
0072 }
0073 bool matches = (options_ & grep::whole_line) ?
0074 regex_match(line, re_, match_flags_) :
0075 regex_search(line, re_, match_flags_);
0076 if (options_ & grep::invert)
0077 matches = !matches;
0078 if (matches)
0079 ++count_;
0080 return matches ? line + traits_type::newline() : string_type();
0081 }
0082
0083
0084 enum flags_ {
0085 f_initialized = 65536
0086 };
0087
0088 regex_type re_;
0089 match_flag_type match_flags_;
0090 int options_;
0091 int count_;
0092 };
0093 BOOST_IOSTREAMS_PIPABLE(basic_grep_filter, 3)
0094
0095 typedef basic_grep_filter<char> grep_filter;
0096 typedef basic_grep_filter<wchar_t> wgrep_filter;
0097
0098
0099
0100 template<typename Ch, typename Tr, typename Alloc>
0101 basic_grep_filter<Ch, Tr, Alloc>::basic_grep_filter
0102 (const regex_type& re, match_flag_type match_flags, int options)
0103 : base_type(true), re_(re), match_flags_(match_flags),
0104 options_(options), count_(0)
0105 { }
0106
0107 } }
0108
0109 #endif