#ifndef BOOST_IOSTREAMS_GREP_HPP_INCLUDED #define BOOST_IOSTREAMS_GREP_HPP_INCLUDED #include #include #include #include namespace boost { namespace iostreams { template struct basic_grep_filter { typedef Ch char_type; struct category: dual_use, filter_tag { }; typedef char_traits traits_type; typedef std::basic_string string_type; typedef boost::basic_regex regex_type; explicit basic_grep_filter(const char* expr) : expr_(expr), eof_(false), iter_(line_.end()) { }; template typename traits_type::int_type get(Source& src) { if (eof_ && iter_ == line_.end()) return EOF; if (iter_ != line_.end()) return next(); for (;;) { typename traits_type::int_type c = iostreams::get(src); if (c == traits_type::would_block()) return c; line_ += c; if (traits_type::is_eof(c)) { eof_ = true; if (boost::regex_search(line_, expr_)) { iter_ = line_.begin(); return next(); } iter_ = line_.end(); return c; } if (c == traits_type::newline()) { if (boost::regex_search(line_, expr_)) { iter_ = line_.begin(); return next(); } line_.erase(); iter_ = line_.end(); } } } template bool put(Sink& snk, char_type c) { if (!traits_type::is_eof(c)) line_ += c; if (traits_type::is_eof(c) || (c == traits_type::newline())) { if (boost::regex_search(line_, expr_)) std::for_each(line_.begin(), line_.end(), writer(snk)); if (traits_type::is_eof(c)) iostreams::put(snk, c); line_.erase(); } return true; } private: char_type next() { char_type c = *iter_; ++iter_; if (iter_ == line_.end()) { line_.erase(); iter_ = line_.end(); } return c; } template struct writer { writer(Sink& snk): snk_(snk) { } void operator()(const char_type& c) { iostreams::put(snk_, c); } private: Sink& snk_; }; private: regex_type expr_; bool eof_; string_type line_; typename string_type::const_iterator iter_; }; typedef basic_grep_filter grep_filter; typedef basic_grep_filter wgrep_filter; } } // End namespaces iostreams, boost. #endif //#ifndef BOOST_IOSTREAMS_GREP_HPP_INCLUDED