internals.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // ----------------------------------------------------------------------------
  2. // internals.hpp : internal structs : stream_format_state, format_item.
  3. // included by format.hpp
  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_INTERNALS_HPP
  11. #define BOOST_FORMAT_INTERNALS_HPP
  12. #include <string>
  13. #include <boost/assert.hpp>
  14. #include <boost/core/ignore_unused.hpp>
  15. #include <boost/optional.hpp>
  16. #include <boost/limits.hpp>
  17. #include <boost/format/detail/compat_workarounds.hpp>
  18. #include <boost/format/alt_sstream.hpp> // used as a dummy stream
  19. namespace boost {
  20. namespace io {
  21. namespace detail {
  22. //---- stream_format_state --------------------------------------------------//
  23. // set of params that define the format state of a stream
  24. template<class Ch, class Tr>
  25. struct stream_format_state
  26. {
  27. typedef BOOST_IO_STD basic_ios<Ch, Tr> basic_ios;
  28. stream_format_state(Ch fill) { reset(fill); }
  29. // stream_format_state(const basic_ios& os) { set_by_stream(os); }
  30. void reset(Ch fill); //- sets to default state.
  31. void set_by_stream(const basic_ios& os); //- sets to os's state.
  32. void apply_on(basic_ios & os, //- applies format_state to the stream
  33. boost::io::detail::locale_t * loc_default = 0) const;
  34. template<class T>
  35. void apply_manip(T manipulator) //- modifies state by applying manipulator
  36. { apply_manip_body<Ch, Tr, T>( *this, manipulator) ; }
  37. // --- data ---
  38. std::streamsize width_;
  39. std::streamsize precision_;
  40. Ch fill_;
  41. std::ios_base::fmtflags flags_;
  42. std::ios_base::iostate rdstate_;
  43. std::ios_base::iostate exceptions_;
  44. boost::optional<boost::io::detail::locale_t> loc_;
  45. };
  46. //---- format_item ---------------------------------------------------------//
  47. // stores all parameters that can be specified in format strings
  48. template<class Ch, class Tr, class Alloc>
  49. struct format_item
  50. {
  51. enum pad_values { zeropad = 1, spacepad =2, centered=4, tabulation = 8 };
  52. // 1. if zeropad is set, all other bits are not,
  53. // 2. if tabulation is set, all others are not.
  54. // centered and spacepad can be mixed freely.
  55. enum arg_values { argN_no_posit = -1, // non-positional directive. will set argN later
  56. argN_tabulation = -2, // tabulation directive. (no argument read)
  57. argN_ignored = -3 // ignored directive. (no argument read)
  58. };
  59. typedef BOOST_IO_STD basic_ios<Ch, Tr> basic_ios;
  60. typedef detail::stream_format_state<Ch, Tr> stream_format_state;
  61. typedef ::std::basic_string<Ch, Tr, Alloc> string_type;
  62. format_item(Ch fill) :argN_(argN_no_posit), fmtstate_(fill),
  63. truncate_(max_streamsize()), pad_scheme_(0) {}
  64. void reset(Ch fill);
  65. void compute_states(); // sets states according to truncate and pad_scheme.
  66. static std::streamsize max_streamsize() {
  67. return (std::numeric_limits<std::streamsize>::max)();
  68. }
  69. // --- data ---
  70. int argN_; //- argument number (starts at 0, eg : %1 => argN=0)
  71. // negative values for items that don't process an argument
  72. string_type res_; //- result of the formatting of this item
  73. string_type appendix_; //- piece of string between this item and the next
  74. stream_format_state fmtstate_;// set by parsing, is only affected by modify_item
  75. std::streamsize truncate_;//- is set for directives like %.5s that ask truncation
  76. unsigned int pad_scheme_;//- several possible padding schemes can mix. see pad_values
  77. };
  78. //--- Definitions ------------------------------------------------------------
  79. // - stream_format_state:: -------------------------------------------------
  80. template<class Ch, class Tr>
  81. void stream_format_state<Ch,Tr>:: apply_on (basic_ios & os,
  82. boost::io::detail::locale_t * loc_default) const {
  83. // If a locale is available, set it first. "os.fill(fill_);" may chrash otherwise.
  84. #if !defined(BOOST_NO_STD_LOCALE)
  85. if(loc_)
  86. os.imbue(loc_.get());
  87. else if(loc_default)
  88. os.imbue(*loc_default);
  89. #else
  90. ignore_unused(loc_default);
  91. #endif
  92. // set the state of this stream according to our params
  93. if(width_ != -1)
  94. os.width(width_);
  95. if(precision_ != -1)
  96. os.precision(precision_);
  97. if(fill_ != 0)
  98. os.fill(fill_);
  99. os.flags(flags_);
  100. os.clear(rdstate_);
  101. os.exceptions(exceptions_);
  102. }
  103. template<class Ch, class Tr>
  104. void stream_format_state<Ch,Tr>:: set_by_stream(const basic_ios& os) {
  105. // set our params according to the state of this stream
  106. flags_ = os.flags();
  107. width_ = os.width();
  108. precision_ = os.precision();
  109. fill_ = os.fill();
  110. rdstate_ = os.rdstate();
  111. exceptions_ = os.exceptions();
  112. }
  113. template<class Ch, class Tr, class T>
  114. void apply_manip_body( stream_format_state<Ch, Tr>& self,
  115. T manipulator) {
  116. // modify our params according to the manipulator
  117. basic_oaltstringstream<Ch, Tr> ss;
  118. self.apply_on( ss );
  119. ss << manipulator;
  120. self.set_by_stream( ss );
  121. }
  122. template<class Ch, class Tr> inline
  123. void stream_format_state<Ch,Tr>:: reset(Ch fill) {
  124. // set our params to standard's default state. cf 27.4.4.1 of the C++ norm
  125. width_=0; precision_=6;
  126. fill_=fill; // default is widen(' '), but we cant compute it without the locale
  127. flags_ = std::ios_base::dec | std::ios_base::skipws;
  128. // the adjust_field part is left equal to 0, which means right.
  129. exceptions_ = std::ios_base::goodbit;
  130. rdstate_ = std::ios_base::goodbit;
  131. }
  132. // --- format_item:: --------------------------------------------------------
  133. template<class Ch, class Tr, class Alloc>
  134. void format_item<Ch, Tr, Alloc>::
  135. reset (Ch fill) {
  136. argN_=argN_no_posit; truncate_ = max_streamsize(); pad_scheme_ =0;
  137. res_.resize(0); appendix_.resize(0);
  138. fmtstate_.reset(fill);
  139. }
  140. template<class Ch, class Tr, class Alloc>
  141. void format_item<Ch, Tr, Alloc>::
  142. compute_states() {
  143. // reflect pad_scheme_ on fmt_state_
  144. // because some pad_schemes has complex consequences on several state params.
  145. if(pad_scheme_ & zeropad) {
  146. // ignore zeropad in left alignment :
  147. if(fmtstate_.flags_ & std::ios_base::left) {
  148. BOOST_ASSERT(!(fmtstate_.flags_ &(std::ios_base::adjustfield ^std::ios_base::left)));
  149. // only left bit might be set. (not right, nor internal)
  150. pad_scheme_ = pad_scheme_ & (~zeropad);
  151. }
  152. else {
  153. pad_scheme_ &= ~spacepad; // printf ignores spacepad when zeropadding
  154. fmtstate_.fill_='0';
  155. fmtstate_.flags_ = (fmtstate_.flags_ & ~std::ios_base::adjustfield)
  156. | std::ios_base::internal;
  157. // removes all adjustfield bits, and adds internal.
  158. }
  159. }
  160. if(pad_scheme_ & spacepad) {
  161. if(fmtstate_.flags_ & std::ios_base::showpos)
  162. pad_scheme_ &= ~spacepad;
  163. }
  164. }
  165. } } } // namespaces boost :: io :: detail
  166. #endif // BOOST_FORMAT_INTERNALS_HPP