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; // avoids 2 cases ( "" and L"" )
41 template<class Ch, class Tr>
42 void do_fill( std::basic_string<Ch,Tr> & s,
45 std::ios_base::fmtflags f,
48 std::streamsize n=w-s.size();
54 s.reserve(w); // allocate once for the 2 inserts
55 const std::streamsize n1 = n /2, n0 = n - n1;
56 s.insert(s.begin(), n0, c);
61 if(f & std::ios_base::left) {
65 s.insert(s.begin(), n, c);
71 #if !defined(BOOST_MSVC) || BOOST_MSVC > 1300
72 template< class Ch, class Tr, class T> inline
73 void put_head(BOOST_IO_STD basic_ostream<Ch, Tr>& , const T& ) {
76 template< class Ch, class Tr, class T> inline
77 void put_head( BOOST_IO_STD basic_ostream<Ch, Tr>& os, const group1<T>& x ) {
78 os << group_head(x.a1_); // send the first N-1 items, not the last
81 template< class Ch, class Tr, class T> inline
82 void put_last( BOOST_IO_STD basic_ostream<Ch, Tr>& os, const T& x ) {
86 template< class Ch, class Tr, class T> inline
87 void put_last( BOOST_IO_STD basic_ostream<Ch, Tr>& os, const group1<T>& x ) {
88 os << group_last(x.a1_); // this selects the last element
91 #ifdef BOOST_OVERLOAD_FOR_NON_CONST
92 template< class Ch, class Tr, class T> inline
93 void put_head( BOOST_IO_STD basic_ostream<Ch, Tr>& , T& ) {
96 template< class Ch, class Tr, class T> inline
97 void put_last( BOOST_IO_STD basic_ostream<Ch, Tr>& os, T& x ) {
102 #else // MSVC needs to be tricked to disambiguate this simple overload..
103 // the trick is in "boost/format/msvc_disambiguater.hpp"
105 template< class Ch, class Tr, class T> inline
106 void put_head( BOOST_IO_STD basic_ostream<Ch, Tr>& os, const T& x ) {
107 disambiguater<Ch, Tr, T>::put_head(os, x, 1L);
109 template< class Ch, class Tr, class T> inline
110 void put_last( BOOST_IO_STD basic_ostream<Ch, Tr>& os, const T& x ) {
111 disambiguater<Ch, Tr, T>::put_last(os, x, 1L);
114 #endif // -msvc workaround
119 template< class Ch, class Tr, class T>
121 const format_item<Ch, Tr>& specs,
122 std::basic_string<Ch, Tr> & res,
123 BOOST_IO_STD basic_ostringstream<Ch, Tr>& oss_ )
125 // does the actual conversion of x, with given params, into a string
126 // using the *supplied* strinstream. (the stream state is important)
128 typedef std::basic_string<Ch, Tr> string_t;
129 typedef format_item<Ch, Tr> format_item_t;
131 stream_format_state<Ch, Tr> prev_state(oss_);
133 specs.state_.apply_on(oss_);
135 // in case x is a group, apply the manip part of it,
136 // in order to find width
140 const std::streamsize w=oss_.width();
141 const std::ios_base::fmtflags fl=oss_.flags();
142 const bool internal = (fl & std::ios_base::internal) != 0;
143 const bool two_phased_filling = internal
144 && ! ( specs.pad_scheme_ & format_item_t::spacepad )
145 && specs.truncate_ < 0 ;
149 if(two_phased_filling)
150 { } // in this case, let the stream do the padding, and later on, we'll do our hack..
159 if (specs.truncate_ >= 0)
160 res.erase(specs.truncate_);
164 if(specs.pad_scheme_ & format_item_t::spacepad)
166 if( res.size()==0 || ( res[0]!='+' && res[0]!='-' ))
168 res.insert(res.begin(), 1, ' '); // insert 1 space at pos 0
172 if(two_phased_filling)
174 if( res.size() - w > 0)
176 // either it was one big arg and we have nothing to do about it,
177 // either it was multi-output with first output filling up all width..
181 string_t tmp = oss_.str(); // minimal-length output
183 if( (d=w - tmp.size()) >0 ) // hum.. then we need to pad.
185 typedef typename string_t::size_type size_type;
187 while( i<tmp.size() && tmp[i] == res[i] ) // find where we should pad.
189 tmp.insert(i, static_cast<size_type>( d ), oss_.fill());
190 std::swap(res, tmp );
194 // size is already >= w, so no padding (cool!)
195 std::swap(res, tmp );
199 { // okay, only one thing was printed and padded, so res is fine.
202 else if(w > 0) // other cases that need do_fill
204 do_fill(res,w,oss_.fill(), fl, (specs.pad_scheme_ & format_item_t::centered) !=0 );
207 prev_state.apply_on(oss_);
218 template< class Ch, class Tr, class T>
219 void distribute(basic_format<Ch,Tr>& self, T x)
220 // call put(x, ..) on every occurence of the current argument :
222 if(self.cur_arg_ >= self.num_args_)
224 if( self.exceptions() & too_many_args_bit )
225 boost::throw_exception(too_many_args()); // too many variables have been supplied !
228 for(unsigned long i=0; i < self.items_.size(); ++i)
230 if(self.items_[i].argN_ == self.cur_arg_)
232 put<Ch, Tr, T> (x, self.items_[i], self.items_[i].res_, self.oss_ );
238 template<class Ch, class Tr, class T>
239 basic_format<Ch, Tr>& feed(basic_format<Ch,Tr>& self, T x)
241 if(self.dumped_) self.clear();
242 distribute<Ch, Tr, T> (self, x);
244 if(self.bound_.size() != 0)
246 while( self.cur_arg_ < self.num_args_ && self.bound_[self.cur_arg_] )
250 // this arg is finished, reset the stream's format state
251 self.state0_.apply_on(self.oss_);
256 } // namespace detail
261 #endif // BOOST_FORMAT_FEED_ARGS_HPP