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;
58 alternate_form = false;
73 _format_char(std::ostream &out, const T &data, Format &fmt)
82 _format_integer(std::ostream &out, const T &data, Format &fmt)
88 out.setf(std::ios::hex, std::ios::basefield);
92 out.setf(std::ios::oct, std::ios::basefield);
96 out.setf(std::ios::dec, std::ios::basefield);
100 if (fmt.alternate_form) {
102 out.setf(std::ios::showbase);
123 out.width(fmt.width);
125 if (fmt.flush_left && !fmt.fill_zero)
126 out.setf(std::ios::left);
129 out.setf(std::ios::showpos);
132 out.setf(std::ios::uppercase);
137 template <typename T>
139 _format_float(std::ostream &out, const T &data, Format &fmt)
143 switch (fmt.float_format) {
144 case Format::scientific:
145 if (fmt.precision != -1) {
147 out.width(fmt.width);
149 if (fmt.precision == 0)
152 out.setf(std::ios::scientific);
154 out.precision(fmt.precision);
157 out.width(fmt.width);
160 out.setf(std::ios::uppercase);
164 if (fmt.precision != -1) {
166 out.width(fmt.width);
168 out.setf(std::ios::fixed);
169 out.precision(fmt.precision);
172 out.width(fmt.width);
177 if (fmt.precision != -1)
178 out.precision(fmt.precision);
181 out.width(fmt.width);
189 template <typename T>
191 _format_string(std::ostream &out, const T &data, Format &fmt)
195 #if defined(__GNUC__) && (__GNUC__ < 3) || 1
197 std::stringstream foo;
199 int flen = foo.str().size();
201 if (fmt.width > flen) {
202 char *spaces = new char[fmt.width - flen + 1];
203 memset(spaces, ' ', fmt.width - flen);
204 spaces[fmt.width - flen] = 0;
207 out << foo.str() << spaces;
209 out << spaces << foo.str();
218 out.width(fmt.width);
220 out.setf(std::ios::left);
226 /////////////////////////////////////////////////////////////////////////////
228 // The code below controls the actual usage of formats for various types
234 template <typename T>
236 format_char(std::ostream &out, const T &data, Format &fmt)
237 { out << "<bad arg type for char format>"; }
240 format_char(std::ostream &out, char data, Format &fmt)
241 { _format_char(out, data, fmt); }
244 format_char(std::ostream &out, unsigned char data, Format &fmt)
245 { _format_char(out, data, fmt); }
248 format_char(std::ostream &out, signed char data, Format &fmt)
249 { _format_char(out, data, fmt); }
252 format_char(std::ostream &out, short data, Format &fmt)
253 { _format_char(out, (char)data, fmt); }
256 format_char(std::ostream &out, unsigned short data, Format &fmt)
257 { _format_char(out, (char)data, fmt); }
260 format_char(std::ostream &out, int data, Format &fmt)
261 { _format_char(out, (char)data, fmt); }
264 format_char(std::ostream &out, unsigned int data, Format &fmt)
265 { _format_char(out, (char)data, fmt); }
268 format_char(std::ostream &out, long data, Format &fmt)
269 { _format_char(out, (char)data, fmt); }
272 format_char(std::ostream &out, unsigned long data, Format &fmt)
273 { _format_char(out, (char)data, fmt); }
276 format_char(std::ostream &out, long long data, Format &fmt)
277 { _format_char(out, (char)data, fmt); }
280 format_char(std::ostream &out, unsigned long long data, Format &fmt)
281 { _format_char(out, (char)data, fmt); }
286 template <typename T>
288 format_integer(std::ostream &out, const T &data, Format &fmt)
289 { _format_integer(out, data, fmt); }
291 format_integer(std::ostream &out, char data, Format &fmt)
292 { _format_integer(out, (int)data, fmt); }
294 format_integer(std::ostream &out, unsigned char data, Format &fmt)
295 { _format_integer(out, (int)data, fmt); }
297 format_integer(std::ostream &out, signed char data, Format &fmt)
298 { _format_integer(out, (int)data, fmt); }
301 format_integer(std::ostream &out, short data, Format &fmt)
302 { _format_integer(out, data, fmt); }
304 format_integer(std::ostream &out, unsigned short data, Format &fmt)
305 { _format_integer(out, data, fmt); }
307 format_integer(std::ostream &out, int data, Format &fmt)
308 { _format_integer(out, data, fmt); }
310 format_integer(std::ostream &out, unsigned int data, Format &fmt)
311 { _format_integer(out, data, fmt); }
313 format_integer(std::ostream &out, long data, Format &fmt)
314 { _format_integer(out, data, fmt); }
316 format_integer(std::ostream &out, unsigned long data, Format &fmt)
317 { _format_integer(out, data, fmt); }
319 format_integer(std::ostream &out, long long data, Format &fmt)
320 { _format_integer(out, data, fmt); }
322 format_integer(std::ostream &out, unsigned long long data, Format &fmt)
323 { _format_integer(out, data, fmt); }
327 // floating point formats
329 template <typename T>
331 format_float(std::ostream &out, const T &data, Format &fmt)
332 { out << "<bad arg type for float format>"; }
335 format_float(std::ostream &out, float data, Format &fmt)
336 { _format_float(out, data, fmt); }
339 format_float(std::ostream &out, double data, Format &fmt)
340 { _format_float(out, data, fmt); }
345 template <typename T>
347 format_string(std::ostream &out, const T &data, Format &fmt)
348 { _format_string(out, data, fmt); }
351 format_string(std::ostream &out, const std::stringstream &data, Format &fmt)
352 { _format_string(out, data.str(), fmt); }
356 #endif // __CPRINTF_FORMATS_HH__