• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //  Copyright (c) 2001-2010 Hartmut Kaiser
2 //
3 //  Distributed under the Boost Software License, Version 1.0. (See accompanying
4 //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 //  The main purpose of this example is to show how a single container type can
7 //  be formatted using different output grammars.
8 
9 #include <boost/config/warning_disable.hpp>
10 #include <boost/spirit/include/karma.hpp>
11 #include <boost/spirit/include/karma_stream.hpp>
12 
13 #include <iostream>
14 #include <vector>
15 #include <algorithm>
16 #include <cstdlib>
17 
18 using namespace boost::spirit;
19 using namespace boost::spirit::ascii;
20 
21 ///////////////////////////////////////////////////////////////////////////////
main()22 int main()
23 {
24     ///////////////////////////////////////////////////////////////////////////
25     // vector
26     std::vector<int> v (8);
27     std::generate(v.begin(), v.end(), std::rand); // randomly fill the vector
28 
29     std::cout << "Output 8 integers from a std::vector<int>..." << std::endl;
30 
31     // output the container as a sequence without any separation
32     std::cout << "...without any separation" << std::endl;
33     std::cout <<
34         karma::format(
35             *int_,                                // format description
36             v                                     // data
37         ) << std::endl << std::endl;
38 
39     // output the container as a space separated sequence
40     std::cout << "...as space delimited list" << std::endl;
41     std::cout <<
42         karma::format_delimited(
43             *int_,                                // format description
44             space,                                // delimiter
45             v                                     // data
46         ) << std::endl << std::endl;
47 
48     std::cout <<
49         karma::format_delimited(
50             '[' << *int_ << ']',                  // format description
51             space,                                // delimiter
52             v                                     // data
53         ) << std::endl << std::endl;
54 
55     // output the container as a comma separated list
56     std::cout << "...as comma separated list" << std::endl;
57     std::cout <<
58         karma::format(
59             int_ % ", ",                          // format description
60             v                                     // data
61         ) << std::endl << std::endl;
62 
63     std::cout <<
64         karma::format(
65             '[' << (int_ % ", ") << ']',          // format description
66             v                                     // data
67         ) << std::endl << std::endl;
68 
69     // output the container as a comma separated list of double's
70     std::cout << "...as comma separated list of doubles" << std::endl;
71     std::cout <<
72         karma::format(
73             double_ % ", ",                       // format description
74             v                                     // data
75         ) << std::endl << std::endl;
76 
77     // output the container as a comma separated list of items enclosed in '()'
78     std::cout << "..as list of ints enclosed in '()'" << std::endl;
79     std::cout <<
80         karma::format(
81             ('(' << int_ << ')') % ", ",          // format description
82             v                                     // data
83         ) << std::endl << std::endl;
84 
85     std::cout <<
86         karma::format(
87             '[' << (
88                 ('(' << int_ << ')') % ", "
89              )  << ']',                           // format description
90             v                                     // data
91         ) << std::endl << std::endl;
92 
93     // output the container as a HTML list
94     std::cout << "...as HTML bullet list" << std::endl;
95     std::cout <<
96         karma::format_delimited(
97             "<ol>" <<
98                 // no delimiting within verbatim
99                 *verbatim["  <li>" << int_ << "</li>"]
100             << "</ol>",                           // format description
101             '\n',                                 // delimiter
102             v                                     // data
103         ) << std::endl;
104 
105     // output the container as right aligned column
106     std::cout << "...right aligned in a column" << std::endl;
107     std::cout <<
108         karma::format_delimited(
109            *verbatim[
110                 "|" << right_align[int_] << "|"
111             ],                                    // format description
112             '\n',                                 // delimiter
113             v                                     // data
114         ) << std::endl;
115 
116     std::cout << std::endl;
117     return 0;
118 }
119 
120