beginpos )
{
displayByteBits( *(msg.buffer()+endpos), 8, 0, false, log );
}
}
/*
* Returns the stream as a string with 0 and 1.
*/
void CBitMemStream::displayStream( const char *title, CLog *log )
{
// nlassert( (_BufPos >= _Buffer.getPtr()) && (_BufPos <= _Buffer.getPtr() + _Buffer.size()) );
nlassert( _Buffer.Pos <= _Buffer.getBuffer().size() );
// Display title and information
string s = (isReading()?string("I"):string("O")) + string("BMS ") + string(title) + ": ";
string sLegend;
// if ( _BufPos == _Buffer.getPtr() )
if ( _Buffer.Pos == 0 )
{
log->displayNL( (s + "Empty").c_str() );
return;
}
// s += NLMISC::toString( "BitPos=%d Pos=%u FreeBits=%u Size=%u ", getPosInBit(), (uint32)(_BufPos-_Buffer.getPtr()), _FreeBits, _Buffer.size() );
s += NLMISC::toString( "BitPos=%d Pos=%u FreeBits=%u Size=%u ", getPosInBit(), _Buffer.Pos, _FreeBits, _Buffer.getBuffer().size() );
log->displayNL( s.c_str() );
s.clear();
// Display bitstream (output: until _BufPos/_FreeBits; input: whole buffer)
_DbgInfo.beginEventBrowsing();
sint32 eventId;
uint32 bitpos = 0;
const uint8 *p;
// uint8 *endPos = isReading() ? (_Buffer.getPtr() + _Buffer.size()) : (_BufPos+1);
const uint8 *endPos = isReading() ? (_Buffer.getBuffer().getPtr() + _Buffer.getBuffer().size()) : (_Buffer.getBuffer().getPtr() + _Buffer.Pos+1);
// for ( p=_Buffer.getPtr(); p!=endPos; ++p )
for ( p=_Buffer.getBuffer().getPtr(); p!=endPos; ++p )
{
sint i;
for ( i=7; i!=-1; --i )
{
//bitpos = (p-_Buffer.getPtr())*8 + (7-i);
if ( bitpos == (uint32)getPosInBit() )
s += ""; // display the current position
s += _DbgInfo.getEventIdAtBitPos( bitpos, &eventId );
s += ( ((*p) >> i) & 1 ) ? '1' : '0';
sLegend += _DbgInfo.getEventLegendAtBitPos( *this, eventId );
++bitpos;
}
s += ' '; // a blank char between each byte
if ( bitpos % 64 == 0 ) // limit to 8 bytes per line
{
log->displayRawNL( s.c_str() );
s.clear();
}
}
if ( bitpos % 64 != 0 )
log->displayRawNL( s.c_str() );
_DbgInfo.endEventBrowsing();
// Display legend
string::size_type lineStart = 0;
string::size_type crp = sLegend.find( '\n', lineStart );
while ( crp != string::npos )
{
log->displayRawNL( sLegend.substr( lineStart, crp-lineStart ).c_str() );
lineStart = crp + 1;
crp = sLegend.find( '\n', lineStart );
}
// sLegend ends with a '\n'
}
/*
* Return a string showing the serial item
*/
std::string CBitMemStream::getSerialItem( const TBMSSerialInfo& serialItem )
{
// Save the current pointers of the stream, and make them point to the required position
uint savedFreeBits = _FreeBits;
uint bytepos = serialItem.BitPos >> 3;
_FreeBits = 8 - (serialItem.BitPos - (bytepos << 3));
// uint8 *savedBufPos = _BufPos;
uint32 savedBufPos = _Buffer.Pos;
// _BufPos = _Buffer.getPtr() + bytepos;
_Buffer.Pos = bytepos;
bool wasOutput = false;;
if ( ! isReading() )
{
setInOut( true ); // lighter than invert()
wasOutput = true;
}
// Read and format string
string s;
if ( getPosInBit() + serialItem.BitSize > lengthR() * 8 )
{
s = "";
}
else
{
switch ( serialItem.Type )
{
case TBMSSerialInfo::B:
{
bool b;
serialBit( b );
s = NLMISC::toString( "%s", b?"TRUE":"FALSE" );
break;
}
case TBMSSerialInfo::U: // no distinction with signed int!
{
uint32 u;
serial( u, serialItem.BitSize );
s = NLMISC::toString( "%u", u );
break;
}
case TBMSSerialInfo::U64: // no distinction with signed int64!
{
uint64 u;
serial( u, serialItem.BitSize );
s = NLMISC::toString( "%"NL_I64"u", u );
break;
}
case TBMSSerialInfo::F: // what about double?
{
float f;
serial( f );
s = NLMISC::toString( "%g", f );
break;
}
case TBMSSerialInfo::BF:
{
CBitSet bs;
bs.resize( serialItem.BitSize );
readBits( bs );
s = bs.toString();
break;
}
case TBMSSerialInfo::Buffer:
{
uint32 len = serialItem.BitSize / 8;
s.resize( len + 2 );
if ( len != 0 )
{
serialBuffer( &((uint8&)(s[1])), len );
string::size_type p;
for ( p=1; p!=len+1; ++p )
{
if ( ! isalnum(s[p]) )
s[p] = '?'; // remove end-of-c_string
}
}
s[0] = '[';
s[len+1] = ']';
break;
}
default:
break;
}
}
// Restore the current pointers
if ( wasOutput )
{
setInOut( false );
}
_FreeBits = savedFreeBits;
// _BufPos = savedBufPos;
_Buffer.Pos = savedBufPos;
return s;
}
} // NLMISC