feed_args.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // -*- C++ -*-
  2. // Boost general library 'format' ---------------------------
  3. // See http://www.boost.org for updates, documentation, and revision history.
  4. // (C) Samuel Krempp 2001
  5. // krempp@crans.ens-cachan.fr
  6. // Permission to copy, use, modify, sell and
  7. // distribute this software is granted provided this copyright notice appears
  8. // in all copies. This software is provided "as is" without express or implied
  9. // warranty, and with no claim as to its suitability for any purpose.
  10. // ideas taken from Rüdiger Loos's format class
  11. // and Karl Nelson's ofstream
  12. // ----------------------------------------------------------------------------
  13. // feed_args.hpp : functions for processing each argument
  14. // (feed, feed_manip, and distribute)
  15. // ----------------------------------------------------------------------------
  16. #ifndef BOOST_FORMAT_FEED_ARGS_HPP
  17. #define BOOST_FORMAT_FEED_ARGS_HPP
  18. #include "boost/format/format_class.hpp"
  19. #include "boost/format/group.hpp"
  20. #include "boost/throw_exception.hpp"
  21. namespace boost {
  22. namespace io {
  23. namespace detail {
  24. namespace {
  25. inline
  26. void empty_buf(BOOST_IO_STD ostringstream & os) {
  27. static const std::string emptyStr;
  28. os.str(emptyStr);
  29. }
  30. void do_pad( std::string & s,
  31. std::streamsize w,
  32. const char c,
  33. std::ios::fmtflags f,
  34. bool center)
  35. // applies centered / left / right padding to the string s.
  36. // Effects : string s is padded.
  37. {
  38. std::streamsize n=w-s.size();
  39. if(n<=0) {
  40. return;
  41. }
  42. if(center)
  43. {
  44. s.reserve(w); // allocate once for the 2 inserts
  45. const std::streamsize n1 = n /2, n0 = n - n1;
  46. s.insert(s.begin(), n0, c);
  47. s.append(n1, c);
  48. }
  49. else
  50. {
  51. if(f & std::ios::left) {
  52. s.append(n, c);
  53. }
  54. else {
  55. s.insert(s.begin(), n, c);
  56. }
  57. }
  58. } // -do_pad(..)
  59. template<class T> inline
  60. void put_head(BOOST_IO_STD ostream& , const T& ) {
  61. }
  62. template<class T> inline
  63. void put_head( BOOST_IO_STD ostream& os, const group1<T>& x ) {
  64. os << group_head(x.a1_); // send the first N-1 items, not the last
  65. }
  66. template<class T> inline
  67. void put_last( BOOST_IO_STD ostream& os, const T& x ) {
  68. os << x ;
  69. }
  70. template<class T> inline
  71. void put_last( BOOST_IO_STD ostream& os, const group1<T>& x ) {
  72. os << group_last(x.a1_); // this selects the last element
  73. }
  74. #ifndef BOOST_NO_OVERLOAD_FOR_NON_CONST
  75. template<class T> inline
  76. void put_head( BOOST_IO_STD ostream& , T& ) {
  77. }
  78. template<class T> inline
  79. void put_last( BOOST_IO_STD ostream& os, T& x ) {
  80. os << x ;
  81. }
  82. #endif
  83. template<class T>
  84. void put( T x,
  85. const format_item& specs,
  86. std::string & res,
  87. BOOST_IO_STD ostringstream& oss_ )
  88. {
  89. // does the actual conversion of x, with given params, into a string
  90. // using the *supplied* strinstream. (the stream state is important)
  91. typedef std::string string_t;
  92. typedef format_item format_item_t;
  93. stream_format_state prev_state(oss_);
  94. specs.state_.apply_on(oss_);
  95. // in case x is a group, apply the manip part of it,
  96. // in order to find width
  97. put_head( oss_, x );
  98. empty_buf( oss_);
  99. const std::streamsize w=oss_.width();
  100. const std::ios::fmtflags fl=oss_.flags();
  101. const bool internal = (fl & std::ios::internal) != 0;
  102. const bool two_stepped_padding = internal
  103. && ! ( specs.pad_scheme_ & format_item_t::spacepad )
  104. && specs.truncate_ < 0 ;
  105. if(! two_stepped_padding)
  106. {
  107. if(w>0) // handle simple padding via do_pad, not natively in stream
  108. oss_.width(0);
  109. put_last( oss_, x);
  110. res = oss_.str();
  111. if (specs.truncate_ >= 0)
  112. res.erase(specs.truncate_);
  113. // complex pads :
  114. if(specs.pad_scheme_ & format_item_t::spacepad)
  115. {
  116. if( res.size()==0 || ( res[0]!='+' && res[0]!='-' ))
  117. {
  118. res.insert(res.begin(), 1, ' '); // insert 1 space at pos 0
  119. }
  120. }
  121. if(w > 0) // need do_pad
  122. {
  123. do_pad(res,w,oss_.fill(), fl, (specs.pad_scheme_ & format_item_t::centered) !=0 );
  124. }
  125. }
  126. else // 2-stepped padding
  127. {
  128. put_last( oss_, x); // oss_.width() may result in padding.
  129. res = oss_.str();
  130. if (specs.truncate_ >= 0)
  131. res.erase(specs.truncate_);
  132. if( res.size() - w > 0)
  133. { // length w exceeded
  134. // either it was multi-output with first output padding up all width..
  135. // either it was one big arg and we are fine.
  136. empty_buf( oss_);
  137. oss_.width(0);
  138. put_last(oss_, x );
  139. string_t tmp = oss_.str(); // minimal-length output
  140. std::streamsize d;
  141. if( (d=w - tmp.size()) <=0 )
  142. {
  143. // minimal length is already >= w, so no padding (cool!)
  144. res.swap(tmp);
  145. }
  146. else
  147. { // hum.. we need to pad (it was necessarily multi-output)
  148. typedef typename string_t::size_type size_type;
  149. size_type i = 0;
  150. while( i<tmp.size() && tmp[i] == res[i] ) // find where we should pad.
  151. ++i;
  152. tmp.insert(i, static_cast<size_type>( d ), oss_.fill());
  153. res.swap( tmp );
  154. }
  155. }
  156. else
  157. { // okay, only one thing was printed and padded, so res is fine.
  158. }
  159. }
  160. prev_state.apply_on(oss_);
  161. empty_buf( oss_);
  162. oss_.clear();
  163. } // end- put(..)
  164. } // local namespace
  165. template<class T>
  166. void distribute(basic_format& self, T x)
  167. // call put(x, ..) on every occurence of the current argument :
  168. {
  169. if(self.cur_arg_ >= self.num_args_)
  170. {
  171. if( self.exceptions() & too_many_args_bit )
  172. boost::throw_exception(too_many_args()); // too many variables have been supplied !
  173. else return;
  174. }
  175. for(unsigned long i=0; i < self.items_.size(); ++i)
  176. {
  177. if(self.items_[i].argN_ == self.cur_arg_)
  178. {
  179. put<T> (x, self.items_[i], self.items_[i].res_, self.oss_ );
  180. }
  181. }
  182. }
  183. template<class T>
  184. basic_format& feed(basic_format& self, T x)
  185. {
  186. if(self.dumped_) self.clear();
  187. distribute<T> (self, x);
  188. ++self.cur_arg_;
  189. if(self.bound_.size() != 0)
  190. {
  191. while( self.cur_arg_ < self.num_args_ && self.bound_[self.cur_arg_] )
  192. ++self.cur_arg_;
  193. }
  194. // this arg is finished, reset the stream's format state
  195. self.state0_.apply_on(self.oss_);
  196. return self;
  197. }
  198. } // namespace detail
  199. } // namespace io
  200. } // namespace boost
  201. #endif // BOOST_FORMAT_FEED_ARGS_HPP