source: NonGTP/Boost/boost/format/feed_args.hpp @ 857

Revision 857, 11.1 KB checked in by igarcia, 18 years ago (diff)
Line 
1// ----------------------------------------------------------------------------
2//  feed_args.hpp :  functions for processing each argument
3//                      (feed, feed_manip, and distribute)
4// ----------------------------------------------------------------------------
5
6//  Copyright Samuel Krempp 2003. Use, modification, and distribution are
7//  subject to the Boost Software License, Version 1.0. (See accompanying
8//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9
10//  See http://www.boost.org/libs/format for library home page
11
12// ----------------------------------------------------------------------------
13
14#ifndef BOOST_FORMAT_FEED_ARGS_HPP
15#define BOOST_FORMAT_FEED_ARGS_HPP
16
17#include <boost/config.hpp>
18#include <boost/assert.hpp>
19#include <boost/throw_exception.hpp>
20
21#include <boost/format/format_class.hpp>
22#include <boost/format/group.hpp>
23#include <boost/format/detail/msvc_disambiguater.hpp>
24
25namespace boost {
26namespace io {
27namespace detail {
28
29    template<class Ch, class Tr, class Alloc>
30    void mk_str( std::basic_string<Ch,Tr, Alloc> & res,
31                 const Ch * beg,
32                 typename std::basic_string<Ch,Tr,Alloc>::size_type size,
33                 std::streamsize w,
34                 const Ch fill_char,
35                 std::ios_base::fmtflags f,
36                 const Ch prefix_space, // 0 if no space-padding
37                 bool center)
38    // applies centered/left/right  padding  to the string  [beg, beg+size[
39    // Effects : the result is placed in res.
40    {
41        typedef typename std::basic_string<Ch,Tr,Alloc>::size_type size_type;
42        res.resize(0);
43        if(w<=0 || static_cast<size_type>(w) <=size) {
44            // no need to pad.
45            res.reserve(size + !!prefix_space);
46            if(prefix_space)
47              res.append(1, prefix_space);
48            res.append(beg, size);
49        }
50        else {
51            std::streamsize n=static_cast<std::streamsize>(w-size-!!prefix_space);
52            std::streamsize n_after = 0, n_before = 0;
53            res.reserve(w); // allocate once for the 2 inserts
54            if(center)
55                n_after = n/2, n_before = n - n_after;
56            else
57                if(f & std::ios_base::left)
58                    n_after = n;
59                else
60                    n_before = n;
61            // now make the res string :
62            if(n_before) res.append(n_before, fill_char);
63            if(prefix_space)
64              res.append(1, prefix_space);
65            res.append(beg, size);
66            if(n_after) res.append(n_after, fill_char);
67        }
68    } // -mk_str(..)
69
70
71#if BOOST_WORKAROUND( BOOST_MSVC, <= 1300) || \
72    BOOST_WORKAROUND(__DECCXX_VER, BOOST_TESTED_AT(60590042))
73// MSVC needs to be tricked to disambiguate this simple overload..
74// the trick is in "boost/format/msvc_disambiguater.hpp"
75 
76    template< class Ch, class Tr, class T> inline
77    void put_head (BOOST_IO_STD basic_ostream<Ch, Tr> & os, const T& x ) {
78        disambiguater<Ch, Tr, T>::put_head(os, x, 1L);
79    }
80    template< class Ch, class Tr, class T> inline
81    void put_last (BOOST_IO_STD basic_ostream<Ch, Tr> & os, const T& x ) {
82        disambiguater<Ch, Tr, T>::put_last(os, x, 1L);
83    }
84
85#else 
86
87    template< class Ch, class Tr, class T> inline
88    void put_head (BOOST_IO_STD basic_ostream<Ch, Tr> &, const T& ) {
89    }
90
91    template< class Ch, class Tr, class T> inline
92    void put_head( BOOST_IO_STD basic_ostream<Ch, Tr> & os, const group1<T>& x ) {
93        os << group_head(x.a1_); // send the first N-1 items, not the last
94    }
95
96    template< class Ch, class Tr, class T> inline
97    void put_last( BOOST_IO_STD basic_ostream<Ch, Tr> & os, const T& x ) {
98        os << x ;
99    }
100
101    template< class Ch, class Tr, class T> inline
102    void put_last( BOOST_IO_STD basic_ostream<Ch, Tr> & os, const group1<T>& x ) {
103        os << group_last(x.a1_); // this selects the last element
104    }
105
106#ifndef BOOST_NO_OVERLOAD_FOR_NON_CONST
107    template< class Ch, class Tr, class T> inline
108    void put_head( BOOST_IO_STD basic_ostream<Ch, Tr> &, T& ) {
109    }
110
111    template< class Ch, class Tr, class T> inline
112    void put_last( BOOST_IO_STD basic_ostream<Ch, Tr> & os, T& x) {
113        os << x ;
114    }
115#endif
116#endif  // -msvc workaround
117
118
119    template< class Ch, class Tr, class Alloc, class T>
120    void put( T x,
121              const format_item<Ch, Tr, Alloc>& specs,
122              typename basic_format<Ch, Tr, Alloc>::string_type& res,
123              typename basic_format<Ch, Tr, Alloc>::internal_streambuf_t & buf,
124              io::detail::locale_t *loc_p = NULL)
125    {
126        // does the actual conversion of x, with given params, into a string
127        // using the supplied stringbuf.
128
129        typedef typename basic_format<Ch, Tr, Alloc>::string_type   string_type;
130        typedef typename basic_format<Ch, Tr, Alloc>::format_item_t format_item_t;
131        typedef typename string_type::size_type size_type;
132
133        basic_oaltstringstream<Ch, Tr, Alloc>  oss( &buf);
134        specs.fmtstate_.apply_on(oss, loc_p);
135
136        // the stream format state can be modified by manipulators in the argument :
137        put_head( oss, x );
138        // in case x is a group, apply the manip part of it,
139        // in order to find width
140
141        const std::ios_base::fmtflags fl=oss.flags();
142        const bool internal = (fl & std::ios_base::internal) != 0;
143        const std::streamsize w = oss.width();
144        const bool two_stepped_padding= internal && (w!=0);
145     
146        res.resize(0);
147        if(! two_stepped_padding) {
148            if(w>0) // handle padding via mk_str, not natively in stream
149                oss.width(0);
150            put_last( oss, x);
151            const Ch * res_beg = buf.pbase();
152            Ch prefix_space = 0;
153            if(specs.pad_scheme_ & format_item_t::spacepad)
154                if(buf.pcount()== 0 ||
155                   (res_beg[0] !=oss.widen('+') && res_beg[0] !=oss.widen('-')  ))
156                    prefix_space = oss.widen(' ');
157            size_type res_size = (std::min)(
158                static_cast<size_type>(specs.truncate_ - !!prefix_space),
159                buf.pcount() );
160            mk_str(res, res_beg, res_size, w, oss.fill(), fl,
161                   prefix_space, (specs.pad_scheme_ & format_item_t::centered) !=0 );
162        }
163        else  { // 2-stepped padding
164            // internal can be implied by zeropad, or user-set.
165            // left, right, and centered alignment overrule internal,
166            // but spacepad or truncate might be mixed with internal (using manipulator)
167            put_last( oss, x); // may pad
168            const Ch * res_beg = buf.pbase();
169            size_type res_size = buf.pcount();
170            bool prefix_space=false;
171            if(specs.pad_scheme_ & format_item_t::spacepad)
172                if(buf.pcount()== 0 ||
173                   (res_beg[0] !=oss.widen('+') && res_beg[0] !=oss.widen('-')  ))
174                    prefix_space = true;
175            if(res_size == static_cast<size_type>(w) && w<=specs.truncate_ && !prefix_space) {
176                // okay, only one thing was printed and padded, so res is fine
177                res.assign(res_beg, res_size);
178            }
179            else { //   length w exceeded
180                // either it was multi-output with first output padding up all width..
181                // either it was one big arg and we are fine.
182                // Note that res_size<w is possible  (in case of bad user-defined formatting)
183                res.assign(res_beg, res_size);
184                res_beg=NULL;  // invalidate pointers.
185               
186                // make a new stream, to start re-formatting from scratch :
187                buf.clear_buffer();
188                basic_oaltstringstream<Ch, Tr, Alloc>  oss2( &buf);
189                specs.fmtstate_.apply_on(oss2, loc_p);
190                put_head( oss2, x );
191
192                oss2.width(0);
193                if(prefix_space)
194                    oss2 << ' ';
195                put_last(oss2, x );
196                if(buf.pcount()==0 && specs.pad_scheme_ & format_item_t::spacepad) {
197                    prefix_space =true;
198                    oss2 << ' ';
199                }
200                // we now have the minimal-length output
201                const Ch * tmp_beg = buf.pbase();
202                size_type tmp_size = (std::min)(static_cast<size_type>(specs.truncate_),
203                                                buf.pcount() );
204                                                   
205               
206                if(static_cast<size_type>(w) <= tmp_size) {
207                    // minimal length is already >= w, so no padding (cool!)
208                        res.assign(tmp_beg, tmp_size);
209                }
210                else { // hum..  we need to pad (multi_output, or spacepad present)
211                    //find where we should pad
212                    size_type sz = (std::min)(res_size+prefix_space, tmp_size);
213                    size_type i = prefix_space;
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                                        std::streamsize d = w - static_cast<std::streamsize>(tmp_size);
218                                        BOOST_ASSERT(d>0);
219                    res.append(static_cast<size_type>( d ), oss2.fill());
220                    res.append(tmp_beg+i, tmp_size-i);
221                    BOOST_ASSERT(i+(tmp_size-i)+(std::max)(d,(std::streamsize)0)
222                                 == static_cast<size_type>(w));
223                    BOOST_ASSERT(res.size() == static_cast<size_type>(w));
224                }
225            }
226        }
227        buf.clear_buffer();
228    } // end- put(..)
229
230
231    template< class Ch, class Tr, class Alloc, class T>
232    void distribute (basic_format<Ch,Tr, Alloc>& 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(self.cur_arg_, self.num_args_));
237            else return;
238        }
239        for(unsigned long i=0; i < self.items_.size(); ++i) {
240            if(self.items_[i].argN_ == self.cur_arg_) {
241                put<Ch, Tr, Alloc, T> (x, self.items_[i], self.items_[i].res_,
242                                self.buf_, boost::get_pointer(self.loc_) );
243            }
244        }
245    }
246
247    template<class Ch, class Tr, class Alloc, class T>
248    basic_format<Ch, Tr, Alloc>& 
249    feed (basic_format<Ch,Tr, Alloc>& self, T x) {
250        if(self.dumped_) self.clear();
251        distribute<Ch, Tr, Alloc, T> (self, x);
252        ++self.cur_arg_;
253        if(self.bound_.size() != 0) {
254                while( self.cur_arg_ < self.num_args_ && self.bound_[self.cur_arg_] )
255                    ++self.cur_arg_;
256        }
257        return self;
258    }
259   
260} // namespace detail
261} // namespace io
262} // namespace boost
263
264
265#endif //  BOOST_FORMAT_FEED_ARGS_HPP
Note: See TracBrowser for help on using the repository browser.