]> git.lyx.org Git - lyx.git/blob - boost/boost/format/format_implementation.hpp
64-bit fix to boost::format.
[lyx.git] / boost / boost / format / format_implementation.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 // format_implementation.hpp  Implementation of the basic_format class
17 // ----------------------------------------------------------------------------
18
19
20 #ifndef BOOST_FORMAT_IMPLEMENTATION_HPP
21 #define BOOST_FORMAT_IMPLEMENTATION_HPP
22
23 #include <boost/throw_exception.hpp>
24 #include <boost/assert.hpp>
25 #include <boost/format/format_class.hpp>
26 #include <algorithm> // std::swap
27
28 namespace boost {
29
30 // --------  format:: -------------------------------------------
31     template< class Ch, class Tr>
32     basic_format<Ch, Tr>:: basic_format(const Ch* str)
33         : style_(0), cur_arg_(0), num_args_(0), dumped_(false),
34           exceptions_(io::all_error_bits)
35     {
36         if( str)
37             parse( str );
38     }
39
40 #ifndef BOOST_NO_STD_LOCALE
41     template< class Ch, class Tr>
42     basic_format<Ch, Tr>:: basic_format(const Ch* str, const std::locale & loc)
43         : style_(0), cur_arg_(0), num_args_(0), dumped_(false),
44           exceptions_(io::all_error_bits)
45     {
46         oss_.imbue( loc );
47         if(str) parse( str );
48     }
49
50     template< class Ch, class Tr>
51     basic_format<Ch, Tr>:: basic_format(const string_t& s, const std::locale & loc)
52         : style_(0), cur_arg_(0), num_args_(0), dumped_(false),
53           exceptions_(io::all_error_bits)
54     {
55         oss_.imbue( loc );
56         parse(s);  
57     }
58 #endif //BOOST_NO_STD_LOCALE
59
60     template< class Ch, class Tr>
61     basic_format<Ch, Tr>:: basic_format(const string_t& s)
62         : style_(0), cur_arg_(0), num_args_(0), dumped_(false),
63           exceptions_(io::all_error_bits)
64     {
65         parse(s);  
66     }
67
68     template< class Ch, class Tr>
69     basic_format<Ch, Tr>:: basic_format(const basic_format& x)
70         : items_(x.items_), bound_(x.bound_), style_(x.style_), 
71           cur_arg_(x.cur_arg_), num_args_(x.num_args_), dumped_(false), 
72           prefix_(x.prefix_), exceptions_(x.exceptions_) 
73     { 
74     } 
75
76     template< class Ch, class Tr>
77     void  basic_format<Ch, Tr>:: swap (basic_format & x) {
78         std::swap(exceptions_, x.exceptions_);
79         std::swap(style_, x.style_); 
80         std::swap(cur_arg_, x.cur_arg_); 
81         std::swap(num_args_, x.num_args_);
82         std::swap(dumped_, x.dumped_);
83
84         items_.swap(x.items_);
85         prefix_.swap(x.prefix_);
86         bound_.swap(x.bound_);
87     }
88
89     template< class Ch, class Tr>
90     basic_format<Ch, Tr>& basic_format<Ch, Tr>:: operator= (const basic_format& x) {
91         if(this == &x)
92             return *this;
93         (basic_format<Ch, Tr>(x)).swap(*this);
94         return *this;
95     }
96
97     template< class Ch, class Tr>
98     unsigned char basic_format<Ch,Tr>:: exceptions() const {
99         return exceptions_; 
100     }
101
102     template< class Ch, class Tr>
103     unsigned char basic_format<Ch,Tr>:: exceptions(unsigned char newexcept) { 
104         unsigned char swp = exceptions_; 
105         exceptions_ = newexcept; 
106         return swp; 
107     }
108
109     template<class Ch, class Tr>
110       void basic_format<Ch, Tr>:: make_or_reuse_data(std::size_t nbitems) {
111         Ch fill = oss_.widen(' ');
112         if(items_.size() == 0)
113             items_.assign( nbitems, format_item_t(fill) );
114         else {
115             bound_.resize(0);
116             items_.resize(nbitems, format_item_t(fill));
117             for(std::size_t i=0; i < nbitems; ++i)
118                 items_[i].reset(fill); //  strings are resized to "", instead of reallocated
119         }
120     }
121
122     template< class Ch, class Tr>
123     basic_format<Ch,Tr>& basic_format<Ch,Tr>:: clear() {
124         // empty the string buffers (except bound arguments)
125         // and make the format object ready for formatting a new set of arguments
126
127         BOOST_ASSERT( bound_.size()==0 || num_args_ == static_cast<int>(bound_.size()) );
128
129         for(unsigned long i=0; i<items_.size(); ++i) {
130             // clear converted strings only if the corresponding argument is not  bound :
131             if( bound_.size()==0 || !bound_[ items_[i].argN_ ] )  items_[i].res_.resize(0);
132         }
133         cur_arg_=0; dumped_=false;
134         // maybe first arg is bound:
135         if(bound_.size() != 0) {
136             while(cur_arg_ < num_args_ && bound_[cur_arg_] )
137                   ++cur_arg_;
138         }
139         return *this;
140     }
141
142     template< class Ch, class Tr>
143     basic_format<Ch,Tr>& basic_format<Ch,Tr>:: clear_binds() {
144         // remove all binds, then clear()
145         bound_.resize(0);
146         clear();
147         return *this;
148     }
149
150     template< class Ch, class Tr>
151     basic_format<Ch,Tr>& basic_format<Ch,Tr>:: clear_bind(int argN) {
152         // remove the bind of ONE argument then clear()
153
154         if(argN<1 || argN > num_args_ || bound_.size()==0 || !bound_[argN-1] ) {
155             if( exceptions() & io::out_of_range_bit )
156                 boost::throw_exception(io::out_of_range()); // arg not in range.
157             else return *this;
158         }
159         bound_[argN-1]=false;
160         clear();
161         return *this;
162     }
163
164
165
166     template< class Ch, class Tr>
167     std::basic_string<Ch,Tr> basic_format<Ch,Tr>:: str() const {
168         dumped_=true;
169         if(items_.size()==0)
170             return prefix_;
171         if( cur_arg_ < num_args_)
172             if( exceptions() & io::too_few_args_bit )
173                 boost::throw_exception(io::too_few_args()); // not enough variables supplied
174         unsigned long i;
175         string_t res;
176         res.reserve(size());
177         res += prefix_;
178         for(i=0; i < items_.size(); ++i) {
179             const format_item_t& item = items_[i];
180             res += item.res_;
181             if( item.argN_ == format_item_t::argN_tabulation) { 
182                 BOOST_ASSERT( item.pad_scheme_ & format_item_t::tabulation);
183                 std::streamsize  n = item.fmtstate_.width_ - res.size();
184                 if( n > 0 )
185                     res.append( n, item.fmtstate_.fill_ );
186             }
187             res += item.appendix_;
188         }
189         return res;
190     }
191     template< class Ch, class Tr>
192     typename basic_format<Ch, Tr>::size_type  basic_format<Ch,Tr>:: 
193     size () const {
194         std::streamsize sz = prefix_.size();
195         unsigned long i;
196         for(i=0; i < items_.size(); ++i) {
197             const format_item_t& item = items_[i];
198             sz += item.res_.size();
199             if( item.argN_ == format_item_t::argN_tabulation)
200                 sz = std::max(sz, item.fmtstate_.width_);
201             sz +=  + item.appendix_.size();
202         }
203         return static_cast<size_type> (sz);
204     }
205
206 namespace io {
207 namespace detail {
208
209     template<class Ch, class Tr, class T> 
210     basic_format<Ch, Tr>&  bind_arg_body( basic_format<Ch, Tr>& self, 
211                                           int argN, const T& val) {
212         // bind one argument to a fixed value
213         // this is persistent over clear() calls, thus also over str() and <<
214         if(self.dumped_) 
215             self.clear(); // needed because we will modify cur_arg_
216         if(argN<1 || argN > self.num_args_) {
217             if( self.exceptions() & io::out_of_range_bit )
218                 boost::throw_exception(io::out_of_range()); // arg not in range.
219             else return self;
220         }
221         if(self.bound_.size()==0) 
222             self.bound_.assign(self.num_args_,false);
223         else 
224             BOOST_ASSERT( self.num_args_ == static_cast<signed int>(self.bound_.size()) );
225         int o_cur_arg = self.cur_arg_;
226         self.cur_arg_ = argN-1; // arrays begin at 0
227
228         self.bound_[self.cur_arg_]=false; // if already set, we unset and re-sets..
229         self.operator%(val); // put val at the right place, because cur_arg is set
230     
231
232         // Now re-position cur_arg before leaving :
233         self.cur_arg_ = o_cur_arg; 
234         self.bound_[argN-1]=true;
235         if(self.cur_arg_ == argN-1 ) {
236             // hum, now this arg is bound, so move to next free arg
237             while(self.cur_arg_ < self.num_args_ && self.bound_[self.cur_arg_])   
238                 ++self.cur_arg_;
239         }
240         // In any case, we either have all args, or are on a non-binded arg :
241         BOOST_ASSERT( self.cur_arg_ >= self.num_args_ || ! self.bound_[self.cur_arg_]);
242         return self;
243     }
244
245     template<class Ch, class Tr, class T> 
246     basic_format<Ch, Tr>&  modify_item_body( basic_format<Ch, Tr>& self,
247                                              int itemN, T manipulator) {
248         // applies a manipulator to the format_item describing a given directive.
249         // this is a permanent change, clear or reset won't cancel that.
250         if(itemN<1 || itemN > static_cast<signed int>(self.items_.size() )) {
251             if( self.exceptions() & io::out_of_range_bit ) 
252                 boost::throw_exception(io::out_of_range()); // item not in range.
253             else return self;
254         }
255         self.items_[itemN-1].fmtstate_. template apply_manip<T> ( manipulator );
256         return self;
257     }
258
259 } // namespace detail
260 } // namespace io
261 } // namespace boost
262
263
264
265 #endif  // BOOST_FORMAT_IMPLEMENTATION_HPP