Logo Search packages:      
Sourcecode: kmess version File versions  Download package

quint64 DataSource::getBits ( quint8  amount,
bool *  ok = 0 
)

Retrieve the next [amount] bits from the data.

Parameters:
amount Number of bits to read
ok If set, it will contain whether the call was successful or not.
Returns:
quint64 with the set of read bits

Definition at line 370 of file datasource.cpp.

References atEnd(), and getBit().

{
  if( amount > 64 )
  {
    qWarning() << "DataSource:getBits() - Cannot retrieve" << amount << "bits, the maximum is 64 bits!";

    if( ok != 0 )
    {
      *ok = false;
    }

    return 0LL;
  }

  bool gotBitOk = false;
  quint8 bitIndex = 0;
  quint64 value = 0;

  while( ! atEnd( true ) && bitIndex < amount )
  {
    bool bit = getBit( &gotBitOk );

    if( ! gotBitOk )
    {
      if( ok != 0 )
      {
        *ok = false;
      }

      return 0LL;
    }

    if( bit )
    {
#if Q_BYTE_ORDER == Q_BIG_ENDIAN
      value |= 1 << bitIndex;
#else
      value |= 1 << ( amount - bitIndex - 1 );
#endif
    }

    ++bitIndex;
  }

  // If more bits must be read, but are not available, abort
#ifdef ISFQT_DEBUG_VERBOSE
  if( ( bitIndex < amount ) && atEnd( true ) )
  {
    qDebug() << "Unable to read all bits: only" << bitIndex << "of" << amount << "were available.";
  }
#endif

  // Done!
  if( ok != 0 )
  {
    *ok = true;
  }

  return value;
}


Generated by  Doxygen 1.6.0   Back to index