1
// ----------------------------------------------------------------------------
2
// sample_userType.cc : example usage of format with a user-defined type
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
#if !(BOOST_WORKAROUND(__GNUC__, < 3) && defined(__STL_CONFIG_H) )
20
// not for broken gcc stdlib
21
#include <boost/io/ios_state.hpp>
24
// not as complete, but compatible with gcc-2.95 :
26
void copyfmt(ios& left, const ios& right) {
27
left.fill(right.fill());
28
left.flags(right.flags() );
29
left.exceptions(right.exceptions());
30
left.width(right.width());
31
left.precision(right.precision());
34
namespace boost { namespace io {
36
std::basic_ios<char> ios_;
39
ios_all_saver(std::ios& right) : ios_(0), target_r(right) {
43
copyfmt(target_r, ios_);
50
// define showpos and noshowpos :
54
ShowPos(bool v) : showpos_(v) {}
56
std::ostream& operator<<(std::ostream& os, const ShowPos& x) {
58
os.setf(ios_base:: showpos);
60
os.unsetf(ios_base:: showpos);
63
ShowPos noshowpos(false);
64
ShowPos showpos(true);
66
#endif // -end gcc-2.95 workarounds
70
//---------------------------------------------------------------------------//
71
// *** an exemple of UDT : a Rational class ****
74
Rational(int n, unsigned int d) : n_(n), d_(d) {}
75
Rational(int n, int d); // convert denominator to unsigned
76
friend std::ostream& operator<<(std::ostream&, const Rational&);
79
unsigned int d_; // denominator
82
Rational::Rational(int n, int d) : n_(n)
84
if(d < 0) { n_ = -n_; d=-d; } // make the denominator always non-negative.
85
d_ = static_cast<unsigned int>(d);
88
std::ostream& operator<<(std::ostream& os, const Rational& r) {
90
streamsize n, s1, s2, s3;
91
streamsize w = os.width(0); // width has to be zeroed before saving state.
92
// boost::io::ios_all_saver bia_saver (os);
94
boost::io::basic_oaltstringstream<char> oss;
98
oss << "/" << noshowpos; // a rational number needs only one sign !
105
os.write(oss.begin(), oss.size());
107
else if(os.flags() & std::ios_base::internal) {
108
std::streamsize n1 = w/2, n2 = w - n1, t;
109
t = (s3-s1) - n2; // is 2d part '/nnn' bigger than 1/2 w ?
111
n1 = w -(s3-s1); // put all paddings on first part.
112
n2 = 0; // minimal width (s3-s2)
115
n2 -= s2-s1; // adjust for '/', n2 is still w/2.
117
os << setw(n1) << r.n_ << "/" << noshowpos << setw(n2) << r.d_;
120
if(! (os.flags() & std::ios_base::left)) {
121
// -> right align. (right bit is set, or no bit is set)
122
os << string(n, ' ');
124
os.write(oss.begin(), s3);
125
if( os.flags() & std::ios_base::left ) {
126
os << string(n, ' ');
138
using boost::io::group;
139
using boost::io::str;
144
cout << "bonjour ! " << endl;
149
cout << showpos << r << ", " << 5 << endl;
150
// prints : "+16/9, +5"
152
cout << format("%02d : [%0+9d] \n") % 1 % r ;
154
cout << format("%02d : [%_+9d] \n") % 2 % Rational(9,160);
156
cout << format("%02d : [%_+9d] \n") % 3 % r;
158
cout << format("%02d : [%_9d] \n") % 4 % Rational(8,1234);
160
cout << format("%02d : [%_9d] \n") % 5 % Rational(1234,8);
162
cout << format("%02d : [%09d] \n") % 6 % Rational(8,1234);
164
cout << format("%02d : [%0+9d] \n") % 7 % Rational(1234,8);
166
cout << format("%02d : [%0+9d] \n") % 8 % Rational(7,12345);
183
cerr << "\n\nEverything went OK, exiting. \n";