2 * Copyright (c) 2003 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;
48 alternate_form = false;
62 format_invalid(std::ostream &out)
66 out << "format invalid!!!" << endl;
72 _format_char(std::ostream &out, const T& data, Format &fmt)
81 _format_integer(std::ostream &out, const T& data, Format &fmt)
87 out.setf(ios::hex, ios::basefield);
91 out.setf(ios::oct, ios::basefield);
95 out.setf(ios::dec, ios::basefield);
99 if (fmt.alternate_form) {
101 out.setf(ios::showbase);
122 out.width(fmt.width);
124 if (fmt.flush_left && !fmt.fill_zero)
128 out.setf(ios::showpos);
131 out.setf(ios::uppercase);
136 template <typename T>
138 _format_float(std::ostream &out, const T& data, Format &fmt)
142 switch (fmt.float_format) {
143 case Format::scientific:
144 if (fmt.precision != -1) {
146 out.width(fmt.width);
148 if (fmt.precision == 0)
151 out.setf(ios::scientific);
153 out.precision(fmt.precision);
156 out.width(fmt.width);
159 out.setf(ios::uppercase);
163 if (fmt.precision != -1) {
165 out.width(fmt.width);
167 out.setf(ios::fixed);
168 out.precision(fmt.precision);
171 out.width(fmt.width);
176 if (fmt.precision != -1)
177 out.precision(fmt.precision);
180 out.width(fmt.width);
188 template <typename T>
190 _format_string(std::ostream &out, const T& data, Format &fmt)
194 #if defined(__GNUC__) && (__GNUC__ < 3) || 1
196 std::stringstream foo;
198 int flen = foo.str().size();
200 if (fmt.width > flen) {
201 char *spaces = new char[fmt.width - flen + 1];
202 memset(spaces, ' ', fmt.width - flen);
203 spaces[fmt.width - flen] = 0;
206 out << foo.str() << spaces;
208 out << spaces << foo.str();
217 out.width(fmt.width);
225 /////////////////////////////////////////////////////////////////////////////
227 // The code below controls the actual usage of formats for various types
233 template <typename T>
235 format_char(std::ostream &out, const T& data, Format &fmt)
236 { format_invalid(out); }
239 format_char(std::ostream &out, char data, Format &fmt)
240 { _format_char(out, data, fmt); }
243 format_char(std::ostream &out, unsigned char data, Format &fmt)
244 { _format_char(out, data, fmt); }
247 format_char(std::ostream &out, signed char data, Format &fmt)
248 { _format_char(out, data, fmt); }
251 format_char(std::ostream &out, short data, Format &fmt)
252 { _format_char(out, (char)data, fmt); }
255 format_char(std::ostream &out, unsigned short data, Format &fmt)
256 { _format_char(out, (char)data, fmt); }
259 format_char(std::ostream &out, int data, Format &fmt)
260 { _format_char(out, (char)data, fmt); }
263 format_char(std::ostream &out, unsigned int data, Format &fmt)
264 { _format_char(out, (char)data, fmt); }
267 format_char(std::ostream &out, long data, Format &fmt)
268 { _format_char(out, (char)data, fmt); }
271 format_char(std::ostream &out, unsigned long data, Format &fmt)
272 { _format_char(out, (char)data, fmt); }
275 format_char(std::ostream &out, long long data, Format &fmt)
276 { _format_char(out, (char)data, fmt); }
279 format_char(std::ostream &out, unsigned long long data, Format &fmt)
280 { _format_char(out, (char)data, fmt); }
285 template <typename T>
287 format_integer(std::ostream &out, const T &data, Format &fmt)
288 { _format_integer(out, data, fmt); }
292 format_integer(std::ostream &out, char data, Format &fmt)
293 { _format_integer(out, data, fmt); }
295 format_integer(std::ostream &out, unsigned char data, Format &fmt)
296 { _format_integer(out, data, fmt); }
298 format_integer(std::ostream &out, signed char data, Format &fmt)
299 { _format_integer(out, 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 { format_invalid(out); }
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); }
354 #endif // __CPRINTF_FORMATS_HH__