tab_expanding_filters.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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='line_wrapping_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='dictionary_filters.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/src/images/next.png'></A>
  18. </DIV>
  19. <!-- End Nav -->
  20. <A NAME="tab_expanding"></A>
  21. <H2>2.2.5. Tab-Expanding Filters</H2>
  22. <P>
  23. Suppose you want to write a filter which replaces each tab character with one or more space characters in such a way that the document appears unchanged when displayed. The basic algorithm is as follows: You examine characters one at a time, forwarding them <I>as-is</I> and keeping track of the current column number. When you encounter a tab character, you replace it with a sequence of space characters whose length depends on the current column count. When you encounter a newline character, you forward it and reset the column count.
  24. </P>
  25. <P>
  26. In the next three sections, I'll express this algorithm as a <A HREF="../classes/stdio_filter.html"><CODE>stdio_filter</CODE></A>, an <A HREF="../concepts/input_filter.html">InputFilter</A> and an <A HREF="../concepts/output_filter.html">OutputFilter</A>. The source code can be found in the header <A HREF="../../example/tab_expanding_filter.hpp"><CODE>&lt;libs/iostreams/example/tab_expanding_filter.hpp&gt;</CODE></A>. These examples were inspired by James Kanze's <CODE>ExpandTabsInserter.hh</CODE> (<I>see</I> <A CLASS="bib_ref" HREF="../bibliography.html#kanze">[Kanze]</A>).
  27. </P>
  28. <A NAME="tab_expanding_stdio_filter"></A>
  29. <H4><CODE>tab_expanding_stdio_filter</CODE></H4>
  30. <P>You can express a tab-expanding Filter as a <A HREF="../classes/stdio_filter.html"><CODE>stdio_filter</CODE></A> as follows:</P>
  31. <PRE class="broken_ie"><SPAN CLASS='preprocessor'>#include</SPAN> <SPAN CLASS="literal">&lt;cstdio&gt;</SPAN> <SPAN CLASS="comment">// EOF</SPAN>
  32. <SPAN CLASS="preprocessor">#include</SPAN> <SPAN CLASS="literal">&lt;iostream&gt;</SPAN> <SPAN CLASS="comment">// cin, cout</SPAN>
  33. <SPAN CLASS="preprocessor">#include</SPAN> <A CLASS="header" HREF="../../../../boost/iostreams/filter/stdio.hpp"><SPAN CLASS="literal">&lt;boost/iostreams/filter/stdio.hpp&gt;</SPAN></A>
  34. <SPAN CLASS="keyword">class</SPAN> tab_expanding_stdio_filter : <SPAN CLASS="keyword"><SPAN CLASS="keyword"><SPAN CLASS="keyword">public</SPAN></SPAN></SPAN> stdio_filter {
  35. <SPAN CLASS="keyword">public</SPAN>:
  36. explicit tab_expanding_stdio_filter(<SPAN CLASS="keyword"><SPAN CLASS="keyword">int</SPAN></SPAN> tab_size = 8)
  37. : tab_size_(tab_size), col_no_(0)
  38. {
  39. assert(tab_size &gt; 0);
  40. }
  41. <SPAN CLASS="keyword">private</SPAN>:
  42. <SPAN CLASS="keyword">void</SPAN> do_filter();
  43. <SPAN CLASS="keyword">void</SPAN> do_close();
  44. <SPAN CLASS="keyword">void</SPAN> put_char(<SPAN CLASS="keyword"><SPAN CLASS="keyword">int</SPAN></SPAN> c);
  45. <SPAN CLASS="keyword"><SPAN CLASS="keyword">int</SPAN></SPAN> tab_size_;
  46. <SPAN CLASS="keyword"><SPAN CLASS="keyword">int</SPAN></SPAN> col_no_;
  47. };
  48. } } } <SPAN CLASS="comment">// End namespace boost::iostreams:example</SPAN></PRE>
  49. <P>The helper function <CODE>put_char</CODE> is identical to <A HREF="line_wrapping_filters.html#line_wrapping_stdio_filter"><CODE>line_wrapping_stdio_filter::put_char</CODE></A>. It writes a character to <CODE>std::cout</CODE> and updates the column count:</P>
  50. <PRE class="broken_ie"> <SPAN CLASS="keyword">void</SPAN> put_char(<SPAN CLASS="keyword"><SPAN CLASS="keyword">int</SPAN></SPAN> c)
  51. {
  52. std::cout.put(c);
  53. <SPAN CLASS="keyword">if</SPAN> (c == <SPAN CLASS="literal">'\n'</SPAN>) {
  54. col_no_ = 0;
  55. } <SPAN CLASS="keyword">else</SPAN> {
  56. ++col_no_;
  57. }
  58. }</PRE>
  59. <P>Using <CODE>put_char</CODE> you can implement <CODE>do_filter</CODE> as follows:</P>
  60. <PRE class="broken_ie"> <SPAN CLASS="keyword">void</SPAN> do_filter()
  61. {
  62. <SPAN CLASS="keyword"><SPAN CLASS="keyword">int</SPAN></SPAN> c;
  63. <SPAN CLASS="keyword">while</SPAN> ((c = std::cin.get()) != <SPAN CLASS="numeric_literal">EOF</SPAN>) {
  64. <SPAN CLASS="keyword">if</SPAN> (c == '\t') {
  65. <SPAN CLASS="keyword"><SPAN CLASS="keyword">int</SPAN></SPAN> spaces = tab_size_ - (col_no_ % tab_size_);
  66. for (; spaces &gt; 0; --spaces)
  67. put_char(' ');
  68. } <SPAN CLASS="keyword">else</SPAN> {
  69. put_char(c);
  70. }
  71. }
  72. }</PRE>
  73. <P>The <CODE>while</CODE> loop reads a character from <CODE>std::cin</CODE> and writes it to <CODE>std::cout</CODE>, unless it is a tab character, in which case it writes an appropriate number of space characters to <CODE>std::cout</CODE>.</P>
  74. <P>As with <A HREF="line_wrapping_filters.html#line_wrapping_stdio_filter"><CODE>line_wrapping_stdio_filter</CODE></A>, the <CODE>virtual</CODE> function <CODE>do_close</CODE> resets the Filter's state:</P>
  75. <PRE class="broken_ie"> <SPAN CLASS="keyword">void</SPAN> do_close() { col_no_ = 0; }</PRE>
  76. <A NAME="tab_expanding_input_filter"></A>
  77. <H4><CODE>tab_expanding_input_filter</CODE></H4>
  78. <P>You can express a tab-expanding Filter as an <A HREF="../concepts/input_filter.html">InputFilter</A> as follows:</P>
  79. <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>
  80. <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">// input_filter</SPAN>
  81. <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>
  82. <SPAN CLASS='keyword'>namespace</SPAN> boost { <SPAN CLASS='keyword'>namespace</SPAN> iostreams { <SPAN CLASS='keyword'>namespace</SPAN> example {
  83. <SPAN CLASS="keyword">class</SPAN> tab_expanding_input_filter : <SPAN CLASS="keyword"><SPAN CLASS="keyword"><SPAN CLASS="keyword">public</SPAN></SPAN></SPAN> input_filter {
  84. <SPAN CLASS="keyword">public</SPAN>:
  85. explicit tab_expanding_input_filter(<SPAN CLASS="keyword"><SPAN CLASS="keyword">int</SPAN></SPAN> tab_size = 8)
  86. : tab_size_(tab_size), col_no_(0), spaces_(0)
  87. {
  88. assert(tab_size &gt; 0);
  89. }
  90. <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> Source&gt;
  91. <SPAN CLASS="keyword"><SPAN CLASS="keyword">int</SPAN></SPAN> get(Source&amp; src);
  92. <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> Source&gt;
  93. <SPAN CLASS="keyword">void</SPAN> close(Source&amp;);
  94. <SPAN CLASS="keyword">private</SPAN>:
  95. <SPAN CLASS="keyword"><SPAN CLASS="keyword">int</SPAN></SPAN> get_char(<SPAN CLASS="keyword"><SPAN CLASS="keyword">int</SPAN></SPAN> c);
  96. <SPAN CLASS="keyword"><SPAN CLASS="keyword">int</SPAN></SPAN> tab_size_;
  97. <SPAN CLASS="keyword"><SPAN CLASS="keyword">int</SPAN></SPAN> col_no_;
  98. <SPAN CLASS="keyword"><SPAN CLASS="keyword">int</SPAN></SPAN> spaces_;
  99. };
  100. } } } <SPAN CLASS="comment">// End namespace boost::iostreams:example</SPAN></PRE>
  101. <P>Let's look first at the helper function <CODE>get_char</CODE>:</P>
  102. <PRE class="broken_ie"> <SPAN CLASS="keyword"><SPAN CLASS="keyword">int</SPAN></SPAN> get_char(<SPAN CLASS="keyword"><SPAN CLASS="keyword">int</SPAN></SPAN> c)
  103. {
  104. <SPAN CLASS="keyword">if</SPAN> (c == <SPAN CLASS="literal">'\n'</SPAN>) {
  105. col_no_ = 0;
  106. } <SPAN CLASS="keyword">else</SPAN> {
  107. ++col_no_;
  108. }
  109. <SPAN CLASS="keyword">return</SPAN> c;
  110. }</PRE>
  111. <P>This function updates the column count based on the given character <CODE>c</CODE> and returns <CODE>c</CODE>. Using <CODE>get_char</CODE> you can implement <CODE>get</CODE> as follows:</P>
  112. <PRE class="broken_ie"> <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> Source&gt;
  113. <SPAN CLASS="keyword"><SPAN CLASS="keyword">int</SPAN></SPAN> get(Source&amp; src)
  114. {
  115. <SPAN CLASS="keyword">if</SPAN> (spaces_ &gt; 0) {
  116. --spaces_;
  117. <SPAN CLASS="keyword">return</SPAN> get_char(' ');
  118. }
  119. <SPAN CLASS="keyword"><SPAN CLASS="keyword">int</SPAN></SPAN> c;
  120. <SPAN CLASS="keyword">if</SPAN> ((c = iostreams::get(src)) == <SPAN CLASS="numeric_literal">EOF</SPAN> || c == WOULD_BLOCK)
  121. <SPAN CLASS="keyword">return</SPAN> c;
  122. <SPAN CLASS="keyword">if</SPAN> (c != '\t')
  123. <SPAN CLASS="keyword">return</SPAN> get_char(c);
  124. <SPAN CLASS='comment'>// Found a tab. Call this filter recursively.</SPAN>
  125. spaces_ = tab_size_ - (col_no_ % tab_size_);
  126. <SPAN CLASS="keyword">return</SPAN> this-&gt;get(src);
  127. }</PRE>
  128. <P>
  129. The implementation is similar to that of <A HREF="line_wrapping_filters.html#line_wrapping_input_filter"><CODE>line_wrapping_input_filter::get</CODE></A>. Since <CODE>get</CODE> can only return a single character at a time, whenever a tab character must be replaced by a sequence of space character, only the first space character can be returned. The rest must be returned by subsequent invocations of <CODE>get</CODE>. The member variable <CODE>spaces_</CODE> is used to store the number of such space characters.
  130. </P>
  131. <P>
  132. The implementation begins by checking whether any space characters remain to be returned. If so, it decrements <CODE>spaces_</CODE> and returns a space. Otherwise, a character is read from <CODE>src</CODE>. Ordinary characters, as well as the special values <CODE>EOF</CODE> and <CODE>WOULD_BLOCK</CODE>, are returned <I>as-is</I>. When a tab character is encountered, the number of spaces which must be returned by future invocations of get is recorded, and a space character is returned.
  133. </P>
  134. <P>As usual, the function <CODE>close</CODE> resets the Filter's state:</P>
  135. <PRE class="broken_ie"> <SPAN CLASS="keyword">void</SPAN> close(Source&amp;)
  136. {
  137. col_no_ = 0;
  138. spaces_ = 0;
  139. }</PRE>
  140. <A NAME="tab_expanding_output_filter"></A>
  141. <H4><CODE>tab_expanding_output_filter</CODE></H4>
  142. <P>You can express a tab-expanding Filter as an <A HREF="../concepts/output_filter.html">OutputFilter</A> as follows:</P>
  143. <PRE class="broken_ie"><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">// output_filter</SPAN>
  144. <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">// put</SPAN>
  145. <SPAN CLASS='keyword'>namespace</SPAN> boost { <SPAN CLASS='keyword'>namespace</SPAN> iostreams { <SPAN CLASS='keyword'>namespace</SPAN> example {
  146. <SPAN CLASS="keyword">class</SPAN> tab_expanding_output_filter : <SPAN CLASS="keyword"><SPAN CLASS="keyword"><SPAN CLASS="keyword">public</SPAN></SPAN></SPAN> output_filter {
  147. <SPAN CLASS="keyword">public</SPAN>:
  148. explicit tab_expanding_output_filter(<SPAN CLASS="keyword"><SPAN CLASS="keyword">int</SPAN></SPAN> tab_size = 8)
  149. : tab_size_(tab_size), col_no_(0), spaces_(0)
  150. {
  151. assert(tab_size &gt; 0);
  152. }
  153. <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> Sink&gt;
  154. <SPAN CLASS="keyword"><SPAN CLASS="keyword">bool</SPAN></SPAN> put(Sink&amp; dest, <SPAN CLASS="keyword"><SPAN CLASS="keyword">int</SPAN></SPAN> c);
  155. <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> Sink&gt;
  156. <SPAN CLASS="keyword">void</SPAN> close(Sink&amp;);
  157. <SPAN CLASS="keyword">private</SPAN>:
  158. <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> Sink&gt;
  159. <SPAN CLASS="keyword"><SPAN CLASS="keyword">bool</SPAN></SPAN> put_char(Sink&amp; dest, <SPAN CLASS="keyword"><SPAN CLASS="keyword">int</SPAN></SPAN> c);
  160. <SPAN CLASS="keyword"><SPAN CLASS="keyword">int</SPAN></SPAN> tab_size_;
  161. <SPAN CLASS="keyword"><SPAN CLASS="keyword">int</SPAN></SPAN> col_no_;
  162. <SPAN CLASS="keyword"><SPAN CLASS="keyword">int</SPAN></SPAN> spaces_;
  163. };
  164. } } } <SPAN CLASS="comment">// End namespace boost::iostreams:example</SPAN></PRE>
  165. <P>The implemenation helper function <CODE>put_char</CODE> is the same as that of <A HREF="line_wrapping_filters.html#line_wrapping_output_filter"><CODE>line_wrapping_output_filter::put_char</CODE></A>: it writes the given character to <CODE>std::cout</CODE> and increments the column number, unless the character is a newline, in which case the column number is reset.</P>
  166. <PRE class="broken_ie"> <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> Sink&gt;
  167. <SPAN CLASS="keyword"><SPAN CLASS="keyword">bool</SPAN></SPAN> put_char(Sink&amp; dest, <SPAN CLASS="keyword"><SPAN CLASS="keyword">int</SPAN></SPAN> c)
  168. {
  169. <SPAN CLASS="keyword">if</SPAN> (!iostreams::put(dest, c))
  170. <SPAN CLASS="keyword">return</SPAN> <SPAN CLASS="keyword">false</SPAN>;
  171. <SPAN CLASS="keyword">if</SPAN> (c != <SPAN CLASS="literal">'\n'</SPAN>)
  172. ++col_no_;
  173. <SPAN CLASS="keyword">else</SPAN>
  174. col_no_ = 0;
  175. <SPAN CLASS="keyword">return</SPAN> <SPAN CLASS="keyword">true</SPAN>;
  176. }</PRE>
  177. <P>Using <CODE>put_char</CODE> you can implement <CODE>put</CODE> as follows:</P>
  178. <PRE class="broken_ie"> <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> Sink&gt;
  179. <SPAN CLASS="keyword"><SPAN CLASS="keyword">bool</SPAN></SPAN> put(Sink&amp; dest, <SPAN CLASS="keyword"><SPAN CLASS="keyword">int</SPAN></SPAN> c)
  180. {
  181. for (; spaces_ &gt; 0; --spaces_)
  182. <SPAN CLASS="keyword">if</SPAN> (!put_char(dest, ' '))
  183. <SPAN CLASS="keyword">return</SPAN> <SPAN CLASS="keyword">false</SPAN>;
  184. <SPAN CLASS="keyword">if</SPAN> (c == '\t') {
  185. spaces_ = tab_size_ - (col_no_ % tab_size_) - 1;
  186. <SPAN CLASS="keyword">return</SPAN> this-&gt;put(dest, ' ');
  187. }
  188. <SPAN CLASS="keyword">return</SPAN> put_char(dest, c);
  189. }</PRE>
  190. <P>
  191. The implementation begins by attempting to write any space characters left over from previously encountered tabs. If successful, it examine the given character <CODE>c</CODE>. If <CODE>c</CODE> is not a tab character, it attempts to write it to <CODE>dest</CODE>. Otherwise, it calculates the number of spaces which must be inserted and calls itself recursively. Using recursion here saves us from having to decrement the member variable <CODE>spaces_</CODE> at two different points in the code.
  192. </P>
  193. <P>
  194. Note that after a tab character is encountered, get will return false until all the associated space characters have been written.
  195. </P>
  196. <P>As usual, the function <CODE>close</CODE> resets the Filter's state:</P>
  197. <PRE class="broken_ie"> <SPAN CLASS="keyword">void</SPAN> close(Source&amp;)
  198. {
  199. col_no_ = 0;
  200. spaces_ = 0;
  201. }</PRE>
  202. <!-- Begin Nav -->
  203. <DIV CLASS='nav'>
  204. <A HREF='line_wrapping_filters.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/src/images/prev.png'></A>
  205. <A HREF='tutorial.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/src/images/up.png'></A>
  206. <A HREF='dictionary_filters.html'><IMG BORDER=0 WIDTH=19 HEIGHT=19 SRC='../../../../doc/src/images/next.png'></A>
  207. </DIV>
  208. <!-- End Nav -->
  209. <!-- Begin Footer -->
  210. <HR>
  211. <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>
  212. <P CLASS="copyright">
  213. 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>)
  214. </P>
  215. <!-- End Footer -->
  216. </BODY>