~ubuntu-branches/ubuntu/wily/bombono-dvd/wily

« back to all changes in this revision

Viewing changes to libs/boost-lib/libs/format/example/sample_advanced.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessio Treglia
  • Date: 2010-11-04 11:46:25 UTC
  • mto: This revision was merged to the branch mainline in revision 8.
  • Revision ID: james.westby@ubuntu.com-20101104114625-8xfdhvhpsm51i0nu
Tags: upstream-0.8.0
ImportĀ upstreamĀ versionĀ 0.8.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// ----------------------------------------------------------------------------
2
 
// sample_advanced.cc :  examples of adanced usage of format 
3
 
// ----------------------------------------------------------------------------
4
 
 
5
 
//  Copyright Samuel Krempp 2003. Use, modification, and distribution are
6
 
//  subject to the Boost Software License, Version 1.0. (See accompanying
7
 
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8
 
 
9
 
//  See http://www.boost.org/libs/format for library home page
10
 
 
11
 
// ----------------------------------------------------------------------------
12
 
 
13
 
#include <iostream>
14
 
#include <iomanip>
15
 
 
16
 
#include "boost/format.hpp"
17
 
 
18
 
 
19
 
namespace MyNS_ForOutput {
20
 
  using std::cout; using std::cerr;
21
 
  using std::string;
22
 
  using std::endl; using std::flush;
23
 
 
24
 
  using boost::format;
25
 
  using boost::io::group;
26
 
}
27
 
 
28
 
namespace MyNS_Manips {
29
 
  using std::setfill;
30
 
  using std::setw;
31
 
  using std::hex ;
32
 
  using std::dec ;
33
 
  using std::showbase ;
34
 
  using std::left ;
35
 
  using std::right ;
36
 
  using std::internal ;
37
 
}
38
 
 
39
 
int main(){
40
 
    using namespace MyNS_ForOutput;
41
 
    using namespace MyNS_Manips;
42
 
 
43
 
    std::string s;
44
 
 
45
 
    //------------------------------------------------------------------------
46
 
    // storing the parsed format-string in a 'formatter' : 
47
 
    // format objects are regular objects that can be copied, assigned, 
48
 
    // fed arguments, dumped to a stream, re-fed arguments, etc... 
49
 
    // So users can use them the way they like.
50
 
 
51
 
    format fmter("%1% %2% %3% %1% \n");
52
 
    fmter % 10 % 20 % 30; 
53
 
    cout  << fmter;
54
 
    //          prints  "10 20 30 10 \n"
55
 
    
56
 
    // note that once the fmter got all its arguments, 
57
 
    // the formatted string stays available  (until next call to '%')
58
 
    //    The result is  available via function str() or stream's << :
59
 
    cout << fmter; 
60
 
    //          prints the same string again.
61
 
 
62
 
 
63
 
    // once you call operator% again, arguments are cleared inside the object
64
 
    // and it is an error to ask for the conversion string before feeding all arguments :
65
 
    fmter % 1001;
66
 
    try  { cout << fmter;   }
67
 
    catch (boost::io::too_few_args& exc) { 
68
 
      cout <<  exc.what() << "***Dont worry, that was planned\n";
69
 
    }
70
 
 
71
 
    // we just need to feed the last two arguments, and it will be ready for output again :
72
 
    cout << fmter % 1002 % 1003;
73
 
    //          prints  "1001 1002 1003 1001 \n"
74
 
 
75
 
    cout  << fmter % 10 % 1 % 2;
76
 
    //          prints  "10 1 2 10 \n"
77
 
 
78
 
 
79
 
 
80
 
    //---------------------------------------------------------------
81
 
    // using format objects 
82
 
 
83
 
    // modify the formatting options for a given directive :
84
 
    fmter = format("%1% %2% %3% %2% %1% \n");
85
 
    fmter.modify_item(4, group(setfill('_'), hex, showbase, setw(5)) );
86
 
    cout << fmter % 1 % 2 % 3;
87
 
    //          prints  "1 2 3 __0x2 1 \n"
88
 
    
89
 
    // bind one of the argumets :
90
 
    fmter.bind_arg(1, 18);
91
 
    cout << fmter % group(hex, showbase, 20) % 30;  // %2 is 20, and 20 == 0x14
92
 
    //          prints  "18 0x14 30  _0x14 18 \n"
93
 
    
94
 
    
95
 
    fmter.modify_item(4, setw(0)); // cancels previous width-5
96
 
    fmter.bind_arg(1, 77); // replace 18 with 77 for first argument.
97
 
    cout << fmter % 10 % 20;
98
 
    //          prints  "77 10 20 0xa 77 \n"
99
 
 
100
 
    try  
101
 
    { 
102
 
      cout << fmter % 6 % 7 % 8;   // Aye ! too many args, because arg1 is bound already
103
 
    }
104
 
    catch (boost::io::too_many_args& exc) 
105
 
    { 
106
 
      cout <<  exc.what() << "***Dont worry, that was planned\n";
107
 
    }
108
 
 
109
 
    // clear regular arguments, but not bound arguments :
110
 
    fmter.clear();
111
 
    cout << fmter % 2 % 3;
112
 
    //          prints "77 2 3 0x2 77 \n"
113
 
 
114
 
    // clear_binds() clears both regular AND bound arguments :
115
 
    fmter.clear_binds(); 
116
 
    cout << fmter % 1 % 2 % 3;
117
 
    //          prints  "1 2 3 0x2 1 \n"
118
 
    
119
 
 
120
 
    // setting desired exceptions :
121
 
    fmter.exceptions( boost::io::all_error_bits ^( boost::io::too_many_args_bit ) );
122
 
    cout << fmter % 1 % 2 % 3 % 4 % 5 % 6 ;
123
 
 
124
 
 
125
 
   // -----------------------------------------------------------
126
 
    // misc:
127
 
 
128
 
    // unsupported printf directives %n and asterisk-fields are purely ignored.
129
 
    // do *NOT* provide an argument for them, it is an error.
130
 
    cout << format("|%5d| %n") % 7 << endl;
131
 
    //          prints  "|    7| "
132
 
    cout << format("|%*.*d|")  % 7 << endl;
133
 
    //          prints "|7|"
134
 
 
135
 
 
136
 
    // truncations of strings :
137
 
    cout << format("%|.2s| %|8c|.\n") % "root" % "user";
138
 
    //          prints  "ro        u.\n"
139
 
 
140
 
 
141
 
    // manipulators conflicting with format-string : manipulators win.
142
 
    cout << format("%2s")  % group(setfill('0'), setw(6), 1) << endl;
143
 
    //          prints  "000001"
144
 
    cout << format("%2$5s %1% %2$3s\n")  % 1    % group(setfill('X'), setw(4), 2) ;
145
 
    //          prints  "XXX2 1 XXX2\n"  
146
 
    //          width is 4, as set by manip, not the format-string.
147
 
    
148
 
    // nesting :
149
 
    cout << format("%2$014x [%1%] %2$05s\n") % (format("%05s / %s") % -18 % 7)
150
 
                                             % group(showbase, -100);
151
 
    //          prints   "0x0000ffffff9c [-0018 / 7] -0100\n"
152
 
 
153
 
 
154
 
    cout << "\n\nEverything went OK, exiting. \n";
155
 
    return 0;
156
 
}