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 <vector>
23
24 namespace client
25 {
26 ///////////////////////////////////////////////////////////////////////////
27 // Our number list parser, please see the example qi/numlist1.cpp for
28 // more information
29 ///////////////////////////////////////////////////////////////////////////
30 template <typename Iterator>
parse_numbers(Iterator first,Iterator last,std::vector<double> & v)31 bool parse_numbers(Iterator first, Iterator last, std::vector<double>& v)
32 {
33 using boost::spirit::qi::double_;
34 using boost::spirit::qi::phrase_parse;
35 using boost::spirit::ascii::space;
36
37 bool r = phrase_parse(first, last, double_ % ',', space, v);
38 if (first != last)
39 return false;
40 return r;
41 }
42
43 ///////////////////////////////////////////////////////////////////////////
44 // Our number list generator
45 ///////////////////////////////////////////////////////////////////////////
46 //[tutorial_karma_numlist2
47 template <typename OutputIterator, typename Container>
generate_numbers(OutputIterator & sink,Container const & v)48 bool generate_numbers(OutputIterator& sink, Container const& v)
49 {
50 using boost::spirit::karma::double_;
51 using boost::spirit::karma::generate_delimited;
52 using boost::spirit::ascii::space;
53
54 bool r = generate_delimited(
55 sink, // destination: output iterator
56 double_ % ',', // the generator
57 space, // the delimiter-generator
58 v // the data to output
59 );
60 return r;
61 }
62 //]
63 }
64
65 ////////////////////////////////////////////////////////////////////////////
66 // Main program
67 ////////////////////////////////////////////////////////////////////////////
68 int
main()69 main()
70 {
71 std::cout << "/////////////////////////////////////////////////////////\n\n";
72 std::cout << "\t\tA comma separated list generator for Spirit...\n\n";
73 std::cout << "/////////////////////////////////////////////////////////\n\n";
74
75 std::cout << "Give me a comma separated list of numbers.\n";
76 std::cout << "Type [q or Q] to quit\n\n";
77
78 std::string str;
79 while (getline(std::cin, str))
80 {
81 if (str.empty() || str[0] == 'q' || str[0] == 'Q')
82 break;
83
84 std::vector<double> v; // here we put the data to generate
85 if (client::parse_numbers(str.begin(), str.end(), v))
86 {
87 // ok, we got some numbers, now print them back out
88 std::cout << "-------------------------\n";
89
90 std::string generated;
91 std::back_insert_iterator<std::string> sink(generated);
92 if (!client::generate_numbers(sink, v))
93 {
94 std::cout << "-------------------------\n";
95 std::cout << "Generating failed\n";
96 std::cout << "-------------------------\n";
97 }
98 else
99 {
100 std::cout << "-------------------------\n";
101 std::cout << "Generated: " << generated << "\n";
102 std::cout << "-------------------------\n";
103 }
104 }
105 else
106 {
107 std::cout << "-------------------------\n";
108 std::cout << "Parsing failed\n";
109 std::cout << "-------------------------\n";
110 }
111 }
112
113 std::cout << "Bye... :-) \n\n";
114 return 0;
115 }
116
117
118