• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //  Copyright (c) 2001-2010 Hartmut Kaiser
2 //
3 //  Distributed under the Boost Software License, Version 1.0. (See accompanying
4 //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 #include <iostream>
7 #include <fstream>
8 #include <vector>
9 
10 #include <boost/spirit/include/qi.hpp>
11 #include <boost/spirit/include/support_multi_pass.hpp>
12 
13 ///////////////////////////////////////////////////////////////////////////////
14 
15 ///////////////////////////////////////////////////////////////////////////////
16 //[tutorial_multi_pass
main()17 int main()
18 {
19     namespace spirit = boost::spirit;
20     using spirit::ascii::space;
21     using spirit::ascii::char_;
22     using spirit::qi::double_;
23     using spirit::qi::eol;
24 
25     std::ifstream in("multi_pass.txt");    // we get our input from this file
26     if (!in.is_open()) {
27         std::cout << "Could not open input file: 'multi_pass.txt'" << std::endl;
28         return -1;
29     }
30 
31     typedef std::istreambuf_iterator<char> base_iterator_type;
32     spirit::multi_pass<base_iterator_type> first =
33         spirit::make_default_multi_pass(base_iterator_type(in));
34 
35     std::vector<double> v;
36     bool result = spirit::qi::phrase_parse(first
37       , spirit::make_default_multi_pass(base_iterator_type())
38       , double_ >> *(',' >> double_)              // recognize list of doubles
39       , space | '#' >> *(char_ - eol) >> eol      // comment skipper
40       , v);                                       // data read from file
41 
42     if (!result) {
43         std::cout << "Failed parsing input file!" << std::endl;
44         return -2;
45     }
46 
47     std::cout << "Successfully parsed input file!" << std::endl;
48     return 0;
49 }
50 //]
51