Chris@16
|
1 // ----------------------------------------------------------------------------
|
Chris@16
|
2 // format_implementation.hpp Implementation of the basic_format class
|
Chris@16
|
3 // ----------------------------------------------------------------------------
|
Chris@16
|
4
|
Chris@16
|
5 // Copyright Samuel Krempp 2003. Use, modification, and distribution are
|
Chris@16
|
6 // subject to the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
7 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
8
|
Chris@16
|
9 // See http://www.boost.org/libs/format for library home page
|
Chris@16
|
10
|
Chris@16
|
11
|
Chris@16
|
12 // ----------------------------------------------------------------------------
|
Chris@16
|
13
|
Chris@16
|
14 #ifndef BOOST_FORMAT_IMPLEMENTATION_HPP
|
Chris@16
|
15 #define BOOST_FORMAT_IMPLEMENTATION_HPP
|
Chris@16
|
16
|
Chris@16
|
17 #include <boost/config.hpp>
|
Chris@16
|
18 #include <boost/throw_exception.hpp>
|
Chris@16
|
19 #include <boost/assert.hpp>
|
Chris@16
|
20 #include <boost/format/format_class.hpp>
|
Chris@16
|
21 #include <algorithm> // std::swap
|
Chris@16
|
22
|
Chris@16
|
23 namespace boost {
|
Chris@16
|
24
|
Chris@16
|
25 // --- basic_format implementation -----------------------------------------//
|
Chris@16
|
26
|
Chris@16
|
27 template< class Ch, class Tr, class Alloc>
|
Chris@16
|
28 basic_format<Ch, Tr, Alloc>:: basic_format(const Ch* s)
|
Chris@16
|
29 : style_(0), cur_arg_(0), num_args_(0), dumped_(false),
|
Chris@16
|
30 exceptions_(io::all_error_bits)
|
Chris@16
|
31 {
|
Chris@16
|
32 if( s)
|
Chris@16
|
33 parse( s );
|
Chris@16
|
34 }
|
Chris@16
|
35
|
Chris@16
|
36 #if !defined(BOOST_NO_STD_LOCALE)
|
Chris@16
|
37 template< class Ch, class Tr, class Alloc>
|
Chris@16
|
38 basic_format<Ch, Tr, Alloc>:: basic_format(const Ch* s, const std::locale & loc)
|
Chris@16
|
39 : style_(0), cur_arg_(0), num_args_(0), dumped_(false),
|
Chris@16
|
40 exceptions_(io::all_error_bits), loc_(loc)
|
Chris@16
|
41 {
|
Chris@16
|
42 if(s) parse( s );
|
Chris@16
|
43 }
|
Chris@16
|
44
|
Chris@16
|
45 template< class Ch, class Tr, class Alloc>
|
Chris@16
|
46 basic_format<Ch, Tr, Alloc>:: basic_format(const string_type& s, const std::locale & loc)
|
Chris@16
|
47 : style_(0), cur_arg_(0), num_args_(0), dumped_(false),
|
Chris@16
|
48 exceptions_(io::all_error_bits), loc_(loc)
|
Chris@16
|
49 {
|
Chris@16
|
50 parse(s);
|
Chris@16
|
51 }
|
Chris@16
|
52 #endif // ! BOOST_NO_STD_LOCALE
|
Chris@16
|
53 template< class Ch, class Tr, class Alloc>
|
Chris@16
|
54 io::detail::locale_t basic_format<Ch, Tr, Alloc>::
|
Chris@16
|
55 getloc() const {
|
Chris@16
|
56 return loc_ ? loc_.get() : io::detail::locale_t();
|
Chris@16
|
57 }
|
Chris@16
|
58
|
Chris@16
|
59 template< class Ch, class Tr, class Alloc>
|
Chris@16
|
60 basic_format<Ch, Tr, Alloc>:: basic_format(const string_type& s)
|
Chris@16
|
61 : style_(0), cur_arg_(0), num_args_(0), dumped_(false),
|
Chris@16
|
62 exceptions_(io::all_error_bits)
|
Chris@16
|
63 {
|
Chris@16
|
64 parse(s);
|
Chris@16
|
65 }
|
Chris@16
|
66
|
Chris@16
|
67 template< class Ch, class Tr, class Alloc> // just don't copy the buf_ member
|
Chris@16
|
68 basic_format<Ch, Tr, Alloc>:: basic_format(const basic_format& x)
|
Chris@16
|
69 : items_(x.items_), bound_(x.bound_), style_(x.style_),
|
Chris@16
|
70 cur_arg_(x.cur_arg_), num_args_(x.num_args_), dumped_(x.dumped_),
|
Chris@16
|
71 prefix_(x.prefix_), exceptions_(x.exceptions_), loc_(x.loc_)
|
Chris@16
|
72 {
|
Chris@16
|
73 }
|
Chris@16
|
74
|
Chris@16
|
75 template< class Ch, class Tr, class Alloc> // just don't copy the buf_ member
|
Chris@16
|
76 basic_format<Ch, Tr, Alloc>& basic_format<Ch, Tr, Alloc>::
|
Chris@16
|
77 operator= (const basic_format& x) {
|
Chris@16
|
78 if(this == &x)
|
Chris@16
|
79 return *this;
|
Chris@16
|
80 (basic_format<Ch, Tr, Alloc>(x)).swap(*this);
|
Chris@16
|
81 return *this;
|
Chris@16
|
82 }
|
Chris@16
|
83 template< class Ch, class Tr, class Alloc>
|
Chris@16
|
84 void basic_format<Ch, Tr, Alloc>::
|
Chris@16
|
85 swap (basic_format & x) {
|
Chris@16
|
86 std::swap(exceptions_, x.exceptions_);
|
Chris@16
|
87 std::swap(style_, x.style_);
|
Chris@16
|
88 std::swap(cur_arg_, x.cur_arg_);
|
Chris@16
|
89 std::swap(num_args_, x.num_args_);
|
Chris@16
|
90 std::swap(dumped_, x.dumped_);
|
Chris@16
|
91
|
Chris@16
|
92 items_.swap(x.items_);
|
Chris@16
|
93 prefix_.swap(x.prefix_);
|
Chris@16
|
94 bound_.swap(x.bound_);
|
Chris@16
|
95 }
|
Chris@16
|
96
|
Chris@16
|
97 template< class Ch, class Tr, class Alloc>
|
Chris@16
|
98 unsigned char basic_format<Ch,Tr, Alloc>:: exceptions() const {
|
Chris@16
|
99 return exceptions_;
|
Chris@16
|
100 }
|
Chris@16
|
101
|
Chris@16
|
102 template< class Ch, class Tr, class Alloc>
|
Chris@16
|
103 unsigned char basic_format<Ch,Tr, Alloc>:: exceptions(unsigned char newexcept) {
|
Chris@16
|
104 unsigned char swp = exceptions_;
|
Chris@16
|
105 exceptions_ = newexcept;
|
Chris@16
|
106 return swp;
|
Chris@16
|
107 }
|
Chris@16
|
108
|
Chris@16
|
109 template<class Ch, class Tr, class Alloc>
|
Chris@16
|
110 void basic_format<Ch, Tr, Alloc>::
|
Chris@16
|
111 make_or_reuse_data (std::size_t nbitems) {
|
Chris@16
|
112 #if !defined(BOOST_NO_STD_LOCALE)
|
Chris@16
|
113 Ch fill = ( BOOST_USE_FACET(std::ctype<Ch>, getloc()) ). widen(' ');
|
Chris@16
|
114 #else
|
Chris@16
|
115 Ch fill = ' ';
|
Chris@16
|
116 #endif
|
Chris@16
|
117 if(items_.size() == 0)
|
Chris@16
|
118 items_.assign( nbitems, format_item_t(fill) );
|
Chris@16
|
119 else {
|
Chris@16
|
120 if(nbitems>items_.size())
|
Chris@16
|
121 items_.resize(nbitems, format_item_t(fill));
|
Chris@16
|
122 bound_.resize(0);
|
Chris@16
|
123 for(std::size_t i=0; i < nbitems; ++i)
|
Chris@16
|
124 items_[i].reset(fill); // strings are resized, instead of reallocated
|
Chris@16
|
125 }
|
Chris@16
|
126 prefix_.resize(0);
|
Chris@16
|
127 }
|
Chris@16
|
128
|
Chris@16
|
129 template< class Ch, class Tr, class Alloc>
|
Chris@16
|
130 basic_format<Ch,Tr, Alloc>& basic_format<Ch,Tr, Alloc>::
|
Chris@16
|
131 clear () {
|
Chris@16
|
132 // empty the string buffers (except bound arguments)
|
Chris@16
|
133 // and make the format object ready for formatting a new set of arguments
|
Chris@16
|
134
|
Chris@16
|
135 BOOST_ASSERT( bound_.size()==0 || num_args_ == static_cast<int>(bound_.size()) );
|
Chris@16
|
136
|
Chris@16
|
137 for(unsigned long i=0; i<items_.size(); ++i) {
|
Chris@16
|
138 // clear converted strings only if the corresponding argument is not bound :
|
Chris@16
|
139 if( bound_.size()==0 || items_[i].argN_<0 || !bound_[ items_[i].argN_ ] )
|
Chris@16
|
140 items_[i].res_.resize(0);
|
Chris@16
|
141 }
|
Chris@16
|
142 cur_arg_=0; dumped_=false;
|
Chris@16
|
143 // maybe first arg is bound:
|
Chris@16
|
144 if(bound_.size() != 0) {
|
Chris@16
|
145 for(; cur_arg_ < num_args_ && bound_[cur_arg_]; ++cur_arg_)
|
Chris@16
|
146 {}
|
Chris@16
|
147 }
|
Chris@16
|
148 return *this;
|
Chris@16
|
149 }
|
Chris@16
|
150
|
Chris@16
|
151 template< class Ch, class Tr, class Alloc>
|
Chris@16
|
152 basic_format<Ch,Tr, Alloc>& basic_format<Ch,Tr, Alloc>::
|
Chris@16
|
153 clear_binds () {
|
Chris@16
|
154 // remove all binds, then clear()
|
Chris@16
|
155 bound_.resize(0);
|
Chris@16
|
156 clear();
|
Chris@16
|
157 return *this;
|
Chris@16
|
158 }
|
Chris@16
|
159
|
Chris@16
|
160 template< class Ch, class Tr, class Alloc>
|
Chris@16
|
161 basic_format<Ch,Tr, Alloc>& basic_format<Ch,Tr, Alloc>::
|
Chris@16
|
162 clear_bind (int argN) {
|
Chris@16
|
163 // remove the bind of ONE argument then clear()
|
Chris@16
|
164 if(argN<1 || argN > num_args_ || bound_.size()==0 || !bound_[argN-1] ) {
|
Chris@16
|
165 if( exceptions() & io::out_of_range_bit)
|
Chris@16
|
166 boost::throw_exception(io::out_of_range(argN, 1, num_args_+1 ) );
|
Chris@16
|
167 else return *this;
|
Chris@16
|
168 }
|
Chris@16
|
169 bound_[argN-1]=false;
|
Chris@16
|
170 clear();
|
Chris@16
|
171 return *this;
|
Chris@16
|
172 }
|
Chris@16
|
173
|
Chris@16
|
174 template< class Ch, class Tr, class Alloc>
|
Chris@16
|
175 int basic_format<Ch,Tr, Alloc>::
|
Chris@16
|
176 bound_args() const {
|
Chris@16
|
177 if(bound_.size()==0)
|
Chris@16
|
178 return 0;
|
Chris@16
|
179 int n=0;
|
Chris@16
|
180 for(int i=0; i<num_args_ ; ++i)
|
Chris@16
|
181 if(bound_[i])
|
Chris@16
|
182 ++n;
|
Chris@16
|
183 return n;
|
Chris@16
|
184 }
|
Chris@16
|
185
|
Chris@16
|
186 template< class Ch, class Tr, class Alloc>
|
Chris@16
|
187 int basic_format<Ch,Tr, Alloc>::
|
Chris@16
|
188 fed_args() const {
|
Chris@16
|
189 if(bound_.size()==0)
|
Chris@16
|
190 return cur_arg_;
|
Chris@16
|
191 int n=0;
|
Chris@16
|
192 for(int i=0; i<cur_arg_ ; ++i)
|
Chris@16
|
193 if(!bound_[i])
|
Chris@16
|
194 ++n;
|
Chris@16
|
195 return n;
|
Chris@16
|
196 }
|
Chris@16
|
197
|
Chris@16
|
198 template< class Ch, class Tr, class Alloc>
|
Chris@16
|
199 int basic_format<Ch,Tr, Alloc>::
|
Chris@16
|
200 cur_arg() const {
|
Chris@16
|
201 return cur_arg_+1; }
|
Chris@16
|
202
|
Chris@16
|
203 template< class Ch, class Tr, class Alloc>
|
Chris@16
|
204 int basic_format<Ch,Tr, Alloc>::
|
Chris@16
|
205 remaining_args() const {
|
Chris@16
|
206 if(bound_.size()==0)
|
Chris@16
|
207 return num_args_-cur_arg_;
|
Chris@16
|
208 int n=0;
|
Chris@16
|
209 for(int i=cur_arg_; i<num_args_ ; ++i)
|
Chris@16
|
210 if(!bound_[i])
|
Chris@16
|
211 ++n;
|
Chris@16
|
212 return n;
|
Chris@16
|
213 }
|
Chris@16
|
214
|
Chris@16
|
215 template< class Ch, class Tr, class Alloc>
|
Chris@16
|
216 typename basic_format<Ch, Tr, Alloc>::string_type
|
Chris@16
|
217 basic_format<Ch,Tr, Alloc>::
|
Chris@16
|
218 str () const {
|
Chris@16
|
219 if(items_.size()==0)
|
Chris@16
|
220 return prefix_;
|
Chris@16
|
221 if( cur_arg_ < num_args_)
|
Chris@16
|
222 if( exceptions() & io::too_few_args_bit )
|
Chris@16
|
223 // not enough variables supplied
|
Chris@16
|
224 boost::throw_exception(io::too_few_args(cur_arg_, num_args_));
|
Chris@16
|
225
|
Chris@16
|
226 unsigned long i;
|
Chris@16
|
227 string_type res;
|
Chris@16
|
228 res.reserve(size());
|
Chris@16
|
229 res += prefix_;
|
Chris@16
|
230 for(i=0; i < items_.size(); ++i) {
|
Chris@16
|
231 const format_item_t& item = items_[i];
|
Chris@16
|
232 res += item.res_;
|
Chris@16
|
233 if( item.argN_ == format_item_t::argN_tabulation) {
|
Chris@16
|
234 BOOST_ASSERT( item.pad_scheme_ & format_item_t::tabulation);
|
Chris@16
|
235 if( static_cast<size_type>(item.fmtstate_.width_) > res.size() )
|
Chris@16
|
236 res.append( static_cast<size_type>(item.fmtstate_.width_) - res.size(),
|
Chris@16
|
237 item.fmtstate_.fill_ );
|
Chris@16
|
238 }
|
Chris@16
|
239 res += item.appendix_;
|
Chris@16
|
240 }
|
Chris@16
|
241 dumped_=true;
|
Chris@16
|
242 return res;
|
Chris@16
|
243 }
|
Chris@16
|
244 template< class Ch, class Tr, class Alloc>
|
Chris@16
|
245 typename std::basic_string<Ch, Tr, Alloc>::size_type basic_format<Ch,Tr, Alloc>::
|
Chris@16
|
246 size () const {
|
Chris@16
|
247 #ifdef BOOST_MSVC
|
Chris@16
|
248 // If std::min<unsigned> or std::max<unsigned> are already instantiated
|
Chris@16
|
249 // at this point then we get a blizzard of warning messages when we call
|
Chris@16
|
250 // those templates with std::size_t as arguments. Weird and very annoyning...
|
Chris@16
|
251 #pragma warning(push)
|
Chris@16
|
252 #pragma warning(disable:4267)
|
Chris@16
|
253 #endif
|
Chris@16
|
254 BOOST_USING_STD_MAX();
|
Chris@16
|
255 size_type sz = prefix_.size();
|
Chris@16
|
256 unsigned long i;
|
Chris@16
|
257 for(i=0; i < items_.size(); ++i) {
|
Chris@16
|
258 const format_item_t& item = items_[i];
|
Chris@16
|
259 sz += item.res_.size();
|
Chris@16
|
260 if( item.argN_ == format_item_t::argN_tabulation)
|
Chris@16
|
261 sz = max BOOST_PREVENT_MACRO_SUBSTITUTION (sz,
|
Chris@16
|
262 static_cast<size_type>(item.fmtstate_.width_) );
|
Chris@16
|
263 sz += item.appendix_.size();
|
Chris@16
|
264 }
|
Chris@16
|
265 return sz;
|
Chris@16
|
266 #ifdef BOOST_MSVC
|
Chris@16
|
267 #pragma warning(pop)
|
Chris@16
|
268 #endif
|
Chris@16
|
269 }
|
Chris@16
|
270
|
Chris@16
|
271 namespace io {
|
Chris@16
|
272 namespace detail {
|
Chris@16
|
273
|
Chris@16
|
274 template<class Ch, class Tr, class Alloc, class T>
|
Chris@16
|
275 basic_format<Ch, Tr, Alloc>&
|
Chris@16
|
276 bind_arg_body (basic_format<Ch, Tr, Alloc>& self, int argN, const T& val) {
|
Chris@16
|
277 // bind one argument to a fixed value
|
Chris@16
|
278 // this is persistent over clear() calls, thus also over str() and <<
|
Chris@16
|
279 if(self.dumped_)
|
Chris@16
|
280 self.clear(); // needed because we will modify cur_arg_
|
Chris@16
|
281 if(argN<1 || argN > self.num_args_) {
|
Chris@16
|
282 if( self.exceptions() & io::out_of_range_bit )
|
Chris@16
|
283 boost::throw_exception(io::out_of_range(argN, 1, self.num_args_+1 ) );
|
Chris@16
|
284 else return self;
|
Chris@16
|
285 }
|
Chris@16
|
286 if(self.bound_.size()==0)
|
Chris@16
|
287 self.bound_.assign(self.num_args_,false);
|
Chris@16
|
288 else
|
Chris@16
|
289 BOOST_ASSERT( self.num_args_ == static_cast<signed int>(self.bound_.size()) );
|
Chris@16
|
290 int o_cur_arg = self.cur_arg_;
|
Chris@16
|
291 self.cur_arg_ = argN-1; // arrays begin at 0
|
Chris@16
|
292
|
Chris@16
|
293 self.bound_[self.cur_arg_]=false; // if already set, we unset and re-sets..
|
Chris@16
|
294 self.operator%(val); // put val at the right place, because cur_arg is set
|
Chris@16
|
295
|
Chris@16
|
296
|
Chris@16
|
297 // Now re-position cur_arg before leaving :
|
Chris@16
|
298 self.cur_arg_ = o_cur_arg;
|
Chris@16
|
299 self.bound_[argN-1]=true;
|
Chris@16
|
300 if(self.cur_arg_ == argN-1 ) {
|
Chris@16
|
301 // hum, now this arg is bound, so move to next free arg
|
Chris@16
|
302 while(self.cur_arg_ < self.num_args_ && self.bound_[self.cur_arg_])
|
Chris@16
|
303 ++self.cur_arg_;
|
Chris@16
|
304 }
|
Chris@16
|
305 // In any case, we either have all args, or are on an unbound arg :
|
Chris@16
|
306 BOOST_ASSERT( self.cur_arg_ >= self.num_args_ || ! self.bound_[self.cur_arg_]);
|
Chris@16
|
307 return self;
|
Chris@16
|
308 }
|
Chris@16
|
309
|
Chris@16
|
310 template<class Ch, class Tr, class Alloc, class T> basic_format<Ch, Tr, Alloc>&
|
Chris@16
|
311 modify_item_body (basic_format<Ch, Tr, Alloc>& self, int itemN, T manipulator) {
|
Chris@16
|
312 // applies a manipulator to the format_item describing a given directive.
|
Chris@16
|
313 // this is a permanent change, clear or reset won't cancel that.
|
Chris@16
|
314 if(itemN<1 || itemN > static_cast<signed int>(self.items_.size() )) {
|
Chris@16
|
315 if( self.exceptions() & io::out_of_range_bit )
|
Chris@16
|
316 boost::throw_exception(io::out_of_range(itemN, 1, static_cast<int>(self.items_.size()) ));
|
Chris@16
|
317 else return self;
|
Chris@16
|
318 }
|
Chris@16
|
319 self.items_[itemN-1].fmtstate_. template apply_manip<T> ( manipulator );
|
Chris@16
|
320 return self;
|
Chris@16
|
321 }
|
Chris@16
|
322
|
Chris@16
|
323 } // namespace detail
|
Chris@16
|
324 } // namespace io
|
Chris@16
|
325 } // namespace boost
|
Chris@16
|
326
|
Chris@16
|
327
|
Chris@16
|
328
|
Chris@16
|
329 #endif // BOOST_FORMAT_IMPLEMENTATION_HPP
|