added string to double conversion functions

This commit is contained in:
Jeff Hill
2004-08-12 16:50:26 +00:00
parent 87023ea4b2
commit ab627a9166
2 changed files with 61 additions and 0 deletions

View File

@@ -17,6 +17,8 @@
#define AIT_CONVERT_SOURCE 1
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#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 *)

View File

@@ -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