Introduce a custom atof function that always assumes data comes in form 10.10 with

the full stop used as a decimal place.
This commit is contained in:
Robert Osfield
2008-07-18 11:39:06 +00:00
parent 46796978fd
commit 18e9d2d53b

View File

@@ -11,11 +11,111 @@
* OpenSceneGraph Public License for more details.
*/
#include <string.h>
#include <math.h>
#include <osg/Notify>
#include <osgDB/Field>
using namespace osgDB;
using namespace std;
double osg_atof(const char* str)
{
const char* ptr = str;
// check if could be a hex number.
if (strncmp(ptr,"0x",2)==0)
{
double value = 0.0;
// skip over leading 0x, and then go through rest of string
// checking to make sure all values are 0...9 or a..f.
ptr+=2;
while (
*ptr!=0 &&
((*ptr>='0' && *ptr<='9') ||
(*ptr>='a' && *ptr<='f') ||
(*ptr>='A' && *ptr<='F'))
)
{
if (*ptr>='0' && *ptr<='9') value = value*16.0 + double(*ptr-'0');
else if (*ptr>='a' && *ptr<='f') value = value*16.0 + double(*ptr-'a'+10);
else if (*ptr>='A' && *ptr<='F') value = value*16.0 + double(*ptr-'A'+10);
++ptr;
}
// osg::notify(osg::NOTICE)<<"Read "<<str<<" result = "<<value<<std::endl;
return value;
}
ptr = str;
bool hadDecimal[2];
double value[2];
double sign[2];
double decimalMultiplier[2];
hadDecimal[0] = hadDecimal[1] = false;
sign[0] = sign[1] = 1.0;
value[0] = value[1] = 0.0;
decimalMultiplier[0] = decimalMultiplier[1] = 0.1;
int pos = 0;
// compute mantissa and exponent parts
while (*ptr!=0 && pos<2)
{
if (*ptr=='+')
{
sign[pos] = 1.0;
}
else if (*ptr=='-')
{
sign[pos] = -1.0;
}
else if (*ptr>='0' && *ptr<='9')
{
if (!hadDecimal[pos])
{
value[pos] = value[pos]*10.0 + double(*ptr-'0');
}
else
{
value[pos] = value[pos] + decimalMultiplier[pos] * double(*ptr-'0');
decimalMultiplier[pos] *= 0.1;
}
}
else if (*ptr=='.')
{
hadDecimal[pos] = true;
}
else if (*ptr=='e' || *ptr=='E')
{
if (pos==1) break;
pos = 1;
}
else
{
break;
}
++ptr;
}
if (pos==0)
{
// osg::notify(osg::NOTICE)<<"Read "<<str<<" result = "<<value[0]*sign[0]<<std::endl;
return value[0]*sign[0];
}
else
{
double mantissa = value[0]*sign[0];
double exponent = value[1]*sign[1];
//osg::notify(osg::NOTICE)<<"Read "<<str<<" mantissa = "<<mantissa<<" exponent="<<exponent<<" result = "<<mantissa*pow(10.0,exponent)<<std::endl;
return mantissa*pow(10.0,exponent);
}
}
Field::Field()
{
_init();
@@ -338,7 +438,7 @@ bool Field::matchFloat(float f) const
getFieldType();
if (_fieldType==REAL || _fieldType==INTEGER)
{
return (float)atof(_fieldCache)==f;
return (float)osg_atof(_fieldCache)==f;
}
else
{
@@ -352,7 +452,7 @@ bool Field::getFloat(float& f) const
getFieldType();
if (_fieldType==REAL || _fieldType==INTEGER)
{
f = (float)atof(_fieldCache);
f = (float)osg_atof(_fieldCache);
return true;
}
else
@@ -366,7 +466,7 @@ bool Field::getFloat(double& f) const
getFieldType();
if (_fieldType==REAL || _fieldType==INTEGER)
{
f = atof(_fieldCache);
f = osg_atof(_fieldCache);
return true;
}
else