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())
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
)
53 saved_flags
= stream
.flags();
54 saved_fill
= stream
.fill();
55 saved_precision
= stream
.precision();
63 Print::process(Format
&fmt
)
88 len
= strcspn(ptr
, "%\n\r\0");
89 stream
.write(ptr
, len
);
99 bool end_number
= false;
100 bool have_precision
= false;
104 stream
.flags((ios::fmtflags
)0);
108 if (*ptr
>= '0' && *ptr
<= '9') {
111 } else if (number
> 0)
116 fmt
.format
= Format::string
;
121 fmt
.format
= Format::character
;
129 fmt
.format
= Format::integer
;
130 fmt
.base
= Format::hex
;
131 fmt
.alternate_form
= true;
136 fmt
.uppercase
= true;
138 fmt
.base
= Format::hex
;
139 fmt
.format
= Format::integer
;
144 fmt
.base
= Format::oct
;
145 fmt
.format
= Format::integer
;
152 fmt
.format
= Format::integer
;
157 fmt
.uppercase
= true;
159 fmt
.format
= Format::floating
;
160 fmt
.float_format
= Format::best
;
165 fmt
.uppercase
= true;
167 fmt
.format
= Format::floating
;
168 fmt
.float_format
= Format::scientific
;
173 fmt
.format
= Format::floating
;
174 fmt
.float_format
= Format::fixed
;
179 stream
<< "we don't do %n!!!\n";
184 fmt
.alternate_form
= true;
188 fmt
.flush_left
= true;
192 fmt
.print_sign
= true;
196 fmt
.blank_space
= true;
202 have_precision
= true;
209 fmt
.fill_zero
= true;
221 number
= number
* 10 + (*ptr
- '0');
225 assert("we shouldn't get here");
235 fmt
.precision
= number
;
256 stream
<< "<extra arg>";
273 len
= strcspn(ptr
, "%\n\r\0");
274 stream
.write(ptr
, len
);
280 stream
.flags(saved_flags
);
281 stream
.fill(saved_fill
);
282 stream
.precision(saved_precision
);
285 /* end namespace cp */ }