cprintf.hh revision 12979
12381SN/A/*
212652Sandreas.sandberg@arm.com * Copyright (c) 2014 ARM Limited
38949Sandreas.hansson@arm.com * Copyright (c) 2002-2006 The Regents of The University of Michigan
48949Sandreas.hansson@arm.com * All rights reserved.
58949Sandreas.hansson@arm.com *
68949Sandreas.hansson@arm.com * Redistribution and use in source and binary forms, with or without
78949Sandreas.hansson@arm.com * modification, are permitted provided that the following conditions are
88949Sandreas.hansson@arm.com * met: redistributions of source code must retain the above copyright
98949Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer;
108949Sandreas.hansson@arm.com * redistributions in binary form must reproduce the above copyright
118949Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer in the
128949Sandreas.hansson@arm.com * documentation and/or other materials provided with the distribution;
138949Sandreas.hansson@arm.com * neither the name of the copyright holders nor the names of its
142592SN/A * contributors may be used to endorse or promote products derived from
1510975Sdavid.hashe@amd.com * this software without specific prior written permission.
162381SN/A *
172381SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
182381SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
192381SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
202381SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
212381SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
222381SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
232381SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
242381SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
252381SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
262381SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
272381SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282381SN/A *
292381SN/A * Authors: Nathan Binkert
302381SN/A *          Steve Reinhardt
312381SN/A *          Andreas Sandberg
322381SN/A */
332381SN/A
342381SN/A#ifndef __BASE_CPRINTF_HH__
352381SN/A#define __BASE_CPRINTF_HH__
362381SN/A
372381SN/A#include <ios>
382381SN/A#include <iostream>
392381SN/A#include <list>
402665Ssaidi@eecs.umich.edu#include <string>
412665Ssaidi@eecs.umich.edu
422665Ssaidi@eecs.umich.edu#include "base/cprintf_formats.hh"
432665Ssaidi@eecs.umich.edu
449031Sandreas.hansson@arm.comnamespace cp {
4512349Snikos.nikoleris@arm.com
462381SN/Astruct Print
472381SN/A{
482381SN/A  protected:
492381SN/A    std::ostream &stream;
502662Sstever@eecs.umich.edu    const char *format;
512381SN/A    const char *ptr;
522381SN/A    bool cont;
532381SN/A
542381SN/A    std::ios::fmtflags saved_flags;
552381SN/A    char saved_fill;
568229Snate@binkert.org    int saved_precision;
573348Sbinkertn@umich.edu    int saved_width;
583348Sbinkertn@umich.edu
593348Sbinkertn@umich.edu    Format fmt;
605735Snate@binkert.org    void process();
614024Sbinkertn@umich.edu    void process_flag();
625735Snate@binkert.org
6312334Sgabeblack@google.com  public:
645314Sstever@gmail.com    Print(std::ostream &stream, const std::string &format);
656216Snate@binkert.org    Print(std::ostream &stream, const char *format);
6613347Sgabeblack@google.com    ~Print();
672392SN/A
684167Sbinkertn@umich.edu    int
692394SN/A    get_number(int data)
708737Skoansin.tan@gmail.com    {
713349Sbinkertn@umich.edu        return data;
722394SN/A    }
732812Srdreslin@umich.edu
7412351Snikos.nikoleris@arm.com    template <typename T>
752812Srdreslin@umich.edu    int
764022Sstever@eecs.umich.edu    get_number(const T& data)
774022Sstever@eecs.umich.edu    {
785735Snate@binkert.org        return 0;
795735Snate@binkert.org    }
804022Sstever@eecs.umich.edu
815735Snate@binkert.org    template <typename T>
825735Snate@binkert.org    void
835735Snate@binkert.org    add_arg(const T &data)
844022Sstever@eecs.umich.edu    {
854022Sstever@eecs.umich.edu        if (!cont)
864022Sstever@eecs.umich.edu            process();
874022Sstever@eecs.umich.edu
884473Sstever@eecs.umich.edu        if (fmt.get_width) {
895319Sstever@gmail.com            fmt.get_width = false;
904022Sstever@eecs.umich.edu            cont = true;
914022Sstever@eecs.umich.edu            fmt.width = get_number(data);
9211199Sandreas.hansson@arm.com            return;
9311199Sandreas.hansson@arm.com        }
9412344Snikos.nikoleris@arm.com
9510883Sali.jafri@arm.com        if (fmt.get_precision) {
964022Sstever@eecs.umich.edu            fmt.get_precision = false;
974022Sstever@eecs.umich.edu            cont = true;
984022Sstever@eecs.umich.edu            fmt.precision = get_number(data);
994022Sstever@eecs.umich.edu            return;
10010886Sandreas.hansson@arm.com        }
1014022Sstever@eecs.umich.edu
1027465Ssteve.reinhardt@amd.com        switch (fmt.format) {
1034628Sstever@eecs.umich.edu          case Format::character:
1047465Ssteve.reinhardt@amd.com            format_char(stream, data, fmt);
1057465Ssteve.reinhardt@amd.com            break;
1064022Sstever@eecs.umich.edu
1074022Sstever@eecs.umich.edu          case Format::integer:
10810885Sandreas.hansson@arm.com            format_integer(stream, data, fmt);
10910885Sandreas.hansson@arm.com            break;
1104626Sstever@eecs.umich.edu
1114626Sstever@eecs.umich.edu          case Format::floating:
1127669Ssteve.reinhardt@amd.com            format_float(stream, data, fmt);
1134626Sstever@eecs.umich.edu            break;
1144040Ssaidi@eecs.umich.edu
1154040Ssaidi@eecs.umich.edu          case Format::string:
1165650Sgblack@eecs.umich.edu            format_string(stream, data, fmt);
1175650Sgblack@eecs.umich.edu            break;
11811256Santhony.gutierrez@amd.com
11911256Santhony.gutierrez@amd.com          default:
12012347Snikos.nikoleris@arm.com            stream << "<bad format>";
12112347Snikos.nikoleris@arm.com            break;
12212347Snikos.nikoleris@arm.com        }
12312347Snikos.nikoleris@arm.com    }
1244870Sstever@eecs.umich.edu
1254870Sstever@eecs.umich.edu    void end_args();
1264870Sstever@eecs.umich.edu};
1274870Sstever@eecs.umich.edu
1284870Sstever@eecs.umich.edu} // namespace cp
1294870Sstever@eecs.umich.edu
1308436SBrad.Beckmann@amd.cominline void
1318436SBrad.Beckmann@amd.comccprintf(cp::Print &print)
1325314Sstever@gmail.com{
1335314Sstever@gmail.com    print.end_args();
1348184Ssomayeh@cs.wisc.edu}
13510886Sandreas.hansson@arm.com
13610886Sandreas.hansson@arm.com
1374022Sstever@eecs.umich.edutemplate<typename T, typename ...Args> void
1384022Sstever@eecs.umich.educcprintf(cp::Print &print, const T &value, const Args &...args)
1394022Sstever@eecs.umich.edu{
1404022Sstever@eecs.umich.edu    print.add_arg(value);
1415735Snate@binkert.org
1425735Snate@binkert.org    ccprintf(print, args...);
1435735Snate@binkert.org}
1444022Sstever@eecs.umich.edu
1454022Sstever@eecs.umich.edu
1464626Sstever@eecs.umich.edutemplate<typename ...Args> void
1474626Sstever@eecs.umich.educcprintf(std::ostream &stream, const char *format, const Args &...args)
1487465Ssteve.reinhardt@amd.com{
1494022Sstever@eecs.umich.edu    cp::Print print(stream, format);
15012347Snikos.nikoleris@arm.com
15111284Sandreas.hansson@arm.com    ccprintf(print, args...);
1524626Sstever@eecs.umich.edu}
1534626Sstever@eecs.umich.edu
1544626Sstever@eecs.umich.edu
15511199Sandreas.hansson@arm.comtemplate<typename ...Args> void
1564022Sstever@eecs.umich.educprintf(const char *format, const Args &...args)
1574022Sstever@eecs.umich.edu{
1586076Sgblack@eecs.umich.edu    ccprintf(std::cout, format, args...);
1594626Sstever@eecs.umich.edu}
1604870Sstever@eecs.umich.edu
1615314Sstever@gmail.comtemplate<typename ...Args> std::string
1628184Ssomayeh@cs.wisc.educsprintf(const char *format, const Args &...args)
16311600Sandreas.hansson@arm.com{
1644022Sstever@eecs.umich.edu    std::stringstream stream;
1654022Sstever@eecs.umich.edu    ccprintf(stream, format, args...);
1664022Sstever@eecs.umich.edu    return stream.str();
1675735Snate@binkert.org}
1685735Snate@binkert.org
1695735Snate@binkert.org/*
1705735Snate@binkert.org * functions again with std::string.  We have both so we don't waste
1715735Snate@binkert.org * time converting const char * to std::string since we don't take
1725735Snate@binkert.org * advantage of it.
1735735Snate@binkert.org */
1744022Sstever@eecs.umich.edutemplate<typename ...Args> void
1755735Snate@binkert.orgccprintf(std::ostream &stream, const std::string &format, const Args &...args)
1765735Snate@binkert.org{
1774022Sstever@eecs.umich.edu    ccprintf(stream, format.c_str(), args...);
1785735Snate@binkert.org}
1794022Sstever@eecs.umich.edu
1804022Sstever@eecs.umich.edutemplate<typename ...Args> void
1814022Sstever@eecs.umich.educprintf(const std::string &format, const Args &...args)
1825735Snate@binkert.org{
1834022Sstever@eecs.umich.edu    ccprintf(std::cout, format.c_str(), args...);
1844022Sstever@eecs.umich.edu}
1854022Sstever@eecs.umich.edu
1864022Sstever@eecs.umich.edutemplate<typename ...Args> std::string
1874022Sstever@eecs.umich.educsprintf(const std::string &format, const Args &...args)
1884022Sstever@eecs.umich.edu{
1895735Snate@binkert.org    return csprintf(format.c_str(), args...);
1905735Snate@binkert.org}
1915735Snate@binkert.org
1924022Sstever@eecs.umich.edu#endif // __CPRINTF_HH__
1934022Sstever@eecs.umich.edu