2 // Boost general library format ---------------------------
3 // See http://www.boost.org for updates, documentation, and revision history.
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.
12 // ideas taken from RĂ¼diger Loos's format class
13 // and Karl Nelson's ofstream
15 // ----------------------------------------------------------------------------
16 // format_implementation.hpp Implementation of the basic_format class
17 // ----------------------------------------------------------------------------
20 #ifndef BOOST_FORMAT_IMPLEMENTATION_HPP
21 #define BOOST_FORMAT_IMPLEMENTATION_HPP
23 #include <boost/throw_exception.hpp>
24 #include <boost/assert.hpp>
25 #include <boost/format/format_class.hpp>
29 // -------- format:: -------------------------------------------
30 template< class Ch, class Tr>
31 basic_format<Ch, Tr> ::basic_format(const Ch* str)
32 : style_(0), cur_arg_(0), num_args_(0), dumped_(false),
33 items_(), oss_(), exceptions_(io::all_error_bits)
35 state0_.set_by_stream(oss_);
37 if( !str) str = emptyStr.c_str();
41 #ifndef BOOST_NO_STD_LOCALE
42 template< class Ch, class Tr>
43 basic_format<Ch, Tr> ::basic_format(const Ch* str, const std::locale & loc)
44 : style_(0), cur_arg_(0), num_args_(0), dumped_(false),
45 items_(), oss_(), exceptions_(io::all_error_bits)
48 state0_.set_by_stream(oss_);
50 if( !str) str = emptyStr.c_str();
54 template< class Ch, class Tr>
55 basic_format<Ch, Tr> ::basic_format(const string_t& s, const std::locale & loc)
56 : style_(0), cur_arg_(0), num_args_(0), dumped_(false),
57 items_(), oss_(), exceptions_(io::all_error_bits)
60 state0_.set_by_stream(oss_);
63 #endif //BOOST_NO_STD_LOCALE
65 template< class Ch, class Tr>
66 basic_format<Ch, Tr> ::basic_format(const string_t& s)
67 : style_(0), cur_arg_(0), num_args_(0), dumped_(false),
68 items_(), oss_(), exceptions_(io::all_error_bits)
70 state0_.set_by_stream(oss_);
74 template< class Ch, class Tr>
75 basic_format<Ch, Tr> :: basic_format(const basic_format& x)
76 : style_(x.style_), cur_arg_(x.cur_arg_), num_args_(x.num_args_), dumped_(false),
77 items_(x.items_), prefix_(x.prefix_), bound_(x.bound_),
78 oss_(), // <- we obviously can't copy x.oss_
79 state0_(x.state0_), exceptions_(x.exceptions_)
81 state0_.apply_on(oss_);
84 template< class Ch, class Tr>
85 basic_format<Ch, Tr>& basic_format<Ch, Tr> ::operator= (const basic_format& x)
90 state0_.apply_on(oss_);
92 // plus all the other (trivial) assignments :
93 exceptions_ = x.exceptions_;
99 num_args_=x.num_args_;
105 template< class Ch, class Tr>
106 unsigned char basic_format<Ch,Tr> ::exceptions() const
111 template< class Ch, class Tr>
112 unsigned char basic_format<Ch,Tr> ::exceptions(unsigned char newexcept)
114 unsigned char swp = exceptions_;
115 exceptions_ = newexcept;
120 template< class Ch, class Tr>
121 basic_format<Ch,Tr>& basic_format<Ch,Tr> ::clear()
122 // empty the string buffers (except bound arguments, see clear_binds() )
123 // and make the format object ready for formatting a new set of arguments
125 BOOST_ASSERT( bound_.size()==0 || num_args_ == static_cast<int>(bound_.size()) );
127 for(unsigned long i=0; i<items_.size(); ++i){
128 items_[i].state_ = items_[i].ref_state_;
129 // clear converted strings only if the corresponding argument is not bound :
130 if( bound_.size()==0 || !bound_[ items_[i].argN_ ] ) items_[i].res_.resize(0);
132 cur_arg_=0; dumped_=false;
133 // maybe first arg is bound:
134 if(bound_.size() != 0)
136 while(cur_arg_ < num_args_ && bound_[cur_arg_] ) ++cur_arg_;
141 template< class Ch, class Tr>
142 basic_format<Ch,Tr>& basic_format<Ch,Tr> ::clear_binds()
143 // cancel all bindings, and clear()
150 template< class Ch, class Tr>
151 basic_format<Ch,Tr>& basic_format<Ch,Tr> ::clear_bind(int argN)
152 // cancel the binding of ONE argument, and clear()
154 if(argN<1 || argN > num_args_ || bound_.size()==0 || !bound_[argN-1] )
156 if( exceptions() & io::out_of_range_bit )
157 boost::throw_exception(io::out_of_range()); // arg not in range.
160 bound_[argN-1]=false;
167 template< class Ch, class Tr>
168 std::basic_string<Ch,Tr> basic_format<Ch,Tr> ::str() const
173 if( cur_arg_ < num_args_)
174 if( exceptions() & io::too_few_args_bit )
175 boost::throw_exception(io::too_few_args()); // not enough variables have been supplied !
177 unsigned long sz = prefix_.size();
179 for(i=0; i < items_.size(); ++i)
180 sz += items_[i].res_.size() + items_[i].appendix_.size();
185 for(i=0; i < items_.size(); ++i)
187 const format_item_t& item = items_[i];
189 if( item.argN_ == format_item_t::argN_tabulation)
191 BOOST_ASSERT( item.pad_scheme_ & format_item_t::tabulation);
192 std::streamsize n = item.state_.width_ - res.size();
194 res.append( n, item.state_.fill_ );
196 res += item.appendix_;
204 template<class Ch, class Tr, class T>
205 basic_format<Ch, Tr>& bind_arg_body( basic_format<Ch, Tr>& self,
208 // bind one argument to a fixed value
209 // this is persistent over clear() calls, thus also over str() and <<
211 if(self.dumped_) self.clear(); // needed, because we will modify cur_arg_..
212 if(argN<1 || argN > self.num_args_)
214 if( self.exceptions() & io::out_of_range_bit )
215 boost::throw_exception(io::out_of_range()); // arg not in range.
218 if(self.bound_.size()==0)
219 self.bound_.assign(self.num_args_,false);
221 BOOST_ASSERT( self.num_args_ == static_cast<signed int>(self.bound_.size()) );
222 int o_cur_arg = self.cur_arg_;
223 self.cur_arg_ = argN-1; // arrays begin at 0
225 self.bound_[self.cur_arg_]=false; // if already set, we unset and re-sets..
226 self.operator%(val); // put val at the right place, because cur_arg is set
229 // Now re-position cur_arg before leaving :
230 self.cur_arg_ = o_cur_arg;
231 self.bound_[argN-1]=true;
232 if(self.cur_arg_ == argN-1 )
233 // hum, now this arg is bound, so move to next free arg
235 while(self.cur_arg_ < self.num_args_ && self.bound_[self.cur_arg_]) ++self.cur_arg_;
237 // In any case, we either have all args, or are on a non-binded arg :
238 BOOST_ASSERT( self.cur_arg_ >= self.num_args_ || ! self.bound_[self.cur_arg_]);
242 template<class Ch, class Tr, class T>
243 basic_format<Ch, Tr>& modify_item_body( basic_format<Ch, Tr>& self,
245 const T& manipulator)
246 // applies a manipulator to the format_item describing a given directive.
247 // this is a permanent change, clear or clear_binds won't cancel that.
249 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.
255 self.items_[itemN-1].ref_state_.apply_manip( manipulator );
256 self.items_[itemN-1].state_ = self.items_[itemN-1].ref_state_;
260 } // namespace detail
268 #endif // BOOST_FORMAT_IMPLEMENTATION_HPP