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.
29 #include "base/cprintf.hh"
36 #include "base/compiler.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();
48 saved_width
= stream
.width();
51 Print::Print(std::ostream
&stream
, const char *format
)
52 : stream(stream
), format(format
), ptr(format
), cont(false)
54 saved_flags
= stream
.flags();
55 saved_fill
= stream
.fill();
56 saved_precision
= stream
.precision();
57 saved_width
= stream
.width();
93 len
= strcspn(ptr
, "%\n\r\0");
94 stream
.write(ptr
, len
);
102 Print::process_flag()
105 bool end_number
= false;
106 bool have_precision
= false;
110 stream
.flags((ios::fmtflags
)0);
114 if (*ptr
>= '0' && *ptr
<= '9') {
117 } else if (number
> 0)
122 fmt
.format
= Format::string
;
127 fmt
.format
= Format::character
;
135 fmt
.format
= Format::integer
;
136 fmt
.base
= Format::hex
;
137 fmt
.alternate_form
= true;
142 fmt
.uppercase
= true;
145 fmt
.base
= Format::hex
;
146 fmt
.format
= Format::integer
;
151 fmt
.base
= Format::oct
;
152 fmt
.format
= Format::integer
;
159 fmt
.format
= Format::integer
;
164 fmt
.uppercase
= true;
167 fmt
.format
= Format::floating
;
168 fmt
.float_format
= Format::best
;
173 fmt
.uppercase
= true;
176 fmt
.format
= Format::floating
;
177 fmt
.float_format
= Format::scientific
;
182 fmt
.format
= Format::floating
;
183 fmt
.float_format
= Format::fixed
;
188 stream
<< "we don't do %n!!!\n";
193 fmt
.alternate_form
= true;
197 fmt
.flush_left
= true;
201 fmt
.print_sign
= true;
205 fmt
.blank_space
= true;
211 have_precision
= true;
218 fmt
.fill_zero
= true;
231 number
= number
* 10 + (*ptr
- '0');
236 fmt
.get_precision
= true;
238 fmt
.get_width
= true;
242 assert(false && "we shouldn't get here");
252 fmt
.precision
= number
;
261 if ((fmt
.format
== Format::integer
) && have_precision
) {
262 // specified a . but not a float, set width
263 fmt
.width
= fmt
.precision
;
264 // precision requries digits for width, must fill with 0
265 fmt
.fill_zero
= true;
266 } else if ((fmt
.format
== Format::floating
) && !have_precision
&&
268 // ambiguous case, matching printf
269 fmt
.precision
= fmt
.width
;
286 stream
<< "<extra arg>";
303 len
= strcspn(ptr
, "%\n\r\0");
304 stream
.write(ptr
, len
);
310 stream
.flags(saved_flags
);
311 stream
.fill(saved_fill
);
312 stream
.precision(saved_precision
);
313 stream
.width(saved_width
);