static inline long parseInt( const wxString& aValue, double aScalar )
{
    double value = LONG_MAX;

    /*
     * In 2011 gEDA/pcb introduced values with units, like "10mm" or "200mil".
     * Unit-less values are still centimils (100000 units per inch), like with
     * the previous format.
     *
     * Distinction between the even older format (mils, 1000 units per inch)
     * and the pre-2011 format is done in ::parseMODULE already; the
     * distinction is by wether an object definition opens with '(' or '['.
     * All values with explicite unit open with a '[' so there's no need to
     * consider this distinction when parsing them.
     *
     * The solution here is to watch for a unit and, if present, convert the
     * value to centimils. All unit-less values are read unaltered. This way
     * the code below can contine to consider all read values to be in mils or
     * centimils. It also matches the strategy gEDA/pcb uses for backwards
     * compatibility with its own layouts.
     *
     * Fortunately gEDA/pcb allows only units 'mil' and 'mm' in files, see
     * definition of ALLOW_READABLE in gEDA/pcb's pcb_printf.h. So we don't
     * have to test for all 11 units gEDA/pcb allows in user dialogs.
     */
    if( aValue.EndsWith( wxT( "mm" ) ) )
    {
        aScalar *= 100000.0 / 25.4;
    }
    else if( aValue.EndsWith( wxT( "mil" ) ) )
    {
        aScalar *= 100.;
    }

    // This conversion reports failure on strings as simple as "1000", still
    // it returns the right result in &value. Thus, ignore the return value.
    aValue.ToCDouble(&value);
    if( value == LONG_MAX ) // conversion really failed
    {
        THROW_IO_ERROR( wxString::Format( _( "Cannot convert \"%s\" to an integer" ),
                                          aValue.GetData() ) );
        return 0;
    }

    return KiROUND( value * aScalar );
}
Esempio n. 2
0
bool CRegTable::TryParseGPR(wxString str, FormatSpecifier format, u32* value)
{
  if (format == FormatSpecifier::Hex8)
  {
    unsigned long val;
    if (str.ToCULong(&val, 16))
    {
      *value = static_cast<u32>(val);
      return true;
    }
    return false;
  }
  if (format == FormatSpecifier::Int)
  {
    long signed_val;
    if (str.ToCLong(&signed_val))
    {
      *value = static_cast<u32>(signed_val);
      return true;
    }
    return false;
  }
  if (format == FormatSpecifier::UInt)
  {
    unsigned long val;
    if (str.ToCULong(&val))
    {
      *value = static_cast<u32>(val);
      return true;
    }
    return false;
  }
  if (format == FormatSpecifier::Float)
  {
    double double_val;
    if (str.ToCDouble(&double_val))
    {
      float float_val = static_cast<float>(double_val);
      std::memcpy(value, &float_val, sizeof(float));
      return true;
    }
    return false;
  }
  return false;
}
Esempio n. 3
0
bool CRegTable::TryParseFPR(wxString str, FormatSpecifier format, unsigned long long* value)
{
  if (format == FormatSpecifier::Hex16)
  {
    return str.ToULongLong(value, 16);
  }
  if (format == FormatSpecifier::Double)
  {
    double double_val;
    if (str.ToCDouble(&double_val))
    {
      std::memcpy(value, &double_val, sizeof(u64));
      return true;
    }
    return false;
  }
  return false;
}