Ejemplo n.º 1
0
/**
 * Formats the given length in the given format.
 *
 * @param length The length in the current unit of the drawing.
 * @param format Format of the string.
 * @param prec Precision of the value (e.g. 0.001 or 1/128 = 0.0078125)
 & @param showUnit Append unit to the value.
 */
QString RS_Units::formatLinear(double length, RS2::Unit unit,
                                 RS2::LinearFormat format,
                                 int prec, bool showUnit) {
    QString ret;

    // unit appended to value (e.g. 'mm'):
    /*QString unitString = "";
    if (showUnit) {
        unitString = unitToSign(unit);
}*/

    // barbarian display: show as fraction:
    switch (format) {
    case RS2::Scientific:
        ret = formatScientific(length, unit, prec, showUnit);
        break;

    case RS2::Decimal:
        ret = formatDecimal(length, unit, prec, showUnit);
        break;

    case RS2::Engineering:
        ret = formatEngineering(length, unit, prec, showUnit);
        break;

    case RS2::Architectural:
        ret = formatArchitectural(length, unit, prec, showUnit);
        break;

    case RS2::Fractional:
        ret = formatFractional(length, unit, prec, showUnit);
        break;

    case RS2::ArchitecturalMetric:
        ret = formatArchitecturalMetric(length, unit, prec, showUnit);
        break;

    default:
        RS_DEBUG->print(RS_Debug::D_WARNING,
                        "RS_Units::formatLinear: Unknown format");
        ret = "";
        break;
    }

    return ret;
}
Ejemplo n.º 2
0
/**
 * Formats the given length in the given format.
 *
 * \param length The length in the current unit of the drawing.
 * \param format Format of the string.
 * \param prec Precision of the value (e.g. 0.001 or 1/128 = 0.0078125)
 & \param showUnit Append unit to the value.
 */
