]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/format/example/sample_formats.cpp
1 // ----------------------------------------------------------------------------
2 // sample_formats.cpp : example of basic 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
10 // ----------------------------------------------------------------------------
16 #include "boost/format.hpp"
18 // 2 custom namespaces, to bring in a few useful names :
20 namespace MyNS_ForOutput
{
21 using std::cout
; using std::cerr
;
23 using std::endl
; using std::flush
;
26 using boost::io::group
;
29 namespace MyNS_Manips
{
34 // gcc-2.95 doesnt define the next ones
35 // using std::showbase ;
38 // using std::internal ;
42 using namespace MyNS_ForOutput
;
43 using namespace MyNS_Manips
;
45 std::cout
<< format("%|1$1| %|2$3|") % "Hello" % 3 << std::endl
;
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
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"
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"
64 // Using manipulators, via 'group' :
65 cout
<< format("%2% %1% %2%\n") % 1 % group(setfill('X'), hex
, setw(4), 16+3) ;
66 // prints "XX13 1 XX13\n"
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"
73 // Taking the string value :
75 s
= str( format(" %d %d ") % 11 % 22 );
76 assert( s
== " 11 22 ");
79 // -----------------------------------------------
82 cout
<< format("%%##%#x ") % 20 << endl
;
86 // -----------------------------------------------
87 // Enforcing the right number of arguments
89 // Too much arguments will throw an exception when feeding the unwanted argument :
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
95 catch (boost::io::too_many_args
& exc
) {
96 cerr
<< exc
.what() << "\n\t\t***Dont worry, that was planned\n";
100 // Too few arguments when requesting the result will also throw an exception :
102 cerr
<< format(" %|3$| ") % 101;
103 // even if %1$ and %2$ are not used, you should have given 3 arguments
105 catch (boost::io::too_few_args
& exc
) {
106 cerr
<< exc
.what() << "\n\t\t***Dont worry, that was planned\n";
110 cerr
<< "\n\nEverything went OK, exiting. \n";