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.
28 * Authors: Nathan Binkert
31 #ifndef __BASE_CPRINTF_FORMATS_HH__
32 #define __BASE_CPRINTF_FORMATS_HH__
48 enum { dec, hex, oct } base;
49 enum { none, string, integer, character, floating } format;
50 enum { best, fixed, scientific } float_format;
60 alternate_form = false;
71 get_precision = false;
78 _format_char(std::ostream &out, const T &data, Format &fmt)
87 _format_integer(std::ostream &out, const T &data, Format &fmt)
91 ios::fmtflags flags(out.flags());
95 out.setf(std::ios::hex, std::ios::basefield);
99 out.setf(std::ios::oct, std::ios::basefield);
103 out.setf(std::ios::dec, std::ios::basefield);
107 if (fmt.alternate_form) {
109 out.setf(std::ios::showbase);
130 out.width(fmt.width);
132 if (fmt.flush_left && !fmt.fill_zero)
133 out.setf(std::ios::left);
136 out.setf(std::ios::showpos);
139 out.setf(std::ios::uppercase);
146 template <typename T>
148 _format_float(std::ostream &out, const T &data, Format &fmt)
152 ios::fmtflags flags(out.flags());
154 switch (fmt.float_format) {
155 case Format::scientific:
156 if (fmt.precision != -1) {
158 out.width(fmt.width);
160 if (fmt.precision == 0)
163 out.setf(std::ios::scientific);
165 out.precision(fmt.precision);
168 out.width(fmt.width);
171 out.setf(std::ios::uppercase);
175 if (fmt.precision != -1) {
177 out.width(fmt.width);
179 out.setf(std::ios::fixed);
180 out.precision(fmt.precision);
183 out.width(fmt.width);
188 if (fmt.precision != -1)
189 out.precision(fmt.precision);
192 out.width(fmt.width);
202 template <typename T>
204 _format_string(std::ostream &out, const T &data, Format &fmt)
208 #if defined(__GNUC__) && (__GNUC__ < 3) || 1
210 std::stringstream foo;
212 int flen = foo.str().size();
214 if (fmt.width > flen) {
215 char *spaces = new char[fmt.width - flen + 1];
216 memset(spaces, ' ', fmt.width - flen);
217 spaces[fmt.width - flen] = 0;
220 out << foo.str() << spaces;
222 out << spaces << foo.str();
231 out.width(fmt.width);
233 out.setf(std::ios::left);
239 /////////////////////////////////////////////////////////////////////////////
241 // The code below controls the actual usage of formats for various types
247 template <typename T>
249 format_char(std::ostream &out, const T &data, Format &fmt)
250 { out << "<bad arg type for char format>"; }
253 format_char(std::ostream &out, char data, Format &fmt)
254 { _format_char(out, data, fmt); }
257 format_char(std::ostream &out, unsigned char data, Format &fmt)
258 { _format_char(out, data, fmt); }
261 format_char(std::ostream &out, signed char data, Format &fmt)
262 { _format_char(out, data, fmt); }
265 format_char(std::ostream &out, short data, Format &fmt)
266 { _format_char(out, (char)data, fmt); }
269 format_char(std::ostream &out, unsigned short data, Format &fmt)
270 { _format_char(out, (char)data, fmt); }
273 format_char(std::ostream &out, int data, Format &fmt)
274 { _format_char(out, (char)data, fmt); }
277 format_char(std::ostream &out, unsigned int data, Format &fmt)
278 { _format_char(out, (char)data, fmt); }
281 format_char(std::ostream &out, long data, Format &fmt)
282 { _format_char(out, (char)data, fmt); }
285 format_char(std::ostream &out, unsigned long data, Format &fmt)
286 { _format_char(out, (char)data, fmt); }
289 format_char(std::ostream &out, long long data, Format &fmt)
290 { _format_char(out, (char)data, fmt); }
293 format_char(std::ostream &out, unsigned long long data, Format &fmt)
294 { _format_char(out, (char)data, fmt); }
299 template <typename T>
301 format_integer(std::ostream &out, const T &data, Format &fmt)
302 { _format_integer(out, data, fmt); }
304 format_integer(std::ostream &out, char data, Format &fmt)
305 { _format_integer(out, (int)data, fmt); }
307 format_integer(std::ostream &out, unsigned char data, Format &fmt)
308 { _format_integer(out, (int)data, fmt); }
310 format_integer(std::ostream &out, signed char data, Format &fmt)
311 { _format_integer(out, (int)data, fmt); }
314 format_integer(std::ostream &out, short data, Format &fmt)
315 { _format_integer(out, data, fmt); }
317 format_integer(std::ostream &out, unsigned short data, Format &fmt)
318 { _format_integer(out, data, fmt); }
320 format_integer(std::ostream &out, int data, Format &fmt)
321 { _format_integer(out, data, fmt); }
323 format_integer(std::ostream &out, unsigned int data, Format &fmt)
324 { _format_integer(out, data, fmt); }
326 format_integer(std::ostream &out, long data, Format &fmt)
327 { _format_integer(out, data, fmt); }
329 format_integer(std::ostream &out, unsigned long data, Format &fmt)
330 { _format_integer(out, data, fmt); }
332 format_integer(std::ostream &out, long long data, Format &fmt)
333 { _format_integer(out, data, fmt); }
335 format_integer(std::ostream &out, unsigned long long data, Format &fmt)
336 { _format_integer(out, data, fmt); }
340 // floating point formats
342 template <typename T>
344 format_float(std::ostream &out, const T &data, Format &fmt)
345 { out << "<bad arg type for float format>"; }
348 format_float(std::ostream &out, float data, Format &fmt)
349 { _format_float(out, data, fmt); }
352 format_float(std::ostream &out, double data, Format &fmt)
353 { _format_float(out, data, fmt); }
358 template <typename T>
360 format_string(std::ostream &out, const T &data, Format &fmt)
361 { _format_string(out, data, fmt); }
364 format_string(std::ostream &out, const std::stringstream &data, Format &fmt)
365 { _format_string(out, data.str(), fmt); }
369 #endif // __CPRINTF_FORMATS_HH__