multichar_filters.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <HTML>
  3. <HEAD>
  4. <TITLE>Tutorial</TITLE>
  5. <LINK REL="stylesheet" HREF="../../../../boost.css">
  6. <LINK REL="stylesheet" HREF="../theme/iostreams.css">
  7. </HEAD>
  8. <BODY>
  9. <!-- Begin Banner -->
  10. <H1 CLASS="title">Tutorial</H1>
  11. <HR CLASS="banner">
  12. <!-- End Banner -->
  13. <!-- Begin Nav -->
  14. <DIV CLASS='nav'>
  15. <A HREF='unix2dos_filters.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/src/images/prev.png'></A>
  16. <A HREF='tutorial.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/src/images/up.png'></A>
  17. <A HREF='dual_use_filters.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/src/images/next.png'></A>
  18. </DIV>
  19. <!-- End Nav -->
  20. <A NAME="multichar"></A>
  21. <H2>2.2.8. Multi-Character Filters</H2>
  22. <P>
  23. All the Filters you've seen so far &#8212; except for those that derive from <CODE>stdio_filter</CODE> &#8212; process characters one at a time. If you instead process several characters at once, you can often reduce the number of function calls it takes to filter a character sequence, resulting in more efficient code. This is what <A HREF="../concepts/multi_character.html">Multi-Character Filters</A> allow you to do.
  24. </P>
  25. <A NAME="multichar_input_filters"></A>
  26. <H4>Multi-Character InputFilters</H4>
  27. <P>A typical narrow-character <A HREF="../concepts/multi_character.html">Multi-Character</A> <A HREF="../concepts/input_filter.html">InputFilter</A> looks like this:<P>
  28. <PRE CLASS="broken_ie"><SPAN CLASS='preprocessor'>#include</SPAN> <SPAN CLASS="literal">&lt;iosfwd&gt;</SPAN> <SPAN CLASS='comment'>// streamsize</SPAN>
  29. <SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS="HEADER" HREF="../../../../boost/iostreams/categories.hpp"><SPAN CLASS='literal'>&lt;boost/iostreams/categories.hpp&gt;</SPAN></A> <SPAN CLASS='comment'>// tags
  30. </SPAN>
  31. <SPAN CLASS='keyword'>class</SPAN> my_input_filter {
  32. <SPAN CLASS='keyword'>public</SPAN>:
  33. <SPAN CLASS='keyword'>typedef</SPAN> <SPAN CLASS='keyword'>char</SPAN> char_type;
  34. <SPAN CLASS='keyword'>struct</SPAN> category
  35. : input_filter_tag,
  36. multichar_tag
  37. { };
  38. <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> Source&gt;
  39. std::streamsize read(Source&amp; src, <SPAN CLASS='keyword'>char</SPAN>* s, std::streamsize n)
  40. {
  41. <SPAN CLASS='comment'>// Read up to n filtered characters into the buffer s,</SPAN>
  42. <SPAN CLASS='comment'>// returning the number of characters read or -1 for EOF.</SPAN>
  43. <SPAN CLASS='comment'>// Use src to access the unfiltered character sequence</SPAN>
  44. }
  45. <SPAN CLASS='comment'>/* Other members */</SPAN>
  46. };</PRE>
  47. <P>
  48. Notice that the member type category is a <CODE>struct</CODE> convertible to <CODE>input_filter_tag</CODE> and to <CODE>multichar_tag</CODE>. This tells the Iostream library that <CODE>my_input_filter</CODE> is a <A HREF="../concepts/multi_character.html">Multi-Character Filter</A> and an <A HREF="../concepts/input_filter.html">InputFilter</A>. You could have achieved the same effect several ways. <I>E.g.</I>,
  49. </P>
  50. <PRE CLASS="broken_ie"> <SPAN CLASS='keyword'>struct</SPAN> category
  51. : input,
  52. filter_tag,
  53. multichar_tag
  54. { };
  55. <SPAN CLASS='comment'>/* or */</SPAN>
  56. <SPAN CLASS='keyword'>typedef</SPAN> multichar_input_filter_tag category;</PRE>
  57. <P>(For details, <I>see</I> <A HREF="../guide/modes.html#mode_tags">Mode Tags</A> and <A HREF="../guide/traits.html#category_tags">Category Tags</A>.)
  58. <P> You could also write the above example as follows:</P>
  59. <PRE CLASS="broken_ie"><SPAN CLASS='preprocessor'>#include</SPAN> <SPAN CLASS="literal">&lt;iosfwd&gt;</SPAN> <SPAN CLASS='comment'>// streamsize</SPAN>
  60. <SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS="HEADER" HREF="../../../../boost/iostreams/concepts.hpp"><SPAN CLASS='literal'>&lt;boost/iostreams/concepts.hpp&gt;</SPAN></A> <SPAN CLASS='comment'>// multichar_input_filter</SPAN>
  61. <SPAN CLASS='keyword'>class</SPAN> my_input_filter : <SPAN CLASS='keyword'>public</SPAN> multichar_input_filter {
  62. <SPAN CLASS='keyword'>public</SPAN>:
  63. <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> Source&gt;
  64. std::streamsize read(<SPAN CLASS='keyword'>char</SPAN>* s, std::streamsize n);
  65. <SPAN CLASS='comment'>/* Other members */</SPAN>
  66. };</PRE>
  67. <P>Here <A HREF="../classes/filter.html#synopsis"><CODE>multichar_input_filter</CODE></A> is a convenience base class which provides the member types <CODE>char_type</CODE> and <CODE>category</CODE>, as well as no-op implementations of member functions <CODE>close</CODE> and <CODE>imbue</CODE>.
  68. <A NAME="shell_comments_multichar_input_filter"></A>
  69. <H4><CODE>shell_comments_multichar_input_filter</CODE></H4>
  70. <P>You can express a <A HREF="shell_comments_filters.html">shell comments Filter</A> as an <A HREF="../concepts/multi_character.html">Multi-Character</A> <A HREF="../concepts/input_filter.html">InputFilter</A> as follows:</P>
  71. <PRE CLASS="broken_ie"><SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS="header" HREF="../../../../boost/iostreams/char_traits.hpp"><SPAN CLASS="literal">&lt;boost/iostreams/char_traits.hpp&gt;</SPAN></A> <SPAN CLASS="comment">// EOF, WOULD_BLOCK</SPAN>
  72. <SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS="header" HREF="../../../../boost/iostreams/concepts.hpp"><SPAN CLASS="literal">&lt;boost/iostreams/concepts.hpp&gt;</SPAN></A> <SPAN CLASS="comment">// multichar_input_filter</SPAN>
  73. <SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS="header" HREF="../../../../boost/iostreams/operations.hpp"><SPAN CLASS="literal">&lt;boost/iostreams/operations.hpp&gt;</SPAN></A> <SPAN CLASS="comment">// get</SPAN>
  74. <SPAN CLASS='keyword'>namespace</SPAN> boost { <SPAN CLASS='keyword'>namespace</SPAN> iostreams { <SPAN CLASS='keyword'>namespace</SPAN> example {
  75. <SPAN CLASS="keyword">class</SPAN> shell_comments_multichar_input_filter : <SPAN CLASS="keyword">public</SPAN> multichar_input_filter {
  76. <SPAN CLASS="keyword">public</SPAN>:
  77. <SPAN CLASS="keyword">explicit</SPAN> shell_comments_multichar_input_filter(<SPAN CLASS="keyword">char</SPAN> comment_char = '#')
  78. : comment_char_(comment_char), skip_(<SPAN CLASS="keyword">false</SPAN>)
  79. { }
  80. <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> Source&gt;
  81. std::streamsize read(Source&amp; src, <SPAN CLASS="keyword">char</SPAN>* s, std::streamsize n)
  82. {
  83. <SPAN CLASS="keyword">for</SPAN> (std::streamsize z = <SPAN CLASS='numeric_literal'>0</SPAN>; z &lt; n; ++z) {
  84. <SPAN CLASS="keyword">int</SPAN> c;
  85. <SPAN CLASS="keyword">while</SPAN> (true) {
  86. <SPAN CLASS="keyword">if</SPAN> ((c = boost::iostreams::get(src)) == <SPAN CLASS='numeric_literal'>EOF</SPAN>)
  87. <SPAN CLASS="keyword">return</SPAN> z != <SPAN CLASS='numeric_literal'>0</SPAN> ? z : <SPAN CLASS='numeric_literal'>-1</SPAN>;
  88. <SPAN CLASS="keyword">else</SPAN> <SPAN CLASS="keyword">if</SPAN> (c == WOULD_BLOCK)
  89. <SPAN CLASS="keyword">return</SPAN> z;
  90. skip_ = c == comment_char_ ?
  91. <SPAN CLASS="keyword">true</SPAN> :
  92. c == '\n' ?
  93. <SPAN CLASS="keyword">false</SPAN> :
  94. skip_;
  95. <SPAN CLASS="keyword">if</SPAN> (!skip_)
  96. <SPAN CLASS="keyword">break</SPAN>;
  97. }
  98. s[z] = c;
  99. }
  100. <SPAN CLASS="keyword">return</SPAN> n;
  101. }
  102. <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> Source&gt;
  103. <SPAN CLASS="keyword">void</SPAN> close(Source&amp;) { skip_ = false; }
  104. <SPAN CLASS="keyword">private</SPAN>:
  105. <SPAN CLASS="keyword">char</SPAN> comment_char_;
  106. <SPAN CLASS="keyword">bool</SPAN> skip_;
  107. };
  108. } } } <SPAN CLASS="comment">// End namespace boost::iostreams:example</SPAN></PRE>
  109. <P>
  110. Note that the implementation of <CODE>read</CODE> is very similar to what you would get if you put the implementation of <A HREF="shell_comments_filters.html#shell_comments_input_filter"><CODE>shell_comments_input_filter::get</CODE></A> inside a <CODE>for</CODE> loop iterating from <CODE>0</CODE> to <CODE>n</CODE>. InputFilters which call themselves recursively, such as <A HREF="tab_expanding_filters.html#tab_expanding_input_filter"><CODE>tab_expanding_input_filter</CODE></A>, are much harder to transform into Multi-Character filters.
  111. </P>
  112. <A NAME="multichar_output_filters"></A>
  113. <H4>Multi-Character OutputFilters</H4>
  114. <P>A typical narrow-character <A HREF="../concepts/multi_character.html">Multi-Character</A> <A HREF="../concepts/output_filter.html">OutputFilter</A> looks like this:<P>
  115. <PRE CLASS="broken_ie"><SPAN CLASS='preprocessor'>#include</SPAN> <SPAN CLASS="literal">&lt;iosfwd&gt;</SPAN> <SPAN CLASS='comment'>// streamsize</SPAN>
  116. <SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS="HEADER" HREF="../../../../boost/iostreams/categories.hpp"><SPAN CLASS='literal'>&lt;boost/iostreams/categories.hpp&gt;</SPAN></A> <SPAN CLASS='comment'>// tags
  117. </SPAN>
  118. <SPAN CLASS='keyword'>class</SPAN> my_output_filter {
  119. <SPAN CLASS='keyword'>public</SPAN>:
  120. <SPAN CLASS='keyword'>typedef</SPAN> <SPAN CLASS='keyword'>char</SPAN> char_type;
  121. <SPAN CLASS='keyword'>struct</SPAN> category
  122. : output_filter_tag,
  123. multichar_tag
  124. { };
  125. <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> Sink&gt;
  126. std::streamsize write(Sink&amp; dest, <SPAN CLASS='keyword'>const</SPAN> <SPAN CLASS='keyword'>char</SPAN>* s, std::streamsize n)
  127. {
  128. <SPAN CLASS='comment'>// Consume up to n filtered characters from the buffer s,</SPAN>
  129. <SPAN CLASS='comment'>// writing filtered characters to dest. Return the number</SPAN>
  130. <SPAN CLASS='comment'>// of characters consumed.</SPAN>
  131. }
  132. <SPAN CLASS='comment'>/* Other members */</SPAN>
  133. };</PRE>
  134. <P>
  135. Notice that the member type category is a <CODE>struct</CODE> convertible to <CODE>keyword</CODE> and to <CODE>multichar_tag</CODE>. This tells the Iostream library that <CODE>my_output_filter</CODE> is a <A HREF="../concepts/multi_character.html">Multi-Character Filter</A> and an <A HREF="../concepts/output_filter.html">OutputFilter</A>. As with <A HREF="../concepts/multi_character.html">Multi-Character</A> <A HREF="../concepts/input_filter.html">InputFilters</A>, you could have achieved the same effect several different ways. <I>E.g.</I>,
  136. </P>
  137. <PRE CLASS="broken_ie"> <SPAN CLASS='keyword'>struct</SPAN> category
  138. : output,
  139. filter_tag,
  140. multichar_tag
  141. { };
  142. <SPAN CLASS='comment'>/* or */</SPAN>
  143. <SPAN CLASS='keyword'>typedef</SPAN> multichar_output_filter_tag category;</PRE>
  144. <P>(For details, <I>see</I> <A HREF="../guide/modes.html#mode_tags">Mode Tags</A> and <A HREF="../guide/traits.html#category_tags">Category Tags</A>.)
  145. <P> You could also write the above example as follows:</P>
  146. <PRE CLASS="broken_ie"><SPAN CLASS='preprocessor'>#include</SPAN> <SPAN CLASS="literal">&lt;iosfwd&gt;</SPAN> <SPAN CLASS='comment'>// streamsize</SPAN>
  147. <SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS="HEADER" HREF="../../../../boost/iostreams/concepts.hpp"><SPAN CLASS='literal'>&lt;boost/iostreams/concepts.hpp&gt;</SPAN></A> <SPAN CLASS='comment'>// multichar_output_filter</SPAN>
  148. <SPAN CLASS='keyword'>class</SPAN> my_output_filter : <SPAN CLASS='keyword'>public</SPAN> multichar_output_filter {
  149. <SPAN CLASS='keyword'>public</SPAN>:
  150. <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> Sink&gt;
  151. std::streamsize write(Sink&amp; dest, <SPAN CLASS='keyword'>const</SPAN> <SPAN CLASS='keyword'>char</SPAN>* s, std::streamsize n);
  152. <SPAN CLASS='comment'>/* Other members */</SPAN>
  153. };</PRE>
  154. <P>Here <A HREF="../classes/filter.html#synopsis"><CODE>multichar_output_filter</CODE></A> is a convenience base class which provides the member types <CODE>char_type</CODE> and <CODE>category</CODE>, as well as no-op implementations of member functions <CODE>close</CODE> and <CODE>imbue</CODE>.
  155. <A NAME="shell_comments_multichar_ouput_filter"></A>
  156. <H4><CODE>shell_comments_multichar_output_filter</CODE></H4>
  157. <P>You can express a <A HREF="shell_comments_filters.html">shell comments Filter</A> as an <A HREF="../concepts/multi_character.html">Multi-Character</A> <A HREF="../concepts/output_filter.html">OutputFilter</A> as follows:</P>
  158. <PRE CLASS="broken_ie"><SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS="header" HREF="../../../../boost/iostreams/char_traits.hpp"><SPAN CLASS="literal">&lt;boost/iostreams/char_traits.hpp&gt;</SPAN></A> <SPAN CLASS="comment">// EOF, WOULD_BLOCK</SPAN>
  159. <SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS="header" HREF="../../../../boost/iostreams/concepts.hpp"><SPAN CLASS="literal">&lt;boost/iostreams/concepts.hpp&gt;</SPAN></A> <SPAN CLASS="comment">// multichar_output_filter</SPAN>
  160. <SPAN CLASS='preprocessor'>#include</SPAN> <A CLASS="header" HREF="../../../../boost/iostreams/operations.hpp"><SPAN CLASS="literal">&lt;boost/iostreams/operations.hpp&gt;</SPAN></A> <SPAN CLASS="comment">// get</SPAN>
  161. <SPAN CLASS='keyword'>namespace</SPAN> boost { <SPAN CLASS='keyword'>namespace</SPAN> iostreams { <SPAN CLASS='keyword'>namespace</SPAN> example {
  162. <SPAN CLASS="keyword">class</SPAN> shell_comments_multichar_output_filter : <SPAN CLASS="keyword">public</SPAN> multichar_output_filter {
  163. <SPAN CLASS="keyword">public</SPAN>:
  164. <SPAN CLASS="keyword">explicit</SPAN> shell_comments_multichar_output_filter(<SPAN CLASS="keyword">char</SPAN> comment_char = '#')
  165. : comment_char_(comment_char), skip_(<SPAN CLASS="keyword">false</SPAN>)
  166. { }
  167. <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> Sink&gt;
  168. std::streamsize write(Sink&amp; dest, <SPAN CLASS="keyword">const</SPAN> char* s, std::streamsize n)
  169. {
  170. std::streamsize z;
  171. <SPAN CLASS="keyword">for</SPAN> (z = <SPAN CLASS='numeric_literal'>0</SPAN>; z &lt; n; ++z) {
  172. <SPAN CLASS="keyword">int</SPAN> c = s[z];
  173. skip_ = c == comment_char_ ?
  174. <SPAN CLASS="keyword">true</SPAN> :
  175. c == <SPAN CLASS='literal'>'\n'</SPAN> ?
  176. <SPAN CLASS="keyword">false</SPAN> :
  177. skip_;
  178. <SPAN CLASS="keyword">if</SPAN> (skip_)
  179. <SPAN CLASS="keyword">continue</SPAN>;
  180. <SPAN CLASS="keyword">if</SPAN> (!iostreams::put(dest, c))
  181. <SPAN CLASS="keyword">break</SPAN>;
  182. }
  183. <SPAN CLASS="keyword">return</SPAN> z;
  184. }
  185. <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> Source&gt;
  186. <SPAN CLASS="keyword">void</SPAN> close(Source&amp;) { skip_ = <SPAN CLASS="keyword">false</SPAN>; }
  187. <SPAN CLASS="keyword">private</SPAN>:
  188. <SPAN CLASS="keyword">char</SPAN> comment_char_;
  189. <SPAN CLASS="keyword">bool</SPAN> skip_;
  190. };
  191. } } } <SPAN CLASS="comment">// End namespace boost::iostreams:example</SPAN></PRE>
  192. <P>
  193. Note that the implementation of <CODE>write</CODE> is very similar to what you would get if you put the implementation of <A HREF="shell_comments_filters.html#shell_comments_output_filter"><CODE>shell_comments_output_filter::put</CODE></A> inside a <CODE>for</CODE> loop iterating from <CODE>0</CODE> to <CODE>n</CODE>. OutputFilters which call themselves recursively, such as <A HREF="unix2dos_filters.html#unix2dos_output_filter"><CODE>unix2dos_output_filter</CODE></A>, are much harder to transform into Multi-Character filters.
  194. </P>
  195. <!-- Begin Nav -->
  196. <DIV CLASS='nav'>
  197. <A HREF='unix2dos_filters.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/src/images/prev.png'></A>
  198. <A HREF='tutorial.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/src/images/up.png'></A>
  199. <A HREF='dual_use_filters.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/src/images/next.png'></A>
  200. </DIV>
  201. <!-- End Nav -->
  202. <!-- Begin Footer -->
  203. <HR>
  204. <P CLASS="copyright">&copy; Copyright 2008 <a href="http://www.coderage.com/" target="_top">CodeRage, LLC</a><br/>&copy; Copyright 2004-2007 <a href="https://www.boost.org/users/people/jonathan_turkanis.html" target="_top">Jonathan Turkanis</a></P>
  205. <P CLASS="copyright">
  206. Use, modification, and distribution are subject to the Boost Software License, Version 2.0. (See accompanying file <A HREF="../../../../LICENSE_1_0.txt">LICENSE_1_0.txt</A> or copy at <A HREF="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</A>)
  207. </P>
  208. <!-- End Footer -->
  209. </BODY>