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() {
}
#define LOKI_PRINTF_STATE_FORWARD(type) \ template <class Device, class Char>
PrintfState& operator()(type par) {\ struct PrintfState {
return (*this)(static_cast< unsigned long >(par)); \ PrintfState(Device dev, const Char * format)
} : device_(dev)
, format_(format)
, result_(0) {
Advance();
}
~PrintfState() {
}
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) {
if (result_ == -1) return *this; // don't even bother
PrintFloatingPoint(n);
return *this;
}
PrintfState& operator()(long 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()(double n) {
if (result_ == -1) return *this; // don't even bother
PrintFloatingPoint(n);
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
PrintfState& operator()(short * pi) {
return StoreCountHelper(pi);
}
// Store the number of characters printed so far
PrintfState& operator()(long * pi) {
return StoreCountHelper(pi);
}
PrintfState& operator()(const char *const s) {
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;
}
PrintfState& operator()(const void *const p) {
return (*this)(reinterpret_cast<uintptr_t>(p));
}
// read the result
operator int() const {
return result_;
}
private:
PrintfState& operator=(const PrintfState&);
template <typename T>
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) { // Store the number of characters printed so far
// look at the format character PrintfState& operator()(int * pi) {
Char formatChar = *format_; return StoreCountHelper(pi);
bool isSigned = formatChar == 'd' || formatChar == 'i'; }
if (formatChar == 'p') {
formatChar = 'x'; // pointers go to hex // Store the number of characters printed so far
SetAlternateForm(); // printed with '0x' in front PrintfState& operator()(short * pi) {
isSigned = true; // that's what gcc does return StoreCountHelper(pi);
} }
if (!strchr("cdiuoxX", formatChar)) {
result_ = -1; // Store the number of characters printed so far
return; PrintfState& operator()(long * pi) {
} return StoreCountHelper(pi);
Char buf[ }
sizeof(unsigned long) * 3 // digits
+ 1 // sign or ' ' PrintfState& operator()(const char *const s) {
+ 2 // 0x or 0X if (result_ == -1) return *this;
+ 1]; // terminating zero ReadLeaders();
const Char *const bufEnd = buf + (sizeof(buf) / sizeof(Char)); const char fmt = *format_;
Char * bufLast = buf + (sizeof(buf) / sizeof(Char) - 1); if (fmt == 'p') {
Char signChar = 0; FormatWithCurrentFlags(reinterpret_cast<uintptr_t>(s));
unsigned int base = 10; return *this;
}
if (formatChar == 'c') { if (fmt != 's') {
// Format only one character result_ = -1;
// The 'fill with zeros' flag is ignored return *this;
ResetFillZeros(); }
*bufLast = static_cast<char>(i); const size_t len = std::min(strlen(s), prec_);
} else { if (width_ > len) {
// TODO: inefficient code, refactor if (LeftJustify()) {
const bool negative = isSigned && static_cast<long>(i) < 0; Write(s, s + len);
if (formatChar == 'o') base = 8; Fill(' ', width_ - len);
else if (formatChar == 'x' || formatChar == 'X') base = 16; } else {
bufLast = isSigned Fill(' ', width_ - len);
? RenderWithoutSign(static_cast<long>(i), bufLast, base, Write(s, s + len);
formatChar == 'X') }
: RenderWithoutSign(i, bufLast, base, } else {
formatChar == 'X'); Write(s, s + len);
// Add the sign }
if (isSigned) { Next();
negative ? signChar = '-' return *this;
: ShowSignAlways() ? signChar = '+' }
: Blank() ? signChar = ' '
: 0; PrintfState& operator()(const void *const p) {
} return (*this)(reinterpret_cast<uintptr_t>(p));
} }
// precision
size_t // read the result
countDigits = bufEnd - bufLast, operator int() const {
countZeros = prec_ != size_t(-1) && countDigits < prec_ && return result_;
formatChar != 'c' }
? prec_ - countDigits
: 0, private:
countBase = base != 10 && AlternateForm() && i != 0 PrintfState& operator=(const PrintfState&);
? (base == 16 ? 2 : countZeros > 0 ? 0 : 1) template <typename T>
: 0, PrintfState& StoreCountHelper(T *const pi) {
countSign = (signChar != 0), if (result_ == -1) return *this; // don't even bother
totalPrintable = countDigits + countZeros + countBase + countSign; ReadLeaders();
size_t countPadLeft = 0, countPadRight = 0; const char fmt = *format_;
if (width_ > totalPrintable) { if (fmt == 'p') { // pointer
if (LeftJustify()) { FormatWithCurrentFlags(reinterpret_cast<unsigned long>(pi));
countPadRight = width_ - totalPrintable; return *this;
countPadLeft = 0; }
} else { if (fmt != 'n') {
countPadLeft = width_ - totalPrintable; result_ = -1;
countPadRight = 0; return *this;
} }
} assert(pi != 0);
if (FillZeros() && prec_ == size_t(-1)) { *pi = result_;
// pad with zeros and no precision - transfer padding to precision Next();
countZeros = countPadLeft; return *this;
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 (result_ < 0) return;
const ptrdiff_t x = e - b;
write(device_, b, e);
result_ += x;
}
template <class Double> void FormatWithCurrentFlags(const unsigned long i) {
void PrintFloatingPoint(Double n) { // look at the format character
const Char *const fmt = format_ - 1; Char formatChar = *format_;
assert(*fmt == '%'); bool isSigned = formatChar == 'd' || formatChar == 'i';
// enforce format string validity if (formatChar == 'p') {
ReadLeaders(); formatChar = 'x'; // pointers go to hex
// enforce format spec SetAlternateForm(); // printed with '0x' in front
if (!strchr("eEfgG", *format_)) { isSigned = true; // that's what gcc does
result_ = -1; }
return; if (!strchr("cdiuoxX", formatChar)) {
} result_ = -1;
// format char validated, copy it to a temp and use legacy sprintf return;
++format_; }
Char fmtBuf[128], resultBuf[1024]; Char buf[
if (format_ >= fmt + sizeof(fmtBuf) / sizeof(Char)) { sizeof(unsigned long) * 3 // digits
result_ = -1; + 1 // sign or ' '
return; + 2 // 0x or 0X
} + 1]; // terminating zero
memcpy(fmtBuf, fmt, (format_ - fmt) * sizeof(Char)); const Char *const bufEnd = buf + (sizeof(buf) / sizeof(Char));
fmtBuf[format_ - fmt] = 0; Char * bufLast = buf + (sizeof(buf) / sizeof(Char) - 1);
#ifdef _MSC_VER Char signChar = 0;
const int stored = _snprintf(resultBuf, unsigned int base = 10;
#else
const int stored = snprintf(resultBuf, if (formatChar == 'c') {
#endif // Format only one character
sizeof(resultBuf) / sizeof(Char), fmtBuf, n); // The 'fill with zeros' flag is ignored
if (stored < 0) { ResetFillZeros();
result_ = -1; *bufLast = static_cast<char>(i);
return; } else {
} // TODO: inefficient code, refactor
Write(resultBuf, resultBuf + strlen(resultBuf)); const bool negative = isSigned && static_cast<long>(i) < 0;
Advance(); // output stuff to the next format directive 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();
}
void Write(const Char* b, const Char* e) {
if (result_ < 0) return;
const ptrdiff_t x = e - b;
write(device_, b, e);
result_ += x;
}
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();
Char* RenderWithoutSign(unsigned long n, char* bufLast, // enforce format spec
unsigned int base, bool uppercase) { if (!strchr("eEfgG", *format_)) {
const Char hex1st = uppercase ? 'A' : 'a'; result_ = -1;
for (;;) { return;
const unsigned long next = n / base; }
Char c = n - next * base; // format char validated, copy it to a temp and use legacy sprintf
c += (c <= 9) ? '0' : hex1st - 10; ++format_;
*bufLast = c; Char fmtBuf[128], resultBuf[1024];
n = next; if (format_ >= fmt + sizeof(fmtBuf) / sizeof(Char)) {
if (n == 0) break; result_ = -1;
--bufLast; return;
} }
return bufLast; memcpy(fmtBuf, fmt, (format_ - fmt) * sizeof(Char));
} fmtBuf[format_ - fmt] = 0;
#ifdef _MSC_VER
char* RenderWithoutSign(long n, char* bufLast, unsigned int base, const int stored = _snprintf(resultBuf,
bool uppercase) { #else
if (n != LONG_MIN) { const int stored = snprintf(resultBuf,
return RenderWithoutSign(static_cast<unsigned long>(n < 0 ? -n : n), #endif
bufLast, base, uppercase); sizeof(resultBuf) / sizeof(Char), fmtBuf, n);
} if (stored < 0) {
// annoying corner case result_ = -1;
char* save = bufLast; return;
++n; }
bufLast = RenderWithoutSign(static_cast<unsigned long>(n), Write(resultBuf, resultBuf + strlen(resultBuf));
bufLast, base, uppercase); Advance(); // output stuff to the next format directive
--(*save); }
return bufLast;
}
void Next() {
++format_;
Advance();
}
void Advance() {
ResetAll();
const Char* begin = format_;
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() {
for (;; ++format_) {
switch (*format_) {
case '-': SetLeftJustify(); break;
case '+': SetShowSignAlways(); break;
case ' ': SetBlank(); break;
case '#': SetAlternateForm(); break;
case '0': SetFillZeros(); break;
default: return;
}
}
}
void ParseDecimalSizeT(size_t& dest) {
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 ReadWidth() {
ParseDecimalSizeT(width_);
}
void ReadPrecision() {
assert(*format_ == '.');
++format_;
ParseDecimalSizeT(prec_);
}
void ReadModifiers() {
switch (*format_) {
case 'h': SetForceShort(); ++format_; break;
case 'l': ++format_; break;
// more (C99 and platform-specific modifiers) to come
}
}
void ReadLeaders() {
ReadFlags();
ReadWidth();
if (*format_ == '.') ReadPrecision();
ReadModifiers();
}
enum {
leftJustify = 1,
showSignAlways = 2,
blank = 4,
alternateForm = 8,
fillZeros = 16,
forceShort = 32
};
bool LeftJustify() const { return (flags_ & leftJustify) != 0; }
bool ShowSignAlways() const { return (flags_ & showSignAlways) != 0; }
void SetWidth(size_t w) { width_ = w; }
void SetLeftJustify() { flags_ |= leftJustify; }
void SetShowSignAlways() { flags_ |= showSignAlways; }
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 SetPrec(size_t p) { prec_ = p; } void Fill(const Char c, size_t n) {
void SetBlank() { flags_ |= blank; } for (; n > 0; --n) {
void SetAlternateForm() { flags_ |= alternateForm; } Write(&c, &c + 1);
void SetFillZeros() { flags_ |= fillZeros; } }
void ResetFillZeros() { flags_ &= ~fillZeros; } }
void SetForceShort() { flags_ |= forceShort; }
Char* RenderWithoutSign(unsigned long n, char* bufLast,
void ResetAll() { unsigned int base, bool uppercase) {
assert(result_ != EOF); const Char hex1st = uppercase ? 'A' : 'a';
width_ = 0; for (;;) {
prec_ = size_t(-1); const unsigned long next = n / base;
flags_ = 0; 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,
bool uppercase) {
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 Next() {
++format_;
Advance();
}
void Advance() {
ResetAll();
const Char* begin = format_;
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() {
for (;; ++format_) {
switch (*format_) {
case '-': SetLeftJustify(); break;
case '+': SetShowSignAlways(); break;
case ' ': SetBlank(); break;
case '#': SetAlternateForm(); break;
case '0': SetFillZeros(); break;
default: return;
}
}
}
void ParseDecimalSizeT(size_t& dest) {
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 ReadWidth() {
ParseDecimalSizeT(width_);
}
void ReadPrecision() {
assert(*format_ == '.');
++format_;
ParseDecimalSizeT(prec_);
}
void ReadModifiers() {
switch (*format_) {
case 'h': SetForceShort(); ++format_; break;
case 'l': ++format_; break;
// more (C99 and platform-specific modifiers) to come
}
}
void ReadLeaders() {
ReadFlags();
ReadWidth();
if (*format_ == '.') ReadPrecision();
ReadModifiers();
}
enum {
leftJustify = 1,
showSignAlways = 2,
blank = 4,
alternateForm = 8,
fillZeros = 16,
forceShort = 32
};
bool LeftJustify() const { return (flags_ & leftJustify) != 0; }
bool ShowSignAlways() const { return (flags_ & showSignAlways) != 0; }
void SetWidth(size_t w) { width_ = w; }
void SetLeftJustify() { flags_ |= leftJustify; }
void SetShowSignAlways() { flags_ |= showSignAlways; }
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; }
// state void SetPrec(size_t p) { prec_ = p; }
Device device_; void SetBlank() { flags_ |= blank; }
const Char* format_; void SetAlternateForm() { flags_ |= alternateForm; }
size_t width_; void SetFillZeros() { flags_ |= fillZeros; }
size_t prec_; void ResetFillZeros() { flags_ &= ~fillZeros; }
unsigned int flags_; void SetForceShort() { flags_ |= forceShort; }
int result_;
}; void ResetAll() {
assert(result_ != EOF);
width_ = 0;
prec_ = size_t(-1);
flags_ = 0;
}
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_