]> git.lyx.org Git - lyx.git/blob - boost/boost/format/feed_args.hpp
64-bit fix to boost::format.
[lyx.git] / boost / boost / format / feed_args.hpp
1 // -*- C++ -*-
2 //  Boost general library 'format'   ---------------------------
3 //  See http://www.boost.org for updates, documentation, and revision history.
4
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.
11
12 // ideas taken from RĂ¼diger Loos's format class
13 // and Karl Nelson's ofstream
14
15 // ----------------------------------------------------------------------------
16 // feed_args.hpp :  functions for processing each argument 
17 //                      (feed, feed_manip, and distribute)
18 // ----------------------------------------------------------------------------
19
20
21 #ifndef BOOST_FORMAT_FEED_ARGS_HPP
22 #define BOOST_FORMAT_FEED_ARGS_HPP
23
24 #include <boost/format/format_class.hpp>
25 #include <boost/format/group.hpp>
26
27 #include <boost/format/detail/msvc_disambiguater.hpp>
28 #include <boost/throw_exception.hpp>
29
30 namespace boost {
31 namespace io {
32 namespace detail {
33 namespace  { 
34
35     template<class Ch, class Tr> 
36     void clear_buffer(io::basic_outsstream<Ch,Tr> & os) { 
37         os.clear_buffer();
38     }
39
40     template<class Ch, class Tr>
41     void mk_str( std::basic_string<Ch,Tr> & res, 
42                  const Ch * beg,
43                  std::streamsize size,
44                  std::streamsize w, 
45                  const Ch fill_char,
46                  std::ios_base::fmtflags f, 
47                  const Ch prefix_space, // 0 if no space-padding
48                  bool center) 
49         // applies centered / left / right  padding  to the string  [beg, beg+size[
50         // Effects : the result is placed in res.
51     {
52         res.resize(0);
53         std::streamsize n=w-size-!!prefix_space;
54         std::streamsize n_after = 0, n_before = 0; 
55
56         if(n<=0) { // no need to pad.
57             res.reserve(size + !!prefix_space);
58         }
59         else { 
60             res.reserve(w); // allocate once for the 2 inserts
61             if(center) 
62                 n_after = n/2, n_before = n - n_after; 
63             else 
64                 if(f & std::ios_base::left)
65                     n_after = n;
66                 else
67                     n_before = n;
68         }
69         // now make the res string :
70         if(n_before) res.append(n_before, fill_char);
71         if(prefix_space) 
72             res.append(1, prefix_space);
73         res.append(beg, size);
74         if(n_after) res.append(n_after, fill_char);
75     } // -mk_str(..) 
76
77
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"
81   
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);
85     }
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);
89     }
90
91 #else  
92
93     template< class Ch, class Tr, class T> inline
94     void put_head(BOOST_IO_STD basic_ostream<Ch, Tr>& , const T& ) {
95     }
96
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
100     }
101
102     template< class Ch, class Tr, class T> inline
103     void put_last( BOOST_IO_STD basic_ostream<Ch, Tr>& os, const T& x ) {
104         os << x ;
105     }
106
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
110     }
111
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& ) {
115     }
116
117     template< class Ch, class Tr, class T> inline
118     void put_last( BOOST_IO_STD basic_ostream<Ch, Tr>& os, T& x ) {
119         os << x ;
120     }
121 #endif
122 #endif  // -msvc workaround
123
124
125     template< class Ch, class Tr, class T> 
126     void put( T x, 
127               const format_item<Ch, Tr>& specs, 
128               std::basic_string<Ch, Tr> & res, 
129               io::basic_outsstream<Ch, Tr>& oss_ ) 
130     {
131         // does the actual conversion of x, with given params, into a string
132         // using the *supplied* strinstream. (the stream state is important)
133
134         typedef std::basic_string<Ch, Tr> string_t;
135         typedef format_item<Ch, Tr>  format_item_t;
136     
137         specs.fmtstate_.apply_on(oss_);
138
139         // the stream format state can be modified by manipulators in the argument :
140         put_head( oss_, x );
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 ?
144
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);
149       
150         res.resize(0);
151         if(! two_stepped_padding) {
152             if(w>0) // handle padding via mk_str, not natively in stream 
153                 oss_.width(0);
154             put_last( oss_, x);
155             const Ch * res_beg = oss_.begin();
156             Ch prefix_space = 0;
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),
163               oss_.pcount());
164
165             mk_str(res, res_beg, res_size, w, oss_.fill(), fl, 
166                    prefix_space, (specs.pad_scheme_ & format_item_t::centered) !=0 );
167         } 
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('-')  ))
179                     prefix_space = true;
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);
183             }
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.
190                 clear_buffer( oss_);
191                 oss_.width(0);
192                 if(prefix_space)
193                     oss_ << ' ';
194                 put_last(oss_, x );
195                 if(oss_.pcount()==0 && specs.pad_scheme_ & format_item_t::spacepad) {
196                     prefix_space =true;
197                     oss_ << ' ';
198                 }
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_));
203                 std::streamsize d;
204                 if( (d=w - tmp_size) <=0 ) { 
205                     // minimal length is already >= w, so no padding (cool!)
206                         res.assign(tmp_beg, tmp_size);
207                 }
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);
221                 }
222             }
223         }
224         clear_buffer( oss_);
225     } // end- put(..)
226
227
228 }  // local namespace
229
230
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
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, T> (x, self.items_[i], self.items_[i].res_, self.oss_ );
242             }
243         }
244     }
245
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);
250         ++self.cur_arg_;
251         if(self.bound_.size() != 0) {
252                 while( self.cur_arg_ < self.num_args_ && self.bound_[self.cur_arg_] )
253                     ++self.cur_arg_;
254         }
255         return self;
256     }
257     
258 } // namespace detail
259 } // namespace io
260 } // namespace boost
261
262
263 #endif //  BOOST_FORMAT_FEED_ARGS_HPP