123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- #ifndef BOOST_FORMAT_INTERNALS_HPP
- #define BOOST_FORMAT_INTERNALS_HPP
- #include <string>
- #include <boost/assert.hpp>
- #include <boost/core/ignore_unused.hpp>
- #include <boost/optional.hpp>
- #include <boost/limits.hpp>
- #include <boost/format/detail/compat_workarounds.hpp>
- #include <boost/format/alt_sstream.hpp> // used as a dummy stream
- namespace boost {
- namespace io {
- namespace detail {
- template<class Ch, class Tr>
- struct stream_format_state
- {
- typedef BOOST_IO_STD basic_ios<Ch, Tr> basic_ios;
- stream_format_state(Ch fill) { reset(fill); }
- void reset(Ch fill);
- void set_by_stream(const basic_ios& os);
- void apply_on(basic_ios & os,
- boost::io::detail::locale_t * loc_default = 0) const;
- template<class T>
- void apply_manip(T manipulator)
- { apply_manip_body<Ch, Tr, T>( *this, manipulator) ; }
-
- std::streamsize width_;
- std::streamsize precision_;
- Ch fill_;
- std::ios_base::fmtflags flags_;
- std::ios_base::iostate rdstate_;
- std::ios_base::iostate exceptions_;
- boost::optional<boost::io::detail::locale_t> loc_;
- };
- template<class Ch, class Tr, class Alloc>
- struct format_item
- {
- enum pad_values { zeropad = 1, spacepad =2, centered=4, tabulation = 8 };
-
-
-
- enum arg_values { argN_no_posit = -1,
- argN_tabulation = -2,
- argN_ignored = -3
- };
- typedef BOOST_IO_STD basic_ios<Ch, Tr> basic_ios;
- typedef detail::stream_format_state<Ch, Tr> stream_format_state;
- typedef ::std::basic_string<Ch, Tr, Alloc> string_type;
- format_item(Ch fill) :argN_(argN_no_posit), fmtstate_(fill),
- truncate_(max_streamsize()), pad_scheme_(0) {}
- void reset(Ch fill);
- void compute_states();
- static std::streamsize max_streamsize() {
- return (std::numeric_limits<std::streamsize>::max)();
- }
-
- int argN_;
-
- string_type res_;
- string_type appendix_;
- stream_format_state fmtstate_;
- std::streamsize truncate_;
- unsigned int pad_scheme_;
- };
- template<class Ch, class Tr>
- void stream_format_state<Ch,Tr>:: apply_on (basic_ios & os,
- boost::io::detail::locale_t * loc_default) const {
-
- #if !defined(BOOST_NO_STD_LOCALE)
- if(loc_)
- os.imbue(loc_.get());
- else if(loc_default)
- os.imbue(*loc_default);
- #else
- ignore_unused(loc_default);
- #endif
-
- if(width_ != -1)
- os.width(width_);
- if(precision_ != -1)
- os.precision(precision_);
- if(fill_ != 0)
- os.fill(fill_);
- os.flags(flags_);
- os.clear(rdstate_);
- os.exceptions(exceptions_);
- }
- template<class Ch, class Tr>
- void stream_format_state<Ch,Tr>:: set_by_stream(const basic_ios& os) {
-
- flags_ = os.flags();
- width_ = os.width();
- precision_ = os.precision();
- fill_ = os.fill();
- rdstate_ = os.rdstate();
- exceptions_ = os.exceptions();
- }
- template<class Ch, class Tr, class T>
- void apply_manip_body( stream_format_state<Ch, Tr>& self,
- T manipulator) {
-
- basic_oaltstringstream<Ch, Tr> ss;
- self.apply_on( ss );
- ss << manipulator;
- self.set_by_stream( ss );
- }
- template<class Ch, class Tr> inline
- void stream_format_state<Ch,Tr>:: reset(Ch fill) {
-
- width_=0; precision_=6;
- fill_=fill;
- flags_ = std::ios_base::dec | std::ios_base::skipws;
-
- exceptions_ = std::ios_base::goodbit;
- rdstate_ = std::ios_base::goodbit;
- }
- template<class Ch, class Tr, class Alloc>
- void format_item<Ch, Tr, Alloc>::
- reset (Ch fill) {
- argN_=argN_no_posit; truncate_ = max_streamsize(); pad_scheme_ =0;
- res_.resize(0); appendix_.resize(0);
- fmtstate_.reset(fill);
- }
- template<class Ch, class Tr, class Alloc>
- void format_item<Ch, Tr, Alloc>::
- compute_states() {
-
-
- if(pad_scheme_ & zeropad) {
-
- if(fmtstate_.flags_ & std::ios_base::left) {
- BOOST_ASSERT(!(fmtstate_.flags_ &(std::ios_base::adjustfield ^std::ios_base::left)));
-
- pad_scheme_ = pad_scheme_ & (~zeropad);
- }
- else {
- pad_scheme_ &= ~spacepad;
- fmtstate_.fill_='0';
- fmtstate_.flags_ = (fmtstate_.flags_ & ~std::ios_base::adjustfield)
- | std::ios_base::internal;
-
- }
- }
- if(pad_scheme_ & spacepad) {
- if(fmtstate_.flags_ & std::ios_base::showpos)
- pad_scheme_ &= ~spacepad;
- }
- }
- } } }
- #endif
|