Utils: Formating helpers (senf::format::eng, senf::format::dumpint)
[senf.git] / senf / Utils / Format.hh
1 // $Id$
2 //
3 // Copyright (C) 2009 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief Format public header */
25
26 #ifndef HH_SENF_senf_Utils_Format_
27 #define HH_SENF_senf_Utils_Format_ 1
28
29 // Custom includes
30 #include <limits>
31 #include <iostream>
32 #include <boost/utility/enable_if.hpp>
33 #include <boost/type_traits/is_signed.hpp>
34 #include <boost/type_traits/is_unsigned.hpp>
35
36 //#include "Format.mpp"
37 ///////////////////////////////hh.p////////////////////////////////////////
38
39 namespace senf {
40 namespace format {
41
42     /** \defgroup senf_utils_format Formating
43      */
44
45 #ifdef DOXYGEN
46
47     /** \brief Format value in engineering representation
48
49         The engineering representation is an exponential representation. Exponents however are
50         always multiples of 3:
51         <pre>
52         123.45   -> 123.450e+00
53         123.45e2 ->  12.345e+03
54         </pre>
55
56         Additionally, an optional delta value may be displayed:
57         <pre>
58         123.45+-1.34e+03
59         </pre>
60
61         senf::format::eng supports several formating options:
62         \par \c std::setw()
63             If the width is set >0, the output will be padded internally. If the width is set to
64             more than the minimal required output width including internal padding, the output is
65             padded on the left or right depending on the streams \c ajustfield setting (changed
66             with \c std::left, \c std:;right or \c std::interal). If the \c adjustfield is set to \c
67             internal, padding is added between the sign and the number.
68
69         \par \c std::\c setprecision()
70             The default stream precision is 6. This will schow values with 6 significant digits. The
71             count includes the number of digits in front of the decimal point.
72
73         \par \c std::\c showbase
74             If the \c showbase flag is set, Instead of writing out the scale exponent in numeric
75             form, output the corresponding SI prefix.
76
77         \par \c std::\c showpos
78             If the \c showpos flag is set, positive values will have a '+' sign.
79
80         \par \c std::\c showpoint
81             If the \c showpoint flag is set, the exponent will be output even if it is 0. Otherwise,
82             if \c width is set, the exponent will be replaced with 4 blanks.
83         
84         \par \c std::\c uppercase
85             If the \c uppercase flag is set, the exponent letter will be an uppercase 'E' instead of
86             'e'. SI prefixes are \e not uppercased, since some SI prefixes differ only in case.
87
88         \par \c std::\c setfill()
89             The fill character is honored for the outside padding but \e not for the internal
90             padding.
91
92         Examples:
93         \code
94             os << senf::format::eng(1.23);
95               -> "1.230"
96             os << std::setw(1) << senf::format::eng(1.23);
97               -> "   1.230    "
98             os << std::setw(25) << std::setprecision(5) << std::showpos << std::uppercase 
99                << std::internal << senf::format::eng(12345,67);
100               -> "+       12.35+-000.07E+03"
101             os << std::showbase << senf::format::eng(12345,67);
102               -> "12.345+-0.067k"
103         \endcode
104         
105         \param[in] v value
106         \param[in] d optional delta
107
108         \ingroup senf_utils_format
109      */
110     streamable_type eng(double v, double d=NAN);
111
112 #else
113
114     struct eng
115     {
116         eng(double v_, double d_ = std::numeric_limits<double>::quiet_NaN());
117
118         double v;
119         double d;
120     };
121
122     std::ostream & operator<<(std::ostream & os, eng const & v);
123
124 #endif
125
126 #ifdef DOXYGEN
127
128     /** \brief Dump integer value with internal representation
129
130         senf::format::dumpint() will output a signed or unsigned numeric argument in the following
131         representations:
132         \li hexadecimal notation
133         \li decimal notation
134         \li byte values interpreted as ASCII characters (in network byte order)
135
136         All fields will be padded depending on the size of the type to a byte boundary (e.g. a 32bit
137         integer type will be padded to 8 hex-digits, 10 decimal digits and 4 ASCII characters)
138
139         \ingroup senf_utils_format
140      */
141     template <class T>
142     streamable_type dumpint(T const & v);
143
144 #else 
145
146     template <class T> 
147     std::string dumpint(T const & v, 
148                         typename boost::enable_if<boost::is_signed<T> >::type * = 0);
149
150     template <class T> 
151     std::string dumpint(T const & v, 
152                         typename boost::enable_if<boost::is_unsigned<T> >::type * = 0);
153
154 #endif
155
156 }}
157
158 ///////////////////////////////hh.e////////////////////////////////////////
159 #include "Format.cci"
160 //#include "Format.ct"
161 #include "Format.cti"
162 #endif
163
164 \f
165 // Local Variables:
166 // mode: c++
167 // fill-column: 100
168 // comment-column: 40
169 // c-file-style: "senf"
170 // indent-tabs-mode: nil
171 // ispell-local-dictionary: "american"
172 // compile-command: "scons -u test"
173 // End: