2008-05-05 12:36:31 +00:00
|
|
|
// Copyright (C) 2002-2007 Nikolaus Gebhardt
|
|
|
|
// This file is part of the "Irrlicht Engine" and the "irrXML" project.
|
|
|
|
// For conditions of distribution and use, see copyright notice in irrlicht.h and irrXML.h
|
2008-08-13 23:46:46 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------
|
|
|
|
// Original description: (Schrompf)
|
2008-05-05 12:36:31 +00:00
|
|
|
// Adapted to the ASSIMP library because the builtin atof indeed takes AGES to parse a
|
|
|
|
// float inside a large string. Before parsing, it does a strlen on the given point.
|
2008-08-13 23:46:46 +00:00
|
|
|
// Changes:
|
|
|
|
// 22nd October 08 (Aramis_acg): Added temporary cast to double, added strtol10_64
|
|
|
|
// to ensure long numbers are handled correctly
|
|
|
|
// ------------------------------------------------------------------------------------
|
|
|
|
|
2008-05-05 12:36:31 +00:00
|
|
|
|
|
|
|
#ifndef __FAST_A_TO_F_H_INCLUDED__
|
|
|
|
#define __FAST_A_TO_F_H_INCLUDED__
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
namespace Assimp
|
|
|
|
{
|
|
|
|
|
|
|
|
const float fast_atof_table[16] = { // we write [16] here instead of [] to work around a swig bug
|
|
|
|
0.f,
|
|
|
|
0.1f,
|
|
|
|
0.01f,
|
|
|
|
0.001f,
|
|
|
|
0.0001f,
|
|
|
|
0.00001f,
|
|
|
|
0.000001f,
|
|
|
|
0.0000001f,
|
|
|
|
0.00000001f,
|
|
|
|
0.000000001f,
|
|
|
|
0.0000000001f,
|
|
|
|
0.00000000001f,
|
|
|
|
0.000000000001f,
|
|
|
|
0.0000000000001f,
|
|
|
|
0.00000000000001f,
|
|
|
|
0.000000000000001f
|
|
|
|
};
|
|
|
|
|
2008-08-13 23:46:46 +00:00
|
|
|
|
2008-10-22 20:06:16 +00:00
|
|
|
// ------------------------------------------------------------------------------------
|
|
|
|
// convert a string in decimal format to a number
|
2008-08-13 23:46:46 +00:00
|
|
|
// ------------------------------------------------------------------------------------
|
2008-05-05 12:36:31 +00:00
|
|
|
inline unsigned int strtol10( const char* in, const char** out=0)
|
|
|
|
{
|
|
|
|
unsigned int value = 0;
|
|
|
|
|
|
|
|
while ( 1 )
|
|
|
|
{
|
|
|
|
if ( *in < '0' || *in > '9' )
|
|
|
|
break;
|
|
|
|
|
|
|
|
value = ( value * 10 ) + ( *in - '0' );
|
|
|
|
++in;
|
|
|
|
}
|
2008-10-13 16:45:48 +00:00
|
|
|
if (out)*out = in;
|
2008-05-05 12:36:31 +00:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2008-10-22 20:06:16 +00:00
|
|
|
// ------------------------------------------------------------------------------------
|
|
|
|
// convert a string in octal format to a number
|
2008-10-13 16:45:48 +00:00
|
|
|
// ------------------------------------------------------------------------------------
|
|
|
|
inline unsigned int strtol8( const char* in, const char** out=0)
|
|
|
|
{
|
|
|
|
unsigned int value = 0;
|
|
|
|
|
|
|
|
while ( 1 )
|
|
|
|
{
|
|
|
|
if ( *in < '0' || *in > '7' )
|
|
|
|
break;
|
|
|
|
|
|
|
|
value = ( value << 3 ) + ( *in - '0' );
|
|
|
|
++in;
|
|
|
|
}
|
|
|
|
if (out)*out = in;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------
|
|
|
|
// convert a string in hex format to a number
|
2008-10-22 20:06:16 +00:00
|
|
|
// ------------------------------------------------------------------------------------
|
2008-10-13 16:45:48 +00:00
|
|
|
inline unsigned int strtol16( const char* in, const char** out=0)
|
|
|
|
{
|
|
|
|
unsigned int value = 0;
|
|
|
|
|
|
|
|
while ( 1 )
|
|
|
|
{
|
|
|
|
if ( *in >= '0' && *in <= '9' )
|
|
|
|
{
|
|
|
|
value = ( value << 4u ) + ( *in - '0' );
|
|
|
|
}
|
|
|
|
else if (*in >= 'A' && *in <= 'F')
|
|
|
|
{
|
|
|
|
value = ( value << 4u ) + ( *in - 'A' ) + 10;
|
|
|
|
}
|
|
|
|
else if (*in >= 'a' && *in <= 'f')
|
|
|
|
{
|
|
|
|
value = ( value << 4u ) + ( *in - 'a' ) + 10;
|
|
|
|
}
|
|
|
|
else break;
|
|
|
|
++in;
|
|
|
|
}
|
|
|
|
if (out)*out = in;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2008-10-22 20:06:16 +00:00
|
|
|
// ------------------------------------------------------------------------------------
|
2008-10-13 16:45:48 +00:00
|
|
|
// convert just one hex digit
|
2008-10-22 20:06:16 +00:00
|
|
|
// ------------------------------------------------------------------------------------
|
2008-10-13 16:45:48 +00:00
|
|
|
inline unsigned int HexDigitToDecimal(char in)
|
|
|
|
{
|
|
|
|
unsigned int out = 0xffffffff;
|
|
|
|
if (in >= '0' && in <= '9')
|
|
|
|
out = in - '0';
|
|
|
|
|
|
|
|
else if (in >= 'a' && in <= 'f')
|
|
|
|
out = 10u + in - 'a';
|
|
|
|
|
|
|
|
else if (in >= 'A' && in <= 'F')
|
|
|
|
out = 10u + in - 'A';
|
|
|
|
|
|
|
|
// return value is 0xffffffff if the input is not a hex digit
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2008-09-05 13:21:01 +00:00
|
|
|
// ------------------------------------------------------------------------------------
|
|
|
|
// signed variant of strtol10
|
2008-10-22 20:06:16 +00:00
|
|
|
// ------------------------------------------------------------------------------------
|
2008-09-05 13:21:01 +00:00
|
|
|
inline int strtol10s( const char* in, const char** out=0)
|
|
|
|
{
|
|
|
|
bool bNeg = false;
|
|
|
|
if ('-' == *in){++in;bNeg = true;}
|
|
|
|
if ('+' == *in)++in;
|
|
|
|
int value = strtol10(in,out);
|
|
|
|
if (bNeg)value = -value;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2008-10-13 16:45:48 +00:00
|
|
|
// ------------------------------------------------------------------------------------
|
2008-10-22 20:06:16 +00:00
|
|
|
// Parse a C++-like integer literal - hex and oct prefixes.
|
2008-10-13 16:45:48 +00:00
|
|
|
// 0xNNNN - hex
|
|
|
|
// 0NNN - oct
|
|
|
|
// NNN - dec
|
2008-10-22 20:06:16 +00:00
|
|
|
// ------------------------------------------------------------------------------------
|
2008-10-13 16:45:48 +00:00
|
|
|
inline unsigned int strtol_cppstyle( const char* in, const char** out=0)
|
|
|
|
{
|
|
|
|
if ('0' == in[0])
|
|
|
|
{
|
2008-10-19 11:32:33 +00:00
|
|
|
return 'x' == in[1] ? strtol16(in+2,out) : strtol8(in+1,out);
|
2008-10-13 16:45:48 +00:00
|
|
|
}
|
|
|
|
return strtol10(in, out);
|
|
|
|
}
|
2008-08-06 23:01:38 +00:00
|
|
|
|
2008-08-13 23:46:46 +00:00
|
|
|
// ------------------------------------------------------------------------------------
|
2008-10-22 20:06:16 +00:00
|
|
|
// Special version of the function, providing higher accuracy
|
|
|
|
// It is mainly used bx fast_atof to prevent ugly integer overflows.
|
|
|
|
// ------------------------------------------------------------------------------------
|
2008-08-06 23:01:38 +00:00
|
|
|
inline uint64_t strtol10_64( const char* in, const char** out=0)
|
|
|
|
{
|
|
|
|
uint64_t value = 0;
|
|
|
|
|
|
|
|
while ( 1 )
|
|
|
|
{
|
|
|
|
if ( *in < '0' || *in > '9' )
|
|
|
|
break;
|
|
|
|
|
|
|
|
value = ( value * 10 ) + ( *in - '0' );
|
|
|
|
++in;
|
|
|
|
}
|
|
|
|
if (out)
|
|
|
|
*out = in;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2008-08-13 23:46:46 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------
|
2008-05-05 12:36:31 +00:00
|
|
|
//! Provides a fast function for converting a string into a float,
|
|
|
|
//! about 6 times faster than atof in win32.
|
|
|
|
// If you find any bugs, please send them to me, niko (at) irrlicht3d.org.
|
2008-10-22 20:06:16 +00:00
|
|
|
// ------------------------------------------------------------------------------------
|
2008-05-05 12:36:31 +00:00
|
|
|
inline const char* fast_atof_move( const char* c, float& out)
|
|
|
|
{
|
|
|
|
bool inv = false;
|
|
|
|
const char *t;
|
|
|
|
float f;
|
|
|
|
|
|
|
|
if (*c=='-')
|
|
|
|
{
|
|
|
|
++c;
|
|
|
|
inv = true;
|
|
|
|
}
|
2008-09-27 16:46:05 +00:00
|
|
|
else if (*c=='+')++c;
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2008-08-06 23:01:38 +00:00
|
|
|
f = (float) strtol10_64 ( c, &c );
|
2008-05-05 12:36:31 +00:00
|
|
|
if (*c == '.')
|
|
|
|
{
|
|
|
|
++c;
|
|
|
|
|
2008-08-06 23:01:38 +00:00
|
|
|
// NOTE: The original implementation is highly unaccurate here
|
|
|
|
// The precision of a single IEEE 754 float is not high enough
|
|
|
|
// everything behind the 6th digit tends to be more inaccurate
|
|
|
|
// than it would need to be.
|
|
|
|
// Casting to double seems to solve the problem.
|
|
|
|
// strtol_64 is used to prevent integer overflow.
|
|
|
|
|
2008-05-05 12:36:31 +00:00
|
|
|
//float pl = (float)strtol(c, &t, 10);
|
2008-08-06 23:01:38 +00:00
|
|
|
double pl = (double) strtol10_64 ( c, &t );
|
2008-05-05 12:36:31 +00:00
|
|
|
pl *= fast_atof_table[t-c];
|
|
|
|
|
2008-08-06 23:01:38 +00:00
|
|
|
f += (float)pl;
|
2008-05-05 12:36:31 +00:00
|
|
|
|
|
|
|
c = t;
|
|
|
|
|
2008-09-27 16:46:05 +00:00
|
|
|
// FIX: a large 'E' should be allowed, too
|
|
|
|
if (*c == 'e' || *c == 'E')
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
|
|
|
++c;
|
|
|
|
bool einv = (*c=='-');
|
|
|
|
if (einv)
|
|
|
|
++c;
|
|
|
|
|
|
|
|
float exp = (float)strtol10(c, &c);
|
|
|
|
if (einv)
|
|
|
|
exp *= -1.0f;
|
|
|
|
|
|
|
|
f *= pow(10.0f, exp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inv)
|
|
|
|
f *= -1.0f;
|
|
|
|
|
|
|
|
out = f;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2008-08-13 23:46:46 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------
|
2008-05-05 12:36:31 +00:00
|
|
|
inline float fast_atof(const char* c)
|
|
|
|
{
|
|
|
|
float ret;
|
|
|
|
fast_atof_move(c, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end of namespace Assimp
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|