grep.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. * File: boost/iostreams/filter/grep.hpp
  7. * Date: Mon May 26 17:48:45 MDT 2008
  8. * Copyright: 2008 CodeRage, LLC
  9. * Author: Jonathan Turkanis
  10. * Contact: turkanis at coderage dot com
  11. *
  12. * Defines the class template basic_grep_filter and its specializations
  13. * grep_filter and wgrep_filter.
  14. */
  15. #ifndef BOOST_IOSTREAMS_GREP_FILTER_HPP_INCLUDED
  16. #define BOOST_IOSTREAMS_GREP_FILTER_HPP_INCLUDED
  17. #if defined(_MSC_VER)
  18. # pragma once
  19. #endif
  20. #include <iostream>
  21. #include <memory> // allocator.
  22. #include <boost/iostreams/char_traits.hpp>
  23. #include <boost/iostreams/filter/line.hpp>
  24. #include <boost/iostreams/pipeline.hpp>
  25. #include <boost/regex.hpp>
  26. namespace boost { namespace iostreams {
  27. namespace grep {
  28. const int invert = 1;
  29. const int whole_line = invert << 1;
  30. } // End namespace grep.
  31. template< typename Ch,
  32. typename Tr = regex_traits<Ch>,
  33. typename Alloc = std::allocator<Ch> >
  34. class basic_grep_filter : public basic_line_filter<Ch, Alloc> {
  35. private:
  36. typedef basic_line_filter<Ch, Alloc> base_type;
  37. public:
  38. typedef typename base_type::char_type char_type;
  39. typedef typename base_type::category category;
  40. typedef char_traits<char_type> traits_type;
  41. typedef typename base_type::string_type string_type;
  42. typedef basic_regex<Ch, Tr> regex_type;
  43. typedef regex_constants::match_flag_type match_flag_type;
  44. basic_grep_filter( const regex_type& re,
  45. match_flag_type match_flags =
  46. regex_constants::match_default,
  47. int options = 0 );
  48. int count() const { return count_; }
  49. template<typename Sink>
  50. void close(Sink& snk, BOOST_IOS::openmode which)
  51. {
  52. base_type::close(snk, which);
  53. options_ &= ~f_initialized;
  54. }
  55. private:
  56. virtual string_type do_filter(const string_type& line)
  57. {
  58. if ((options_ & f_initialized) == 0) {
  59. options_ |= f_initialized;
  60. count_ = 0;
  61. }
  62. bool matches = (options_ & grep::whole_line) ?
  63. regex_match(line, re_, match_flags_) :
  64. regex_search(line, re_, match_flags_);
  65. if (options_ & grep::invert)
  66. matches = !matches;
  67. if (matches)
  68. ++count_;
  69. return matches ? line + traits_type::newline() : string_type();
  70. }
  71. // Private flags bitwise OR'd with constants from namespace grep
  72. enum flags_ {
  73. f_initialized = 65536
  74. };
  75. regex_type re_;
  76. match_flag_type match_flags_;
  77. int options_;
  78. int count_;
  79. };
  80. BOOST_IOSTREAMS_PIPABLE(basic_grep_filter, 3)
  81. typedef basic_grep_filter<char> grep_filter;
  82. typedef basic_grep_filter<wchar_t> wgrep_filter;
  83. //------------------Implementation of basic_grep_filter-----------------------//
  84. template<typename Ch, typename Tr, typename Alloc>
  85. basic_grep_filter<Ch, Tr, Alloc>::basic_grep_filter
  86. (const regex_type& re, match_flag_type match_flags, int options)
  87. : base_type(true), re_(re), match_flags_(match_flags),
  88. options_(options), count_(0)
  89. { }
  90. } } // End namespaces iostreams, boost.
  91. #endif // #ifndef BOOST_IOSTREAMS_REGEX_FILTER_HPP_INCLUDED