Static Public Member Functions | Static Private Member Functions
FIX::DoubleConvertor Struct Reference

Converts double to/from a string. More...

#include <FieldConvertors.h>

List of all members.

Static Public Member Functions

static std::string convert (double value, int padding=0)
static bool convert (const std::string &value, double &result)
static double convert (const std::string &value) throw ( FieldConvertError )

Static Private Member Functions

static double fast_atof (const char *p)

Detailed Description

Converts double to/from a string.

Definition at line 304 of file FieldConvertors.h.


Member Function Documentation

static std::string FIX::DoubleConvertor::convert ( double  value,
int  padding = 0 
) [inline, static]

Definition at line 401 of file FieldConvertors.h.

References STRING_SPRINTF.

Referenced by FIX::DataDictionary::checkValidFormat(), convert(), FIX::Dictionary::getDouble(), FIX::DoubleField::getValue(), FIX::Dictionary::setDouble(), and FIX::DoubleField::setValue().

  {
    char result[32];
    char *end = 0;

    int size;
    if( value == 0 || value > 0.0001 || value <= -0.0001 )
    {
      size = STRING_SPRINTF( result, "%.15g", value );

      if( padding > 0 )
      {
        char* point = result;
        end = result + size - 1;
        while( *point != '.' && *point != 0 )
          point++;

        if( *point == 0 )
        {
          end = point;
          *point = '.';
          size++;
        }
        int needed = padding - (int)(end - point);

        while( needed-- > 0 )
        {
          *(++end) = '0';
          size++;
        }
        *(end+1) = 0;
      }
    }
    else
    {
      size = STRING_SPRINTF( result, "%.15f", value );
      // strip trailing 0's
      end = result + size - 1;

      if( padding > 0 )
      {
        int discard = 15 - padding;

        while( (*end == '0') && (discard-- > 0) )
        {
          *(end--) = 0;
          size--;
        }
     }
     else
     {
       while( *end == '0' )
       {
         *(end--) = 0;
         size--;
       }
     }
   }

   return std::string( result, size );
}
static bool FIX::DoubleConvertor::convert ( const std::string &  value,
double &  result 
) [inline, static]

Definition at line 463 of file FieldConvertors.h.

References fast_atof(), and IS_DIGIT.

{
  const char * i = value.c_str();

  // Catch null strings
  if( !*i ) return false;
  // Eat leading '-' and recheck for null string
  if( *i == '-' && !*++i ) return false;

  bool haveDigit = false;

  if( IS_DIGIT(*i) )
  {
    haveDigit = true;
    while( IS_DIGIT (*++i) );
  }

  if( *i == '.' && IS_DIGIT(*++i) )
  {
    haveDigit = true;
    while( IS_DIGIT (*++i) );
  }

  if( *i || !haveDigit ) return false;
    
  result = fast_atof( value.c_str() );
  return true;
  }
static double FIX::DoubleConvertor::convert ( const std::string &  value) throw ( FieldConvertError ) [inline, static]

Definition at line 492 of file FieldConvertors.h.

References convert().

  {
    double result = 0.0;
    if( !convert( value, result ) )
      throw FieldConvertError(value);
    else
      return result;
  }
static double FIX::DoubleConvertor::fast_atof ( const char *  p) [inline, static, private]

Definition at line 316 of file FieldConvertors.h.

References IS_DIGIT, and IS_SPACE.

Referenced by convert().

  {
    bool frac(false);
    double sign(1.), value(0.), scale(1.);

    while (IS_SPACE(*p))
      ++p;

    // Get sign, if any.
    if (*p == '-')
    {
      sign = -1.;
      ++p;
    }
    else if (*p == '+')
      ++p;

    // Get digits before decimal point or exponent, if any.
    while (IS_DIGIT(*p))
    {
      value = value * 10. + (*p - '0');
      ++p;
    }

    // Get digits after decimal point, if any.
    if (*p == '.')
    {
      ++p;
      double pow10(10.);
      while (IS_DIGIT(*p))
      {
        value += (*p - '0') / pow10;
        pow10 *= 10.;
        ++p;
      }
    }

    // Handle exponent, if any.
    if (toupper(*p) == 'E')
    {
      unsigned int expon(0);
      ++p;

      // Get sign of exponent, if any.
      if (*p == '-')
      {
        frac = true;
        ++p;
      }
      else if (*p == '+')
        ++p;

      // Get digits of exponent, if any.
      while (IS_DIGIT(*p))
      {
        expon = expon * 10 + (*p - '0');
        ++p;
      }
      if (expon > 308)
        expon = 308;

      // Calculate scaling factor.
      while (expon >= 50)
      {
        scale *= 1E50;
        expon -= 50;
      }
      while (expon >= 8)
      {
        scale *= 1E8;
        expon -=  8;
      }
      while (expon > 0)
      {
        scale *= 10.0;
        expon -=  1;
      }
    }

    // Return signed and scaled floating point result.
    return sign * (frac ? (value / scale) : (value * scale));
  }

The documentation for this struct was generated from the following file:

Generated on Mon Jun 23 2014 23:49:39 for QuickFIX by doxygen 1.7.6.1 written by Dimitri van Heesch, © 1997-2001