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