cprintf.hh revision 7811
12SN/A/* 24039Sbinkertn@umich.edu * Copyright (c) 2002-2006 The Regents of The University of Michigan 32SN/A * All rights reserved. 42SN/A * 52SN/A * Redistribution and use in source and binary forms, with or without 62SN/A * modification, are permitted provided that the following conditions are 72SN/A * met: redistributions of source code must retain the above copyright 82SN/A * notice, this list of conditions and the following disclaimer; 92SN/A * redistributions in binary form must reproduce the above copyright 102SN/A * notice, this list of conditions and the following disclaimer in the 112SN/A * documentation and/or other materials provided with the distribution; 122SN/A * neither the name of the copyright holders nor the names of its 132SN/A * contributors may be used to endorse or promote products derived from 142SN/A * this software without specific prior written permission. 152SN/A * 162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272665Ssaidi@eecs.umich.edu * 282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert 292665Ssaidi@eecs.umich.edu * Steve Reinhardt 302SN/A */ 312SN/A 324039Sbinkertn@umich.edu#ifndef __BASE_CPRINTF_HH__ 334039Sbinkertn@umich.edu#define __BASE_CPRINTF_HH__ 342SN/A 354039Sbinkertn@umich.edu#include <ios> 362SN/A#include <iostream> 372SN/A#include <list> 382SN/A#include <string> 392SN/A 404039Sbinkertn@umich.edu#include "base/varargs.hh" 412621SN/A#include "base/cprintf_formats.hh" 422621SN/A 432SN/Anamespace cp { 442SN/A 454039Sbinkertn@umich.edu#define CPRINTF_DECLARATION VARARGS_DECLARATION(cp::Print) 464039Sbinkertn@umich.edu#define CPRINTF_DEFINITION VARARGS_DEFINITION(cp::Print) 474039Sbinkertn@umich.edu 484039Sbinkertn@umich.edustruct Print 492SN/A{ 504039Sbinkertn@umich.edu protected: 514039Sbinkertn@umich.edu std::ostream &stream; 524039Sbinkertn@umich.edu const char *format; 534039Sbinkertn@umich.edu const char *ptr; 545756Snate@binkert.org bool cont; 554039Sbinkertn@umich.edu 564039Sbinkertn@umich.edu std::ios::fmtflags saved_flags; 574039Sbinkertn@umich.edu char saved_fill; 584039Sbinkertn@umich.edu int saved_precision; 594039Sbinkertn@umich.edu 605756Snate@binkert.org Format fmt; 615756Snate@binkert.org void process(); 624039Sbinkertn@umich.edu 634039Sbinkertn@umich.edu public: 644039Sbinkertn@umich.edu Print(std::ostream &stream, const std::string &format); 654039Sbinkertn@umich.edu Print(std::ostream &stream, const char *format); 664039Sbinkertn@umich.edu ~Print(); 672SN/A 685756Snate@binkert.org int 695756Snate@binkert.org get_number(int data) 705756Snate@binkert.org { 715756Snate@binkert.org return data; 725756Snate@binkert.org } 735756Snate@binkert.org 745756Snate@binkert.org template <typename T> 755756Snate@binkert.org int 765756Snate@binkert.org get_number(const T& data) 775756Snate@binkert.org { 785756Snate@binkert.org return 0; 795756Snate@binkert.org } 805756Snate@binkert.org 812SN/A template <typename T> 824039Sbinkertn@umich.edu void 834039Sbinkertn@umich.edu add_arg(const T &data) 842SN/A { 855756Snate@binkert.org if (!cont) 865756Snate@binkert.org process(); 875756Snate@binkert.org 885756Snate@binkert.org if (fmt.get_width) { 895756Snate@binkert.org fmt.get_width = false; 905756Snate@binkert.org cont = true; 915756Snate@binkert.org fmt.width = get_number(data); 925756Snate@binkert.org return; 935756Snate@binkert.org } 945756Snate@binkert.org 955756Snate@binkert.org if (fmt.get_precision) { 965756Snate@binkert.org fmt.get_precision = false; 975756Snate@binkert.org cont = true; 985756Snate@binkert.org fmt.precision = get_number(data); 995756Snate@binkert.org return; 1005756Snate@binkert.org } 1012SN/A 1024039Sbinkertn@umich.edu switch (fmt.format) { 1034039Sbinkertn@umich.edu case Format::character: 1044039Sbinkertn@umich.edu format_char(stream, data, fmt); 1054039Sbinkertn@umich.edu break; 1062SN/A 1074039Sbinkertn@umich.edu case Format::integer: 1084039Sbinkertn@umich.edu format_integer(stream, data, fmt); 1094039Sbinkertn@umich.edu break; 1102SN/A 1114039Sbinkertn@umich.edu case Format::floating: 1124039Sbinkertn@umich.edu format_float(stream, data, fmt); 1134039Sbinkertn@umich.edu break; 1142SN/A 1154039Sbinkertn@umich.edu case Format::string: 1164039Sbinkertn@umich.edu format_string(stream, data, fmt); 1174039Sbinkertn@umich.edu break; 1182SN/A 1194039Sbinkertn@umich.edu default: 1204039Sbinkertn@umich.edu stream << "<bad format>"; 1214039Sbinkertn@umich.edu break; 1222SN/A } 1232SN/A } 1242SN/A 1254039Sbinkertn@umich.edu void end_args(); 1262SN/A}; 1272SN/A 1287811Ssteve.reinhardt@amd.com} // namespace cp 1294039Sbinkertn@umich.edu 1304039Sbinkertn@umich.edutypedef VarArgs::List<cp::Print> CPrintfArgsList; 1314039Sbinkertn@umich.edu 1324039Sbinkertn@umich.eduinline void 1334039Sbinkertn@umich.educcprintf(std::ostream &stream, const char *format, const CPrintfArgsList &args) 1342SN/A{ 1354039Sbinkertn@umich.edu cp::Print print(stream, format); 1364039Sbinkertn@umich.edu args.add_args(print); 1372SN/A} 1382SN/A 1394039Sbinkertn@umich.eduinline void 1404039Sbinkertn@umich.educcprintf(std::ostream &stream, const char *format, CPRINTF_DECLARATION) 1414039Sbinkertn@umich.edu{ 1424039Sbinkertn@umich.edu cp::Print print(stream, format); 1434039Sbinkertn@umich.edu VARARGS_ADDARGS(print); 1444039Sbinkertn@umich.edu} 1452SN/A 1464039Sbinkertn@umich.eduinline void 1474039Sbinkertn@umich.educprintf(const char *format, CPRINTF_DECLARATION) 1484039Sbinkertn@umich.edu{ 1494039Sbinkertn@umich.edu ccprintf(std::cout, format, VARARGS_ALLARGS); 1504039Sbinkertn@umich.edu} 1512SN/A 1524039Sbinkertn@umich.eduinline std::string 1534039Sbinkertn@umich.educsprintf(const char *format, CPRINTF_DECLARATION) 1544039Sbinkertn@umich.edu{ 1554039Sbinkertn@umich.edu std::stringstream stream; 1564039Sbinkertn@umich.edu ccprintf(stream, format, VARARGS_ALLARGS); 1574039Sbinkertn@umich.edu return stream.str(); 1584039Sbinkertn@umich.edu} 1594039Sbinkertn@umich.edu 1604039Sbinkertn@umich.edu/* 1614039Sbinkertn@umich.edu * functions again with std::string. We have both so we don't waste 1624039Sbinkertn@umich.edu * time converting const char * to std::string since we don't take 1634039Sbinkertn@umich.edu * advantage of it. 1644039Sbinkertn@umich.edu */ 1652SN/Ainline void 1664039Sbinkertn@umich.educcprintf(std::ostream &stream, const std::string &format, 1674039Sbinkertn@umich.edu const CPrintfArgsList &args) 1684039Sbinkertn@umich.edu{ 1694039Sbinkertn@umich.edu ccprintf(stream, format.c_str(), args); 1704039Sbinkertn@umich.edu} 1712SN/A 1722SN/Ainline void 1734039Sbinkertn@umich.educcprintf(std::ostream &stream, const std::string &format, CPRINTF_DECLARATION) 1744039Sbinkertn@umich.edu{ 1754213Ssaidi@eecs.umich.edu ccprintf(stream, format.c_str(), VARARGS_ALLARGS); 1764039Sbinkertn@umich.edu} 1772SN/A 1784039Sbinkertn@umich.eduinline void 1794039Sbinkertn@umich.educprintf(const std::string &format, CPRINTF_DECLARATION) 1804039Sbinkertn@umich.edu{ 1814213Ssaidi@eecs.umich.edu ccprintf(std::cout, format.c_str(), VARARGS_ALLARGS); 1824039Sbinkertn@umich.edu} 1834039Sbinkertn@umich.edu 1842SN/Ainline std::string 1854039Sbinkertn@umich.educsprintf(const std::string &format, CPRINTF_DECLARATION) 1864039Sbinkertn@umich.edu{ 1874039Sbinkertn@umich.edu std::stringstream stream; 1884213Ssaidi@eecs.umich.edu ccprintf(stream, format.c_str(), VARARGS_ALLARGS); 1894039Sbinkertn@umich.edu return stream.str(); 1902SN/A} 1912SN/A 1922SN/A#endif // __CPRINTF_HH__ 193