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