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