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