4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief Format non-inline non-template implementation */
31 #include <boost/io/ios_state.hpp>
35 //#include "Format.mpp"
37 ///////////////////////////////cc.p////////////////////////////////////////
41 char const SIPrefix[] = { 'y', 'z', 'a', 'f', 'p', 'n', 'u', 'm',
43 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y' };
44 unsigned const SIScales = 8;
48 prefix_ std::ostream & senf::format::operator<<(std::ostream & os, eng const & v_)
50 boost::io::ios_base_all_saver ibas (os);
51 boost::io::ios_fill_saver ifs (os);
53 unsigned prec (os.precision());
56 unsigned w (os.width());
57 char fill (os.fill());
58 unsigned minw (prec+2+((os.flags() & std::ios_base::showbase) ? 1 : 4));
59 std::ios_base::fmtflags align (os.flags() & std::ios_base::adjustfield);
60 if (! std::isnan(v_.d))
63 double ref (std::fabs(v_.v));
66 if (! std::isnan(v_.d)) d = std::fabs(v_.d);
70 while (ref >= 1000.0) {
76 while (ref > 0 && ref < 1) {
83 os << std::dec << std::setprecision(prec-3) << std::fixed;
85 if ((align == 0 || align == std::ios_base::right || align == std::ios_base::internal)
87 os << std::setw(prec+2+w-minw);
89 os << std::right << std::setfill(' ') << std::setw(prec+2);
95 os << std::setfill('0') << std::noshowpos;
96 if (! std::isnan(v_.d)) {
99 os << std::setw(prec+1);
103 if ((os.flags() & std::ios_base::showbase) && unsigned(std::abs(scale/3)) <= SIScales) {
104 if (w > 0 || scale != 0)
105 os << SIPrefix[scale/3+SIScales];
107 else if ((os.flags() & std::ios_base::showpoint) || scale != 0)
108 os << ((os.flags() & std::ios_base::uppercase)?'E':'e')
109 << std::showpos << std::internal << std::setw(3) << scale;
112 if (w > minw && align == std::ios_base::left)
113 os << std::setfill(fill) << std::setw(w-minw) << "";
118 prefix_ std::string senf::format::detail::dumpintSigned(long long v, unsigned bits)
120 if (v<0) return dumpintUnsigned(-v,bits,-1);
121 else return dumpintUnsigned(v,bits,+1);
124 prefix_ std::string senf::format::detail::dumpintUnsigned(unsigned long long v, unsigned bits,
127 int bytes ((bits+7)/8);
128 int digs (int(2.4*bytes)+1);
129 std::stringstream ss;
130 ss << (sign ? (sign<0 ? "-" : " ") : "")
131 << "0x" << std::setw(2*bytes) << std::setfill('0') << std::hex
133 << " (" << std::setw(digs+(sign ? 1 : 0)) << std::setfill(' ') << std::dec;
135 ss << sign*static_cast<long long>(v);
139 for (int i (bytes-1); i>=0; --i) {
140 char c ((v>>(8*i))&0xff);
141 ss << ((c>=32 && c<=127) ? c : '.');
148 ///////////////////////////////cc.e////////////////////////////////////////
150 //#include "Format.mpp"
156 // comment-column: 40
157 // c-file-style: "senf"
158 // indent-tabs-mode: nil
159 // ispell-local-dictionary: "american"
160 // compile-command: "scons -u test"