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