feed_args.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. // ----------------------------------------------------------------------------
  2. // feed_args.hpp : functions for processing each argument
  3. // (feed, feed_manip, and distribute)
  4. // ----------------------------------------------------------------------------
  5. // Copyright Samuel Krempp 2003. Use, modification, and distribution are
  6. // subject to the Boost Software License, Version 1.0. (See accompanying
  7. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. // See http://www.boost.org/libs/format for library home page
  9. // ----------------------------------------------------------------------------
  10. #ifndef BOOST_FORMAT_FEED_ARGS_HPP
  11. #define BOOST_FORMAT_FEED_ARGS_HPP
  12. #include <boost/config.hpp>
  13. #include <boost/assert.hpp>
  14. #include <boost/throw_exception.hpp>
  15. #include <boost/format/format_class.hpp>
  16. #include <boost/format/group.hpp>
  17. #include <boost/format/detail/msvc_disambiguater.hpp>
  18. namespace boost {
  19. namespace io {
  20. namespace detail {
  21. template<class Ch, class Tr, class Alloc>
  22. void mk_str( std::basic_string<Ch,Tr, Alloc> & res,
  23. const Ch * beg,
  24. typename std::basic_string<Ch,Tr,Alloc>::size_type size,
  25. std::streamsize w,
  26. const Ch fill_char,
  27. std::ios_base::fmtflags f,
  28. const Ch prefix_space, // 0 if no space-padding
  29. bool center)
  30. // applies centered/left/right padding to the string [beg, beg+size[
  31. // Effects : the result is placed in res.
  32. {
  33. typedef typename std::basic_string<Ch,Tr,Alloc>::size_type size_type;
  34. res.resize(0);
  35. if(w<=0 || static_cast<size_type>(w) <=size) {
  36. // no need to pad.
  37. res.reserve(size + !!prefix_space);
  38. if(prefix_space)
  39. res.append(1, prefix_space);
  40. if (size)
  41. res.append(beg, size);
  42. }
  43. else {
  44. std::streamsize n=static_cast<std::streamsize>(w-size-!!prefix_space);
  45. std::streamsize n_after = 0, n_before = 0;
  46. res.reserve(static_cast<size_type>(w)); // allocate once for the 2 inserts
  47. if(center)
  48. n_after = n/2, n_before = n - n_after;
  49. else
  50. if(f & std::ios_base::left)
  51. n_after = n;
  52. else
  53. n_before = n;
  54. // now make the res string :
  55. if(n_before) res.append(static_cast<size_type>(n_before), fill_char);
  56. if(prefix_space)
  57. res.append(1, prefix_space);
  58. if (size)
  59. res.append(beg, size);
  60. if(n_after) res.append(static_cast<size_type>(n_after), fill_char);
  61. }
  62. } // -mk_str(..)
  63. #if BOOST_WORKAROUND(__DECCXX_VER, BOOST_TESTED_AT(60590042))
  64. // __DECCXX needs to be tricked to disambiguate this simple overload..
  65. // the trick is in "boost/format/msvc_disambiguater.hpp"
  66. template< class Ch, class Tr, class T> inline
  67. void put_head (BOOST_IO_STD basic_ostream<Ch, Tr> & os, const T& x ) {
  68. disambiguater<Ch, Tr, T>::put_head(os, x, 1L);
  69. }
  70. template< class Ch, class Tr, class T> inline
  71. void put_last (BOOST_IO_STD basic_ostream<Ch, Tr> & os, const T& x ) {
  72. disambiguater<Ch, Tr, T>::put_last(os, x, 1L);
  73. }
  74. #else
  75. template< class Ch, class Tr, class T> inline
  76. void put_head (BOOST_IO_STD basic_ostream<Ch, Tr> &, const T& ) {
  77. }
  78. template< class Ch, class Tr, class T> inline
  79. void put_head( BOOST_IO_STD basic_ostream<Ch, Tr> & os, const group1<T>& x ) {
  80. os << group_head(x.a1_); // send the first N-1 items, not the last
  81. }
  82. template< class Ch, class Tr, class T> inline
  83. void put_last( BOOST_IO_STD basic_ostream<Ch, Tr> & os, const T& x ) {
  84. os << x ;
  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. #ifndef BOOST_NO_OVERLOAD_FOR_NON_CONST
  91. template< class Ch, class Tr, class T> inline
  92. void put_head( BOOST_IO_STD basic_ostream<Ch, Tr> &, T& ) {
  93. }
  94. template< class Ch, class Tr, class T> inline
  95. void put_last( BOOST_IO_STD basic_ostream<Ch, Tr> & os, T& x) {
  96. os << x ;
  97. }
  98. #endif
  99. #endif // -__DECCXX workaround
  100. template< class Ch, class Tr, class T>
  101. void call_put_head(BOOST_IO_STD basic_ostream<Ch, Tr> & os, const void* x) {
  102. put_head(os, *(static_cast<T const *>(x)));
  103. }
  104. template< class Ch, class Tr, class T>
  105. void call_put_last(BOOST_IO_STD basic_ostream<Ch, Tr> & os, const void* x) {
  106. put_last(os, *(static_cast<T const *>(x)));
  107. }
  108. template< class Ch, class Tr>
  109. struct put_holder {
  110. template<class T>
  111. put_holder(T& t)
  112. : arg(&t),
  113. put_head(&call_put_head<Ch, Tr, T>),
  114. put_last(&call_put_last<Ch, Tr, T>)
  115. {}
  116. const void* arg;
  117. void (*put_head)(BOOST_IO_STD basic_ostream<Ch, Tr> & os, const void* x);
  118. void (*put_last)(BOOST_IO_STD basic_ostream<Ch, Tr> & os, const void* x);
  119. };
  120. template< class Ch, class Tr> inline
  121. void put_head( BOOST_IO_STD basic_ostream<Ch, Tr> & os, const put_holder<Ch, Tr>& t) {
  122. t.put_head(os, t.arg);
  123. }
  124. template< class Ch, class Tr> inline
  125. void put_last( BOOST_IO_STD basic_ostream<Ch, Tr> & os, const put_holder<Ch, Tr>& t) {
  126. t.put_last(os, t.arg);
  127. }
  128. template< class Ch, class Tr, class Alloc, class T>
  129. void put( T x,
  130. const format_item<Ch, Tr, Alloc>& specs,
  131. typename basic_format<Ch, Tr, Alloc>::string_type& res,
  132. typename basic_format<Ch, Tr, Alloc>::internal_streambuf_t & buf,
  133. io::detail::locale_t *loc_p = NULL)
  134. {
  135. #ifdef BOOST_MSVC
  136. // If std::min<unsigned> or std::max<unsigned> are already instantiated
  137. // at this point then we get a blizzard of warning messages when we call
  138. // those templates with std::size_t as arguments. Weird and very annoyning...
  139. #pragma warning(push)
  140. #pragma warning(disable:4267)
  141. #endif
  142. // does the actual conversion of x, with given params, into a string
  143. // using the supplied stringbuf.
  144. typedef typename basic_format<Ch, Tr, Alloc>::string_type string_type;
  145. typedef typename basic_format<Ch, Tr, Alloc>::format_item_t format_item_t;
  146. typedef typename string_type::size_type size_type;
  147. basic_oaltstringstream<Ch, Tr, Alloc> oss( &buf);
  148. #if !defined(BOOST_NO_STD_LOCALE)
  149. if(loc_p != NULL)
  150. oss.imbue(*loc_p);
  151. #endif
  152. specs.fmtstate_.apply_on(oss, loc_p);
  153. // the stream format state can be modified by manipulators in the argument :
  154. put_head( oss, x );
  155. // in case x is a group, apply the manip part of it,
  156. // in order to find width
  157. const std::ios_base::fmtflags fl=oss.flags();
  158. const bool internal = (fl & std::ios_base::internal) != 0;
  159. const std::streamsize w = oss.width();
  160. const bool two_stepped_padding= internal && (w!=0);
  161. res.resize(0);
  162. if(! two_stepped_padding) {
  163. if(w>0) // handle padding via mk_str, not natively in stream
  164. oss.width(0);
  165. put_last( oss, x);
  166. const Ch * res_beg = buf.pbase();
  167. Ch prefix_space = 0;
  168. if(specs.pad_scheme_ & format_item_t::spacepad)
  169. if(buf.pcount()== 0 ||
  170. (res_beg[0] !=oss.widen('+') && res_beg[0] !=oss.widen('-') ))
  171. prefix_space = oss.widen(' ');
  172. size_type res_size = (std::min)(
  173. (static_cast<size_type>((specs.truncate_ & (std::numeric_limits<size_type>::max)())) - !!prefix_space),
  174. buf.pcount() );
  175. mk_str(res, res_beg, res_size, w, oss.fill(), fl,
  176. prefix_space, (specs.pad_scheme_ & format_item_t::centered) !=0 );
  177. }
  178. else { // 2-stepped padding
  179. // internal can be implied by zeropad, or user-set.
  180. // left, right, and centered alignment overrule internal,
  181. // but spacepad or truncate might be mixed with internal (using manipulator)
  182. put_last( oss, x); // may pad
  183. const Ch * res_beg = buf.pbase();
  184. size_type res_size = buf.pcount();
  185. bool prefix_space=false;
  186. if(specs.pad_scheme_ & format_item_t::spacepad)
  187. if(buf.pcount()== 0 ||
  188. (res_beg[0] !=oss.widen('+') && res_beg[0] !=oss.widen('-') ))
  189. prefix_space = true;
  190. if(res_size == static_cast<size_type>(w) && w<=specs.truncate_ && !prefix_space) {
  191. // okay, only one thing was printed and padded, so res is fine
  192. res.assign(res_beg, res_size);
  193. }
  194. else { // length w exceeded
  195. // either it was multi-output with first output padding up all width..
  196. // either it was one big arg and we are fine.
  197. // Note that res_size<w is possible (in case of bad user-defined formatting)
  198. res.assign(res_beg, res_size);
  199. res_beg=NULL; // invalidate pointers.
  200. // make a new stream, to start re-formatting from scratch :
  201. buf.clear_buffer();
  202. basic_oaltstringstream<Ch, Tr, Alloc> oss2( &buf);
  203. specs.fmtstate_.apply_on(oss2, loc_p);
  204. put_head( oss2, x );
  205. oss2.width(0);
  206. if(prefix_space)
  207. oss2 << ' ';
  208. put_last(oss2, x );
  209. if(buf.pcount()==0 && specs.pad_scheme_ & format_item_t::spacepad) {
  210. prefix_space =true;
  211. oss2 << ' ';
  212. }
  213. // we now have the minimal-length output
  214. const Ch * tmp_beg = buf.pbase();
  215. size_type tmp_size = (std::min)(
  216. (static_cast<size_type>(specs.truncate_ & (std::numeric_limits<size_type>::max)())),
  217. buf.pcount());
  218. if(static_cast<size_type>(w) <= tmp_size) {
  219. // minimal length is already >= w, so no padding (cool!)
  220. res.assign(tmp_beg, tmp_size);
  221. }
  222. else { // hum.. we need to pad (multi_output, or spacepad present)
  223. //find where we should pad
  224. size_type sz = (std::min)(res_size + (prefix_space ? 1 : 0), tmp_size);
  225. size_type i = prefix_space;
  226. for(; i<sz && tmp_beg[i] == res[i - (prefix_space ? 1 : 0)]; ++i) {}
  227. if(i>=tmp_size) i=prefix_space;
  228. res.assign(tmp_beg, i);
  229. std::streamsize d = w - static_cast<std::streamsize>(tmp_size);
  230. BOOST_ASSERT(d>0);
  231. res.append(static_cast<size_type>( d ), oss2.fill());
  232. res.append(tmp_beg+i, tmp_size-i);
  233. BOOST_ASSERT(i+(tmp_size-i)+(std::max)(d,(std::streamsize)0)
  234. == static_cast<size_type>(w));
  235. BOOST_ASSERT(res.size() == static_cast<size_type>(w));
  236. }
  237. }
  238. }
  239. buf.clear_buffer();
  240. #ifdef BOOST_MSVC
  241. #pragma warning(pop)
  242. #endif
  243. } // end- put(..)
  244. template< class Ch, class Tr, class Alloc, class T>
  245. void distribute (basic_format<Ch,Tr, Alloc>& self, T x) {
  246. // call put(x, ..) on every occurrence of the current argument :
  247. if(self.cur_arg_ >= self.num_args_) {
  248. if( self.exceptions() & too_many_args_bit )
  249. boost::throw_exception(too_many_args(self.cur_arg_, self.num_args_));
  250. else return;
  251. }
  252. for(unsigned long i=0; i < self.items_.size(); ++i) {
  253. if(self.items_[i].argN_ == self.cur_arg_) {
  254. put<Ch, Tr, Alloc, T> (x, self.items_[i], self.items_[i].res_,
  255. self.buf_, boost::get_pointer(self.loc_) );
  256. }
  257. }
  258. }
  259. template<class Ch, class Tr, class Alloc, class T>
  260. basic_format<Ch, Tr, Alloc>&
  261. feed_impl (basic_format<Ch,Tr, Alloc>& self, T x) {
  262. if(self.dumped_) self.clear();
  263. distribute<Ch, Tr, Alloc, T> (self, x);
  264. ++self.cur_arg_;
  265. if(self.bound_.size() != 0) {
  266. while( self.cur_arg_ < self.num_args_ && self.bound_[self.cur_arg_] )
  267. ++self.cur_arg_;
  268. }
  269. return self;
  270. }
  271. template<class Ch, class Tr, class Alloc, class T> inline
  272. basic_format<Ch, Tr, Alloc>&
  273. feed (basic_format<Ch,Tr, Alloc>& self, T x) {
  274. return feed_impl<Ch, Tr, Alloc, const put_holder<Ch, Tr>&>(self, put_holder<Ch, Tr>(x));
  275. }
  276. } // namespace detail
  277. } // namespace io
  278. } // namespace boost
  279. #endif // BOOST_FORMAT_FEED_ARGS_HPP