382c870255dcd94dda370d15fc254bfbd30cb763
[senf.git] / senf / Utils / Format.cc
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 non-inline non-template implementation */
25
26 #include "Format.hh"
27 #include "Format.ih"
28
29 // Custom includes
30 #include <cmath>
31 #include <boost/io/ios_state.hpp>
32 #include <iomanip>
33 #include <sstream>
34
35 //#include "Format.mpp"
36 #define prefix_
37 //-/////////////////////////////////////////////////////////////////////////////////////////////////
38
39 namespace {
40
41     char const SIPrefix[] = { 'y', 'z', 'a', 'f', 'p', 'n', 'u', 'm',
42                               ' ',
43                               'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y' };
44     unsigned const SIScales = 8;
45
46 }
47
48 prefix_ std::ostream & senf::format::operator<<(std::ostream & os, eng const & v_)
49 {
50     boost::io::ios_base_all_saver ibas (os);
51     boost::io::ios_fill_saver ifs (os);
52
53     os.setf(v_.flags_, v_.mask_);
54     if (v_.havePrecision_)
55         os.precision(v_.precision_);
56     if (v_.haveWidth_)
57         os.width(v_.width_);
58     if (v_.haveFill_)
59         os.fill(v_.fill_);
60
61     unsigned prec (os.precision());
62     if (prec < 4)
63         prec = 4;
64     unsigned w (os.width());
65     char fill (os.fill());
66     unsigned minw (prec+2+((os.flags() & std::ios_base::showbase) ? 1 : 4));
67     std::ios_base::fmtflags flags (os.flags());
68     std::ios_base::fmtflags align (flags & std::ios_base::adjustfield);
69     if (! std::isnan(v_.d_))
70         minw += prec+3;
71
72     float ref (std::fabs(v_.v_));
73     float v (v_.v_);
74     float d (0.0);
75     if (! std::isnan(v_.d_)) d = std::fabs(v_.d_);
76     int scale (0);
77
78     if (d > ref) ref = d;
79     while (ref >= 1000.0) {
80         ref /= 1000.0;
81         v /= 1000.0;
82         d /= 1000.0;
83         scale += 3;
84     }
85     while (ref > 0 && ref < 1) {
86         ref *= 1000.0;
87         v *= 1000.0;
88         d *= 1000.0;
89         scale -= 3;
90     }
91
92     os << std::dec << std::setprecision(prec-3) << std::fixed;
93     if (w > 0) {
94         if ((align == 0 || align == std::ios_base::right || align == std::ios_base::internal))
95             os << std::setw(prec+2+(w>minw ? w-minw : 0));
96         else
97             os << std::right << std::setfill(' ') << std::setw(prec+2);
98     }
99     else
100         os << std::right;
101     os << v;
102
103     os << std::setfill('0') << std::noshowpos;
104     if (! std::isnan(v_.d_)) {
105         os << "+-";
106         if (w > 0)
107             os << std::setw(prec+1);
108         os << d;
109     }
110
111     if ((flags & std::ios_base::showbase) && unsigned(std::abs(scale/3)) <= SIScales) {
112         if (w > 0 || scale != 0)
113             os << SIPrefix[scale/3+SIScales];
114     }
115     else if ((flags & std::ios_base::showpoint) || scale != 0)
116         os << ((flags & std::ios_base::uppercase)?'E':'e')
117            << std::showpos << std::internal << std::setw(3) << scale;
118     else if (w > 0)
119         os << "    ";
120     if (w > minw && align == std::ios_base::left)
121         os << std::setfill(fill) << std::setw(w-minw) << "";
122
123     return os;
124 }
125
126 prefix_ std::string senf::format::detail::dumpintSigned(long long v, unsigned bits)
127 {
128     if (v<0) return dumpintUnsigned(-v,bits,-1);
129     else     return dumpintUnsigned(v,bits,+1);
130 }
131
132 prefix_ std::string senf::format::detail::dumpintUnsigned(unsigned long long v, unsigned bits,
133                                                           int sign)
134 {
135     int bytes ((bits+7)/8);
136     int digs (int(2.4*bytes)+1);
137     std::stringstream ss;
138     ss << (sign ? (sign<0 ? "-" : " ") : "")
139        << "0x" << std::setw(2*bytes) << std::setfill('0') << std::hex
140        << 1u*v
141        << " (" << std::setw(digs+(sign ? 1 : 0)) << std::setfill(' ') << std::dec;
142     if (sign)
143         ss << sign*static_cast<long long>(v);
144     else
145         ss << 1u*v;
146     ss << ") (";
147     for (int i (bytes-1); i>=0; --i) {
148         char c ((v>>(8*i))&0xff);
149         ss << ((c>=32 && c<=126) ? c : '.');
150     }
151     ss << ')';
152     return ss.str();
153 }
154
155 unsigned int senf::format::IndentHelper::static_level = 0;
156
157
158 //-/////////////////////////////////////////////////////////////////////////////////////////////////
159 #undef prefix_
160 //#include "Format.mpp"
161
162 \f
163 // Local Variables:
164 // mode: c++
165 // fill-column: 100
166 // comment-column: 40
167 // c-file-style: "senf"
168 // indent-tabs-mode: nil
169 // ispell-local-dictionary: "american"
170 // compile-command: "scons -u test"
171 // End: