From ab627a91662522dde8630b1333c1d22cbfaeada4 Mon Sep 17 00:00:00 2001 From: Jeff Hill Date: Thu, 12 Aug 2004 16:50:26 +0000 Subject: [PATCH] added string to double conversion functions --- src/gdd/aitConvert.cc | 54 +++++++++++++++++++++++++++++++++++++++++++ src/gdd/aitConvert.h | 7 ++++++ 2 files changed, 61 insertions(+) diff --git a/src/gdd/aitConvert.cc b/src/gdd/aitConvert.cc index ea6f6fd12..b627343d7 100644 --- a/src/gdd/aitConvert.cc +++ b/src/gdd/aitConvert.cc @@ -17,6 +17,8 @@ #define AIT_CONVERT_SOURCE 1 #include #include +#include +#include "epicsStdio.h" #define epicsExportSharedSymbols #include "aitConvert.h" @@ -38,6 +40,58 @@ int aitNoConvert(void* /*dest*/,const void* /*src*/,aitIndex /*count*/, const gd /* put the fixed conversion functions here (ones not generated) */ +bool getStringAsDouble ( const char * pString, + const gddEnumStringTable * pEST, double & result ) +{ + if ( ! pString ) { + return false; + } + double ftmp; + unsigned itmp; + if ( pEST && pEST->getIndex ( pString, itmp ) ) { + ftmp = itmp; + } + else { + int j = sscanf ( pString,"%lf", &ftmp ); + if ( j != 1 ) { + j = sscanf ( pString,"%x", &itmp ); + if ( j == 1 ) { + ftmp = itmp; + } + else { + return false; + } + } + } + result = ftmp; + return true; +} + +bool putDoubleToString ( + const double in, const gddEnumStringTable * pEST, + char * pString, size_t strSize ) +{ + if ( strSize == 0u ) { + return false; + } + if ( pEST && in >= 0 && in <= UINT_MAX ) { + unsigned utmp = static_cast < unsigned > ( in ); + pEST->getString ( utmp, pString, strSize ); + if ( pString[0] != '\0' ) { + return true; + } + } + int nChar = epicsSnprintf ( + pString, strSize-1, "%g", in ); + if ( nChar <= 0 ) { + return false; + } + size_t nCharU = static_cast < size_t > ( nChar ); + nChar = min ( nCharU, strSize-1 ) + 1; + memset ( &pString[nChar], '\0', strSize - nChar ); + return true; +} + /* ------- extra string conversion functions --------- */ static int aitConvertStringString(void* d,const void* s, aitIndex c, const gddEnumStringTable *) diff --git a/src/gdd/aitConvert.h b/src/gdd/aitConvert.h index 7deb392d7..6f90f508b 100644 --- a/src/gdd/aitConvert.h +++ b/src/gdd/aitConvert.h @@ -157,6 +157,13 @@ inline void aitFromNetOrder64(aitUint64* dest, aitUint64* src) #define aitFromNetFloat64 aitFromNetOrder64 #define aitFromNetFloat32 aitFromNetOrder32 +bool getStringAsDouble ( const char * pString, + const gddEnumStringTable *pEST, double & result ); + +bool putDoubleToString ( + const double in, const gddEnumStringTable * pEST, + char * pString, size_t strSize ); + #if defined ( _MSC_VER ) && _MSC_VER < 1200 # pragma warning ( pop ) #endif