1// ----------------------------------------------------------------------------
2// sample_formats.cpp : example of basic 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#include <iostream>
13#include <iomanip>
14#include <cassert>
15
16#include "boost/format.hpp"
17
18// 2 custom namespaces, to bring in a few useful names :
19
20namespace MyNS_ForOutput {
21 using std::cout; using std::cerr;
22 using std::string;
23 using std::endl; using std::flush;
24
25 using boost::format;
26 using boost::io::group;
27}
28
29namespace MyNS_Manips {
30 using std::setfill;
31 using std::setw;
32 using std::hex ;
33 using std::dec ;
34// gcc-2.95 doesnt define the next ones
35// using std::showbase ;
36// using std::left ;
37// using std::right ;
38// using std::internal ;
39}
40
41int main(){
42 using namespace MyNS_ForOutput;
43 using namespace MyNS_Manips;
44
45 std::cout << format("%|1$1| %|2$3|") % "Hello" % 3 << std::endl;
46
47 // Reordering :
48 cout << format("%1% %2% %3% %2% %1% \n") % "o" % "oo" % "O"; // 'simple' style.
49 // prints "o oo O oo o \n"
50 cout << format("(x,y) = (%1$+5d,%2$+5d) \n") % -23 % 35; // Posix-Printf style
51
52
53 // No reordering :
54 cout << format("writing %s, x=%s : %d-th step \n") % "toto" % 40.23 % 50;
55 // prints "writing toto, x=40.23 : 50-th step \n"
56
57 cout << format("(x,y) = (%+5d,%+5d) \n") % -23 % 35;
58 cout << format("(x,y) = (%|+5|,%|+5|) \n") % -23 % 35;
59 cout << format("(x,y) = (%|1$+5|,%|2$+5|) \n") % -23 % 35;
60 // all those are the same, it prints "(x,y) = ( -23, +35) \n"
61
62
63
64 // Using manipulators, via 'group' :
65 cout << format("%2% %1% %2%\n") % 1 % group(a1: setfill('X'), a2: hex, a3: setw(4), var: 16+3) ;
66 // prints "XX13 1 XX13\n"
67
68
69 // printf directives's type-flag can be used to pass formatting options :
70 cout << format("_%1$4d_ is : _%1$#4x_, _%1$#4o_, and _%1$s_ by default\n") % 18;
71 // prints "_ 18_ is : _0x12_, _ 022_, and _18_ by default\n"
72
73 // Taking the string value :
74 std::string s;
75 s= str( f: format(" %d %d ") % 11 % 22 );
76 assert( s == " 11 22 ");
77
78
79 // -----------------------------------------------
80 // %% prints '%'
81
82 cout << format("%%##%#x ") % 20 << endl;
83 // prints "%##0x14 "
84
85
86 // -----------------------------------------------
87 // Enforcing the right number of arguments
88
89 // Too much arguments will throw an exception when feeding the unwanted argument :
90 try {
91 format(" %1% %1% ") % 101 % 102;
92 // the format-string refers to ONE argument, twice. not 2 arguments.
93 // thus giving 2 arguments is an error
94 }
95 catch (boost::io::too_many_args& exc) {
96 cerr << exc.what() << "\n\t\t***Dont worry, that was planned\n";
97 }
98
99
100 // Too few arguments when requesting the result will also throw an exception :
101 try {
102 cerr << format(" %|3$| ") % 101;
103 // even if %1$ and %2$ are not used, you should have given 3 arguments
104 }
105 catch (boost::io::too_few_args& exc) {
106 cerr << exc.what() << "\n\t\t***Dont worry, that was planned\n";
107 }
108
109
110 cerr << "\n\nEverything went OK, exiting. \n";
111 return 0;
112}
113

source code of boost/libs/format/example/sample_formats.cpp