From 1ba6da438b163ab2ad424044227f7b0fc798bd55 Mon Sep 17 00:00:00 2001 From: Jeff Hill Date: Thu, 23 Jul 2009 00:23:17 +0000 Subject: [PATCH] partial fix for mantis 352 --- src/gdd/aitConvert.cc | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/gdd/aitConvert.cc b/src/gdd/aitConvert.cc index 16e729519..b42bfa5f5 100644 --- a/src/gdd/aitConvert.cc +++ b/src/gdd/aitConvert.cc @@ -21,6 +21,8 @@ #include #include #include "epicsStdio.h" +#include "cvtFast.h" + #define epicsExportSharedSymbols #include "aitConvert.h" @@ -69,7 +71,7 @@ bool putDoubleToString ( const double in, const gddEnumStringTable * pEST, char * pString, size_t strSize ) { - if ( strSize == 0u ) { + if ( strSize <= 1u ) { return false; } if ( pEST && in >= 0 && in <= UINT_MAX ) { @@ -79,11 +81,33 @@ bool putDoubleToString ( return true; } } +#if 1 + bool cvtDoubleToStringInRange = + ( in < 1.e4 && in > 1.e-4 ) || + ( in > -1.e4 && in < -1.e-4 ) || + in == 0.0; + // conservative + static const unsigned cvtDoubleToStringSizeMax = 15; + int nChar; + if ( cvtDoubleToStringInRange && + strSize > cvtDoubleToStringSizeMax ) { + nChar = cvtDoubleToString ( in, pString, 4 ); + } + else { + nChar = epicsSnprintf ( + pString, strSize-1, "%g", in ); + } + if ( nChar < 1 ) { + return false; + } + assert ( nChar < strSize ); +#else int nChar = epicsSnprintf ( pString, strSize-1, "%g", in ); if ( nChar <= 0 ) { return false; } +#endif size_t nCharU = static_cast < size_t > ( nChar ); nChar = epicsMin ( nCharU, strSize-1 ) + 1; memset ( &pString[nChar], '\0', strSize - nChar );