sl@0: // ---------------------------------------------------------------------------- sl@0: // internals.hpp : internal structs : stream_format_state, format_item. sl@0: // included by format.hpp sl@0: // ---------------------------------------------------------------------------- sl@0: sl@0: // Copyright Samuel Krempp 2003. Use, modification, and distribution are sl@0: // subject to the Boost Software License, Version 1.0. (See accompanying sl@0: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) sl@0: sl@0: // See http://www.boost.org/libs/format for library home page sl@0: sl@0: // ---------------------------------------------------------------------------- sl@0: sl@0: #ifndef BOOST_FORMAT_INTERNALS_HPP sl@0: #define BOOST_FORMAT_INTERNALS_HPP sl@0: sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include // used as a dummy stream sl@0: sl@0: namespace boost { sl@0: namespace io { sl@0: namespace detail { sl@0: sl@0: sl@0: //---- stream_format_state --------------------------------------------------// sl@0: sl@0: // set of params that define the format state of a stream sl@0: template sl@0: struct stream_format_state sl@0: { sl@0: typedef BOOST_IO_STD basic_ios basic_ios; sl@0: sl@0: stream_format_state(Ch fill) { reset(fill); } sl@0: // stream_format_state(const basic_ios& os) { set_by_stream(os); } sl@0: sl@0: void reset(Ch fill); //- sets to default state. sl@0: void set_by_stream(const basic_ios& os); //- sets to os's state. sl@0: void apply_on(basic_ios & os, //- applies format_state to the stream sl@0: boost::io::detail::locale_t * loc_default = 0) const; sl@0: template sl@0: void apply_manip(T manipulator) //- modifies state by applying manipulator sl@0: { apply_manip_body( *this, manipulator) ; } sl@0: sl@0: // --- data --- sl@0: std::streamsize width_; sl@0: std::streamsize precision_; sl@0: Ch fill_; sl@0: std::ios_base::fmtflags flags_; sl@0: std::ios_base::iostate rdstate_; sl@0: std::ios_base::iostate exceptions_; sl@0: boost::optional loc_; sl@0: }; sl@0: sl@0: sl@0: //---- format_item ---------------------------------------------------------// sl@0: sl@0: // stores all parameters that can be specified in format strings sl@0: template sl@0: struct format_item sl@0: { sl@0: enum pad_values { zeropad = 1, spacepad =2, centered=4, tabulation = 8 }; sl@0: // 1. if zeropad is set, all other bits are not, sl@0: // 2. if tabulation is set, all others are not. sl@0: // centered and spacepad can be mixed freely. sl@0: enum arg_values { argN_no_posit = -1, // non-positional directive. will set argN later sl@0: argN_tabulation = -2, // tabulation directive. (no argument read) sl@0: argN_ignored = -3 // ignored directive. (no argument read) sl@0: }; sl@0: typedef BOOST_IO_STD basic_ios basic_ios; sl@0: typedef detail::stream_format_state stream_format_state; sl@0: typedef ::std::basic_string string_type; sl@0: sl@0: format_item(Ch fill) :argN_(argN_no_posit), fmtstate_(fill), sl@0: truncate_(max_streamsize()), pad_scheme_(0) {} sl@0: void reset(Ch fill); sl@0: void compute_states(); // sets states according to truncate and pad_scheme. sl@0: sl@0: static std::streamsize max_streamsize() { sl@0: return (std::numeric_limits::max)(); sl@0: } sl@0: sl@0: // --- data --- sl@0: int argN_; //- argument number (starts at 0, eg : %1 => argN=0) sl@0: // negative values for items that don't process an argument sl@0: string_type res_; //- result of the formatting of this item sl@0: string_type appendix_; //- piece of string between this item and the next sl@0: sl@0: stream_format_state fmtstate_;// set by parsing, is only affected by modify_item sl@0: sl@0: std::streamsize truncate_;//- is set for directives like %.5s that ask truncation sl@0: unsigned int pad_scheme_;//- several possible padding schemes can mix. see pad_values sl@0: }; sl@0: sl@0: sl@0: sl@0: //--- Definitions ------------------------------------------------------------ sl@0: sl@0: // - stream_format_state:: ------------------------------------------------- sl@0: template sl@0: void stream_format_state:: apply_on (basic_ios & os, sl@0: boost::io::detail::locale_t * loc_default) const { sl@0: // set the state of this stream according to our params sl@0: if(width_ != -1) sl@0: os.width(width_); sl@0: if(precision_ != -1) sl@0: os.precision(precision_); sl@0: if(fill_ != 0) sl@0: os.fill(fill_); sl@0: os.flags(flags_); sl@0: os.clear(rdstate_); sl@0: os.exceptions(exceptions_); sl@0: #if !defined(BOOST_NO_STD_LOCALE) sl@0: if(loc_) sl@0: os.imbue(loc_.get()); sl@0: else if(loc_default) sl@0: os.imbue(*loc_default); sl@0: #endif sl@0: } sl@0: sl@0: template sl@0: void stream_format_state:: set_by_stream(const basic_ios& os) { sl@0: // set our params according to the state of this stream sl@0: flags_ = os.flags(); sl@0: width_ = os.width(); sl@0: precision_ = os.precision(); sl@0: fill_ = os.fill(); sl@0: rdstate_ = os.rdstate(); sl@0: exceptions_ = os.exceptions(); sl@0: } sl@0: sl@0: sl@0: template sl@0: void apply_manip_body( stream_format_state& self, sl@0: T manipulator) { sl@0: // modify our params according to the manipulator sl@0: basic_oaltstringstream ss; sl@0: self.apply_on( ss ); sl@0: ss << manipulator; sl@0: self.set_by_stream( ss ); sl@0: } sl@0: sl@0: template inline sl@0: void stream_format_state:: reset(Ch fill) { sl@0: // set our params to standard's default state. cf § 27.4.4.1 of the C++ norm sl@0: width_=0; precision_=6; sl@0: fill_=fill; // default is widen(' '), but we cant compute it without the locale sl@0: flags_ = std::ios_base::dec | std::ios_base::skipws; sl@0: // the adjust_field part is left equal to 0, which means right. sl@0: exceptions_ = std::ios_base::goodbit; sl@0: rdstate_ = std::ios_base::goodbit; sl@0: } sl@0: sl@0: sl@0: // --- format_item:: -------------------------------------------------------- sl@0: sl@0: template sl@0: void format_item:: sl@0: reset (Ch fill) { sl@0: argN_=argN_no_posit; truncate_ = max_streamsize(); pad_scheme_ =0; sl@0: res_.resize(0); appendix_.resize(0); sl@0: fmtstate_.reset(fill); sl@0: } sl@0: sl@0: template sl@0: void format_item:: sl@0: compute_states() { sl@0: // reflect pad_scheme_ on fmt_state_ sl@0: // because some pad_schemes has complex consequences on several state params. sl@0: if(pad_scheme_ & zeropad) { sl@0: // ignore zeropad in left alignment : sl@0: if(fmtstate_.flags_ & std::ios_base::left) { sl@0: BOOST_ASSERT(!(fmtstate_.flags_ &(std::ios_base::adjustfield ^std::ios_base::left))); sl@0: // only left bit might be set. (not right, nor internal) sl@0: pad_scheme_ = pad_scheme_ & (~zeropad); sl@0: } sl@0: else { sl@0: pad_scheme_ &= ~spacepad; // printf ignores spacepad when zeropadding sl@0: fmtstate_.fill_='0'; sl@0: fmtstate_.flags_ = (fmtstate_.flags_ & ~std::ios_base::adjustfield) sl@0: | std::ios_base::internal; sl@0: // removes all adjustfield bits, and adds internal. sl@0: } sl@0: } sl@0: if(pad_scheme_ & spacepad) { sl@0: if(fmtstate_.flags_ & std::ios_base::showpos) sl@0: pad_scheme_ &= ~spacepad; sl@0: } sl@0: } sl@0: sl@0: sl@0: } } } // namespaces boost :: io :: detail sl@0: sl@0: sl@0: #endif // BOOST_FORMAT_INTERNALS_HPP