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__
77 alternateForm = false;
95 _formatChar(std::ostream &out, const T &data, Format &fmt)
100 template <typename T>
102 _formatInteger(std::ostream &out, const T &data, Format &fmt)
104 std::ios::fmtflags flags(out.flags());
108 out.setf(std::ios::hex, std::ios::basefield);
112 out.setf(std::ios::oct, std::ios::basefield);
116 out.setf(std::ios::dec, std::ios::basefield);
120 if (fmt.alternateForm) {
122 out.setf(std::ios::showbase);
143 out.width(fmt.width);
145 if (fmt.flushLeft && !fmt.fillZero)
146 out.setf(std::ios::left);
149 out.setf(std::ios::showpos);
152 out.setf(std::ios::uppercase);
159 template <typename T>
161 _formatFloat(std::ostream &out, const T &data, Format &fmt)
163 std::ios::fmtflags flags(out.flags());
168 switch (fmt.floatFormat) {
169 case Format::Scientific:
170 if (fmt.precision != -1) {
172 out.width(fmt.width);
174 if (fmt.precision == 0)
177 out.setf(std::ios::scientific);
179 out.precision(fmt.precision);
180 } else if (fmt.width > 0) {
181 out.width(fmt.width);
185 out.setf(std::ios::uppercase);
189 if (fmt.precision != -1) {
191 out.width(fmt.width);
193 out.setf(std::ios::fixed);
194 out.precision(fmt.precision);
195 } else if (fmt.width > 0) {
196 out.width(fmt.width);
202 if (fmt.precision != -1)
203 out.precision(fmt.precision);
206 out.width(fmt.width);
216 template <typename T>
218 _formatString(std::ostream &out, const T &data, Format &fmt)
221 std::stringstream foo;
223 int flen = foo.str().size();
225 if (fmt.width > flen) {
226 char spaces[fmt.width - flen + 1];
227 std::memset(spaces, ' ', fmt.width - flen);
228 spaces[fmt.width - flen] = 0;
231 out << foo.str() << spaces;
233 out << spaces << foo.str();
242 /////////////////////////////////////////////////////////////////////////////
244 // The code below controls the actual usage of formats for various types
250 template <typename T>
252 formatChar(std::ostream &out, const T &data, Format &fmt)
254 out << "<bad arg type for char format>";
258 formatChar(std::ostream &out, char data, Format &fmt)
260 _formatChar(out, data, fmt);
264 formatChar(std::ostream &out, unsigned char data, Format &fmt)
266 _formatChar(out, data, fmt);
270 formatChar(std::ostream &out, signed char data, Format &fmt)
272 _formatChar(out, data, fmt);
276 formatChar(std::ostream &out, short data, Format &fmt)
278 _formatChar(out, (char)data, fmt);
282 formatChar(std::ostream &out, unsigned short data, Format &fmt)
284 _formatChar(out, (char)data, fmt);
288 formatChar(std::ostream &out, int data, Format &fmt)
290 _formatChar(out, (char)data, fmt);
294 formatChar(std::ostream &out, unsigned int data, Format &fmt)
296 _formatChar(out, (char)data, fmt);
300 formatChar(std::ostream &out, long data, Format &fmt)
302 _formatChar(out, (char)data, fmt);
306 formatChar(std::ostream &out, unsigned long data, Format &fmt)
308 _formatChar(out, (char)data, fmt);
312 formatChar(std::ostream &out, long long data, Format &fmt)
314 _formatChar(out, (char)data, fmt);
318 formatChar(std::ostream &out, unsigned long long data, Format &fmt)
320 _formatChar(out, (char)data, fmt);
326 template <typename T>
328 formatInteger(std::ostream &out, const T &data, Format &fmt)
330 _formatInteger(out, data, fmt);
333 formatInteger(std::ostream &out, char data, Format &fmt)
335 _formatInteger(out, (int)data, fmt);
338 formatInteger(std::ostream &out, unsigned char data, Format &fmt)
340 _formatInteger(out, (int)data, fmt);
343 formatInteger(std::ostream &out, signed char data, Format &fmt)
345 _formatInteger(out, (int)data, fmt);
348 formatInteger(std::ostream &out, const unsigned char *data, Format &fmt)
350 _formatInteger(out, (uintptr_t)data, fmt);
353 formatInteger(std::ostream &out, const signed char *data, Format &fmt)
355 _formatInteger(out, (uintptr_t)data, fmt);
359 // floating point formats
361 template <typename T>
363 formatFloat(std::ostream &out, const T &data, Format &fmt)
365 out << "<bad arg type for float format>";
369 formatFloat(std::ostream &out, float data, Format &fmt)
371 _formatFloat(out, data, fmt);
375 formatFloat(std::ostream &out, double data, Format &fmt)
377 _formatFloat(out, data, fmt);
383 template <typename T>
385 formatString(std::ostream &out, const T &data, Format &fmt)
387 _formatString(out, data, fmt);
392 #endif // __CPRINTF_FORMATS_HH__