2 // Boost general library 'format' ---------------------------
3 // See http://www.boost.org for updates, documentation, and revision history.
5 // (C) Samuel Krempp 2001
6 // krempp@crans.ens-cachan.fr
7 // Permission to copy, use, modify, sell and
8 // distribute this software is granted provided this copyright notice appears
9 // in all copies. This software is provided "as is" without express or implied
10 // warranty, and with no claim as to its suitability for any purpose.
12 // ideas taken from Rüdiger Loos's format class
13 // and Karl Nelson's ofstream
15 // ----------------------------------------------------------------------------
16 // internals.hpp : internal structs. included by format.hpp
17 // stream_format_state, and format_item
18 // ----------------------------------------------------------------------------
21 #ifndef BOOST_FORMAT_INTERNALS_HPP
22 #define BOOST_FORMAT_INTERNALS_HPP
27 #include <boost/assert.hpp>
28 #include <boost/format/outsstream.hpp>
29 #include <boost/limits.hpp>
36 //----- stream_format_state --------------------------------------------------//
38 // set of params that define the format state of a stream
39 template<class Ch, class Tr>
40 struct stream_format_state
42 typedef BOOST_IO_STD basic_ios<Ch, Tr> basic_ios;
44 stream_format_state(Ch fill) { reset(fill); }
45 stream_format_state(const basic_ios& os) { set_by_stream(os); }
47 void reset(Ch fill); //- sets to default state.
48 void set_by_stream(const basic_ios& os); //- sets to os's state.
49 void apply_on(basic_ios & os) const; //- applies format_state to the stream
51 void apply_manip(T manipulator) //- modifies state by applying manipulator
52 { apply_manip_body<Ch, Tr, T>( *this, manipulator) ; }
55 std::streamsize width_;
56 std::streamsize precision_;
58 std::ios_base::fmtflags flags_;
59 std::ios_base::iostate rdstate_;
60 std::ios_base::iostate exceptions_;
64 //----- format_item ---------------------------------------------------------//
66 // stores all parameters that can be specified in format strings
67 template<class Ch, class Tr>
70 enum pad_values { zeropad = 1, spacepad =2, centered=4, tabulation = 8 };
71 // 1. if zeropad is set, all other bits are not,
72 // 2. if tabulation is set, all others are not.
73 // centered and spacepad can be mixed freely.
74 enum arg_values { argN_no_posit = -1, // non-positional directive. will set argN later
75 argN_tabulation = -2, // tabulation directive. (no argument read)
76 argN_ignored = -3 // ignored directive. (no argument read)
78 typedef BOOST_IO_STD basic_ios<Ch, Tr> basic_ios;
79 typedef detail::stream_format_state<Ch, Tr> stream_format_state;
80 typedef std::basic_string<Ch, Tr> string_t;
82 format_item(Ch fill) :argN_(argN_no_posit), fmtstate_(fill),
83 truncate_(max_streamsize()), pad_scheme_(0) {}
85 void compute_states(); // sets states according to truncate and pad_scheme.
87 static std::streamsize max_streamsize() {
88 return std::numeric_limits<std::streamsize>::max();
92 int argN_; //- argument number (starts at 0, eg : %1 => argN=0)
93 // negative values for items that don't process an argument
94 string_t res_; //- result of the formatting of this item
95 string_t appendix_; //- piece of string between this item and the next
97 stream_format_state fmtstate_;// set by parsing, is only affected by modify_item
99 std::streamsize truncate_; //- is set for directives like %.5s that ask truncation
100 unsigned int pad_scheme_;//- several possible padding schemes can mix. see pad_values
105 //---- Definitions ------------------------------------------------------------
107 // --- stream_format_state:: -------------------------------------------------
108 template<class Ch, class Tr>
109 void stream_format_state<Ch,Tr>:: apply_on(basic_ios & os) const {
110 // set the state of this stream according to our params
114 os.precision(precision_);
119 os.exceptions(exceptions_);
122 template<class Ch, class Tr>
123 void stream_format_state<Ch,Tr>:: set_by_stream(const basic_ios& os) {
124 // set our params according to the state of this stream
127 precision_ = os.precision();
129 rdstate_ = os.rdstate();
130 exceptions_ = os.exceptions();
134 template<class Ch, class Tr, class T>
135 void apply_manip_body( stream_format_state<Ch, Tr>& self,
137 // modify our params according to the manipulator
138 basic_outsstream<Ch, Tr> ss;
141 self.set_by_stream( ss );
144 template<class Ch, class Tr> inline
145 void stream_format_state<Ch,Tr>:: reset(Ch fill) {
146 // set our params to standard's default state. cf § 27.4.4.1 of the C++ norm
147 width_=0; precision_=6;
148 fill_=fill; // default is widen(' '), but we cant compute it without the locale
149 flags_ = std::ios_base::dec | std::ios_base::skipws;
150 // the adjust_field part is left equal to 0, which means right.
151 exceptions_ = std::ios_base::goodbit;
152 rdstate_ = std::ios_base::goodbit;
156 // --- format_item:: --------------------------------------------------------
158 template<class Ch, class Tr>
159 void format_item<Ch, Tr>::
161 argN_=argN_no_posit; truncate_ = max_streamsize(); pad_scheme_ =0;
162 res_.resize(0); appendix_.resize(0);
163 fmtstate_.reset(fill);
166 template<class Ch, class Tr>
167 void format_item<Ch, Tr>:: compute_states() {
168 // reflect pad_scheme_ on fmt_state_
169 // because some pad_schemes has complex consequences on several state params.
170 if(pad_scheme_ & zeropad) {
171 // ignore zeropad in left alignment :
172 if(fmtstate_.flags_ & std::ios_base::left) {
173 BOOST_ASSERT(!(fmtstate_.flags_ &(std::ios_base::adjustfield ^std::ios_base::left)));
174 // only left bit might be set. (not right, nor internal)
175 pad_scheme_ = pad_scheme_ & (~zeropad);
178 pad_scheme_ &= ~spacepad; // printf ignores spacepad when zeropadding
180 fmtstate_.flags_ = (fmtstate_.flags_ & ~std::ios_base::adjustfield)
181 | std::ios_base::internal;
182 // removes all adjustfield bits, and adds internal.
185 if(pad_scheme_ & spacepad) {
186 if(fmtstate_.flags_ & std::ios_base::showpos)
187 pad_scheme_ &= ~spacepad;
192 } } } // namespaces boost :: io :: detail
195 #endif // BOOST_FORMAT_INTERNALS_HPP