2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #ifndef __CPRINTF_FORMATS_HH__
30 #define __CPRINTF_FORMATS_HH__
40 enum { dec, hex, oct } base;
41 enum { none, string, integer, character, floating } format;
42 enum { best, fixed, scientific } float_format;
50 alternate_form = false;
65 _format_char(std::ostream &out, const T &data, Format &fmt)
74 _format_integer(std::ostream &out, const T &data, Format &fmt)
80 out.setf(ios::hex, ios::basefield);
84 out.setf(ios::oct, ios::basefield);
88 out.setf(ios::dec, ios::basefield);
92 if (fmt.alternate_form) {
94 out.setf(ios::showbase);
115 out.width(fmt.width);
117 if (fmt.flush_left && !fmt.fill_zero)
121 out.setf(ios::showpos);
124 out.setf(ios::uppercase);
129 template <typename T>
131 _format_float(std::ostream &out, const T &data, Format &fmt)
135 switch (fmt.float_format) {
136 case Format::scientific:
137 if (fmt.precision != -1) {
139 out.width(fmt.width);
141 if (fmt.precision == 0)
144 out.setf(ios::scientific);
146 out.precision(fmt.precision);
149 out.width(fmt.width);
152 out.setf(ios::uppercase);
156 if (fmt.precision != -1) {
158 out.width(fmt.width);
160 out.setf(ios::fixed);
161 out.precision(fmt.precision);
164 out.width(fmt.width);
169 if (fmt.precision != -1)
170 out.precision(fmt.precision);
173 out.width(fmt.width);
181 template <typename T>
183 _format_string(std::ostream &out, const T &data, Format &fmt)
187 #if defined(__GNUC__) && (__GNUC__ < 3) || 1
189 std::stringstream foo;
191 int flen = foo.str().size();
193 if (fmt.width > flen) {
194 char *spaces = new char[fmt.width - flen + 1];
195 memset(spaces, ' ', fmt.width - flen);
196 spaces[fmt.width - flen] = 0;
199 out << foo.str() << spaces;
201 out << spaces << foo.str();
210 out.width(fmt.width);
218 /////////////////////////////////////////////////////////////////////////////
220 // The code below controls the actual usage of formats for various types
226 template <typename T>
228 format_char(std::ostream &out, const T &data, Format &fmt)
229 { out << "<bad arg type for char format>"; }
232 format_char(std::ostream &out, char data, Format &fmt)
233 { _format_char(out, data, fmt); }
236 format_char(std::ostream &out, unsigned char data, Format &fmt)
237 { _format_char(out, data, fmt); }
240 format_char(std::ostream &out, signed char data, Format &fmt)
241 { _format_char(out, data, fmt); }
244 format_char(std::ostream &out, short data, Format &fmt)
245 { _format_char(out, (char)data, fmt); }
248 format_char(std::ostream &out, unsigned short data, Format &fmt)
249 { _format_char(out, (char)data, fmt); }
252 format_char(std::ostream &out, int data, Format &fmt)
253 { _format_char(out, (char)data, fmt); }
256 format_char(std::ostream &out, unsigned int data, Format &fmt)
257 { _format_char(out, (char)data, fmt); }
260 format_char(std::ostream &out, long data, Format &fmt)
261 { _format_char(out, (char)data, fmt); }
264 format_char(std::ostream &out, unsigned long data, Format &fmt)
265 { _format_char(out, (char)data, fmt); }
268 format_char(std::ostream &out, long long data, Format &fmt)
269 { _format_char(out, (char)data, fmt); }
272 format_char(std::ostream &out, unsigned long long data, Format &fmt)
273 { _format_char(out, (char)data, fmt); }
278 template <typename T>
280 format_integer(std::ostream &out, const T &data, Format &fmt)
281 { _format_integer(out, data, fmt); }
283 format_integer(std::ostream &out, char data, Format &fmt)
284 { _format_integer(out, data, fmt); }
286 format_integer(std::ostream &out, unsigned char data, Format &fmt)
287 { _format_integer(out, data, fmt); }
289 format_integer(std::ostream &out, signed char data, Format &fmt)
290 { _format_integer(out, data, fmt); }
293 format_integer(std::ostream &out, short data, Format &fmt)
294 { _format_integer(out, data, fmt); }
296 format_integer(std::ostream &out, unsigned short data, Format &fmt)
297 { _format_integer(out, data, fmt); }
299 format_integer(std::ostream &out, int data, Format &fmt)
300 { _format_integer(out, data, fmt); }
302 format_integer(std::ostream &out, unsigned int data, Format &fmt)
303 { _format_integer(out, data, fmt); }
305 format_integer(std::ostream &out, long data, Format &fmt)
306 { _format_integer(out, data, fmt); }
308 format_integer(std::ostream &out, unsigned long data, Format &fmt)
309 { _format_integer(out, data, fmt); }
311 format_integer(std::ostream &out, long long data, Format &fmt)
312 { _format_integer(out, data, fmt); }
314 format_integer(std::ostream &out, unsigned long long data, Format &fmt)
315 { _format_integer(out, data, fmt); }
319 // floating point formats
321 template <typename T>
323 format_float(std::ostream &out, const T &data, Format &fmt)
324 { out << "<bad arg type for float format>"; }
327 format_float(std::ostream &out, float data, Format &fmt)
328 { _format_float(out, data, fmt); }
331 format_float(std::ostream &out, double data, Format &fmt)
332 { _format_float(out, data, fmt); }
337 template <typename T>
339 format_string(std::ostream &out, const T &data, Format &fmt)
340 { _format_string(out, data, fmt); }
343 format_string(std::ostream &out, const std::stringstream &data, Format &fmt)
344 { _format_string(out, data.str(), fmt); }
346 #endif // __CPRINTF_FORMATS_HH__