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/detail/msvc_disambiguater.hpp>
28 #include <boost/throw_exception.hpp>
35 template<class Ch, class Tr>
36 void clear_buffer(io::basic_outsstream<Ch,Tr> & os) {
40 template<class Ch, class Tr>
41 void mk_str( std::basic_string<Ch,Tr> & res,
46 std::ios_base::fmtflags f,
47 const Ch prefix_space, // 0 if no space-padding
49 // applies centered / left / right padding to the string [beg, beg+size[
50 // Effects : the result is placed in res.
53 std::streamsize n=w-size-!!prefix_space;
54 std::streamsize n_after = 0, n_before = 0;
56 if(n<=0) { // no need to pad.
57 res.reserve(size + !!prefix_space);
60 res.reserve(w); // allocate once for the 2 inserts
62 n_after = n/2, n_before = n - n_after;
64 if(f & std::ios_base::left)
69 // now make the res string :
70 if(n_before) res.append(n_before, fill_char);
72 res.append(1, prefix_space);
73 res.append(beg, size);
74 if(n_after) res.append(n_after, fill_char);
78 #if BOOST_WORKAROUND( BOOST_MSVC, <= 1300)
79 // MSVC needs to be tricked to disambiguate this simple overload..
80 // the trick is in "boost/format/msvc_disambiguater.hpp"
82 template< class Ch, class Tr, class T> inline
83 void put_head( BOOST_IO_STD basic_ostream<Ch, Tr>& os, const T& x ) {
84 disambiguater<Ch, Tr, T>::put_head(os, x, 1L);
86 template< class Ch, class Tr, class T> inline
87 void put_last( BOOST_IO_STD basic_ostream<Ch, Tr>& os, const T& x ) {
88 disambiguater<Ch, Tr, T>::put_last(os, x, 1L);
93 template< class Ch, class Tr, class T> inline
94 void put_head(BOOST_IO_STD basic_ostream<Ch, Tr>& , const T& ) {
97 template< class Ch, class Tr, class T> inline
98 void put_head( BOOST_IO_STD basic_ostream<Ch, Tr>& os, const group1<T>& x ) {
99 os << group_head(x.a1_); // send the first N-1 items, not the last
102 template< class Ch, class Tr, class T> inline
103 void put_last( BOOST_IO_STD basic_ostream<Ch, Tr>& os, const T& x ) {
107 template< class Ch, class Tr, class T> inline
108 void put_last( BOOST_IO_STD basic_ostream<Ch, Tr>& os, const group1<T>& x ) {
109 os << group_last(x.a1_); // this selects the last element
112 #ifndef BOOST_NO_OVERLOAD_FOR_NON_CONST
113 template< class Ch, class Tr, class T> inline
114 void put_head( BOOST_IO_STD basic_ostream<Ch, Tr>& , T& ) {
117 template< class Ch, class Tr, class T> inline
118 void put_last( BOOST_IO_STD basic_ostream<Ch, Tr>& os, T& x ) {
122 #endif // -msvc workaround
125 template< class Ch, class Tr, class T>
127 const format_item<Ch, Tr>& specs,
128 std::basic_string<Ch, Tr> & res,
129 io::basic_outsstream<Ch, Tr>& oss_ )
131 // does the actual conversion of x, with given params, into a string
132 // using the *supplied* strinstream. (the stream state is important)
134 typedef std::basic_string<Ch, Tr> string_t;
135 typedef format_item<Ch, Tr> format_item_t;
137 specs.fmtstate_.apply_on(oss_);
139 // the stream format state can be modified by manipulators in the argument :
141 // in case x is a group, apply the manip part of it,
142 // in order to find width
143 // clear_buffer( oss_); // fixme. is it necessary ?
145 const std::ios_base::fmtflags fl=oss_.flags();
146 const bool internal = (fl & std::ios_base::internal) != 0;
147 const std::streamsize w = oss_.width();
148 const bool two_stepped_padding= internal && (w!=0);
151 if(! two_stepped_padding) {
152 if(w>0) // handle padding via mk_str, not natively in stream
155 const Ch * res_beg = oss_.begin();
157 if(specs.pad_scheme_ & format_item_t::spacepad)
158 if(oss_.pcount()== 0 ||
159 (res_beg[0] !=oss_.widen('+') && res_beg[0] !=oss_.widen('-') ))
160 prefix_space = oss_.widen(' ');
161 std::streamsize res_size = std::min(
162 static_cast<std::streamsize>(specs.truncate_ - !!prefix_space),
165 mk_str(res, res_beg, res_size, w, oss_.fill(), fl,
166 prefix_space, (specs.pad_scheme_ & format_item_t::centered) !=0 );
168 else { // 2-stepped padding
169 // internal can be implied by zeropad, or user-set.
170 // left, right, and centered alignment overrule internal,
171 // but spacepad or truncate might be mixed with internal (using manipulator)
172 put_last( oss_, x); // may pad
173 const Ch * res_beg = oss_.begin();
174 std::streamsize res_size = oss_.pcount();
175 bool prefix_space=false;
176 if(specs.pad_scheme_ & format_item_t::spacepad)
177 if(oss_.pcount()== 0 ||
178 (res_beg[0] !=oss_.widen('+') && res_beg[0] !=oss_.widen('-') ))
180 if(res_size == w && w<=specs.truncate_ && !prefix_space) {
181 // okay, only one thing was printed and padded, so res is fine
182 res.assign(res_beg, res_size);
184 else { // length w exceeded
185 // either it was multi-output with first output padding up all width..
186 // either it was one big arg and we are fine.
187 //BOOST_ASSERT(res_size > w); //res_size<w means buggy user-defined formatting
188 res.assign(res_beg, res_size);
189 res_beg=NULL; // invalidate pointers.
195 if(oss_.pcount()==0 && specs.pad_scheme_ & format_item_t::spacepad) {
199 // minimal-length output
200 const Ch * tmp_beg = oss_.begin();
201 std::streamsize tmp_size = std::min(oss_.pcount(),
202 static_cast<std::streamsize>(specs.truncate_));
204 if( (d=w - tmp_size) <=0 ) {
205 // minimal length is already >= w, so no padding (cool!)
206 res.assign(tmp_beg, tmp_size);
208 else { // hum.. we need to pad (multi_output, or spacepad present)
209 typedef typename string_t::size_type size_type;
210 std::streamsize i = prefix_space;
211 //find where we should pad
212 //BOOST_ASSERT( static_cast<size_t>(tmp_size-prefix_space <= res.size() ));
213 std::streamsize sz = std::min(res_size+prefix_space, tmp_size);
214 for(; i<sz && tmp_beg[i] == res[i-prefix_space]; ++i){}
215 if(i>=tmp_size) i=prefix_space;
216 res.assign(tmp_beg, i);
217 if(d>0) res.append(static_cast<size_type>( d ), oss_.fill());
218 res.append(tmp_beg+i, tmp_size-i);
219 assert(i+(tmp_size-i)+std::max(d,(std::streamsize)0) == w);
220 assert(res.size() == (std::size_t)w);
231 template< class Ch, class Tr, class T>
232 void distribute(basic_format<Ch,Tr>& self, T x) {
233 // call put(x, ..) on every occurence of the current argument :
234 if(self.cur_arg_ >= self.num_args_) {
235 if( self.exceptions() & too_many_args_bit )
236 boost::throw_exception(too_many_args()); // too many variables supplied
239 for(unsigned long i=0; i < self.items_.size(); ++i) {
240 if(self.items_[i].argN_ == self.cur_arg_) {
241 put<Ch, Tr, T> (x, self.items_[i], self.items_[i].res_, self.oss_ );
246 template<class Ch, class Tr, class T>
247 basic_format<Ch, Tr>& feed(basic_format<Ch,Tr>& self, T x) {
248 if(self.dumped_) self.clear();
249 distribute<Ch, Tr, T> (self, x);
251 if(self.bound_.size() != 0) {
252 while( self.cur_arg_ < self.num_args_ && self.bound_[self.cur_arg_] )
258 } // namespace detail
263 #endif // BOOST_FORMAT_FEED_ARGS_HPP