2 * Copyright (c) 2002-2006 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.
28 * Authors: Nathan Binkert
36 #include "base/cprintf.hh"
42 Print::Print(std::ostream
&stream
, const std::string
&format
)
43 : stream(stream
), format(format
.c_str()), ptr(format
.c_str()), cont(false)
45 saved_flags
= stream
.flags();
46 saved_fill
= stream
.fill();
47 saved_precision
= stream
.precision();
50 Print::Print(std::ostream
&stream
, const char *format
)
51 : stream(stream
), format(format
), ptr(format
), cont(false)
53 saved_flags
= stream
.flags();
54 saved_fill
= stream
.fill();
55 saved_precision
= stream
.precision();
91 len
= strcspn(ptr
, "%\n\r\0");
92 stream
.write(ptr
, len
);
100 Print::process_flag()
103 bool end_number
= false;
104 bool have_precision
= false;
108 stream
.flags((ios::fmtflags
)0);
112 if (*ptr
>= '0' && *ptr
<= '9') {
115 } else if (number
> 0)
120 fmt
.format
= Format::string
;
125 fmt
.format
= Format::character
;
133 fmt
.format
= Format::integer
;
134 fmt
.base
= Format::hex
;
135 fmt
.alternate_form
= true;
140 fmt
.uppercase
= true;
142 fmt
.base
= Format::hex
;
143 fmt
.format
= Format::integer
;
148 fmt
.base
= Format::oct
;
149 fmt
.format
= Format::integer
;
156 fmt
.format
= Format::integer
;
161 fmt
.uppercase
= true;
163 fmt
.format
= Format::floating
;
164 fmt
.float_format
= Format::best
;
169 fmt
.uppercase
= true;
171 fmt
.format
= Format::floating
;
172 fmt
.float_format
= Format::scientific
;
177 fmt
.format
= Format::floating
;
178 fmt
.float_format
= Format::fixed
;
183 stream
<< "we don't do %n!!!\n";
188 fmt
.alternate_form
= true;
192 fmt
.flush_left
= true;
196 fmt
.print_sign
= true;
200 fmt
.blank_space
= true;
206 have_precision
= true;
213 fmt
.fill_zero
= true;
225 number
= number
* 10 + (*ptr
- '0');
230 fmt
.get_precision
= true;
232 fmt
.get_width
= true;
236 assert(false && "we shouldn't get here");
246 fmt
.precision
= number
;
255 if ((fmt
.format
== Format::integer
) && have_precision
) {
256 // specified a . but not a float, set width
257 fmt
.width
= fmt
.precision
;
258 // precision requries digits for width, must fill with 0
259 fmt
.fill_zero
= true;
260 } else if ((fmt
.format
== Format::floating
) && !have_precision
&&
262 // ambiguous case, matching printf
263 fmt
.precision
= fmt
.width
;
280 stream
<< "<extra arg>";
297 len
= strcspn(ptr
, "%\n\r\0");
298 stream
.write(ptr
, len
);
304 stream
.flags(saved_flags
);
305 stream
.fill(saved_fill
);
306 stream
.precision(saved_precision
);