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.
28 * Authors: Nathan Binkert
32 #ifndef __BASE_CPRINTF_HH__
33 #define __BASE_CPRINTF_HH__
40 #include "base/varargs.hh"
41 #include "base/cprintf_formats.hh"
45 #define CPRINTF_DECLARATION VARARGS_DECLARATION(cp::Print)
46 #define CPRINTF_DEFINITION VARARGS_DEFINITION(cp::Print)
56 std::ios::fmtflags saved_flags;
64 Print(std::ostream &stream, const std::string &format);
65 Print(std::ostream &stream, const char *format);
76 get_number(const T& data)
83 add_arg(const T &data)
89 fmt.get_width = false;
91 fmt.width = get_number(data);
95 if (fmt.get_precision) {
96 fmt.get_precision = false;
98 fmt.precision = get_number(data);
102 switch (fmt.format) {
103 case Format::character:
104 format_char(stream, data, fmt);
107 case Format::integer:
108 format_integer(stream, data, fmt);
111 case Format::floating:
112 format_float(stream, data, fmt);
116 format_string(stream, data, fmt);
120 stream << "<bad format>";
128 /* end namespace cp */ }
130 typedef VarArgs::List<cp::Print> CPrintfArgsList;
133 ccprintf(std::ostream &stream, const char *format, const CPrintfArgsList &args)
135 cp::Print print(stream, format);
136 args.add_args(print);
140 ccprintf(std::ostream &stream, const char *format, CPRINTF_DECLARATION)
142 cp::Print print(stream, format);
143 VARARGS_ADDARGS(print);
147 cprintf(const char *format, CPRINTF_DECLARATION)
149 ccprintf(std::cout, format, VARARGS_ALLARGS);
153 csprintf(const char *format, CPRINTF_DECLARATION)
155 std::stringstream stream;
156 ccprintf(stream, format, VARARGS_ALLARGS);
161 * functions again with std::string. We have both so we don't waste
162 * time converting const char * to std::string since we don't take
166 ccprintf(std::ostream &stream, const std::string &format,
167 const CPrintfArgsList &args)
169 ccprintf(stream, format.c_str(), args);
173 ccprintf(std::ostream &stream, const std::string &format, CPRINTF_DECLARATION)
175 ccprintf(stream, format.c_str(), VARARGS_ALLARGS);
179 cprintf(const std::string &format, CPRINTF_DECLARATION)
181 ccprintf(std::cout, format.c_str(), VARARGS_ALLARGS);
185 csprintf(const std::string &format, CPRINTF_DECLARATION)
187 std::stringstream stream;
188 ccprintf(stream, format.c_str(), VARARGS_ALLARGS);
192 #endif // __CPRINTF_HH__