cprintf.hh revision 9331
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
408229Snate@binkert.org#include "base/cprintf_formats.hh"
414039Sbinkertn@umich.edu#include "base/varargs.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();
629331Schander.sudanthi@arm.com    void process_flag();
634039Sbinkertn@umich.edu
644039Sbinkertn@umich.edu  public:
654039Sbinkertn@umich.edu    Print(std::ostream &stream, const std::string &format);
664039Sbinkertn@umich.edu    Print(std::ostream &stream, const char *format);
674039Sbinkertn@umich.edu    ~Print();
682SN/A
695756Snate@binkert.org    int
705756Snate@binkert.org    get_number(int data)
715756Snate@binkert.org    {
725756Snate@binkert.org        return data;
735756Snate@binkert.org    }
745756Snate@binkert.org
755756Snate@binkert.org    template <typename T>
765756Snate@binkert.org    int
775756Snate@binkert.org    get_number(const T& data)
785756Snate@binkert.org    {
795756Snate@binkert.org        return 0;
805756Snate@binkert.org    }
815756Snate@binkert.org
822SN/A    template <typename T>
834039Sbinkertn@umich.edu    void
844039Sbinkertn@umich.edu    add_arg(const T &data)
852SN/A    {
865756Snate@binkert.org        if (!cont)
875756Snate@binkert.org            process();
885756Snate@binkert.org
895756Snate@binkert.org        if (fmt.get_width) {
905756Snate@binkert.org            fmt.get_width = false;
915756Snate@binkert.org            cont = true;
925756Snate@binkert.org            fmt.width = get_number(data);
935756Snate@binkert.org            return;
945756Snate@binkert.org        }
955756Snate@binkert.org
965756Snate@binkert.org        if (fmt.get_precision) {
975756Snate@binkert.org            fmt.get_precision = false;
985756Snate@binkert.org            cont = true;
995756Snate@binkert.org            fmt.precision = get_number(data);
1005756Snate@binkert.org            return;
1015756Snate@binkert.org        }
1022SN/A
1034039Sbinkertn@umich.edu        switch (fmt.format) {
1044039Sbinkertn@umich.edu          case Format::character:
1054039Sbinkertn@umich.edu            format_char(stream, data, fmt);
1064039Sbinkertn@umich.edu            break;
1072SN/A
1084039Sbinkertn@umich.edu          case Format::integer:
1094039Sbinkertn@umich.edu            format_integer(stream, data, fmt);
1104039Sbinkertn@umich.edu            break;
1112SN/A
1124039Sbinkertn@umich.edu          case Format::floating:
1134039Sbinkertn@umich.edu            format_float(stream, data, fmt);
1144039Sbinkertn@umich.edu            break;
1152SN/A
1164039Sbinkertn@umich.edu          case Format::string:
1174039Sbinkertn@umich.edu            format_string(stream, data, fmt);
1184039Sbinkertn@umich.edu            break;
1192SN/A
1204039Sbinkertn@umich.edu          default:
1214039Sbinkertn@umich.edu            stream << "<bad format>";
1224039Sbinkertn@umich.edu            break;
1232SN/A        }
1242SN/A    }
1252SN/A
1264039Sbinkertn@umich.edu    void end_args();
1272SN/A};
1282SN/A
1297811Ssteve.reinhardt@amd.com} // namespace cp
1304039Sbinkertn@umich.edu
1314039Sbinkertn@umich.edutypedef VarArgs::List<cp::Print> CPrintfArgsList;
1324039Sbinkertn@umich.edu
1334039Sbinkertn@umich.eduinline void
1344039Sbinkertn@umich.educcprintf(std::ostream &stream, const char *format, const CPrintfArgsList &args)
1352SN/A{
1364039Sbinkertn@umich.edu    cp::Print print(stream, format);
1374039Sbinkertn@umich.edu    args.add_args(print);
1382SN/A}
1392SN/A
1404039Sbinkertn@umich.eduinline void
1414039Sbinkertn@umich.educcprintf(std::ostream &stream, const char *format, CPRINTF_DECLARATION)
1424039Sbinkertn@umich.edu{
1434039Sbinkertn@umich.edu    cp::Print print(stream, format);
1444039Sbinkertn@umich.edu    VARARGS_ADDARGS(print);
1454039Sbinkertn@umich.edu}
1462SN/A
1474039Sbinkertn@umich.eduinline void
1484039Sbinkertn@umich.educprintf(const char *format, CPRINTF_DECLARATION)
1494039Sbinkertn@umich.edu{
1504039Sbinkertn@umich.edu    ccprintf(std::cout, format, VARARGS_ALLARGS);
1514039Sbinkertn@umich.edu}
1522SN/A
1534039Sbinkertn@umich.eduinline std::string
1544039Sbinkertn@umich.educsprintf(const char *format, CPRINTF_DECLARATION)
1554039Sbinkertn@umich.edu{
1564039Sbinkertn@umich.edu    std::stringstream stream;
1574039Sbinkertn@umich.edu    ccprintf(stream, format, VARARGS_ALLARGS);
1584039Sbinkertn@umich.edu    return stream.str();
1594039Sbinkertn@umich.edu}
1604039Sbinkertn@umich.edu
1614039Sbinkertn@umich.edu/*
1624039Sbinkertn@umich.edu * functions again with std::string.  We have both so we don't waste
1634039Sbinkertn@umich.edu * time converting const char * to std::string since we don't take
1644039Sbinkertn@umich.edu * advantage of it.
1654039Sbinkertn@umich.edu */
1662SN/Ainline void
1674039Sbinkertn@umich.educcprintf(std::ostream &stream, const std::string &format,
1684039Sbinkertn@umich.edu         const CPrintfArgsList &args)
1694039Sbinkertn@umich.edu{
1704039Sbinkertn@umich.edu    ccprintf(stream, format.c_str(), args);
1714039Sbinkertn@umich.edu}
1722SN/A
1732SN/Ainline void
1744039Sbinkertn@umich.educcprintf(std::ostream &stream, const std::string &format, CPRINTF_DECLARATION)
1754039Sbinkertn@umich.edu{
1764213Ssaidi@eecs.umich.edu    ccprintf(stream, format.c_str(), VARARGS_ALLARGS);
1774039Sbinkertn@umich.edu}
1782SN/A
1794039Sbinkertn@umich.eduinline void
1804039Sbinkertn@umich.educprintf(const std::string &format, CPRINTF_DECLARATION)
1814039Sbinkertn@umich.edu{
1824213Ssaidi@eecs.umich.edu    ccprintf(std::cout, format.c_str(), VARARGS_ALLARGS);
1834039Sbinkertn@umich.edu}
1844039Sbinkertn@umich.edu
1852SN/Ainline std::string
1864039Sbinkertn@umich.educsprintf(const std::string &format, CPRINTF_DECLARATION)
1874039Sbinkertn@umich.edu{
1884039Sbinkertn@umich.edu    std::stringstream stream;
1894213Ssaidi@eecs.umich.edu    ccprintf(stream, format.c_str(), VARARGS_ALLARGS);
1904039Sbinkertn@umich.edu    return stream.str();
1912SN/A}
1922SN/A
1932SN/A#endif // __CPRINTF_HH__
194