1
// ----------------------------------------------------------------------------
2
// sample_advanced.cc : examples of adanced usage of format
3
// ----------------------------------------------------------------------------
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)
9
// See http://www.boost.org/libs/format for library home page
11
// ----------------------------------------------------------------------------
16
#include "boost/format.hpp"
19
namespace MyNS_ForOutput {
20
using std::cout; using std::cerr;
22
using std::endl; using std::flush;
25
using boost::io::group;
28
namespace MyNS_Manips {
40
using namespace MyNS_ForOutput;
41
using namespace MyNS_Manips;
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.
51
format fmter("%1% %2% %3% %1% \n");
54
// prints "10 20 30 10 \n"
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 << :
60
// prints the same string again.
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 :
66
try { cout << fmter; }
67
catch (boost::io::too_few_args& exc) {
68
cout << exc.what() << "***Dont worry, that was planned\n";
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"
75
cout << fmter % 10 % 1 % 2;
76
// prints "10 1 2 10 \n"
80
//---------------------------------------------------------------
81
// using format objects
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"
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"
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"
102
cout << fmter % 6 % 7 % 8; // Aye ! too many args, because arg1 is bound already
104
catch (boost::io::too_many_args& exc)
106
cout << exc.what() << "***Dont worry, that was planned\n";
109
// clear regular arguments, but not bound arguments :
111
cout << fmter % 2 % 3;
112
// prints "77 2 3 0x2 77 \n"
114
// clear_binds() clears both regular AND bound arguments :
116
cout << fmter % 1 % 2 % 3;
117
// prints "1 2 3 0x2 1 \n"
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 ;
125
// -----------------------------------------------------------
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;
132
cout << format("|%*.*d|") % 7 << endl;
136
// truncations of strings :
137
cout << format("%|.2s| %|8c|.\n") % "root" % "user";
141
// manipulators conflicting with format-string : manipulators win.
142
cout << format("%2s") % group(setfill('0'), setw(6), 1) << endl;
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.
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"
154
cout << "\n\nEverything went OK, exiting. \n";