2 // Boost general library 'format' ---------------------------
3 // See http://www.boost.org for updates, documentation, and revision history.
5 // (C) Samuel Krempp 2001
6 // krempp@crans.ens-cachan.fr
7 // Permission to copy, use, modify, sell and
8 // distribute this software is granted provided this copyright notice appears
9 // in all copies. This software is provided "as is" without express or implied
10 // warranty, and with no claim as to its suitability for any purpose.
12 // ideas taken from RĂ¼diger Loos's format class
13 // and Karl Nelson's ofstream
15 // ----------------------------------------------------------------------------
16 // internals.hpp : internal structs. included by format.hpp
17 // stream_format_state, and format_item
18 // ----------------------------------------------------------------------------
21 #ifndef BOOST_FORMAT_INTERNALS_HPP
22 #define BOOST_FORMAT_INTERNALS_HPP
34 // set of params that define the format state of a stream
36 template<class Ch, class Tr>
37 struct stream_format_state
39 typedef BOOST_IO_STD basic_ios<Ch, Tr> basic_ios;
41 std::streamsize width_;
42 std::streamsize precision_;
44 std::ios_base::fmtflags flags_;
46 stream_format_state() : width_(-1), precision_(-1), fill_(0), flags_(std::ios_base::dec) {}
47 stream_format_state(basic_ios& os) {set_by_stream(os); }
49 void apply_on(basic_ios & os) const; //- applies format_state to the stream
50 template<class T> void apply_manip(T manipulator) //- modifies state by applying manipulator.
51 { apply_manip_body<Ch, Tr, T>( *this, manipulator) ; }
52 void reset(); //- sets to default state.
53 void set_by_stream(const basic_ios& os); //- sets to os's state.
59 // format_item : stores all parameters that can be defined by directives in the format-string
61 template<class Ch, class Tr>
64 enum pad_values { zeropad = 1, spacepad =2, centered=4, tabulation = 8 };
66 enum arg_values { argN_no_posit = -1, // non-positional directive. argN will be set later.
67 argN_tabulation = -2, // tabulation directive. (no argument read)
68 argN_ignored = -3 // ignored directive. (no argument read)
70 typedef BOOST_IO_STD basic_ios<Ch, Tr> basic_ios;
71 typedef detail::stream_format_state<Ch, Tr> stream_format_state;
72 typedef std::basic_string<Ch, Tr> string_t;
73 typedef BOOST_IO_STD basic_ostringstream<Ch, Tr> internal_stream_t;
76 int argN_; //- argument number (starts at 0, eg : %1 => argN=0)
77 // negative values are used for items that don't process
79 string_t res_; //- result of the formatting of this item
80 string_t appendix_; //- piece of string between this item and the next
82 stream_format_state ref_state_;// set by parsing the format_string, is only affected by modify_item
83 stream_format_state state_; // always same as ref_state, _unless_ modified by manipulators 'group(..)'
85 // non-stream format-state parameters
86 signed int truncate_; //- is >=0 for directives like %.5s (take 5 chars from the string)
87 unsigned int pad_scheme_; //- several possible padding schemes can mix. see pad_values
89 format_item() : argN_(argN_no_posit), truncate_(-1), pad_scheme_(0) {}
91 void compute_states(); // sets states according to truncate and pad_scheme.
96 // -----------------------------------------------------------
98 // -----------------------------------------------------------
100 // --- stream_format_state:: -------------------------------------------
101 template<class Ch, class Tr> inline
102 void stream_format_state<Ch,Tr> ::apply_on(basic_ios & os) const
103 // set the state of this stream according to our params
108 os.precision(precision_);
114 template<class Ch, class Tr> inline
115 void stream_format_state<Ch,Tr> ::set_by_stream(const basic_ios& os)
116 // set our params according to the state of this stream
120 precision_ = os.precision();
124 template<class Ch, class Tr, class T> inline
125 void apply_manip_body( stream_format_state<Ch, Tr>& self,
127 // modify our params according to the manipulator
129 BOOST_IO_STD basic_stringstream<Ch, Tr> ss;
132 self.set_by_stream( ss );
135 template<class Ch, class Tr> inline
136 void stream_format_state<Ch,Tr> ::reset()
137 // set our params to standard's default state
139 width_=-1; precision_=-1; fill_=0;
140 flags_ = std::ios_base::dec;
144 // --- format_items:: -------------------------------------------
145 template<class Ch, class Tr> inline
146 void format_item<Ch, Tr> ::compute_states()
147 // reflect pad_scheme_ on state_ and ref_state_
148 // because some pad_schemes has complex consequences on several state params.
150 if(pad_scheme_ & zeropad)
152 if(ref_state_.flags_ & std::ios_base::left)
154 pad_scheme_ = pad_scheme_ & (~zeropad); // ignore zeropad in left alignment
158 ref_state_.fill_='0';
159 ref_state_.flags_ |= std::ios_base::internal;
166 } } } // namespaces boost :: io :: detail
169 #endif // BOOST_FORMAT_INTERNALS_HPP