move Printf/SPrintf into the Loki namespace

git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@433 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
syntheticpp 2006-01-05 20:41:47 +00:00
parent 70fdd98598
commit 90f1278043

View file

@ -25,490 +25,495 @@
#include <cassert> #include <cassert>
#include <locale> #include <locale>
// Crude writing method: writes straight to the file, unbuffered namespace Loki
// Must be combined with a buffer to work properly (and efficiently) {
void write(std::FILE* f, const char* from, const char* to) { // Crude writing method: writes straight to the file, unbuffered
assert(from <= to); // Must be combined with a buffer to work properly (and efficiently)
fwrite(from, 1, to - from, f);
}
// Write to a string void write(std::FILE* f, const char* from, const char* to) {
assert(from <= to);
fwrite(from, 1, to - from, f);
}
void write(std::string& s, const char* from, const char* to) { // Write to a string
assert(from <= to);
s.append(from, to);
}
// Write to a fixed-size buffer void write(std::string& s, const char* from, const char* to) {
assert(from <= to);
s.append(from, to);
}
template <class Char> // Write to a fixed-size buffer
void write(std::pair<Char*, std::size_t>& s, const Char* from, const Char* to) {
assert(from <= to);
if (from + s.second > to) throw std::overflow_error("");
s.first = copy(from, to, s.first);
s.second -= to - from;
}
//////////////////////////////////////////////////////////////////////////////// template <class Char>
// PrintfState class template void write(std::pair<Char*, std::size_t>& s, const Char* from, const Char* to) {
// Holds the formatting state, and implements operator() to format stuff assert(from <= to);
// Todo: make sure errors are handled properly if (from + s.second > to) throw std::overflow_error("");
//////////////////////////////////////////////////////////////////////////////// s.first = copy(from, to, s.first);
s.second -= to - from;
}
template <class Device, class Char> ////////////////////////////////////////////////////////////////////////////////
struct PrintfState { // PrintfState class template
PrintfState(Device dev, const Char * format) // Holds the formatting state, and implements operator() to format stuff
: device_(dev) // Todo: make sure errors are handled properly
, format_(format) ////////////////////////////////////////////////////////////////////////////////
, result_(0) {
Advance();
}
~PrintfState() { template <class Device, class Char>
} struct PrintfState {
PrintfState(Device dev, const Char * format)
: device_(dev)
, format_(format)
, result_(0) {
Advance();
}
#define LOKI_PRINTF_STATE_FORWARD(type) \ ~PrintfState() {
PrintfState& operator()(type par) {\ }
return (*this)(static_cast< unsigned long >(par)); \
}
LOKI_PRINTF_STATE_FORWARD(bool) #define LOKI_PRINTF_STATE_FORWARD(type) \
LOKI_PRINTF_STATE_FORWARD(char) PrintfState& operator()(type par) {\
LOKI_PRINTF_STATE_FORWARD(signed char) return (*this)(static_cast< unsigned long >(par)); \
LOKI_PRINTF_STATE_FORWARD(unsigned char) }
LOKI_PRINTF_STATE_FORWARD(short)
LOKI_PRINTF_STATE_FORWARD(unsigned short)
LOKI_PRINTF_STATE_FORWARD(int)
LOKI_PRINTF_STATE_FORWARD(unsigned)
LOKI_PRINTF_STATE_FORWARD(long)
// Print (or gobble in case of the "*" specifier) an int LOKI_PRINTF_STATE_FORWARD(bool)
PrintfState& operator()(unsigned long i) { LOKI_PRINTF_STATE_FORWARD(char)
if (result_ == -1) return *this; // don't even bother LOKI_PRINTF_STATE_FORWARD(signed char)
// % [flags] [width] [.prec] [modifier] type_char LOKI_PRINTF_STATE_FORWARD(unsigned char)
// Fetch the flags LOKI_PRINTF_STATE_FORWARD(short)
ReadFlags(); LOKI_PRINTF_STATE_FORWARD(unsigned short)
if (*format_ == '*') { LOKI_PRINTF_STATE_FORWARD(int)
// read the width and get out LOKI_PRINTF_STATE_FORWARD(unsigned)
SetWidth(static_cast<size_t>(i)); LOKI_PRINTF_STATE_FORWARD(long)
++format_;
return *this;
}
ReadWidth();
// precision
if (*format_ == '.') {
// deal with precision
if (format_[1] == '*') {
// read the precision and get out
SetPrec(static_cast<size_t>(i));
format_ += 2;
return *this;
}
ReadPrecision();
}
ReadModifiers();
// input size modifier
if (ForceShort()) {
// short int
const Char c = *format_;
if (c == 'x' || c == 'X' || c == 'u' || c == 'o') {
i = static_cast<unsigned long>(static_cast<unsigned short>(i));
}
}
FormatWithCurrentFlags(i);
return *this;
}
PrintfState& operator()(double n) { // Print (or gobble in case of the "*" specifier) an int
if (result_ == -1) return *this; // don't even bother PrintfState& operator()(unsigned long i) {
PrintFloatingPoint(n); if (result_ == -1) return *this; // don't even bother
return *this; // % [flags] [width] [.prec] [modifier] type_char
} // Fetch the flags
ReadFlags();
if (*format_ == '*') {
// read the width and get out
SetWidth(static_cast<size_t>(i));
++format_;
return *this;
}
ReadWidth();
// precision
if (*format_ == '.') {
// deal with precision
if (format_[1] == '*') {
// read the precision and get out
SetPrec(static_cast<size_t>(i));
format_ += 2;
return *this;
}
ReadPrecision();
}
ReadModifiers();
// input size modifier
if (ForceShort()) {
// short int
const Char c = *format_;
if (c == 'x' || c == 'X' || c == 'u' || c == 'o') {
i = static_cast<unsigned long>(static_cast<unsigned short>(i));
}
}
FormatWithCurrentFlags(i);
return *this;
}
PrintfState& operator()(long double n) { PrintfState& operator()(double n) {
if (result_ == -1) return *this; // don't even bother if (result_ == -1) return *this; // don't even bother
PrintFloatingPoint(n); PrintFloatingPoint(n);
return *this; return *this;
} }
// Store the number of characters printed so far PrintfState& operator()(long double n) {
PrintfState& operator()(int * pi) { if (result_ == -1) return *this; // don't even bother
return StoreCountHelper(pi); PrintFloatingPoint(n);
} return *this;
}
// Store the number of characters printed so far // Store the number of characters printed so far
PrintfState& operator()(short * pi) { PrintfState& operator()(int * pi) {
return StoreCountHelper(pi); return StoreCountHelper(pi);
} }
// Store the number of characters printed so far // Store the number of characters printed so far
PrintfState& operator()(long * pi) { PrintfState& operator()(short * pi) {
return StoreCountHelper(pi); return StoreCountHelper(pi);
} }
PrintfState& operator()(const char *const s) { // Store the number of characters printed so far
if (result_ == -1) return *this; PrintfState& operator()(long * pi) {
ReadLeaders(); return StoreCountHelper(pi);
const char fmt = *format_; }
if (fmt == 'p') {
FormatWithCurrentFlags(reinterpret_cast<uintptr_t>(s));
return *this;
}
if (fmt != 's') {
result_ = -1;
return *this;
}
const size_t len = std::min(strlen(s), prec_);
if (width_ > len) {
if (LeftJustify()) {
Write(s, s + len);
Fill(' ', width_ - len);
} else {
Fill(' ', width_ - len);
Write(s, s + len);
}
} else {
Write(s, s + len);
}
Next();
return *this;
}
PrintfState& operator()(const void *const p) { PrintfState& operator()(const char *const s) {
return (*this)(reinterpret_cast<uintptr_t>(p)); if (result_ == -1) return *this;
} ReadLeaders();
const char fmt = *format_;
if (fmt == 'p') {
FormatWithCurrentFlags(reinterpret_cast<uintptr_t>(s));
return *this;
}
if (fmt != 's') {
result_ = -1;
return *this;
}
const size_t len = std::min(strlen(s), prec_);
if (width_ > len) {
if (LeftJustify()) {
Write(s, s + len);
Fill(' ', width_ - len);
} else {
Fill(' ', width_ - len);
Write(s, s + len);
}
} else {
Write(s, s + len);
}
Next();
return *this;
}
// read the result PrintfState& operator()(const void *const p) {
operator int() const { return (*this)(reinterpret_cast<uintptr_t>(p));
return result_; }
}
private: // read the result
PrintfState& operator=(const PrintfState&); operator int() const {
template <typename T> return result_;
PrintfState& StoreCountHelper(T *const pi) { }
if (result_ == -1) return *this; // don't even bother
ReadLeaders();
const char fmt = *format_;
if (fmt == 'p') { // pointer
FormatWithCurrentFlags(reinterpret_cast<unsigned long>(pi));
return *this;
}
if (fmt != 'n') {
result_ = -1;
return *this;
}
assert(pi != 0);
*pi = result_;
Next();
return *this;
}
void FormatWithCurrentFlags(const unsigned long i) { private:
// look at the format character PrintfState& operator=(const PrintfState&);
Char formatChar = *format_; template <typename T>
bool isSigned = formatChar == 'd' || formatChar == 'i'; PrintfState& StoreCountHelper(T *const pi) {
if (formatChar == 'p') { if (result_ == -1) return *this; // don't even bother
formatChar = 'x'; // pointers go to hex ReadLeaders();
SetAlternateForm(); // printed with '0x' in front const char fmt = *format_;
isSigned = true; // that's what gcc does if (fmt == 'p') { // pointer
} FormatWithCurrentFlags(reinterpret_cast<unsigned long>(pi));
if (!strchr("cdiuoxX", formatChar)) { return *this;
result_ = -1; }
return; if (fmt != 'n') {
} result_ = -1;
Char buf[ return *this;
sizeof(unsigned long) * 3 // digits }
+ 1 // sign or ' ' assert(pi != 0);
+ 2 // 0x or 0X *pi = result_;
+ 1]; // terminating zero Next();
const Char *const bufEnd = buf + (sizeof(buf) / sizeof(Char)); return *this;
Char * bufLast = buf + (sizeof(buf) / sizeof(Char) - 1); }
Char signChar = 0;
unsigned int base = 10;
if (formatChar == 'c') { void FormatWithCurrentFlags(const unsigned long i) {
// Format only one character // look at the format character
// The 'fill with zeros' flag is ignored Char formatChar = *format_;
ResetFillZeros(); bool isSigned = formatChar == 'd' || formatChar == 'i';
*bufLast = static_cast<char>(i); if (formatChar == 'p') {
} else { formatChar = 'x'; // pointers go to hex
// TODO: inefficient code, refactor SetAlternateForm(); // printed with '0x' in front
const bool negative = isSigned && static_cast<long>(i) < 0; isSigned = true; // that's what gcc does
if (formatChar == 'o') base = 8; }
else if (formatChar == 'x' || formatChar == 'X') base = 16; if (!strchr("cdiuoxX", formatChar)) {
bufLast = isSigned result_ = -1;
? RenderWithoutSign(static_cast<long>(i), bufLast, base, return;
formatChar == 'X') }
: RenderWithoutSign(i, bufLast, base, Char buf[
formatChar == 'X'); sizeof(unsigned long) * 3 // digits
// Add the sign + 1 // sign or ' '
if (isSigned) { + 2 // 0x or 0X
negative ? signChar = '-' + 1]; // terminating zero
: ShowSignAlways() ? signChar = '+' const Char *const bufEnd = buf + (sizeof(buf) / sizeof(Char));
: Blank() ? signChar = ' ' Char * bufLast = buf + (sizeof(buf) / sizeof(Char) - 1);
: 0; Char signChar = 0;
} unsigned int base = 10;
}
// precision
size_t
countDigits = bufEnd - bufLast,
countZeros = prec_ != size_t(-1) && countDigits < prec_ &&
formatChar != 'c'
? prec_ - countDigits
: 0,
countBase = base != 10 && AlternateForm() && i != 0
? (base == 16 ? 2 : countZeros > 0 ? 0 : 1)
: 0,
countSign = (signChar != 0),
totalPrintable = countDigits + countZeros + countBase + countSign;
size_t countPadLeft = 0, countPadRight = 0;
if (width_ > totalPrintable) {
if (LeftJustify()) {
countPadRight = width_ - totalPrintable;
countPadLeft = 0;
} else {
countPadLeft = width_ - totalPrintable;
countPadRight = 0;
}
}
if (FillZeros() && prec_ == size_t(-1)) {
// pad with zeros and no precision - transfer padding to precision
countZeros = countPadLeft;
countPadLeft = 0;
}
// ok, all computed, ready to print to device
Fill(' ', countPadLeft);
if (signChar != 0) Write(&signChar, &signChar + 1);
if (countBase > 0) Fill('0', 1);
if (countBase == 2) Fill(formatChar, 1);
Fill('0', countZeros);
Write(bufLast, bufEnd);
Fill(' ', countPadRight);
// done, advance
Next();
}
void Write(const Char* b, const Char* e) { if (formatChar == 'c') {
if (result_ < 0) return; // Format only one character
const ptrdiff_t x = e - b; // The 'fill with zeros' flag is ignored
write(device_, b, e); ResetFillZeros();
result_ += x; *bufLast = static_cast<char>(i);
} } else {
// TODO: inefficient code, refactor
const bool negative = isSigned && static_cast<long>(i) < 0;
if (formatChar == 'o') base = 8;
else if (formatChar == 'x' || formatChar == 'X') base = 16;
bufLast = isSigned
? RenderWithoutSign(static_cast<long>(i), bufLast, base,
formatChar == 'X')
: RenderWithoutSign(i, bufLast, base,
formatChar == 'X');
// Add the sign
if (isSigned) {
negative ? signChar = '-'
: ShowSignAlways() ? signChar = '+'
: Blank() ? signChar = ' '
: 0;
}
}
// precision
size_t
countDigits = bufEnd - bufLast,
countZeros = prec_ != size_t(-1) && countDigits < prec_ &&
formatChar != 'c'
? prec_ - countDigits
: 0,
countBase = base != 10 && AlternateForm() && i != 0
? (base == 16 ? 2 : countZeros > 0 ? 0 : 1)
: 0,
countSign = (signChar != 0),
totalPrintable = countDigits + countZeros + countBase + countSign;
size_t countPadLeft = 0, countPadRight = 0;
if (width_ > totalPrintable) {
if (LeftJustify()) {
countPadRight = width_ - totalPrintable;
countPadLeft = 0;
} else {
countPadLeft = width_ - totalPrintable;
countPadRight = 0;
}
}
if (FillZeros() && prec_ == size_t(-1)) {
// pad with zeros and no precision - transfer padding to precision
countZeros = countPadLeft;
countPadLeft = 0;
}
// ok, all computed, ready to print to device
Fill(' ', countPadLeft);
if (signChar != 0) Write(&signChar, &signChar + 1);
if (countBase > 0) Fill('0', 1);
if (countBase == 2) Fill(formatChar, 1);
Fill('0', countZeros);
Write(bufLast, bufEnd);
Fill(' ', countPadRight);
// done, advance
Next();
}
template <class Double> void Write(const Char* b, const Char* e) {
void PrintFloatingPoint(Double n) { if (result_ < 0) return;
const Char *const fmt = format_ - 1; const ptrdiff_t x = e - b;
assert(*fmt == '%'); write(device_, b, e);
// enforce format string validity result_ += x;
ReadLeaders(); }
// enforce format spec
if (!strchr("eEfgG", *format_)) {
result_ = -1;
return;
}
// format char validated, copy it to a temp and use legacy sprintf
++format_;
Char fmtBuf[128], resultBuf[1024];
if (format_ >= fmt + sizeof(fmtBuf) / sizeof(Char)) {
result_ = -1;
return;
}
memcpy(fmtBuf, fmt, (format_ - fmt) * sizeof(Char));
fmtBuf[format_ - fmt] = 0;
#ifdef _MSC_VER
const int stored = _snprintf(resultBuf,
#else
const int stored = snprintf(resultBuf,
#endif
sizeof(resultBuf) / sizeof(Char), fmtBuf, n);
if (stored < 0) {
result_ = -1;
return;
}
Write(resultBuf, resultBuf + strlen(resultBuf));
Advance(); // output stuff to the next format directive
}
void Fill(const Char c, size_t n) { template <class Double>
for (; n > 0; --n) { void PrintFloatingPoint(Double n) {
Write(&c, &c + 1); const Char *const fmt = format_ - 1;
} assert(*fmt == '%');
} // enforce format string validity
ReadLeaders();
// enforce format spec
if (!strchr("eEfgG", *format_)) {
result_ = -1;
return;
}
// format char validated, copy it to a temp and use legacy sprintf
++format_;
Char fmtBuf[128], resultBuf[1024];
if (format_ >= fmt + sizeof(fmtBuf) / sizeof(Char)) {
result_ = -1;
return;
}
memcpy(fmtBuf, fmt, (format_ - fmt) * sizeof(Char));
fmtBuf[format_ - fmt] = 0;
#ifdef _MSC_VER
const int stored = _snprintf(resultBuf,
#else
const int stored = snprintf(resultBuf,
#endif
sizeof(resultBuf) / sizeof(Char), fmtBuf, n);
if (stored < 0) {
result_ = -1;
return;
}
Write(resultBuf, resultBuf + strlen(resultBuf));
Advance(); // output stuff to the next format directive
}
Char* RenderWithoutSign(unsigned long n, char* bufLast, void Fill(const Char c, size_t n) {
unsigned int base, bool uppercase) { for (; n > 0; --n) {
const Char hex1st = uppercase ? 'A' : 'a'; Write(&c, &c + 1);
for (;;) { }
const unsigned long next = n / base; }
Char c = n - next * base;
c += (c <= 9) ? '0' : hex1st - 10;
*bufLast = c;
n = next;
if (n == 0) break;
--bufLast;
}
return bufLast;
}
char* RenderWithoutSign(long n, char* bufLast, unsigned int base, Char* RenderWithoutSign(unsigned long n, char* bufLast,
bool uppercase) { unsigned int base, bool uppercase) {
if (n != LONG_MIN) { const Char hex1st = uppercase ? 'A' : 'a';
return RenderWithoutSign(static_cast<unsigned long>(n < 0 ? -n : n), for (;;) {
bufLast, base, uppercase); const unsigned long next = n / base;
} Char c = n - next * base;
// annoying corner case c += (c <= 9) ? '0' : hex1st - 10;
char* save = bufLast; *bufLast = c;
++n; n = next;
bufLast = RenderWithoutSign(static_cast<unsigned long>(n), if (n == 0) break;
bufLast, base, uppercase); --bufLast;
--(*save); }
return bufLast; return bufLast;
} }
void Next() { char* RenderWithoutSign(long n, char* bufLast, unsigned int base,
++format_; bool uppercase) {
Advance(); if (n != LONG_MIN) {
} return RenderWithoutSign(static_cast<unsigned long>(n < 0 ? -n : n),
bufLast, base, uppercase);
}
// annoying corner case
char* save = bufLast;
++n;
bufLast = RenderWithoutSign(static_cast<unsigned long>(n),
bufLast, base, uppercase);
--(*save);
return bufLast;
}
void Advance() { void Next() {
ResetAll(); ++format_;
const Char* begin = format_; Advance();
for (;;) { }
if (*format_ == '%') {
if (format_[1] != '%') { // It's a format specifier
Write(begin, format_);
++format_;
break;
}
// It's a "%%"
Write(begin, ++format_);
begin = ++format_;
continue;
}
if (*format_ == 0) {
Write(begin, format_);
break;
}
++format_;
}
}
void ReadFlags() { void Advance() {
for (;; ++format_) { ResetAll();
switch (*format_) { const Char* begin = format_;
case '-': SetLeftJustify(); break; for (;;) {
case '+': SetShowSignAlways(); break; if (*format_ == '%') {
case ' ': SetBlank(); break; if (format_[1] != '%') { // It's a format specifier
case '#': SetAlternateForm(); break; Write(begin, format_);
case '0': SetFillZeros(); break; ++format_;
default: return; break;
} }
} // It's a "%%"
} Write(begin, ++format_);
begin = ++format_;
continue;
}
if (*format_ == 0) {
Write(begin, format_);
break;
}
++format_;
}
}
void ParseDecimalSizeT(size_t& dest) { void ReadFlags() {
if (!std::isdigit(*format_, std::locale())) return; for (;; ++format_) {
size_t r = 0; switch (*format_) {
do { case '-': SetLeftJustify(); break;
// TODO: inefficient - rewrite case '+': SetShowSignAlways(); break;
r *= 10; case ' ': SetBlank(); break;
r += *format_ - '0'; case '#': SetAlternateForm(); break;
++format_; case '0': SetFillZeros(); break;
} while (std::isdigit(*format_, std::locale())); default: return;
dest = r; }
} }
}
void ReadWidth() { void ParseDecimalSizeT(size_t& dest) {
ParseDecimalSizeT(width_); if (!std::isdigit(*format_, std::locale())) return;
} size_t r = 0;
do {
// TODO: inefficient - rewrite
r *= 10;
r += *format_ - '0';
++format_;
} while (std::isdigit(*format_, std::locale()));
dest = r;
}
void ReadPrecision() { void ReadWidth() {
assert(*format_ == '.'); ParseDecimalSizeT(width_);
++format_; }
ParseDecimalSizeT(prec_);
}
void ReadModifiers() { void ReadPrecision() {
switch (*format_) { assert(*format_ == '.');
case 'h': SetForceShort(); ++format_; break; ++format_;
case 'l': ++format_; break; ParseDecimalSizeT(prec_);
// more (C99 and platform-specific modifiers) to come }
}
}
void ReadLeaders() { void ReadModifiers() {
ReadFlags(); switch (*format_) {
ReadWidth(); case 'h': SetForceShort(); ++format_; break;
if (*format_ == '.') ReadPrecision(); case 'l': ++format_; break;
ReadModifiers(); // more (C99 and platform-specific modifiers) to come
} }
}
enum { void ReadLeaders() {
leftJustify = 1, ReadFlags();
showSignAlways = 2, ReadWidth();
blank = 4, if (*format_ == '.') ReadPrecision();
alternateForm = 8, ReadModifiers();
fillZeros = 16, }
forceShort = 32
};
bool LeftJustify() const { return (flags_ & leftJustify) != 0; } enum {
bool ShowSignAlways() const { return (flags_ & showSignAlways) != 0; } leftJustify = 1,
void SetWidth(size_t w) { width_ = w; } showSignAlways = 2,
void SetLeftJustify() { flags_ |= leftJustify; } blank = 4,
void SetShowSignAlways() { flags_ |= showSignAlways; } alternateForm = 8,
bool Blank() const { return (flags_ & blank) != 0; } fillZeros = 16,
bool AlternateForm() const { return (flags_ & alternateForm) != 0; } forceShort = 32
bool FillZeros() const { return (flags_ & fillZeros) != 0; } };
bool ForceShort() const { return (flags_ & forceShort) != 0; }
void SetPrec(size_t p) { prec_ = p; } bool LeftJustify() const { return (flags_ & leftJustify) != 0; }
void SetBlank() { flags_ |= blank; } bool ShowSignAlways() const { return (flags_ & showSignAlways) != 0; }
void SetAlternateForm() { flags_ |= alternateForm; } void SetWidth(size_t w) { width_ = w; }
void SetFillZeros() { flags_ |= fillZeros; } void SetLeftJustify() { flags_ |= leftJustify; }
void ResetFillZeros() { flags_ &= ~fillZeros; } void SetShowSignAlways() { flags_ |= showSignAlways; }
void SetForceShort() { flags_ |= forceShort; } bool Blank() const { return (flags_ & blank) != 0; }
bool AlternateForm() const { return (flags_ & alternateForm) != 0; }
bool FillZeros() const { return (flags_ & fillZeros) != 0; }
bool ForceShort() const { return (flags_ & forceShort) != 0; }
void ResetAll() { void SetPrec(size_t p) { prec_ = p; }
assert(result_ != EOF); void SetBlank() { flags_ |= blank; }
width_ = 0; void SetAlternateForm() { flags_ |= alternateForm; }
prec_ = size_t(-1); void SetFillZeros() { flags_ |= fillZeros; }
flags_ = 0; void ResetFillZeros() { flags_ &= ~fillZeros; }
} void SetForceShort() { flags_ |= forceShort; }
// state void ResetAll() {
Device device_; assert(result_ != EOF);
const Char* format_; width_ = 0;
size_t width_; prec_ = size_t(-1);
size_t prec_; flags_ = 0;
unsigned int flags_; }
int result_;
};
PrintfState<std::FILE*, char> Printf(const char* format) { // state
return PrintfState<std::FILE*, char>(stdout, format); Device device_;
} const Char* format_;
size_t width_;
size_t prec_;
unsigned int flags_;
int result_;
};
PrintfState<std::FILE*, char> FPrintf(FILE* f, const char* format) { PrintfState<std::FILE*, char> Printf(const char* format) {
return PrintfState<std::FILE*, char>(f, format); return PrintfState<std::FILE*, char>(stdout, format);
} }
PrintfState<std::string&, char> SPrintf(std::string& s, const char* format) { PrintfState<std::FILE*, char> FPrintf(FILE* f, const char* format) {
return PrintfState<std::string&, char>(s, format); return PrintfState<std::FILE*, char>(f, format);
} }
template <class T, class Char> PrintfState<std::string&, char> SPrintf(std::string& s, const char* format) {
PrintfState<T&, Char> XPrintf(T& device, const Char* format) { return PrintfState<std::string&, char>(s, format);
return PrintfState<T&, Char>(device, format); }
}
template <class Char, std::size_t N> template <class T, class Char>
PrintfState<std::pair<Char*, std::size_t>, Char> PrintfState<T&, Char> XPrintf(T& device, const Char* format) {
BufPrintf(Char (&buf)[N], const Char* format) { return PrintfState<T&, Char>(device, format);
std::pair<Char*, std::size_t> temp(buf, N); }
return PrintfState<std::pair<Char*, std::size_t>, Char>(temp, format);
} template <class Char, std::size_t N>
PrintfState<std::pair<Char*, std::size_t>, Char>
BufPrintf(Char (&buf)[N], const Char* format) {
std::pair<Char*, std::size_t> temp(buf, N);
return PrintfState<std::pair<Char*, std::size_t>, Char>(temp, format);
}
}// namespace Loki
#endif //SAFEFORMAT_H_ #endif //SAFEFORMAT_H_