num_list1.cpp 4.2 KB

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