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 __BASE_CPRINTF_FORMATS_HH__
30 #define __BASE_CPRINTF_FORMATS_HH__
46 enum { dec, hex, oct } base;
47 enum { none, string, integer, character, floating } format;
48 enum { best, fixed, scientific } float_format;
58 alternate_form = false;
69 get_precision = false;
76 _format_char(std::ostream &out, const T &data, Format &fmt)
85 _format_integer(std::ostream &out, const T &data, Format &fmt)
89 ios::fmtflags flags(out.flags());
93 out.setf(std::ios::hex, std::ios::basefield);
97 out.setf(std::ios::oct, std::ios::basefield);
101 out.setf(std::ios::dec, std::ios::basefield);
105 if (fmt.alternate_form) {
107 out.setf(std::ios::showbase);
128 out.width(fmt.width);
130 if (fmt.flush_left && !fmt.fill_zero)
131 out.setf(std::ios::left);
134 out.setf(std::ios::showpos);
137 out.setf(std::ios::uppercase);
144 template <typename T>
146 _format_float(std::ostream &out, const T &data, Format &fmt)
150 ios::fmtflags flags(out.flags());
155 switch (fmt.float_format) {
156 case Format::scientific:
157 if (fmt.precision != -1) {
159 out.width(fmt.width);
161 if (fmt.precision == 0)
164 out.setf(std::ios::scientific);
166 out.precision(fmt.precision);
169 out.width(fmt.width);
172 out.setf(std::ios::uppercase);
176 if (fmt.precision != -1) {
178 out.width(fmt.width);
180 out.setf(std::ios::fixed);
181 out.precision(fmt.precision);
184 out.width(fmt.width);
189 if (fmt.precision != -1)
190 out.precision(fmt.precision);
193 out.width(fmt.width);
203 template <typename T>
205 _format_string(std::ostream &out, const T &data, Format &fmt)
209 #if defined(__GNUC__) && (__GNUC__ < 3) || 1
211 std::stringstream foo;
213 int flen = foo.str().size();
215 if (fmt.width > flen) {
216 char *spaces = new char[fmt.width - flen + 1];
217 memset(spaces, ' ', fmt.width - flen);
218 spaces[fmt.width - flen] = 0;
221 out << foo.str() << spaces;
223 out << spaces << foo.str();
232 out.width(fmt.width);
234 out.setf(std::ios::left);
240 /////////////////////////////////////////////////////////////////////////////
242 // The code below controls the actual usage of formats for various types
248 template <typename T>
250 format_char(std::ostream &out, const T &data, Format &fmt)
251 { out << "<bad arg type for char format>"; }
254 format_char(std::ostream &out, char data, Format &fmt)
255 { _format_char(out, data, fmt); }
258 format_char(std::ostream &out, unsigned char data, Format &fmt)
259 { _format_char(out, data, fmt); }
262 format_char(std::ostream &out, signed char data, Format &fmt)
263 { _format_char(out, data, fmt); }
266 format_char(std::ostream &out, short data, Format &fmt)
267 { _format_char(out, (char)data, fmt); }
270 format_char(std::ostream &out, unsigned short data, Format &fmt)
271 { _format_char(out, (char)data, fmt); }
274 format_char(std::ostream &out, int data, Format &fmt)
275 { _format_char(out, (char)data, fmt); }
278 format_char(std::ostream &out, unsigned int data, Format &fmt)
279 { _format_char(out, (char)data, fmt); }
282 format_char(std::ostream &out, long data, Format &fmt)
283 { _format_char(out, (char)data, fmt); }
286 format_char(std::ostream &out, unsigned long data, Format &fmt)
287 { _format_char(out, (char)data, fmt); }
290 format_char(std::ostream &out, long long data, Format &fmt)
291 { _format_char(out, (char)data, fmt); }
294 format_char(std::ostream &out, unsigned long long data, Format &fmt)
295 { _format_char(out, (char)data, fmt); }
300 template <typename T>
302 format_integer(std::ostream &out, const T &data, Format &fmt)
303 { _format_integer(out, data, fmt); }
305 format_integer(std::ostream &out, char data, Format &fmt)
306 { _format_integer(out, (int)data, fmt); }
308 format_integer(std::ostream &out, unsigned char data, Format &fmt)
309 { _format_integer(out, (int)data, fmt); }
311 format_integer(std::ostream &out, signed char data, Format &fmt)
312 { _format_integer(out, (int)data, fmt); }
314 format_integer(std::ostream &out, const unsigned char *data, Format &fmt)
315 { _format_integer(out, (uintptr_t)data, fmt); }
317 format_integer(std::ostream &out, const signed char *data, Format &fmt)
318 { _format_integer(out, (uintptr_t)data, fmt); }
321 // floating point formats
323 template <typename T>
325 format_float(std::ostream &out, const T &data, Format &fmt)
326 { out << "<bad arg type for float format>"; }
329 format_float(std::ostream &out, float data, Format &fmt)
330 { _format_float(out, data, fmt); }
333 format_float(std::ostream &out, double data, Format &fmt)
334 { _format_float(out, data, fmt); }
339 template <typename T>
341 format_string(std::ostream &out, const T &data, Format &fmt)
342 { _format_string(out, data, fmt); }
346 #endif // __CPRINTF_FORMATS_HH__