QString RUnit::formatLinear(double length, RS::Unit unit,
                            RS::LinearFormat format,
                            int prec, bool showUnit,
                            bool showLeadingZeroes, 
                            bool showTrailingZeroes,
                            bool onlyPreciseResult) {
    QString ret;

    // imperial display: show as fraction:
    switch (format) {
    case RS::Scientific:
        ret = formatScientific(length, unit, prec, showUnit, 
            showLeadingZeroes, showTrailingZeroes, onlyPreciseResult);
        break;

    case RS::Decimal:
        ret = formatDecimal(length, unit, prec, showUnit,
            showLeadingZeroes, showTrailingZeroes, onlyPreciseResult);
        break;

    case RS::Engineering:
        ret = formatEngineering(length, unit, prec, showUnit,
            showLeadingZeroes, showTrailingZeroes, onlyPreciseResult);
        break;

    case RS::Architectural:
    case RS::ArchitecturalStacked:
        ret = formatArchitectural(length, unit, prec, showUnit,
            showLeadingZeroes, showTrailingZeroes, onlyPreciseResult);
        break;

    case RS::Fractional:
    case RS::FractionalStacked:
        ret = formatFractional(length, unit, prec, showUnit,
            showLeadingZeroes, showTrailingZeroes, onlyPreciseResult);
        break;

    default:
        qWarning() << "RUnit::formatLinear: Unknown format";
        ret = "";
        break;
    }
    
    return ret;
}
Ejemplo n.º 3
0
void Format::formatInt( int64 number, String &target, bool bUseGroup )
{

   if ( m_numFormat == e_scientific )
   {
      formatScientific( (numeric) number, target );
      return;
   }

   if ( number == 0 )
   {
      target += "0";
      return;
   }

   // prepare the buffer
   const int bufSize = 132; // int64 binary size + separator per each bit + minus format + zero
   uint32 buffer[ bufSize ];

   uint32 pos = bufSize - 2;
   buffer[ pos+1 ] = 0; // allow room for post formatters


   if ( number < 0 )
   {
      number = - number;
   }

   // init grouping
   int grp = m_grouping;

   // init base
   int base = 10;
   uint32 baseHexChr = (uint32) 'X';
   switch( m_numFormat )
   {
      case e_decimal: base = 10; break;
      case e_binary: case e_binaryB: base = 2; break;

      case e_octalZero:
         target.append( '0' );
         // fallthrough
      case e_octal:
         base = 8;
      break;

      case e_cHexLower:
         target.append( '0' );
         target.append( 'x' );
         // fallthrough
      case e_hexLower:
         base = 16;
         baseHexChr = 'a';
      break;

      case e_cHexUpper:
         target.append( '0' );
         target.append( 'x' );
         // fallthrough
      case e_hexUpper:
         base = 16;
         baseHexChr = 'A';
      break;

      default:
         break;
   }

   while( number != 0 )
   {
      uint32 cipher =(uint32) (number % base);
      if ( cipher < 10 )
      {
         buffer[pos--] = (char) ( cipher + 0x30 );
      }
      else
      {
         buffer[pos--] = (char) ( (cipher-10) + baseHexChr );
      }
      number /= base;

      if( number != 0 && bUseGroup && m_grouping != 0 )
      {
         if( --grp == 0 ) {
            buffer[pos--] = (char) m_thousandSep;
            grp = m_grouping;
         }
      }
   }

   pos++;

   // unroll the parsed buffer
   while( buffer[pos] != 0 )
   {
      target += buffer[pos];
      ++pos;
   }

   if( m_numFormat == e_decimal )
   {
      if( m_decimals != 0 )
      {
         target.append( m_decimalSep );
         for( int d = 0; d < m_decimals; d ++ )
            target.append( '0' );
      }
   }
   else if ( m_numFormat == e_binaryB )
   {
      target.append( 'b' );
   }
}
Ejemplo n.º 4
0
bool Format::format( VMachine *vm, const Item &source, String &target )
{
   String sBuffer;

   switch( source.type() )
   {
      case FLC_ITEM_NIL:
         switch( m_nilFormat )
         {
            case e_nilEmpty: break;
            case e_nilNil: sBuffer = "Nil"; break;
            case e_nilN: sBuffer = "N"; break;
            case e_nilnil: sBuffer = "nil"; break;
            case e_nilNA: sBuffer = "N/A"; break;
            case e_nilNone: sBuffer = "None"; break;
            case e_nilNULL: sBuffer = "NULL"; break;
            case e_nilNull: sBuffer = "Null"; break;
            case e_nilPad: sBuffer.append( m_paddingChr );
         }
         applyPad( sBuffer );

      break;

      case FLC_ITEM_UNB:
         sBuffer = "_";
         applyPad( sBuffer );
         break;


      //==================================================
      // Parse an integer
      //
      case FLC_ITEM_INT:
      {
         int64 num = source.asInteger();

         // number formats are compatible with string formats
         if ( m_convType != e_tNum && m_convType != e_tStr )
         {
            return processMismatch( vm, source, target );
         }

         formatInt( num, sBuffer, true );

         // minus sign must be added AFTER padding with parentesis/fixed size or with *End signs,
         // else it must be added before.
         if ( negBeforePad() )
         {
            applyNeg( sBuffer, num );
            applyPad( sBuffer );
         }
         else {
            applyPad( sBuffer, negPadSize( num ) );
            applyNeg( sBuffer, num );
         }
      }
      break;

      //==================================================
      // Parse double format
      //
      case FLC_ITEM_NUM:
      {
         numeric num = source.asNumeric();

         // number formats are compatible with string formats
         if ( m_convType != e_tNum && m_convType != e_tStr )
         {
            return processMismatch( vm, source, target );
         }

         if( m_numFormat == e_scientific )
         {
           formatScientific( num, sBuffer );
         }
         else {
            double intPart, fractPart;
            bool bNeg, bIntIsZero;
            fractPart = modf( num, &intPart );
            if ( intPart < 0.0 ) {
               intPart = -intPart;
               fractPart = -fractPart;
               bNeg = true;
               bIntIsZero = false;
            }
            else
            {
               bIntIsZero = intPart > 0.0 ? false : true;

               if ( fractPart < 0.0 )
               {
                  fractPart = -fractPart;
                  // draw neg sign only if < 0 but int
                  bNeg = true;
               }
               else
                  bNeg = false;
            }



            String precPart;
            int base = 10;
            switch( m_numFormat )
            {
               case e_binary: case e_binaryB: base = 2; break;
               case e_octalZero: case e_octal: base = 8; break;
               case e_cHexUpper: case e_hexUpper: case e_cHexLower: case e_hexLower: base = 16; break;
               default:
                  break;
            }

            while( intPart > 9e14 )
            {
               intPart /= base;
               precPart.append( '0' );
            }

            // manual round
            if( (pow((double)10.0,-(m_decimals+1)) *5)+fractPart >=1.0)
            {
               intPart++;
               bIntIsZero = false;
            }

            uint8 decs = m_decimals;
            m_decimals = 0;


            formatInt( (int64) intPart, sBuffer, false );
            sBuffer.append( precPart );

            // now we can add the grouping
            if ( m_grouping > 0 )
            {
               String token;
               token.append( m_thousandSep );
               uint32 pos = sBuffer.size();

               while( pos > m_grouping )
               {
                  pos -= m_grouping;
                  sBuffer.insert( pos, 0, token );
               }
            }

            // finally add decimals
            m_decimals = decs;
            if( base == 10 && m_decimals > 0 )
            {

               char bufFmt[32];
               char buffer[255];
               sprintf( bufFmt, "%%.%df", m_decimals );
               sprintf( buffer, bufFmt, fractPart );
               sBuffer.append( m_decimalSep );
               sBuffer.append( buffer + 2 );
            }
            else if ( bIntIsZero )
            {
               // do not print -0!
               bNeg = false;
            }

            // we must fix the number.
            num = bNeg ? -1.0 : 1.0;
         }

         // minus sign must be added AFTER padding with parentesis/fixed size or with *End signs,
         // else it must be added before.
         if ( negBeforePad() )
         {
            applyNeg( sBuffer, (int64) num );
            applyPad( sBuffer );
         }
         else {
            applyPad( sBuffer, negPadSize( (int64) num ) );
            applyNeg( sBuffer, (int64) num );
         }
      }
      break;

      case FLC_ITEM_RANGE:
      {
         // number formats are compatible with string formats
         if ( m_convType != e_tNum && m_convType != e_tStr )
         {
            return processMismatch( vm, source, target );
         }

         int64 begin = source.asRangeStart();
         String sBuf1, sBuf2, sBuf3;

         formatInt( begin, sBuf1, true );

         //apply negative format now.
         applyNeg( sBuf1, (int64) begin );
         if ( ! source.asRangeIsOpen() )
         {
            int64 end = source.asRangeEnd();
            formatInt( end, sBuf2, true );
            applyNeg( sBuf2, (int64) end );
            
            int64 step = source.asRangeStep();
            if ( (begin <= end && step != 1) ||
                 (begin > end && step != -1 ) )
            {
               formatInt( step, sBuf3, true );
               applyNeg( sBuf3, (int64) step );
               sBuffer = "[" + sBuf1 + ":" + sBuf2 + ":" + sBuf3 + "]";
            }
            else
               sBuffer = "[" + sBuf1 + ":" + sBuf2 + "]";
         }
         else
            sBuffer = "[" + sBuf1 + ":" + sBuf2 + "]";

         applyPad( sBuffer );
      }
      break;

      case FLC_ITEM_STRING:
      {
         // number formats are compatible with string formats
         if ( m_convType != e_tStr )
         {
            return processMismatch( vm, source, target );
         }

         sBuffer = *source.asString();
         applyPad( sBuffer );
      }
      break;


      case FLC_ITEM_OBJECT:
      {
         // try to format the object
         if( vm != 0 )
         {
            if( m_posOfObjectFmt != String::npos )
            {
               vm->itemToString( sBuffer, &source, m_originalFormat.subString( m_posOfObjectFmt + 1 ) );
            }
            else {
               vm->itemToString( sBuffer, &source );
            }
         }
         else {
            return processMismatch( vm, source, target );
         }

         applyPad( sBuffer );
      }
      break;

      default:
         return processMismatch( vm, source, target );
   }

   // out of bounds?
   if ( m_size > 0 && m_fixedSize && sBuffer.length() > m_size ) {
      return false;
   }

   target += sBuffer;
   return true;
}