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__
45 enum { dec, hex, oct } base;
46 enum { none, string, integer, character, floating } format;
47 enum { best, fixed, scientific } float_format;
55 alternate_form = false;
70 _format_char(std::ostream &out, const T &data, Format &fmt)
79 _format_integer(std::ostream &out, const T &data, Format &fmt)
85 out.setf(ios::hex, ios::basefield);
89 out.setf(ios::oct, ios::basefield);
93 out.setf(ios::dec, ios::basefield);
97 if (fmt.alternate_form) {
99 out.setf(ios::showbase);
120 out.width(fmt.width);
122 if (fmt.flush_left && !fmt.fill_zero)
126 out.setf(ios::showpos);
129 out.setf(ios::uppercase);
134 template <typename T>
136 _format_float(std::ostream &out, const T &data, Format &fmt)
140 switch (fmt.float_format) {
141 case Format::scientific:
142 if (fmt.precision != -1) {
144 out.width(fmt.width);
146 if (fmt.precision == 0)
149 out.setf(ios::scientific);
151 out.precision(fmt.precision);
154 out.width(fmt.width);
157 out.setf(ios::uppercase);
161 if (fmt.precision != -1) {
163 out.width(fmt.width);
165 out.setf(ios::fixed);
166 out.precision(fmt.precision);
169 out.width(fmt.width);
174 if (fmt.precision != -1)
175 out.precision(fmt.precision);
178 out.width(fmt.width);
186 template <typename T>
188 _format_string(std::ostream &out, const T &data, Format &fmt)
192 #if defined(__GNUC__) && (__GNUC__ < 3) || 1
194 std::stringstream foo;
196 int flen = foo.str().size();
198 if (fmt.width > flen) {
199 char *spaces = new char[fmt.width - flen + 1];
200 memset(spaces, ' ', fmt.width - flen);
201 spaces[fmt.width - flen] = 0;
204 out << foo.str() << spaces;
206 out << spaces << foo.str();
215 out.width(fmt.width);
223 /////////////////////////////////////////////////////////////////////////////
225 // The code below controls the actual usage of formats for various types
231 template <typename T>
233 format_char(std::ostream &out, const T &data, Format &fmt)
234 { out << "<bad arg type for char format>"; }
237 format_char(std::ostream &out, char data, Format &fmt)
238 { _format_char(out, data, fmt); }
241 format_char(std::ostream &out, unsigned char data, Format &fmt)
242 { _format_char(out, data, fmt); }
245 format_char(std::ostream &out, signed char data, Format &fmt)
246 { _format_char(out, data, fmt); }
249 format_char(std::ostream &out, short data, Format &fmt)
250 { _format_char(out, (char)data, fmt); }
253 format_char(std::ostream &out, unsigned short data, Format &fmt)
254 { _format_char(out, (char)data, fmt); }
257 format_char(std::ostream &out, int data, Format &fmt)
258 { _format_char(out, (char)data, fmt); }
261 format_char(std::ostream &out, unsigned int data, Format &fmt)
262 { _format_char(out, (char)data, fmt); }
265 format_char(std::ostream &out, long data, Format &fmt)
266 { _format_char(out, (char)data, fmt); }
269 format_char(std::ostream &out, unsigned long data, Format &fmt)
270 { _format_char(out, (char)data, fmt); }
273 format_char(std::ostream &out, long long data, Format &fmt)
274 { _format_char(out, (char)data, fmt); }
277 format_char(std::ostream &out, unsigned long long data, Format &fmt)
278 { _format_char(out, (char)data, fmt); }
283 template <typename T>
285 format_integer(std::ostream &out, const T &data, Format &fmt)
286 { _format_integer(out, data, fmt); }
288 format_integer(std::ostream &out, char data, Format &fmt)
289 { _format_integer(out, data, fmt); }
291 format_integer(std::ostream &out, unsigned char data, Format &fmt)
292 { _format_integer(out, data, fmt); }
294 format_integer(std::ostream &out, signed char data, Format &fmt)
295 { _format_integer(out, data, fmt); }
298 format_integer(std::ostream &out, short data, Format &fmt)
299 { _format_integer(out, data, fmt); }
301 format_integer(std::ostream &out, unsigned short data, Format &fmt)
302 { _format_integer(out, data, fmt); }
304 format_integer(std::ostream &out, int data, Format &fmt)
305 { _format_integer(out, data, fmt); }
307 format_integer(std::ostream &out, unsigned int data, Format &fmt)
308 { _format_integer(out, data, fmt); }
310 format_integer(std::ostream &out, long data, Format &fmt)
311 { _format_integer(out, data, fmt); }
313 format_integer(std::ostream &out, unsigned long data, Format &fmt)
314 { _format_integer(out, data, fmt); }
316 format_integer(std::ostream &out, long long data, Format &fmt)
317 { _format_integer(out, data, fmt); }
319 format_integer(std::ostream &out, unsigned long long data, Format &fmt)
320 { _format_integer(out, data, fmt); }
324 // floating point formats
326 template <typename T>
328 format_float(std::ostream &out, const T &data, Format &fmt)
329 { out << "<bad arg type for float format>"; }
332 format_float(std::ostream &out, float data, Format &fmt)
333 { _format_float(out, data, fmt); }
336 format_float(std::ostream &out, double data, Format &fmt)
337 { _format_float(out, data, fmt); }
342 template <typename T>
344 format_string(std::ostream &out, const T &data, Format &fmt)
345 { _format_string(out, data, fmt); }
348 format_string(std::ostream &out, const std::stringstream &data, Format &fmt)
349 { _format_string(out, data.str(), fmt); }
353 #endif // __CPRINTF_FORMATS_HH__