input_filter.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <HTML>
  3. <HEAD>
  4. <TITLE>InputFilter</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">InputFilter</H1>
  11. <HR CLASS="banner">
  12. <!-- End Banner -->
  13. <DL CLASS="page-index" STYLE="margin-top:0">
  14. <DT><A HREF="#definition">Definition</A>
  15. <DT><A HREF="#description">Description</A>
  16. <DT><A HREF="#examples">Examples</A>
  17. <DT><A HREF="#detailed_specification">Detailed Specification</A>
  18. </DL>
  19. <A NAME="definition"></A>
  20. <H2>Definition</H2>
  21. <P>
  22. An InputFilter is a <A HREF="filter.html">Filter</A> whose <A HREF="../guide/modes.html">mode</A> refines <A HREF="../guide/modes.html#input">input</A>.
  23. </P>
  24. <A NAME="description"></A>
  25. <H2>Description</H2>
  26. <P>
  27. An InputFilter operates on the character sequence controlled by a <A HREF="source.html">Source</A>, providing access to a filtered sequence having the same character type. It may expose the filtered sequence in two ways:
  28. <OL>
  29. <LI STYLE="list-style-type:lower-roman">
  30. by defining a member function <CODE>get</CODE>
  31. </LI>
  32. <LI STYLE="list-style-type:lower-roman">
  33. by defining a member function <CODE>read</CODE>
  34. </OL>
  35. The second alternative is provided for enhanced performance. InputFilters implementing this alternative are referred to as <I>Multi-Character</I>. (<I>See</I> <A HREF="multi_character.html">Multi-Character Filter</A>.)
  36. </P>
  37. <A NAME="examples"></A>
  38. <H2>Examples</H2>
  39. <H4>I. Ordinary InputFilter</H4>
  40. <P>
  41. The following example shows an InputFilter which removes all non-alphabetic characters from a sequence.
  42. </P>
  43. <PRE CLASS="broken_ie"> <SPAN CLASS="preprocessor">#include</SPAN> <SPAN CLASS="literal">&lt;ctype.h&gt;</SPAN> <SPAN CLASS="comment">// isalpha</SPAN>
  44. <SPAN CLASS="preprocessor">#include</SPAN> <SPAN CLASS="literal">&lt;cstdio.h&gt;</SPAN> <SPAN CLASS="comment">// EOF</SPAN>
  45. <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">// input_filter_tag</SPAN>
  46. <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, WOULD_BLOCK</SPAN>
  47. <SPAN CLASS="keyword">using</SPAN> <SPAN CLASS="keyword">namespace</SPAN> std;
  48. <SPAN CLASS="keyword">using</SPAN> <SPAN CLASS="keyword">namespace</SPAN> boost::iostreams;
  49. <SPAN CLASS="keyword">struct</SPAN> alphabetic_input_filter {
  50. <SPAN CLASS="keyword">typedef</SPAN> <SPAN CLASS="keyword">char</SPAN> char_type;
  51. <SPAN CLASS="keyword">typedef</SPAN> input_filter_tag category;
  52. <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> Source&gt;
  53. <SPAN CLASS="keyword">int</SPAN> get(Source&amp; src)
  54. {
  55. <SPAN CLASS="keyword">int</SPAN> c;
  56. <SPAN CLASS="keyword">while</SPAN> ( (c = boost::iostreams::get(src)) != EOF &amp;&amp;
  57. c != WOULD_BLOCK &amp;&amp;
  58. !isalpha((<SPAN CLASS="keyword">unsigned</SPAN> <SPAN CLASS="keyword">char</SPAN>) c) )
  59. ;
  60. <SPAN CLASS="keyword">return</SPAN> c;
  61. }
  62. };</PRE>
  63. <P>
  64. Here <CODE>char_type</CODE> is the <A HREF="../guide/traits.html#char_type">character type</A> of the Filter, <CODE>input_filter_tag</CODE> is a <A HREF="../guide/traits.html#category_tags">category tag</A> identifying the Filter as a model of InputFilter, and the function <A HREF="../functions/get.html"><CODE>boost::iostreams::get</CODE></A> reads a character from an arbitrary Source.<A CLASS="footnote_ref" NAME="note_1_ref" HREF="#note_1"><SUP>[1]</SUP></A> The constant <A HREF="../classes/char_traits.html#WOULD_BLOCK"><CODE>WOULD_BLOCK</CODE></A>, defined in the header <A HREF="../../../../boost/iostreams/char_traits.hpp"><CODE>&lt;boost/iostreams/char_traits.hpp&gt;</CODE></A>, is used to indicate that input is temporarily unavilable.
  65. </P>
  66. <P>
  67. The Iostreams library defines two convenience classes, <A HREF="../classes/filter.html#synopsis"><CODE>input_filter</CODE></A> and <A HREF="../classes/filter.html#synopsis"><CODE>input_wfilter</CODE></A>, which provide member <CODE>typedef</CODE>s <CODE>char_type</CODE> and <CODE>category</CODE> as well as default implementations of several member functions. When defining a new model of InputFilter, it is often sufficient to derive from <CODE>input_filter</CODE> or <CODE>input_wfilter</CODE> and to define a member function <CODE>get</CODE>.
  68. </P>
  69. <H4>II. Multi-Character InputFilter</H4>
  70. <P>
  71. The following example shows a Multi-Character InputFilter which performs the same filtering operation as the Filter in Example I.
  72. </P>
  73. <PRE CLASS="broken_ie"> <SPAN CLASS="preprocessor">#include</SPAN> <SPAN CLASS="literal">&lt;ctype.h&gt;</SPAN> <SPAN CLASS="comment">// isalpha</SPAN>
  74. <SPAN CLASS="preprocessor">#include</SPAN> <SPAN CLASS="literal">&lt;cstdio.h&gt;</SPAN> <SPAN CLASS="comment">// EOF</SPAN>
  75. <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">// input_filter_tag</SPAN>
  76. <SPAN CLASS="preprocessor">#include</SPAN> <A CLASS="header" HREF="../../../../boost/iostreams/concepts.hpp"><SPAN CLASS="literal">&lt;boost/iostreams/operations.hpp&gt;</SPAN></A> <SPAN CLASS="comment">// get</SPAN>
  77. <SPAN CLASS="keyword">using</SPAN> <SPAN CLASS="keyword">namespace</SPAN> std;
  78. <SPAN CLASS="keyword">using</SPAN> <SPAN CLASS="keyword">namespace</SPAN> boost::io;
  79. <SPAN CLASS="keyword">struct</SPAN> alphabetic_input_filter {
  80. <SPAN CLASS="keyword">typedef</SPAN> <SPAN CLASS="keyword">char</SPAN> char_type;
  81. <SPAN CLASS="keyword">typedef</SPAN> multichar_input_filter_tag category;
  82. <SPAN CLASS="keyword">template</SPAN>&lt;<SPAN CLASS="keyword">typename</SPAN> Source&gt;
  83. streamsize read(Source&amp; src, <SPAN CLASS="keyword">char</SPAN>* s, streamsize n)
  84. {
  85. <SPAN CLASS="keyword">int</SPAN> c;
  86. <SPAN CLASS="keyword">char</SPAN>* first = s;
  87. <SPAN CLASS="keyword">char</SPAN>* last = s + n;
  88. <SPAN CLASS="keyword">while</SPAN> ( first != last &amp;&amp;
  89. (c = boost::iostreams::get(src)) != EOF &amp;&amp;
  90. c != WOULD_BLOCK &amp;&amp;
  91. isalpha((<SPAN CLASS="keyword">unsigned</SPAN> <SPAN CLASS="keyword">char</SPAN>) c) )
  92. {
  93. *first++ = c;
  94. }
  95. streamsize result = <SPAN CLASS="keyword">static_cast</SPAN>&lt;streamsize&gt;(first - s);
  96. <SPAN CLASS="keyword">return</SPAN> result == <SPAN CLASS='numeric_literal'>0</SPAN> &amp;&amp; c != WOULD_BLOCK ?
  97. <SPAN CLASS='numeric_literal'>-1</SPAN> :
  98. result;
  99. }
  100. };</PRE>
  101. <P>
  102. Here <CODE>multichar_input_filter_tag</CODE> is a <A HREF="../guide/traits.html#category">category tag</A> identifying the Filter as a Multi-Character InputFilter.
  103. </P>
  104. <P>
  105. The Iostreams library defines two convenience classes, <A HREF="../classes/filter.html#synopsis"><CODE>multichar_input_filter</CODE></A> and <A HREF="../classes/filter.html#synopsis"><CODE>multichar_input_wfilter</CODE></A>, which provide the member <CODE>typedef</CODE>s <CODE>char_type</CODE> and <CODE>category</CODE> as well as default implementations of several member functions. When defining a new Multi-Character InputFilter, it is often sufficient to derive from <CODE>multichar_input_filter</CODE> or <CODE>multichar_input_wfilter</CODE> and to define a member function <CODE>read</CODE>.
  106. </P>
  107. <A NAME="detailed_specification"></A>
  108. <H2>Refinement of</H2>
  109. <P><A HREF="filter.html">Filter</A>.</P>
  110. <H2>Associated Types</H2>
  111. <TABLE CELLPADDING="5" BORDER="1">
  112. <TR><TD>Character type</TD><TD>The type of the characters in the filtered sequences</TD></TR>
  113. <TR>
  114. <TD>Category</TD>
  115. <TD>
  116. A type convertible to <A HREF="../guide/traits.html#category_tags"><CODE>filter_tag</CODE></A> and to <A HREF="../guide/modes.html#input"><CODE>input</CODE></A>
  117. </TD>
  118. </TR>
  119. <TR>
  120. <TD>Mode</TD>
  121. <TD>
  122. The unique <I>most-derived</I> <A HREF="../guide/modes.html#mode_tags">mode tag</A> to which Category is convertible
  123. </TD>
  124. </TR>
  125. </TABLE>
  126. <H2>Notation</H2>
  127. <TABLE CELLPADDING="2">
  128. <TR><TD><CODE>F</CODE></TD><TD>- A type which is a model of InputFilter</TD></TR>
  129. <TR><TD><CODE>D</CODE></TD><TD>- A type which is a model of <A HREF="device.html">Device</A>, with the same character type as <CODE>F</CODE> and with mode refining the mode of <CODE>F</CODE></TD></TR>
  130. <TR><TD><CODE>Ch</CODE></TD><TD>- The character type of <CODE>F</CODE></TD></TR>
  131. <TR><TD><CODE>Tr</CODE></A></TD><TD>- <A HREF="../classes/char_traits.html"><CODE>boost::iostreams::char_traits&lt;Ch&gt;</CODE></A></TD></TR>
  132. <TR><TD><CODE>f</CODE></TD><TD>- Object of type <CODE>F</CODE></TD></TR>
  133. <TR><TD><CODE>d</CODE></TD><TD>- Object of type <CODE>D</CODE></TD></TR>
  134. <TR><TD><CODE>s</CODE></TD><TD>- Object of type <CODE>Ch*</CODE></TD></TR>
  135. <TR><TD><CODE>n</CODE></TD><TD>- Object of type <CODE>std::streamsize</CODE></TD></TR>
  136. <TR><TD><CODE>io</CODE></TD><TD>- Alias for namespace <CODE>boost::iostreams</CODE></TD></TR>
  137. </TABLE>
  138. <A NAME="semantics"></A>
  139. <H2>Valid Expressions / Semantics</H2>
  140. <TABLE CELLPADDING="5" BORDER="1">
  141. <TR><TH>Expression</TH><TH>Expression Type</TH><TH>Category Precondition</TH><TH>Semantics</TH></TR>
  142. <TR>
  143. <TD>
  144. <PRE CLASS="plain_code"><CODE>typename <A HREF="../guide/traits.html#char_type_of_ref">char_type_of</A>&lt;F&gt;::type</CODE></PRE>
  145. </TD>
  146. <TD><CODE>typename</CODE> of the character type</TD>
  147. <TD ALIGN="center">-</TD><TD ALIGN="center">-</TD>
  148. </TR>
  149. <TR>
  150. <TD>
  151. <PRE CLASS="plain_code"><CODE>typename <A HREF="../guide/traits.html#category_ref">category_of</A>&lt;F&gt;::type</CODE></PRE>
  152. </TD>
  153. <TD><CODE>typename</CODE> of the category</TD>
  154. <TD ALIGN="center">-</TD><TD ALIGN="center">-</TD>
  155. </TR>
  156. <TR>
  157. <TD><PRE CLASS="plain_code"><CODE>f.get(d)</CODE></PRE></TD>
  158. <TD><CODE>Tr::int_type</CODE></TD>
  159. <TD>
  160. Convertible to <A HREF="../guide/modes.html#mode_tags"><CODE>input</CODE></A> but not to <A HREF="../guide/traits.html#category_tags"><CODE>multichar_tag</CODE></A>
  161. </TD>
  162. <TD>
  163. Returns the next character in the input sequence controlled by <CODE>f</CODE>, <CODE>Tr::eof()</CODE> if the end of the sequence has been reached or <CODE>Tr::would_block()</CODE> if input is temporarily unavilable because a call to <CODE>d</CODE> has produced fewer characters than requested. The input sequence controlled by <CODE>d</CODE> may be accessed using <A HREF="../functions/get.html"><CODE>io::get</CODE></A>, <A HREF="../functions/read.html"><CODE>io::read</CODE></A> and <A HREF="../functions/putback.html"><CODE>io::putback</CODE></A>.
  164. </TD>
  165. </TR>
  166. <TR>
  167. <TD><PRE CLASS="plain_code"><CODE>f.read(d, s, n)</CODE></PRE></TD>
  168. <TD><PRE CLASS="plain_code"><CODE>std::streamsize</CODE></PRE></TD>
  169. <TD>
  170. Convertible to <A HREF="../guide/modes.html#mode_tags"><CODE>input</CODE></A> and to <A HREF="../guide/traits.html#category_tags"><CODE>multichar_tag</CODE></A>
  171. </TD>
  172. <TD>
  173. Reads up to <CODE>n</CODE> characters from the input sequence controlled by <CODE>f</CODE> into the buffer <CODE>s</CODE>, returning the number of characters read or <CODE>-1</CODE> to indicate end-of-sequence. A value less than <CODE>n</CODE> may be returned only at end-of-sequence or if input is temporarily unavilable because a call to <CODE>d</CODE> has produced fewer characters than requested. The input sequence controlled by <CODE>d</CODE> may be accessed using <A HREF="../functions/get.html"><CODE>io::get</CODE></A>, <A HREF="../functions/read.html"><CODE>io::read</CODE></A> and <A HREF="../functions/putback.html"><CODE>io::putback</CODE></A>.
  174. </TD>
  175. </TR>
  176. </TABLE>
  177. <H2>Exceptions</H2>
  178. <P>
  179. Errors which occur during the execution of <CODE>get</CODE> and <CODE>read</CODE> are indicated by throwing exceptions. Reaching the end of the sequence is not an error.
  180. </P>
  181. <P>
  182. After an exception is thrown, an InputFilter must be in a consistent state; further i/o operations may throw exceptions but must have well-defined behaviour. Furthermore, unless it is <A HREF="closable.html">Closable</A>, it must be ready to begin processing a new character sequence.
  183. </P>
  184. <H2>Models</H2>
  185. <UL>
  186. <LI>The <A HREF="../guide/text_processing.html">Text Processing Filters</A>.
  187. <LI>The compression and decompression filters.
  188. </UL>
  189. <H2>Acknowledgments</H2>
  190. <P>
  191. The concept InputFilter was inspired by the <I>extractors</I> of <A CLASS="footnote_ref" HREF="../bibliography.html#kanze">[Kanze]</A>.
  192. </P>
  193. <!-- Begin Footnotes -->
  194. <HR>
  195. <P>
  196. <A CLASS="footnote_ref" NAME="note_1" HREF="#note_1_ref"><SUP>[1]</SUP></A>Technically, <CODE>boost::iostreams::get</CODE> requires that a Source be <A HREF="../concepts/direct.html"><I>indirect</I></A>.
  197. </P>
  198. <!-- End Footnotes -->
  199. <!-- Begin Footer -->
  200. <HR>
  201. <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>
  202. <P CLASS="copyright">
  203. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <A HREF="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</A>)
  204. </P>
  205. <!-- End Footer -->
  206. </BODY>