]> git.lyx.org Git - lyx.git/blob - boost/boost/format/feed_args.hpp
update to boost 1.30.1
[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/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 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;
38     os.str(emptyStr); 
39   }
40
41   template<class Ch, class Tr>
42   void do_pad( std::basic_string<Ch,Tr> & s, 
43                 std::streamsize w, 
44                 const Ch c, 
45                 std::ios_base::fmtflags f, 
46                 bool center) 
47     // applies centered / left / right  padding  to the string s.
48     // Effects : string s is padded.
49   {
50     std::streamsize n=w-s.size();
51     if(n<=0) {
52       return;
53     }
54     if(center) 
55       {
56         s.reserve(w); // allocate once for the 2 inserts
57         const std::streamsize n1 = n /2, n0 = n - n1; 
58         s.insert(s.begin(), n0, c);
59         s.append(n1, c);
60       } 
61     else 
62       {
63         if(f & std::ios_base::left) {
64           s.append(n, c);
65         }
66         else {
67           s.insert(s.begin(), n, c);
68         }
69       }
70   } // -do_pad(..) 
71
72
73 #if BOOST_WORKAROUND( BOOST_MSVC, BOOST_TESTED_AT(1300))
74 // MSVC needs to be tricked to disambiguate this simple overload..
75 // the trick is in "boost/format/msvc_disambiguater.hpp"
76   
77   template< class Ch, class Tr, class T> inline
78   void put_head( BOOST_IO_STD basic_ostream<Ch, Tr>& os, const T& x ) {
79     disambiguater<Ch, Tr, T>::put_head(os, x, 1L);
80   }
81   template< class Ch, class Tr, class T> inline
82   void put_last( BOOST_IO_STD basic_ostream<Ch, Tr>& os, const T& x ) {
83     disambiguater<Ch, Tr, T>::put_last(os, x, 1L);
84   }
85
86 #else  
87
88   template< class Ch, class Tr, class T> inline
89   void put_head(BOOST_IO_STD basic_ostream<Ch, Tr>& , const T& ) {
90   }
91
92   template< class Ch, class Tr, class T> inline
93   void put_head( BOOST_IO_STD basic_ostream<Ch, Tr>& os, const group1<T>& x ) {
94     os << group_head(x.a1_); // send the first N-1 items, not the last
95   }
96
97   template< class Ch, class Tr, class T> inline
98   void put_last( BOOST_IO_STD basic_ostream<Ch, Tr>& os, const T& x ) {
99     os << x ;
100   }
101
102   template< class Ch, class Tr, class T> inline
103   void put_last( BOOST_IO_STD basic_ostream<Ch, Tr>& os, const group1<T>& x ) {
104     os << group_last(x.a1_); // this selects the last element
105   }
106
107 #ifndef BOOST_NO_OVERLOAD_FOR_NON_CONST 
108   template< class Ch, class Tr, class T> inline
109   void put_head( BOOST_IO_STD basic_ostream<Ch, Tr>& , T& ) {
110   }
111
112   template< class Ch, class Tr, class T> inline
113   void put_last( BOOST_IO_STD basic_ostream<Ch, Tr>& os, T& x ) {
114     os << x ;
115   }
116 #endif
117 #endif  // -msvc workaround
118
119
120
121   
122 template< class Ch, class Tr, class T> 
123 void put( T x, 
124           const format_item<Ch, Tr>& specs, 
125           std::basic_string<Ch, Tr> & res, 
126           BOOST_IO_STD basic_ostringstream<Ch, Tr>& oss_ )
127 {
128   // does the actual conversion of x, with given params, into a string
129   // using the *supplied* strinstream. (the stream state is important)
130
131   typedef std::basic_string<Ch, Tr> string_t;
132   typedef format_item<Ch, Tr>  format_item_t;
133
134   stream_format_state<Ch, Tr>   prev_state(oss_);
135     
136   specs.state_.apply_on(oss_);
137
138   // in case x is a group, apply the manip part of it, 
139   // in order to find width
140   put_head( oss_, x );
141   empty_buf( oss_);
142
143   const std::streamsize w=oss_.width();
144   const std::ios_base::fmtflags fl=oss_.flags();
145   const bool internal = (fl & std::ios_base::internal) != 0;
146   const bool two_stepped_padding = internal
147     &&  ! ( specs.pad_scheme_ & format_item_t::spacepad ) 
148     && specs.truncate_ < 0 ;
149       
150
151   if(! two_stepped_padding) 
152     {
153       if(w>0) // handle simple padding via do_pad, not natively in stream 
154         oss_.width(0);
155       put_last( oss_, x);
156       res = oss_.str();
157
158       if (specs.truncate_ >= 0)
159         res.erase(specs.truncate_);
160
161       // complex pads :
162       if(specs.pad_scheme_ & format_item_t::spacepad)
163         {
164           if( res.size()==0 ||   ( res[0]!='+' && res[0]!='-'  ))
165             {
166               res.insert(res.begin(), 1, ' '); // insert 1 space at  pos 0
167             }
168         }
169       if(w > 0) // need do_pad
170         {
171           do_pad(res,w,oss_.fill(), fl, (specs.pad_scheme_ & format_item_t::centered) !=0 );
172         }
173     } 
174   else  // 2-stepped padding
175     {
176       put_last( oss_, x); // oss_.width() may result in padding.
177       res = oss_.str();
178       
179       if (specs.truncate_ >= 0)
180         res.erase(specs.truncate_);
181
182       if( res.size() - w > 0)
183         { //   length w exceeded
184           // either it was multi-output with first output padding up all width..
185           // either it was one big arg and we are fine.
186           empty_buf( oss_);
187           oss_.width(0);
188           put_last(oss_, x );
189           string_t tmp = oss_.str();  // minimal-length output
190           std::streamsize d;
191           if( (d=w - tmp.size()) <=0 ) 
192             {
193               // minimal length is already >= w, so no padding  (cool!)
194               res.swap(tmp);
195             }
196           else
197             { // hum..  we need to pad (it was necessarily multi-output)
198               typedef typename string_t::size_type size_type;
199               size_type i = 0;
200               while( i<tmp.size() && tmp[i] == res[i] ) // find where we should pad.
201                 ++i;
202               tmp.insert(i, static_cast<size_type>( d ), oss_.fill());
203               res.swap( tmp );
204             }
205         }
206       else 
207         { // okay, only one thing was printed and padded, so res is fine.
208         }
209     }
210
211   prev_state.apply_on(oss_);
212   empty_buf( oss_);
213   oss_.clear();
214 } // end- put(..)
215
216
217 }  // local namespace
218
219
220
221
222
223 template< class Ch, class Tr, class T> 
224 void distribute(basic_format<Ch,Tr>& self, T x) 
225   // call put(x, ..) on every occurence of the current argument :
226 {
227   if(self.cur_arg_ >= self.num_args_)
228     {
229       if( self.exceptions() & too_many_args_bit )
230         boost::throw_exception(too_many_args()); // too many variables have been supplied !
231       else return;
232     }
233   for(unsigned long i=0; i < self.items_.size(); ++i)
234     {
235       if(self.items_[i].argN_ == self.cur_arg_)
236         {
237           put<Ch, Tr, T> (x, self.items_[i], self.items_[i].res_, self.oss_ );
238         }
239     }
240 }
241
242 template<class Ch, class Tr, class T> 
243 basic_format<Ch, Tr>&  feed(basic_format<Ch,Tr>& self, T x) 
244 {
245   if(self.dumped_) self.clear();
246   distribute<Ch, Tr, T> (self, x);
247   ++self.cur_arg_;
248   if(self.bound_.size() != 0)
249     {
250       while( self.cur_arg_ < self.num_args_ && self.bound_[self.cur_arg_] )
251         ++self.cur_arg_;
252     }
253
254   // this arg is finished, reset the stream's format state
255   self.state0_.apply_on(self.oss_);
256   return self;
257 }
258     
259
260 } // namespace detail
261 } // namespace io
262 } // namespace boost
263
264
265 #endif //  BOOST_FORMAT_FEED_ARGS_HPP