gem5  v22.1.0.0
cprintf_formats.hh
Go to the documentation of this file.
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 __BASE_CPRINTF_FORMATS_HH__
30 #define __BASE_CPRINTF_FORMATS_HH__
31 
32 #include <cstring>
33 #include <ostream>
34 #include <sstream>
35 
36 namespace gem5
37 {
38 
39 namespace cp
40 {
41 
42 struct Format
43 {
45  bool flushLeft;
46  bool printSign;
47  bool blankSpace;
48  bool fillZero;
49  bool uppercase;
50  enum
51  {
52  Dec,
53  Hex,
54  Oct
55  } base;
56  enum
57  {
62  Floating
63  } format;
64  enum
65  {
70  int precision;
71  int width;
73  bool getWidth;
74 
75  Format() { clear(); }
76 
77  void
79  {
80  alternateForm = false;
81  flushLeft = false;
82  printSign = false;
83  blankSpace = false;
84  fillZero = false;
85  uppercase = false;
86  base = Dec;
87  format = None;
88  floatFormat = Best;
89  precision = -1;
90  width = 0;
91  getPrecision = false;
92  getWidth = false;
93  }
94 };
95 
96 template <typename T>
97 static inline void
98 _formatChar(std::ostream &out, const T &data, Format &fmt)
99 {
100  out << data;
101 }
102 
103 template <typename T>
104 static inline void
105 _formatInteger(std::ostream &out, const T &data, Format &fmt)
106 {
107  std::ios::fmtflags flags(out.flags());
108 
109  switch (fmt.base) {
110  case Format::Hex:
111  out.setf(std::ios::hex, std::ios::basefield);
112  break;
113 
114  case Format::Oct:
115  out.setf(std::ios::oct, std::ios::basefield);
116  break;
117 
118  case Format::Dec:
119  out.setf(std::ios::dec, std::ios::basefield);
120  break;
121  }
122 
123  if (fmt.alternateForm) {
124  if (!fmt.fillZero) {
125  out.setf(std::ios::showbase);
126  } else {
127  switch (fmt.base) {
128  case Format::Hex:
129  out << "0x";
130  fmt.width -= 2;
131  break;
132  case Format::Oct:
133  out << "0";
134  fmt.width -= 1;
135  break;
136  case Format::Dec:
137  break;
138  }
139  }
140  }
141 
142  if (fmt.fillZero)
143  out.fill('0');
144 
145  if (fmt.width > 0)
146  out.width(fmt.width);
147 
148  if (fmt.flushLeft && !fmt.fillZero)
149  out.setf(std::ios::left);
150 
151  if (fmt.printSign)
152  out.setf(std::ios::showpos);
153 
154  if (fmt.uppercase)
155  out.setf(std::ios::uppercase);
156 
157  out << data;
158 
159  out.flags(flags);
160 }
161 
162 template <typename T>
163 static inline void
164 _formatFloat(std::ostream &out, const T &data, Format &fmt)
165 {
166  std::ios::fmtflags flags(out.flags());
167 
168  if (fmt.fillZero)
169  out.fill('0');
170 
171  switch (fmt.floatFormat) {
172  case Format::Scientific:
173  if (fmt.precision != -1) {
174  if (fmt.width > 0)
175  out.width(fmt.width);
176 
177  if (fmt.precision == 0)
178  fmt.precision = 1;
179  else
180  out.setf(std::ios::scientific);
181 
182  out.precision(fmt.precision);
183  } else if (fmt.width > 0) {
184  out.width(fmt.width);
185  }
186 
187  if (fmt.uppercase)
188  out.setf(std::ios::uppercase);
189  break;
190 
191  case Format::Fixed:
192  if (fmt.precision != -1) {
193  if (fmt.width > 0)
194  out.width(fmt.width);
195 
196  out.setf(std::ios::fixed);
197  out.precision(fmt.precision);
198  } else if (fmt.width > 0) {
199  out.width(fmt.width);
200  }
201 
202  break;
203 
204  default:
205  if (fmt.precision != -1)
206  out.precision(fmt.precision);
207 
208  if (fmt.width > 0)
209  out.width(fmt.width);
210 
211  break;
212  }
213 
214  out << data;
215 
216  out.flags(flags);
217 }
218 
219 template <typename T>
220 static inline void
221 _formatString(std::ostream &out, const T &data, Format &fmt)
222 {
223  if (fmt.width > 0) {
224  std::stringstream foo;
225  foo << data;
226  int flen = foo.str().size();
227 
228  if (fmt.width > flen) {
229  char spaces[fmt.width - flen + 1];
230  std::memset(spaces, ' ', fmt.width - flen);
231  spaces[fmt.width - flen] = 0;
232 
233  if (fmt.flushLeft)
234  out << foo.str() << spaces;
235  else
236  out << spaces << foo.str();
237  } else {
238  out << data;
239  }
240  } else {
241  out << data;
242  }
243 }
244 
246 //
247 // The code below controls the actual usage of formats for various types
248 //
249 
250 //
251 // character formats
252 //
253 template <typename T>
254 static inline void
255 formatChar(std::ostream &out, const T &data, Format &fmt)
256 {
257  out << "<bad arg type for char format>";
258 }
259 
260 static inline void
261 formatChar(std::ostream &out, char data, Format &fmt)
262 {
263  _formatChar(out, data, fmt);
264 }
265 
266 static inline void
267 formatChar(std::ostream &out, unsigned char data, Format &fmt)
268 {
269  _formatChar(out, data, fmt);
270 }
271 
272 static inline void
273 formatChar(std::ostream &out, signed char data, Format &fmt)
274 {
275  _formatChar(out, data, fmt);
276 }
277 
278 static inline void
279 formatChar(std::ostream &out, short data, Format &fmt)
280 {
281  _formatChar(out, (char)data, fmt);
282 }
283 
284 static inline void
285 formatChar(std::ostream &out, unsigned short data, Format &fmt)
286 {
287  _formatChar(out, (char)data, fmt);
288 }
289 
290 static inline void
291 formatChar(std::ostream &out, int data, Format &fmt)
292 {
293  _formatChar(out, (char)data, fmt);
294 }
295 
296 static inline void
297 formatChar(std::ostream &out, unsigned int data, Format &fmt)
298 {
299  _formatChar(out, (char)data, fmt);
300 }
301 
302 static inline void
303 formatChar(std::ostream &out, long data, Format &fmt)
304 {
305  _formatChar(out, (char)data, fmt);
306 }
307 
308 static inline void
309 formatChar(std::ostream &out, unsigned long data, Format &fmt)
310 {
311  _formatChar(out, (char)data, fmt);
312 }
313 
314 static inline void
315 formatChar(std::ostream &out, long long data, Format &fmt)
316 {
317  _formatChar(out, (char)data, fmt);
318 }
319 
320 static inline void
321 formatChar(std::ostream &out, unsigned long long data, Format &fmt)
322 {
323  _formatChar(out, (char)data, fmt);
324 }
325 
326 //
327 // integer formats
328 //
329 template <typename T>
330 static inline void
331 formatInteger(std::ostream &out, const T &data, Format &fmt)
332 {
333  _formatInteger(out, data, fmt);
334 }
335 static inline void
336 formatInteger(std::ostream &out, char data, Format &fmt)
337 {
338  _formatInteger(out, (int)data, fmt);
339 }
340 static inline void
341 formatInteger(std::ostream &out, unsigned char data, Format &fmt)
342 {
343  _formatInteger(out, (int)data, fmt);
344 }
345 static inline void
346 formatInteger(std::ostream &out, signed char data, Format &fmt)
347 {
348  _formatInteger(out, (int)data, fmt);
349 }
350 static inline void
351 formatInteger(std::ostream &out, const unsigned char *data, Format &fmt)
352 {
353  _formatInteger(out, (uintptr_t)data, fmt);
354 }
355 static inline void
356 formatInteger(std::ostream &out, const signed char *data, Format &fmt)
357 {
358  _formatInteger(out, (uintptr_t)data, fmt);
359 }
360 
361 //
362 // floating point formats
363 //
364 template <typename T>
365 static inline void
366 formatFloat(std::ostream &out, const T &data, Format &fmt)
367 {
368  out << "<bad arg type for float format>";
369 }
370 
371 static inline void
372 formatFloat(std::ostream &out, float data, Format &fmt)
373 {
374  _formatFloat(out, data, fmt);
375 }
376 
377 static inline void
378 formatFloat(std::ostream &out, double data, Format &fmt)
379 {
380  _formatFloat(out, data, fmt);
381 }
382 
383 //
384 // string formats
385 //
386 template <typename T>
387 static inline void
388 formatString(std::ostream &out, const T &data, Format &fmt)
389 {
390  _formatString(out, data, fmt);
391 }
392 
393 } // namespace cp
394 } // namespace gem5
395 
396 #endif // __CPRINTF_FORMATS_HH__
const char data[]
uint8_t flags
Definition: helpers.cc:66
static void _formatChar(std::ostream &out, const T &data, Format &fmt)
static void formatChar(std::ostream &out, const T &data, Format &fmt)
static void _formatInteger(std::ostream &out, const T &data, Format &fmt)
static void _formatFloat(std::ostream &out, const T &data, Format &fmt)
static void formatInteger(std::ostream &out, const T &data, Format &fmt)
static void _formatString(std::ostream &out, const T &data, Format &fmt)
static void formatString(std::ostream &out, const T &data, Format &fmt)
static void formatFloat(std::ostream &out, const T &data, Format &fmt)
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
enum gem5::cp::Format::@34 base
enum gem5::cp::Format::@35 format
enum gem5::cp::Format::@36 floatFormat

Generated on Wed Dec 21 2022 10:22:28 for gem5 by doxygen 1.9.1