]> git.lyx.org Git - lyx.git/blob - boost/boost/format/internals.hpp
update boost to pre-1.30.0
[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 #include <sstream>
27
28 namespace boost {
29 namespace io {
30 namespace detail {
31
32
33 // --------------
34 // set of params that define the format state of a stream
35
36 template<class Ch, class Tr> 
37 struct stream_format_state 
38 {
39   typedef BOOST_IO_STD basic_ios<Ch, Tr>   basic_ios;
40
41   std::streamsize width_;
42   std::streamsize precision_;
43   Ch fill_; 
44   std::ios_base::fmtflags flags_;
45
46   stream_format_state()       : width_(-1), precision_(-1), fill_(0), flags_(std::ios_base::dec)  {}
47   stream_format_state(basic_ios& os)                  {set_by_stream(os); }
48
49   void apply_on(basic_ios & os) const;                //- applies format_state to the stream
50   template<class T> void apply_manip(T manipulator)   //- modifies state by applying manipulator.
51        { apply_manip_body<Ch, Tr, T>( *this, manipulator) ; }
52   void reset();                                       //- sets to default state.
53   void set_by_stream(const basic_ios& os);            //- sets to os's state.
54 };  
55
56
57
58 // --------------
59 // format_item : stores all parameters that can be defined by directives in the format-string
60
61 template<class Ch, class Tr>  
62 struct format_item 
63 {     
64   enum pad_values { zeropad = 1, spacepad =2, centered=4, tabulation = 8 };
65
66   enum arg_values { argN_no_posit   = -1, // non-positional directive. argN will be set later.
67                     argN_tabulation = -2, // tabulation directive. (no argument read) 
68                     argN_ignored    = -3  // ignored directive. (no argument read)
69   };
70   typedef BOOST_IO_STD basic_ios<Ch, Tr>              basic_ios;
71   typedef detail::stream_format_state<Ch, Tr>         stream_format_state;
72   typedef std::basic_string<Ch, Tr>           string_t;
73   typedef BOOST_IO_STD basic_ostringstream<Ch, Tr>    internal_stream_t;
74
75
76   int         argN_;           //- argument number (starts at 0,  eg : %1 => argN=0)
77                                //  negative values are used for items that don't process
78                                //  an argument
79   string_t    res_;            //- result of the formatting of this item
80   string_t    appendix_;       //- piece of string between this item and the next
81
82   stream_format_state ref_state_;// set by parsing the format_string, is only affected by modify_item
83   stream_format_state state_;  // always same as ref_state, _unless_ modified by manipulators 'group(..)'
84
85   // non-stream format-state parameters
86   signed int truncate_;        //- is >=0 for directives like %.5s (take 5 chars from the string)
87   unsigned int pad_scheme_;    //- several possible padding schemes can mix. see pad_values
88
89   format_item() : argN_(argN_no_posit), truncate_(-1), pad_scheme_(0)  {}
90
91   void compute_states();      // sets states  according to truncate and pad_scheme.
92 }; 
93
94
95
96 // -----------------------------------------------------------
97 // Definitions
98 // -----------------------------------------------------------
99
100 // --- stream_format_state:: -------------------------------------------
101 template<class Ch, class Tr> inline
102 void stream_format_state<Ch,Tr> ::apply_on(basic_ios & os) const
103   // set the state of this stream according to our params
104 {
105       if(width_ != -1)
106         os.width(width_);
107       if(precision_ != -1)
108         os.precision(precision_);
109       if(fill_ != 0)
110         os.fill(fill_);
111       os.flags(flags_);
112 }
113
114 template<class Ch, class Tr>      inline
115 void stream_format_state<Ch,Tr> ::set_by_stream(const basic_ios& os) 
116   // set our params according to the state of this stream
117 {
118       flags_ = os.flags();
119       width_ = os.width();
120       precision_ = os.precision();
121       fill_ = os.fill();
122 }
123
124 template<class Ch, class Tr, class T>  inline
125 void apply_manip_body( stream_format_state<Ch, Tr>& self,
126                        T manipulator) 
127   // modify our params according to the manipulator
128 {
129       BOOST_IO_STD basic_stringstream<Ch, Tr>  ss;
130       self.apply_on( ss );
131       ss << manipulator;
132       self.set_by_stream( ss );
133 }
134
135 template<class Ch, class Tr> inline
136 void stream_format_state<Ch,Tr> ::reset() 
137   // set our params to standard's default state
138 {
139       width_=-1; precision_=-1; fill_=0; 
140       flags_ = std::ios_base::dec; 
141 }
142
143
144 // --- format_items:: -------------------------------------------
145 template<class Ch, class Tr> inline
146 void format_item<Ch, Tr> ::compute_states() 
147   // reflect pad_scheme_   on  state_ and ref_state_ 
148   //   because some pad_schemes has complex consequences on several state params.
149 {
150   if(pad_scheme_ & zeropad) 
151   {
152     if(ref_state_.flags_ & std::ios_base::left) 
153     {
154       pad_scheme_ = pad_scheme_ & (~zeropad); // ignore zeropad in left alignment
155     }
156     else 
157     { 
158       ref_state_.fill_='0'; 
159       ref_state_.flags_ |= std::ios_base::internal;
160     }
161   }
162   state_ = ref_state_;
163 }
164
165
166 } } } // namespaces boost :: io :: detail
167
168
169 #endif // BOOST_FORMAT_INTERNALS_HPP