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();
90 len
= strcspn(ptr
, "%\n\r\0");
91 stream
.write(ptr
, len
);
101 bool end_number
= false;
102 bool have_precision
= false;
106 stream
.flags((ios::fmtflags
)0);
110 if (*ptr
>= '0' && *ptr
<= '9') {
113 } else if (number
> 0)
118 fmt
.format
= Format::string
;
123 fmt
.format
= Format::character
;
131 fmt
.format
= Format::integer
;
132 fmt
.base
= Format::hex
;
133 fmt
.alternate_form
= true;
138 fmt
.uppercase
= true;
140 fmt
.base
= Format::hex
;
141 fmt
.format
= Format::integer
;
146 fmt
.base
= Format::oct
;
147 fmt
.format
= Format::integer
;
154 fmt
.format
= Format::integer
;
159 fmt
.uppercase
= true;
161 fmt
.format
= Format::floating
;
162 fmt
.float_format
= Format::best
;
167 fmt
.uppercase
= true;
169 fmt
.format
= Format::floating
;
170 fmt
.float_format
= Format::scientific
;
175 fmt
.format
= Format::floating
;
176 fmt
.float_format
= Format::fixed
;
181 stream
<< "we don't do %n!!!\n";
186 fmt
.alternate_form
= true;
190 fmt
.flush_left
= true;
194 fmt
.print_sign
= true;
198 fmt
.blank_space
= true;
204 have_precision
= true;
211 fmt
.fill_zero
= true;
223 number
= number
* 10 + (*ptr
- '0');
228 fmt
.get_precision
= true;
230 fmt
.get_width
= true;
234 assert(false && "we shouldn't get here");
244 fmt
.precision
= number
;
265 stream
<< "<extra arg>";
282 len
= strcspn(ptr
, "%\n\r\0");
283 stream
.write(ptr
, len
);
289 stream
.flags(saved_flags
);
290 stream
.fill(saved_fill
);
291 stream
.precision(saved_precision
);