Chris@16: // ---------------------------------------------------------------------------- Chris@16: // feed_args.hpp : functions for processing each argument Chris@16: // (feed, feed_manip, and distribute) Chris@16: // ---------------------------------------------------------------------------- Chris@16: Chris@16: // Copyright Samuel Krempp 2003. Use, modification, and distribution are Chris@16: // subject to the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: // See http://www.boost.org/libs/format for library home page Chris@16: Chris@16: // ---------------------------------------------------------------------------- Chris@16: Chris@16: #ifndef BOOST_FORMAT_FEED_ARGS_HPP Chris@16: #define BOOST_FORMAT_FEED_ARGS_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace io { Chris@16: namespace detail { Chris@16: Chris@16: template Chris@16: void mk_str( std::basic_string & res, Chris@16: const Ch * beg, Chris@16: typename std::basic_string::size_type size, Chris@16: std::streamsize w, Chris@16: const Ch fill_char, Chris@16: std::ios_base::fmtflags f, Chris@16: const Ch prefix_space, // 0 if no space-padding Chris@16: bool center) Chris@16: // applies centered/left/right padding to the string [beg, beg+size[ Chris@16: // Effects : the result is placed in res. Chris@16: { Chris@16: typedef typename std::basic_string::size_type size_type; Chris@16: res.resize(0); Chris@16: if(w<=0 || static_cast(w) <=size) { Chris@16: // no need to pad. Chris@16: res.reserve(size + !!prefix_space); Chris@16: if(prefix_space) Chris@16: res.append(1, prefix_space); Chris@16: if (size) Chris@16: res.append(beg, size); Chris@16: } Chris@16: else { Chris@16: std::streamsize n=static_cast(w-size-!!prefix_space); Chris@16: std::streamsize n_after = 0, n_before = 0; Chris@16: res.reserve(static_cast(w)); // allocate once for the 2 inserts Chris@16: if(center) Chris@16: n_after = n/2, n_before = n - n_after; Chris@16: else Chris@16: if(f & std::ios_base::left) Chris@16: n_after = n; Chris@16: else Chris@16: n_before = n; Chris@16: // now make the res string : Chris@16: if(n_before) res.append(static_cast(n_before), fill_char); Chris@16: if(prefix_space) Chris@16: res.append(1, prefix_space); Chris@16: if (size) Chris@16: res.append(beg, size); Chris@16: if(n_after) res.append(static_cast(n_after), fill_char); Chris@16: } Chris@16: } // -mk_str(..) Chris@16: Chris@16: Chris@101: #if BOOST_WORKAROUND(__DECCXX_VER, BOOST_TESTED_AT(60590042)) Chris@101: // __DECCXX needs to be tricked to disambiguate this simple overload.. Chris@16: // the trick is in "boost/format/msvc_disambiguater.hpp" Chris@16: Chris@16: template< class Ch, class Tr, class T> inline Chris@16: void put_head (BOOST_IO_STD basic_ostream & os, const T& x ) { Chris@16: disambiguater::put_head(os, x, 1L); Chris@16: } Chris@16: template< class Ch, class Tr, class T> inline Chris@16: void put_last (BOOST_IO_STD basic_ostream & os, const T& x ) { Chris@16: disambiguater::put_last(os, x, 1L); Chris@16: } Chris@16: Chris@16: #else Chris@16: Chris@16: template< class Ch, class Tr, class T> inline Chris@16: void put_head (BOOST_IO_STD basic_ostream &, const T& ) { Chris@16: } Chris@16: Chris@16: template< class Ch, class Tr, class T> inline Chris@16: void put_head( BOOST_IO_STD basic_ostream & os, const group1& x ) { Chris@16: os << group_head(x.a1_); // send the first N-1 items, not the last Chris@16: } Chris@16: Chris@16: template< class Ch, class Tr, class T> inline Chris@16: void put_last( BOOST_IO_STD basic_ostream & os, const T& x ) { Chris@16: os << x ; Chris@16: } Chris@16: Chris@16: template< class Ch, class Tr, class T> inline Chris@16: void put_last( BOOST_IO_STD basic_ostream & os, const group1& x ) { Chris@16: os << group_last(x.a1_); // this selects the last element Chris@16: } Chris@16: Chris@16: #ifndef BOOST_NO_OVERLOAD_FOR_NON_CONST Chris@16: template< class Ch, class Tr, class T> inline Chris@16: void put_head( BOOST_IO_STD basic_ostream &, T& ) { Chris@16: } Chris@16: Chris@16: template< class Ch, class Tr, class T> inline Chris@16: void put_last( BOOST_IO_STD basic_ostream & os, T& x) { Chris@16: os << x ; Chris@16: } Chris@16: #endif Chris@101: #endif // -__DECCXX workaround Chris@101: Chris@101: template< class Ch, class Tr, class T> Chris@101: void call_put_head(BOOST_IO_STD basic_ostream & os, const void* x) { Chris@101: put_head(os, *(typename ::boost::remove_reference::type*)x); Chris@101: } Chris@101: Chris@101: template< class Ch, class Tr, class T> Chris@101: void call_put_last(BOOST_IO_STD basic_ostream & os, const void* x) { Chris@101: put_last(os, *(T*)x); Chris@101: } Chris@101: Chris@101: template< class Ch, class Tr> Chris@101: struct put_holder { Chris@101: template Chris@101: put_holder(T& t) Chris@101: : arg(&t), Chris@101: put_head(&call_put_head), Chris@101: put_last(&call_put_last) Chris@101: {} Chris@101: const void* arg; Chris@101: void (*put_head)(BOOST_IO_STD basic_ostream & os, const void* x); Chris@101: void (*put_last)(BOOST_IO_STD basic_ostream & os, const void* x); Chris@101: }; Chris@101: Chris@101: template< class Ch, class Tr> inline Chris@101: void put_head( BOOST_IO_STD basic_ostream & os, const put_holder& t) { Chris@101: t.put_head(os, t.arg); Chris@101: } Chris@101: Chris@101: template< class Ch, class Tr> inline Chris@101: void put_last( BOOST_IO_STD basic_ostream & os, const put_holder& t) { Chris@101: t.put_last(os, t.arg); Chris@101: } Chris@16: Chris@16: Chris@16: template< class Ch, class Tr, class Alloc, class T> Chris@16: void put( T x, Chris@16: const format_item& specs, Chris@16: typename basic_format::string_type& res, Chris@16: typename basic_format::internal_streambuf_t & buf, Chris@16: io::detail::locale_t *loc_p = NULL) Chris@16: { Chris@16: #ifdef BOOST_MSVC Chris@16: // If std::min or std::max are already instantiated Chris@16: // at this point then we get a blizzard of warning messages when we call Chris@16: // those templates with std::size_t as arguments. Weird and very annoyning... Chris@16: #pragma warning(push) Chris@16: #pragma warning(disable:4267) Chris@16: #endif Chris@16: // does the actual conversion of x, with given params, into a string Chris@16: // using the supplied stringbuf. Chris@16: Chris@16: typedef typename basic_format::string_type string_type; Chris@16: typedef typename basic_format::format_item_t format_item_t; Chris@16: typedef typename string_type::size_type size_type; Chris@16: Chris@16: basic_oaltstringstream oss( &buf); Chris@16: specs.fmtstate_.apply_on(oss, loc_p); Chris@16: Chris@16: // the stream format state can be modified by manipulators in the argument : Chris@16: put_head( oss, x ); Chris@16: // in case x is a group, apply the manip part of it, Chris@16: // in order to find width Chris@16: Chris@16: const std::ios_base::fmtflags fl=oss.flags(); Chris@16: const bool internal = (fl & std::ios_base::internal) != 0; Chris@16: const std::streamsize w = oss.width(); Chris@16: const bool two_stepped_padding= internal && (w!=0); Chris@16: Chris@16: res.resize(0); Chris@16: if(! two_stepped_padding) { Chris@16: if(w>0) // handle padding via mk_str, not natively in stream Chris@16: oss.width(0); Chris@16: put_last( oss, x); Chris@16: const Ch * res_beg = buf.pbase(); Chris@16: Ch prefix_space = 0; Chris@16: if(specs.pad_scheme_ & format_item_t::spacepad) Chris@16: if(buf.pcount()== 0 || Chris@16: (res_beg[0] !=oss.widen('+') && res_beg[0] !=oss.widen('-') )) Chris@16: prefix_space = oss.widen(' '); Chris@16: size_type res_size = (std::min)( Chris@16: static_cast(specs.truncate_ - !!prefix_space), Chris@16: buf.pcount() ); Chris@16: mk_str(res, res_beg, res_size, w, oss.fill(), fl, Chris@16: prefix_space, (specs.pad_scheme_ & format_item_t::centered) !=0 ); Chris@16: } Chris@16: else { // 2-stepped padding Chris@16: // internal can be implied by zeropad, or user-set. Chris@16: // left, right, and centered alignment overrule internal, Chris@16: // but spacepad or truncate might be mixed with internal (using manipulator) Chris@16: put_last( oss, x); // may pad Chris@16: const Ch * res_beg = buf.pbase(); Chris@16: size_type res_size = buf.pcount(); Chris@16: bool prefix_space=false; Chris@16: if(specs.pad_scheme_ & format_item_t::spacepad) Chris@16: if(buf.pcount()== 0 || Chris@16: (res_beg[0] !=oss.widen('+') && res_beg[0] !=oss.widen('-') )) Chris@16: prefix_space = true; Chris@16: if(res_size == static_cast(w) && w<=specs.truncate_ && !prefix_space) { Chris@16: // okay, only one thing was printed and padded, so res is fine Chris@16: res.assign(res_beg, res_size); Chris@16: } Chris@16: else { // length w exceeded Chris@16: // either it was multi-output with first output padding up all width.. Chris@16: // either it was one big arg and we are fine. Chris@16: // Note that res_size oss2( &buf); Chris@16: specs.fmtstate_.apply_on(oss2, loc_p); Chris@16: put_head( oss2, x ); Chris@16: Chris@16: oss2.width(0); Chris@16: if(prefix_space) Chris@16: oss2 << ' '; Chris@16: put_last(oss2, x ); Chris@16: if(buf.pcount()==0 && specs.pad_scheme_ & format_item_t::spacepad) { Chris@16: prefix_space =true; Chris@16: oss2 << ' '; Chris@16: } Chris@16: // we now have the minimal-length output Chris@16: const Ch * tmp_beg = buf.pbase(); Chris@16: size_type tmp_size = (std::min)(static_cast(specs.truncate_), Chris@16: buf.pcount() ); Chris@16: Chris@16: Chris@16: if(static_cast(w) <= tmp_size) { Chris@16: // minimal length is already >= w, so no padding (cool!) Chris@16: res.assign(tmp_beg, tmp_size); Chris@16: } Chris@16: else { // hum.. we need to pad (multi_output, or spacepad present) Chris@16: //find where we should pad Chris@16: size_type sz = (std::min)(res_size + (prefix_space ? 1 : 0), tmp_size); Chris@16: size_type i = prefix_space; Chris@16: for(; i=tmp_size) i=prefix_space; Chris@16: res.assign(tmp_beg, i); Chris@16: std::streamsize d = w - static_cast(tmp_size); Chris@16: BOOST_ASSERT(d>0); Chris@16: res.append(static_cast( d ), oss2.fill()); Chris@16: res.append(tmp_beg+i, tmp_size-i); Chris@16: BOOST_ASSERT(i+(tmp_size-i)+(std::max)(d,(std::streamsize)0) Chris@16: == static_cast(w)); Chris@16: BOOST_ASSERT(res.size() == static_cast(w)); Chris@16: } Chris@16: } Chris@16: } Chris@16: buf.clear_buffer(); Chris@16: #ifdef BOOST_MSVC Chris@16: #pragma warning(pop) Chris@16: #endif Chris@16: } // end- put(..) Chris@16: Chris@16: Chris@16: template< class Ch, class Tr, class Alloc, class T> Chris@16: void distribute (basic_format& self, T x) { Chris@16: // call put(x, ..) on every occurence of the current argument : Chris@16: if(self.cur_arg_ >= self.num_args_) { Chris@16: if( self.exceptions() & too_many_args_bit ) Chris@16: boost::throw_exception(too_many_args(self.cur_arg_, self.num_args_)); Chris@16: else return; Chris@16: } Chris@16: for(unsigned long i=0; i < self.items_.size(); ++i) { Chris@16: if(self.items_[i].argN_ == self.cur_arg_) { Chris@16: put (x, self.items_[i], self.items_[i].res_, Chris@16: self.buf_, boost::get_pointer(self.loc_) ); Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: basic_format& Chris@101: feed_impl (basic_format& self, T x) { Chris@16: if(self.dumped_) self.clear(); Chris@16: distribute (self, x); Chris@16: ++self.cur_arg_; Chris@16: if(self.bound_.size() != 0) { Chris@16: while( self.cur_arg_ < self.num_args_ && self.bound_[self.cur_arg_] ) Chris@16: ++self.cur_arg_; Chris@16: } Chris@16: return self; Chris@16: } Chris@101: Chris@101: template inline Chris@101: basic_format& Chris@101: feed (basic_format& self, T x) { Chris@101: return feed_impl&>(self, put_holder(x)); Chris@101: } Chris@16: Chris@16: } // namespace detail Chris@16: } // namespace io Chris@16: } // namespace boost Chris@16: Chris@16: Chris@16: #endif // BOOST_FORMAT_FEED_ARGS_HPP