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