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