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