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