• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2002-2010 Hartmut Kaiser
3     Copyright (c) 2002-2010 Joel de Guzman
4 
5     Distributed under the Boost Software License, Version 1.0. (See accompanying
6     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 =============================================================================*/
8 ///////////////////////////////////////////////////////////////////////////////
9 //
10 //  This sample demonstrates a generator for a comma separated list of numbers.
11 //  No actions. It is based on the example qi/num_lists.cpp for reading in
12 //  some numbers to generate.
13 //
14 ///////////////////////////////////////////////////////////////////////////////
15 
16 #include <boost/config/warning_disable.hpp>
17 #include <boost/spirit/include/qi.hpp>
18 #include <boost/spirit/include/karma.hpp>
19 
20 #include <iostream>
21 #include <string>
22 #include <list>
23 
24 namespace client
25 {
26     namespace qi = boost::spirit::qi;
27     namespace karma = boost::spirit::karma;
28     namespace ascii = boost::spirit::ascii;
29 
30     ///////////////////////////////////////////////////////////////////////////
31     //  Our number list parser, please see the example qi/numlist1.cpp for
32     //  more information
33     ///////////////////////////////////////////////////////////////////////////
34     template <typename Iterator>
parse_numbers(Iterator first,Iterator last,std::list<double> & v)35     bool parse_numbers(Iterator first, Iterator last, std::list<double>& v)
36     {
37         using qi::double_;
38         using qi::phrase_parse;
39         using ascii::space;
40 
41         bool r = phrase_parse(first, last, double_ >> *(',' >> double_), space, v);
42         if (first != last)
43             return false;
44         return r;
45     }
46 
47     ///////////////////////////////////////////////////////////////////////////
48     //  Our number list generator
49     ///////////////////////////////////////////////////////////////////////////
50     //[tutorial_karma_numlist1
51     template <typename OutputIterator>
generate_numbers(OutputIterator & sink,std::list<double> const & v)52     bool generate_numbers(OutputIterator& sink, std::list<double> const& v)
53     {
54         using karma::double_;
55         using karma::generate_delimited;
56         using ascii::space;
57 
58         bool r = generate_delimited(
59             sink,                           // destination: output iterator
60             double_ << *(',' << double_),   // the generator
61             space,                          // the delimiter-generator
62             v                               // the data to output
63         );
64         return r;
65     }
66     //]
67 }
68 
69 ////////////////////////////////////////////////////////////////////////////
70 //  Main program
71 ////////////////////////////////////////////////////////////////////////////
72 int
main()73 main()
74 {
75     std::cout << "/////////////////////////////////////////////////////////\n\n";
76     std::cout << "\t\tA comma separated list generator for Spirit...\n\n";
77     std::cout << "/////////////////////////////////////////////////////////\n\n";
78 
79     std::cout << "Give me a comma separated list of numbers.\n";
80     std::cout << "Type [q or Q] to quit\n\n";
81 
82     std::string str;
83     while (getline(std::cin, str))
84     {
85         if (str.empty() || str[0] == 'q' || str[0] == 'Q')
86             break;
87 
88         std::list<double> v;      // here we put the data to generate
89         if (client::parse_numbers(str.begin(), str.end(), v))
90         {
91             // ok, we got some numbers, now print them back out
92             std::cout << "-------------------------\n";
93 
94             std::string generated;
95             std::back_insert_iterator<std::string> sink(generated);
96             if (!client::generate_numbers(sink, v))
97             {
98                 std::cout << "-------------------------\n";
99                 std::cout << "Generating failed\n";
100                 std::cout << "-------------------------\n";
101             }
102             else
103             {
104                 std::cout << "-------------------------\n";
105                 std::cout << "Generated: " << generated << "\n";
106                 std::cout << "-------------------------\n";
107             }
108         }
109         else
110         {
111             std::cout << "-------------------------\n";
112             std::cout << "Parsing failed\n";
113             std::cout << "-------------------------\n";
114         }
115     }
116 
117     std::cout << "Bye... :-) \n\n";
118     return 0;
119 }
120 
121 
122