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
35 // set of params that define the format state of a stream
37 template<class Ch, class Tr>
38 struct stream_format_state
40 typedef BOOST_IO_STD basic_ios<Ch, Tr> basic_ios;
42 std::streamsize width_;
43 std::streamsize precision_;
45 std::ios_base::fmtflags flags_;
47 stream_format_state() : width_(-1), precision_(-1), fill_(0), flags_(std::ios_base::dec) {}
48 stream_format_state(basic_ios& os) {set_by_stream(os); }
50 void apply_on(basic_ios & os) const; //- applies format_state to the stream
51 template<class T> void apply_manip(T manipulator) //- modifies state by applying manipulator.
52 { apply_manip_body<Ch, Tr, T>( *this, manipulator) ; }
53 void reset(); //- sets to default state.
54 void set_by_stream(const basic_ios& os); //- sets to os's state.
60 // format_item : stores all parameters that can be defined by directives in the format-string
62 template<class Ch, class Tr>
65 enum pad_values { zeropad = 1, spacepad =2, centered=4, tabulation = 8 };
67 enum arg_values { argN_no_posit = -1, // non-positional directive. argN will be set later.
68 argN_tabulation = -2, // tabulation directive. (no argument read)
69 argN_ignored = -3 // ignored directive. (no argument read)
71 typedef BOOST_IO_STD basic_ios<Ch, Tr> basic_ios;
72 typedef detail::stream_format_state<Ch, Tr> stream_format_state;
73 typedef std::basic_string<Ch, Tr> string_t;
74 typedef BOOST_IO_STD basic_ostringstream<Ch, Tr> internal_stream_t;
77 int argN_; //- argument number (starts at 0, eg : %1 => argN=0)
78 // negative values are used for items that don't process
80 string_t res_; //- result of the formatting of this item
81 string_t appendix_; //- piece of string between this item and the next
83 stream_format_state state_; // can be modified by manipulators
84 stream_format_state ref_state_;// set from the format_string, is only affected by modify_item
86 // non-stream format-state parameters
87 signed int truncate_; //- is >=0 for directives like %.5s (take 5 chars from the string)
88 unsigned int pad_scheme_; //- several possible padding schemes can mix. see pad_values
90 format_item() : argN_(argN_no_posit), truncate_(-1), pad_scheme_(0) {}
92 void compute_states(); // sets states according to truncate and pad_scheme.
97 // -----------------------------------------------------------
99 // -----------------------------------------------------------
101 // --- stream_format_state:: -------------------------------------------
102 template<class Ch, class Tr> inline
103 void stream_format_state<Ch,Tr> ::apply_on(basic_ios & os) const
104 // set the state of this stream according to our params
109 os.precision(precision_);
115 template<class Ch, class Tr> inline
116 void stream_format_state<Ch,Tr> ::set_by_stream(const basic_ios& os)
117 // set our params according to the state of this stream
121 precision_ = os.precision();
125 template<class Ch, class Tr, class T> inline
126 void apply_manip_body( stream_format_state<Ch, Tr>& self,
128 // modify our params according to the manipulator
130 BOOST_IO_STD basic_stringstream<Ch, Tr> ss;
133 self.set_by_stream( ss );
136 template<class Ch, class Tr> inline
137 void stream_format_state<Ch,Tr> ::reset()
138 // set our params to standard's default state
140 width_=-1; precision_=-1; fill_=0;
141 flags_ = std::ios_base::dec;
145 // --- format_items:: -------------------------------------------
146 template<class Ch, class Tr> inline
147 void format_item<Ch, Tr> ::compute_states()
148 // reflect pad_scheme_ on state_ and ref_state_
149 // because some apd_schemes has complex consequences on several state params.
151 if(pad_scheme_ & zeropad)
153 if(ref_state_.flags_ & std::ios_base::left)
155 pad_scheme_ = pad_scheme_ & (~zeropad); // ignore zeropad in left alignment
159 ref_state_.fill_='0';
160 ref_state_.flags_ |= std::ios_base::internal;
167 } } } // namespaces boost :: io :: detail
170 #endif // BOOST_FORMAT_INTERNALS_HPP