New directory structure:
[gem5.git] / src / base / cprintf_formats.hh
1 /*
2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
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.
15 *
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.
27 */
28
29 #ifndef __CPRINTF_FORMATS_HH__
30 #define __CPRINTF_FORMATS_HH__
31
32 #include <sstream>
33 #include <ostream>
34
35 namespace cp {
36
37 struct Format
38 {
39 bool alternate_form;
40 bool flush_left;
41 bool print_sign;
42 bool blank_space;
43 bool fill_zero;
44 bool uppercase;
45 enum { dec, hex, oct } base;
46 enum { none, string, integer, character, floating } format;
47 enum { best, fixed, scientific } float_format;
48 int precision;
49 int width;
50
51 Format() { clear(); }
52
53 void clear()
54 {
55 alternate_form = false;
56 flush_left = false;
57 print_sign = false;
58 blank_space = false;
59 fill_zero = false;
60 uppercase = false;
61 base = dec;
62 format = none;
63 precision = -1;
64 width = 0;
65 }
66 };
67
68 template <typename T>
69 inline void
70 _format_char(std::ostream &out, const T &data, Format &fmt)
71 {
72 using namespace std;
73
74 out << data;
75 }
76
77 template <typename T>
78 inline void
79 _format_integer(std::ostream &out, const T &data, Format &fmt)
80 {
81 using namespace std;
82
83 switch (fmt.base) {
84 case Format::hex:
85 out.setf(ios::hex, ios::basefield);
86 break;
87
88 case Format::oct:
89 out.setf(ios::oct, ios::basefield);
90 break;
91
92 case Format::dec:
93 out.setf(ios::dec, ios::basefield);
94 break;
95 }
96
97 if (fmt.alternate_form) {
98 if (!fmt.fill_zero)
99 out.setf(ios::showbase);
100 else {
101 switch (fmt.base) {
102 case Format::hex:
103 out << "0x";
104 fmt.width -= 2;
105 break;
106 case Format::oct:
107 out << "0";
108 fmt.width -= 1;
109 break;
110 case Format::dec:
111 break;
112 }
113 }
114 }
115
116 if (fmt.fill_zero)
117 out.fill('0');
118
119 if (fmt.width > 0)
120 out.width(fmt.width);
121
122 if (fmt.flush_left && !fmt.fill_zero)
123 out.setf(ios::left);
124
125 if (fmt.print_sign)
126 out.setf(ios::showpos);
127
128 if (fmt.uppercase)
129 out.setf(ios::uppercase);
130
131 out << data;
132 }
133
134 template <typename T>
135 inline void
136 _format_float(std::ostream &out, const T &data, Format &fmt)
137 {
138 using namespace std;
139
140 switch (fmt.float_format) {
141 case Format::scientific:
142 if (fmt.precision != -1) {
143 if (fmt.width > 0)
144 out.width(fmt.width);
145
146 if (fmt.precision == 0)
147 fmt.precision = 1;
148 else
149 out.setf(ios::scientific);
150
151 out.precision(fmt.precision);
152 } else
153 if (fmt.width > 0)
154 out.width(fmt.width);
155
156 if (fmt.uppercase)
157 out.setf(ios::uppercase);
158 break;
159
160 case Format::fixed:
161 if (fmt.precision != -1) {
162 if (fmt.width > 0)
163 out.width(fmt.width);
164
165 out.setf(ios::fixed);
166 out.precision(fmt.precision);
167 } else
168 if (fmt.width > 0)
169 out.width(fmt.width);
170
171 break;
172
173 default:
174 if (fmt.precision != -1)
175 out.precision(fmt.precision);
176
177 if (fmt.width > 0)
178 out.width(fmt.width);
179
180 break;
181 }
182
183 out << data;
184 }
185
186 template <typename T>
187 inline void
188 _format_string(std::ostream &out, const T &data, Format &fmt)
189 {
190 using namespace std;
191
192 #if defined(__GNUC__) && (__GNUC__ < 3) || 1
193 if (fmt.width > 0) {
194 std::stringstream foo;
195 foo << data;
196 int flen = foo.str().size();
197
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;
202
203 if (fmt.flush_left)
204 out << foo.str() << spaces;
205 else
206 out << spaces << foo.str();
207
208 delete [] spaces;
209 } else
210 out << data;
211 } else
212 out << data;
213 #else
214 if (fmt.width > 0)
215 out.width(fmt.width);
216 if (fmt.flush_left)
217 out.setf(ios::left);
218
219 out << data;
220 #endif
221 }
222
223 /////////////////////////////////////////////////////////////////////////////
224 //
225 // The code below controls the actual usage of formats for various types
226 //
227
228 //
229 // character formats
230 //
231 template <typename T>
232 inline void
233 format_char(std::ostream &out, const T &data, Format &fmt)
234 { out << "<bad arg type for char format>"; }
235
236 inline void
237 format_char(std::ostream &out, char data, Format &fmt)
238 { _format_char(out, data, fmt); }
239
240 inline void
241 format_char(std::ostream &out, unsigned char data, Format &fmt)
242 { _format_char(out, data, fmt); }
243
244 inline void
245 format_char(std::ostream &out, signed char data, Format &fmt)
246 { _format_char(out, data, fmt); }
247
248 inline void
249 format_char(std::ostream &out, short data, Format &fmt)
250 { _format_char(out, (char)data, fmt); }
251
252 inline void
253 format_char(std::ostream &out, unsigned short data, Format &fmt)
254 { _format_char(out, (char)data, fmt); }
255
256 inline void
257 format_char(std::ostream &out, int data, Format &fmt)
258 { _format_char(out, (char)data, fmt); }
259
260 inline void
261 format_char(std::ostream &out, unsigned int data, Format &fmt)
262 { _format_char(out, (char)data, fmt); }
263
264 inline void
265 format_char(std::ostream &out, long data, Format &fmt)
266 { _format_char(out, (char)data, fmt); }
267
268 inline void
269 format_char(std::ostream &out, unsigned long data, Format &fmt)
270 { _format_char(out, (char)data, fmt); }
271
272 inline void
273 format_char(std::ostream &out, long long data, Format &fmt)
274 { _format_char(out, (char)data, fmt); }
275
276 inline void
277 format_char(std::ostream &out, unsigned long long data, Format &fmt)
278 { _format_char(out, (char)data, fmt); }
279
280 //
281 // integer formats
282 //
283 template <typename T>
284 inline void
285 format_integer(std::ostream &out, const T &data, Format &fmt)
286 { _format_integer(out, data, fmt); }
287 inline void
288 format_integer(std::ostream &out, char data, Format &fmt)
289 { _format_integer(out, data, fmt); }
290 inline void
291 format_integer(std::ostream &out, unsigned char data, Format &fmt)
292 { _format_integer(out, data, fmt); }
293 inline void
294 format_integer(std::ostream &out, signed char data, Format &fmt)
295 { _format_integer(out, data, fmt); }
296 #if 0
297 inline void
298 format_integer(std::ostream &out, short data, Format &fmt)
299 { _format_integer(out, data, fmt); }
300 inline void
301 format_integer(std::ostream &out, unsigned short data, Format &fmt)
302 { _format_integer(out, data, fmt); }
303 inline void
304 format_integer(std::ostream &out, int data, Format &fmt)
305 { _format_integer(out, data, fmt); }
306 inline void
307 format_integer(std::ostream &out, unsigned int data, Format &fmt)
308 { _format_integer(out, data, fmt); }
309 inline void
310 format_integer(std::ostream &out, long data, Format &fmt)
311 { _format_integer(out, data, fmt); }
312 inline void
313 format_integer(std::ostream &out, unsigned long data, Format &fmt)
314 { _format_integer(out, data, fmt); }
315 inline void
316 format_integer(std::ostream &out, long long data, Format &fmt)
317 { _format_integer(out, data, fmt); }
318 inline void
319 format_integer(std::ostream &out, unsigned long long data, Format &fmt)
320 { _format_integer(out, data, fmt); }
321 #endif
322
323 //
324 // floating point formats
325 //
326 template <typename T>
327 inline void
328 format_float(std::ostream &out, const T &data, Format &fmt)
329 { out << "<bad arg type for float format>"; }
330
331 inline void
332 format_float(std::ostream &out, float data, Format &fmt)
333 { _format_float(out, data, fmt); }
334
335 inline void
336 format_float(std::ostream &out, double data, Format &fmt)
337 { _format_float(out, data, fmt); }
338
339 //
340 // string formats
341 //
342 template <typename T>
343 inline void
344 format_string(std::ostream &out, const T &data, Format &fmt)
345 { _format_string(out, data, fmt); }
346
347 inline void
348 format_string(std::ostream &out, const std::stringstream &data, Format &fmt)
349 { _format_string(out, data.str(), fmt); }
350
351 } // namespace cp
352
353 #endif // __CPRINTF_FORMATS_HH__