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