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 // feed_args.hpp : functions for processing each argument
17 // (feed, feed_manip, and distribute)
18 // ----------------------------------------------------------------------------
21 #ifndef BOOST_FORMAT_FEED_ARGS_HPP
22 #define BOOST_FORMAT_FEED_ARGS_HPP
24 #include "boost/format/format_class.hpp"
25 #include "boost/format/group.hpp"
27 #include "boost/format/msvc_disambiguater.hpp"
28 #include "boost/throw_exception.hpp"
35 template<class Tr, class Ch> inline
36 void empty_buf(BOOST_IO_STD basic_ostringstream<Ch,Tr> & os) {
37 static const std::basic_string<Ch, Tr> emptyStr;
41 template<class Ch, class Tr>
42 void do_pad( std::basic_string<Ch,Tr> & s,
45 std::ios_base::fmtflags f,
47 // applies centered / left / right padding to the string s.
48 // Effects : string s is padded.
50 std::streamsize n=w-s.size();
56 s.reserve(w); // allocate once for the 2 inserts
57 const std::streamsize n1 = n /2, n0 = n - n1;
58 s.insert(s.begin(), n0, c);
63 if(f & std::ios_base::left) {
67 s.insert(s.begin(), n, c);
73 #if BOOST_WORKAROUND( BOOST_MSVC, <= 1300)
74 // MSVC needs to be tricked to disambiguate this simple overload..
75 // the trick is in "boost/format/msvc_disambiguater.hpp"
77 template< class Ch, class Tr, class T> inline
78 void put_head( BOOST_IO_STD basic_ostream<Ch, Tr>& os, const T& x ) {
79 disambiguater<Ch, Tr, T>::put_head(os, x, 1L);
81 template< class Ch, class Tr, class T> inline
82 void put_last( BOOST_IO_STD basic_ostream<Ch, Tr>& os, const T& x ) {
83 disambiguater<Ch, Tr, T>::put_last(os, x, 1L);
88 template< class Ch, class Tr, class T> inline
89 void put_head(BOOST_IO_STD basic_ostream<Ch, Tr>& , const T& ) {
92 template< class Ch, class Tr, class T> inline
93 void put_head( BOOST_IO_STD basic_ostream<Ch, Tr>& os, const group1<T>& x ) {
94 os << group_head(x.a1_); // send the first N-1 items, not the last
97 template< class Ch, class Tr, class T> inline
98 void put_last( BOOST_IO_STD basic_ostream<Ch, Tr>& os, const T& x ) {
102 template< class Ch, class Tr, class T> inline
103 void put_last( BOOST_IO_STD basic_ostream<Ch, Tr>& os, const group1<T>& x ) {
104 os << group_last(x.a1_); // this selects the last element
107 #ifndef BOOST_NO_OVERLOAD_FOR_NON_CONST
108 template< class Ch, class Tr, class T> inline
109 void put_head( BOOST_IO_STD basic_ostream<Ch, Tr>& , T& ) {
112 template< class Ch, class Tr, class T> inline
113 void put_last( BOOST_IO_STD basic_ostream<Ch, Tr>& os, T& x ) {
117 #endif // -msvc workaround
122 template< class Ch, class Tr, class T>
124 const format_item<Ch, Tr>& specs,
125 std::basic_string<Ch, Tr> & res,
126 BOOST_IO_STD basic_ostringstream<Ch, Tr>& oss_ )
128 // does the actual conversion of x, with given params, into a string
129 // using the *supplied* strinstream. (the stream state is important)
131 typedef std::basic_string<Ch, Tr> string_t;
132 typedef format_item<Ch, Tr> format_item_t;
134 stream_format_state<Ch, Tr> prev_state(oss_);
136 specs.state_.apply_on(oss_);
138 // in case x is a group, apply the manip part of it,
139 // in order to find width
143 const std::streamsize w=oss_.width();
144 const std::ios_base::fmtflags fl=oss_.flags();
145 const bool internal = (fl & std::ios_base::internal) != 0;
146 const bool two_stepped_padding = internal
147 && ! ( specs.pad_scheme_ & format_item_t::spacepad )
148 && specs.truncate_ < 0 ;
151 if(! two_stepped_padding)
153 if(w>0) // handle simple padding via do_pad, not natively in stream
158 if (specs.truncate_ >= 0 && static_cast<unsigned int>(specs.truncate_) < res.size() )
159 res.erase(specs.truncate_);
162 if(specs.pad_scheme_ & format_item_t::spacepad)
164 if( res.size()==0 || ( res[0]!='+' && res[0]!='-' ))
166 res.insert(res.begin(), 1, ' '); // insert 1 space at pos 0
169 if(w > 0) // need do_pad
171 do_pad(res,w,oss_.fill(), fl, (specs.pad_scheme_ & format_item_t::centered) !=0 );
174 else // 2-stepped padding
176 put_last( oss_, x); // oss_.width() may result in padding.
179 if (specs.truncate_ >= 0)
180 res.erase(specs.truncate_);
182 if( res.size() - w > 0)
183 { // length w exceeded
184 // either it was multi-output with first output padding up all width..
185 // either it was one big arg and we are fine.
189 string_t tmp = oss_.str(); // minimal-length output
191 if( (d=w - tmp.size()) <=0 )
193 // minimal length is already >= w, so no padding (cool!)
197 { // hum.. we need to pad (it was necessarily multi-output)
198 typedef typename string_t::size_type size_type;
200 while( i<tmp.size() && tmp[i] == res[i] ) // find where we should pad.
202 tmp.insert(i, static_cast<size_type>( d ), oss_.fill());
207 { // okay, only one thing was printed and padded, so res is fine.
211 prev_state.apply_on(oss_);
223 template< class Ch, class Tr, class T>
224 void distribute(basic_format<Ch,Tr>& self, T x)
225 // call put(x, ..) on every occurence of the current argument :
227 if(self.cur_arg_ >= self.num_args_)
229 if( self.exceptions() & too_many_args_bit )
230 boost::throw_exception(too_many_args()); // too many variables have been supplied !
233 for(unsigned long i=0; i < self.items_.size(); ++i)
235 if(self.items_[i].argN_ == self.cur_arg_)
237 put<Ch, Tr, T> (x, self.items_[i], self.items_[i].res_, self.oss_ );
242 template<class Ch, class Tr, class T>
243 basic_format<Ch, Tr>& feed(basic_format<Ch,Tr>& self, T x)
245 if(self.dumped_) self.clear();
246 distribute<Ch, Tr, T> (self, x);
248 if(self.bound_.size() != 0)
250 while( self.cur_arg_ < self.num_args_ && self.bound_[self.cur_arg_] )
254 // this arg is finished, reset the stream's format state
255 self.state0_.apply_on(self.oss_);
260 } // namespace detail
265 #endif // BOOST_FORMAT_FEED_ARGS_HPP