1/* 2 * Copyright (c) 2002-2006 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 * Authors: Nathan Binkert 29 */ 30 31#include <cassert> 32#include <iomanip> 33#include <iostream> 34#include <sstream> 35 36#include "base/cprintf.hh" 37 38using namespace std; 39 40namespace cp { 41 42Print::Print(std::ostream &stream, const std::string &format) 43 : stream(stream), format(format.c_str()), ptr(format.c_str()), cont(false) 44{ 45 saved_flags = stream.flags(); 46 saved_fill = stream.fill(); 47 saved_precision = stream.precision(); 48} 49 50Print::Print(std::ostream &stream, const char *format) 51 : stream(stream), format(format), ptr(format), cont(false) 52{ 53 saved_flags = stream.flags(); 54 saved_fill = stream.fill(); 55 saved_precision = stream.precision(); 56} 57 58Print::~Print() 59{ 60} 61 62void 63Print::process() 64{ 65 fmt.clear(); 66 67 size_t len; 68 69 while (*ptr) { 70 switch (*ptr) { 71 case '%':
| 1/* 2 * Copyright (c) 2002-2006 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 * Authors: Nathan Binkert 29 */ 30 31#include <cassert> 32#include <iomanip> 33#include <iostream> 34#include <sstream> 35 36#include "base/cprintf.hh" 37 38using namespace std; 39 40namespace cp { 41 42Print::Print(std::ostream &stream, const std::string &format) 43 : stream(stream), format(format.c_str()), ptr(format.c_str()), cont(false) 44{ 45 saved_flags = stream.flags(); 46 saved_fill = stream.fill(); 47 saved_precision = stream.precision(); 48} 49 50Print::Print(std::ostream &stream, const char *format) 51 : stream(stream), format(format), ptr(format), cont(false) 52{ 53 saved_flags = stream.flags(); 54 saved_fill = stream.fill(); 55 saved_precision = stream.precision(); 56} 57 58Print::~Print() 59{ 60} 61 62void 63Print::process() 64{ 65 fmt.clear(); 66 67 size_t len; 68 69 while (*ptr) { 70 switch (*ptr) { 71 case '%':
|
75 stream.put('%'); 76 ptr += 2; 77 break; 78 79 case '\n': 80 stream << endl; 81 ++ptr; 82 break; 83 case '\r': 84 ++ptr; 85 if (*ptr != '\n') 86 stream << endl; 87 break; 88 89 default: 90 len = strcspn(ptr, "%\n\r\0"); 91 stream.write(ptr, len); 92 ptr += len; 93 break; 94 } 95 }
| 76 stream.put('%'); 77 ptr += 2; 78 break; 79 80 case '\n': 81 stream << endl; 82 ++ptr; 83 break; 84 case '\r': 85 ++ptr; 86 if (*ptr != '\n') 87 stream << endl; 88 break; 89 90 default: 91 len = strcspn(ptr, "%\n\r\0"); 92 stream.write(ptr, len); 93 ptr += len; 94 break; 95 } 96 }
|
253 ++ptr; 254} 255 256void 257Print::end_args() 258{ 259 size_t len; 260 261 while (*ptr) { 262 switch (*ptr) { 263 case '%': 264 if (ptr[1] != '%') 265 stream << "<extra arg>"; 266 267 stream.put('%'); 268 ptr += 2; 269 break; 270 271 case '\n': 272 stream << endl; 273 ++ptr; 274 break; 275 case '\r': 276 ++ptr; 277 if (*ptr != '\n') 278 stream << endl; 279 break; 280 281 default: 282 len = strcspn(ptr, "%\n\r\0"); 283 stream.write(ptr, len); 284 ptr += len; 285 break; 286 } 287 } 288 289 stream.flags(saved_flags); 290 stream.fill(saved_fill); 291 stream.precision(saved_precision); 292} 293 294} // namespace cp
| 268 ++ptr; 269} 270 271void 272Print::end_args() 273{ 274 size_t len; 275 276 while (*ptr) { 277 switch (*ptr) { 278 case '%': 279 if (ptr[1] != '%') 280 stream << "<extra arg>"; 281 282 stream.put('%'); 283 ptr += 2; 284 break; 285 286 case '\n': 287 stream << endl; 288 ++ptr; 289 break; 290 case '\r': 291 ++ptr; 292 if (*ptr != '\n') 293 stream << endl; 294 break; 295 296 default: 297 len = strcspn(ptr, "%\n\r\0"); 298 stream.write(ptr, len); 299 ptr += len; 300 break; 301 } 302 } 303 304 stream.flags(saved_flags); 305 stream.fill(saved_fill); 306 stream.precision(saved_precision); 307} 308 309} // namespace cp
|