From 32b4c0a8fa8c8a7f61ada00f0dbe76d25a04d8bd Mon Sep 17 00:00:00 2001 From: syntheticpp Date: Sun, 8 Jan 2006 14:38:29 +0000 Subject: [PATCH] fix printing pointers git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@449 7ec92016-0320-0410-acc4-a06ded1c099a --- include/loki/SafeFormat.h | 23 +++++++++++++++-------- test/SafeFormat/main.cpp | 4 +--- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/include/loki/SafeFormat.h b/include/loki/SafeFormat.h index ec6b005..b0ffd95 100755 --- a/include/loki/SafeFormat.h +++ b/include/loki/SafeFormat.h @@ -1,5 +1,6 @@ //////////////////////////////////////////////////////////////////////////////// // Copyright (c) 2005 by Andrei Alexandrescu +// Copyright (c) 2006 Peter Kümmel // Permission to use, copy, modify, distribute, and sell this software for any // purpose is hereby granted without fee, provided that the above copyright // notice appear in all copies and that both that copyright notice and this @@ -138,16 +139,22 @@ namespace Loki FormatWithCurrentFlags(i); return *this; } + + PrintfState& operator()(void* n) { + if (result_ == -1) return *this; // don't even bother + PrintUsing_printf(n,"p"); + return *this; + } PrintfState& operator()(double n) { if (result_ == -1) return *this; // don't even bother - PrintFloatingPoint(n); + PrintUsing_printf(n,"eEfgG"); return *this; } PrintfState& operator()(long double n) { if (result_ == -1) return *this; // don't even bother - PrintFloatingPoint(n); + PrintUsing_printf(n,"eEfgG"); return *this; } @@ -320,14 +327,14 @@ namespace Loki result_ += x; } - template - void PrintFloatingPoint(Double n) { + template + void PrintUsing_printf(Value n, const char* check_fmt_char) { const Char *const fmt = format_ - 1; assert(*fmt == '%'); // enforce format string validity ReadLeaders(); // enforce format spec - if (!strchr("eEfgG", *format_)) { + if (!strchr(check_fmt_char, *format_)) { result_ = -1; return; } @@ -340,11 +347,11 @@ namespace Loki } memcpy(fmtBuf, fmt, (format_ - fmt) * sizeof(Char)); fmtBuf[format_ - fmt] = 0; - #ifdef _MSC_VER +#ifdef _MSC_VER const int stored = _snprintf_s(resultBuf, - #else +#else const int stored = snprintf(resultBuf, - #endif +#endif sizeof(resultBuf) / sizeof(Char), fmtBuf, n); if (stored < 0) { result_ = -1; diff --git a/test/SafeFormat/main.cpp b/test/SafeFormat/main.cpp index 6508923..e7f3d91 100755 --- a/test/SafeFormat/main.cpp +++ b/test/SafeFormat/main.cpp @@ -176,9 +176,7 @@ int main(int argc, char** argv) } // Generate a random type character - /* disable %p tests - static const string type("cdeEfgGinopsuxX");*/ - static const string type("cdeEfgGinosuxX"); + static const string type("cdeEfgGinopsuxX"); const char typeSpec = type[RandomInt(0u, type.size() - 1)]; // Generate an optional type prefix