file_iterator.html 7.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <html>
  2. <head>
  3. <title>File Iterator</title>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  5. <link rel="stylesheet" href="theme/style.css" type="text/css">
  6. </head>
  7. <body>
  8. <table width="100%" border="0" background="theme/bkd2.gif" cellspacing="2">
  9. <tr>
  10. <td width="10">
  11. </td>
  12. <td width="85%">
  13. <font size="6" face="Verdana, Arial, Helvetica, sans-serif"><b>File Iterator</b></font>
  14. </td>
  15. <td width="112"><a href="http://spirit.sf.net"><img src="theme/spirit.gif" width="112" height="48" align="right" border="0"></a></td>
  16. </tr>
  17. </table>
  18. <br>
  19. <table border="0">
  20. <tr>
  21. <td width="10"></td>
  22. <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
  23. <td width="30"><a href="multi_pass.html"><img src="theme/l_arr.gif" border="0"></a></td>
  24. <td width="30"><a href="position_iterator.html"><img src="theme/r_arr.gif" border="0"></a></td>
  25. </tr>
  26. </table>
  27. <p>Since Spirit is a back-tracking parser, it requires at least a forward iterator.
  28. In particular, an input iterator is not sufficient. Many times it is convenient
  29. to read the input to a parser from a file, but the STL file iterators are input
  30. iterators. To get around this limitation, Spirit has a utility class <tt>file_iterator</tt>,
  31. which is a read-only random-access iterator for files.</p>
  32. <p>To use the Spirit file iterator, simply create a file iterator with the path
  33. to the file you wish to parse, and then create an EOF iterator for the file:</p>
  34. <pre><span class=identifier> </span><span class=preprocessor>#include </span><span class=special>&lt;</span><span class=identifier>boost</span><span class=special>/</span><span class=identifier>spirit</span><span class=special>/</span><span class=identifier>iterator</span><span class=special>/</span><span class=identifier>file_iterator</span><span class=special>.</span><span class=identifier>hpp</span><span class=special>&gt; </span><span class=comment>// the header file</span></pre>
  35. <pre> <span class=identifier>file_iterator</span><span class=special>&lt;&gt; </span><span class=identifier>first</span><span class=special>(</span><span class=string>&quot;input.dat&quot;</span><span class=special>);
  36. </span><span class=keyword>if </span><span class=special>(!</span><span class=identifier>first</span><span class=special>)
  37. {
  38. </span><span class=identifier>std</span><span class=special>::</span><span class=identifier>cout </span><span class=special>&lt;&lt; </span><span class=string>&quot;Unable to open file!\n&quot;</span><span class=special>;
  39. </span><span class=comment>// Clean up, throw an exception, whatever
  40. </span><span class=keyword>return </span><span class=special>-</span><span class=number>1</span><span class=special>;
  41. }
  42. </span><span class=identifier>file_iterator</span><span class=special>&lt;&gt; </span><span class=identifier>last </span><span class=special>= </span><span class=identifier>first</span><span class=special>.</span><span class=identifier>make_end</span><span class=special>();</span></pre>
  43. <p>You now have a pair of iterators to use with Spirit . If your parser is fully
  44. parametrized (no hard-coded <tt>&lt;char const *&gt;</tt>), it is a simple matter
  45. of redefining the iterator type to <tt>file_iterator</tt>:<br>
  46. </p>
  47. <pre> <span class=keyword>typedef char </span><span class="identifier">char_t</span><span class=special>;
  48. </span><span class=keyword>typedef </span><span class=identifier>file_iterator </span><span class=special>&lt;</span><span class=keyword>char</span><span class=identifier>_t</span><span class=special>&gt; </span><span class=identifier>iterator_t</span><span class=special>;
  49. </span><span class=keyword>typedef </span><span class=identifier>scanner</span><span class=special>&lt;</span><span class=identifier>iterator_t</span><span class=special>&gt; </span><span class=identifier>scanner_t</span><span class=special>;
  50. </span><span class=keyword>typedef </span><span class=identifier>rule </span><span class=special>&lt;</span><span class=identifier>scanner_t</span><span class=special>&gt; </span><span class=identifier>rule_t</span><span class=special>;
  51. </span><span class=identifier>rule_t my_rule</span><span class=special>;
  52. </span><span class=comment>// Define your rule
  53. </span><span class=identifier>parse_info</span><span class=special>&lt;</span><span class=identifier>iterator_t</span><span class=special>&gt; </span><span class=identifier>info </span><span class=special>= </span><span class=identifier>parse</span><span class=special>(</span><span class=identifier>first</span><span class=special>, </span><span class=identifier>last</span><span class=special>, </span><span class=identifier>my_rule</span><span class=special>);</span></pre>
  54. <p>Of course, you don't have to deal with the <a href="faq.html#scanner_business">scanner-business</a>
  55. at all if you use grammars rather than rules as arguments to the parse functions.
  56. You simply pass the iterator pairs and the grammar as is:<span class=special><br>
  57. </span></p>
  58. <pre> <span class=identifier>my_grammar </span><span class=identifier>g</span><span class=special>;
  59. </span><span class=identifier>parse_info</span><span class=special>&lt;</span><span class=identifier>iterator_t</span><span class=special>&gt; </span><span class=identifier>info </span><span class=special>= </span><span class=identifier>parse</span><span class=special>(</span><span class=identifier>first</span><span class=special>, </span><span class=identifier>last</span><span class=special>, </span><span class=identifier>g</span><span class=special>);</span></pre>
  60. <table width="80%" border="0" align="center">
  61. <tr>
  62. <td class="note_box"><img src="theme/bulb.gif" width="13" height="18"><b>
  63. Generic iterator</b><br>
  64. <br>
  65. The Spirit file iterator can be parameterized with any type that is default
  66. constructible and assignable. It transparently supports large files (greater
  67. than 2GB) on systems that provide an appropriate interface. The file iterator
  68. can be useful outside of Spirit as well. For instance, the Boost.Tokenizer
  69. package requires a bidirectional iterator, which is provided by file_iterator.</td>
  70. </tr>
  71. </table>
  72. <p><img src="theme/lens.gif" width="15" height="16"> See <a href="../example/fundamental/file_parser.cpp">file_parser.cpp</a> for a compilable example. This is part of the Spirit distribution.</p>
  73. <table border="0">
  74. <tr>
  75. <td width="10"></td>
  76. <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
  77. <td width="30"><a href="multi_pass.html"><img src="theme/l_arr.gif" border="0"></a></td>
  78. <td width="30"><a href="position_iterator.html"><img src="theme/r_arr.gif" border="0"></a></td>
  79. </tr>
  80. </table>
  81. <br>
  82. <hr size="1">
  83. <p class="copyright">Copyright &copy; 2002 Jeff Westfahl</p>
  84. <p class="copyright"><font size="2"> Use, modification and distribution is subject to the Boost Software
  85. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  86. http://www.boost.org/LICENSE_1_0.txt)
  87. </font> </p>
  88. <p class="copyright">&nbsp;</p>
  89. </body>
  90. </html>