num_list2.cpp 4.2 KB

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