nbitstream.h
来自「奇迹世界公用文件源代码,研究网络游戏的朋友可以研究下」· C头文件 代码 · 共 715 行 · 第 1/2 页
H
715 行
*/
inline
void
nBitStream::ReadCustom(void* ptr, int size)
{
ASSERT(readable);
ASSERT(ptr != 0);
ASSERT(size > 0);
ASSERT(BitsLeft() >= size * 8);
// advance to next byte in stream
// if current bit offset is greater 0
if (currentBit > 0)
{
currentByte++;
currentBit = 0;
}
memcpy(ptr, &stream[currentByte], size);
currentByte += size;
}
//------------------------------------------------------------------------------
/**
*/
inline
void
nBitStream::ReadBitStream(nBitStream& stream)
{
ASSERT(stream.streamSize <= (BitsLeft() * 8));
ReadCustom(stream.stream, stream.streamSize);
}
//------------------------------------------------------------------------------
/**
*/
inline
void
nBitStream::WriteBitStream(nBitStream& stream)
{
ASSERT(stream.streamSize <= (BitsLeft() * 8));
WriteCustom(stream.stream, stream.streamSize);
}
//------------------------------------------------------------------------------
/**
*/
inline
void
nBitStream::Set(const uchar* s, int size)
{
// require
ASSERT(s != 0);
ASSERT(size > 0);
// delete old stream, if exits
if (stream != 0) delete[] stream;
// create and copy stream
stream = new unsigned char[size];
ASSERT(stream != 0);
streamSize = size;
memcpy(stream, s, size);
}
//------------------------------------------------------------------------------
/**
*/
inline
const uchar*
nBitStream::Get() const
{
return stream;
}
//------------------------------------------------------------------------------
/**
*/
inline
void
nBitStream::SetSize(int size)
{
ASSERT(size > 0);
if (stream != 0)
{
delete stream;
}
stream = new unsigned char[size];
ASSERT(stream != 0);
streamSize = size;
Clear();
ASSERT(streamSize == size);
}
//------------------------------------------------------------------------------
/**
*/
inline
int
nBitStream::GetSize() const
{
return streamSize;
}
//------------------------------------------------------------------------------
/**
*/
inline
void
nBitStream::SetPos(int pos)
{
ASSERT(!writeable); // Can't change bit pos while writing!!!
ASSERT((0 <= pos) && (pos <= streamSize * 8));
currentByte = pos / 8;
currentBit = pos % 8;
}
//------------------------------------------------------------------------------
/**
*/
inline
int
nBitStream::GetPos() const
{
return (currentByte * 8) + currentBit;
}
//------------------------------------------------------------------------------
/**
*/
inline
int
nBitStream::BitsLeft() const
{
return (GetSize() * 8) - GetPos();
}
//------------------------------------------------------------------------------
/**
*/
inline
void
nBitStream::Copy(const nBitStream& source)
{
ASSERT(source.streamSize > 0);
ASSERT(source.streamSize == streamSize);
ASSERT(stream != 0);
ASSERT(source.stream != 0);
memcpy(stream, source.stream, source.streamSize);
currentBit = streamSize * 8;
}
//------------------------------------------------------------------------------
/**
*/
inline
void
nBitStream::BeginWrite()
{
ASSERT(!writeable);
ASSERT(!readable);
Clear();
SetPos(0);
writeable = true;
}
//------------------------------------------------------------------------------
/**
*/
inline
void
nBitStream::EndWrite()
{
ASSERT(writeable);
ASSERT(!readable);
writeable = false;
}
//------------------------------------------------------------------------------
/**
*/
inline
void
nBitStream::BeginRead()
{
ASSERT(!readable);
ASSERT(!writeable);
SetPos(0);
readable = true;
}
//------------------------------------------------------------------------------
/**
*/
inline
void
nBitStream::EndRead()
{
ASSERT(readable);
ASSERT(!writeable);
readable = false;
}
//------------------------------------------------------------------------------
/**
*/
inline
bool
nBitStream::IsWriteable() const
{
return writeable;
}
//------------------------------------------------------------------------------
/**
*/
inline
bool
nBitStream::IsReadable() const
{
return readable;
}
//------------------------------------------------------------------------------
/**
Print debug data to stdout.
*/
inline
void
nBitStream::Print(int lines)
{
// require
ASSERT(stream != 0);
// print out the first bits
for (int i = 0; i < lines; i++)
{
n_printf("%4.d: ", i + 1);
char ch = stream[i];
for (int j = 0; j < 8; j++)
{
if ( ch & ( 1 << ( 7 - j ) ) )
{
n_printf( "%d", 1 );
}
else
{
n_printf( "%d", 0 );
}
}
n_printf( "\n" );
}
}
//------------------------------------------------------------------------------
/**
Assignment operator.
*/
inline
nBitStream&
nBitStream::operator = (const nBitStream& s)
{
ASSERT(s.streamSize > 0);
ASSERT(s.streamSize == streamSize);
ASSERT(stream != 0);
ASSERT(s.stream != 0);
Copy(s);
return *this;
}
//------------------------------------------------------------------------------
/**
Equality operator.
*/
inline
bool
nBitStream::operator == (const nBitStream& s) const
{
if (streamSize != s.streamSize) return false;
return (memcmp(stream, s.stream, streamSize) == 0);
}
//------------------------------------------------------------------------------
/**
Inequality operator.
*/
inline
bool
nBitStream::operator != (const nBitStream& s) const
{
if (streamSize != s.streamSize) return true;
return (memcmp(stream, s.stream, streamSize) != 0);
}
//------------------------------------------------------------------------------
/**
*/
inline
void
nBitStream::WriteBit(bool bit)
{
ASSERT(stream != 0);
ASSERT(BitsLeft() >= 1);
ASSERT(IsWriteable());
// Set bit at current bit in current byte;
// assume that all upcomming bits are cleared
if (bit)
{
int v = (bit) ? 1 : 0;
stream[currentByte] |= (v << (7 - currentBit));
}
// Advance to next byte if current bit is 8!
if (++currentBit == 8)
{
currentByte++;
currentBit = 0;
}
}
//------------------------------------------------------------------------------
/**
*/
inline
bool
nBitStream::ReadBit()
{
ASSERT(stream != 0);
ASSERT(BitsLeft() >= 1);
ASSERT(IsReadable());
// Read current bit
bool flag = ((stream[currentByte] & (1 << (7 - currentBit))) != 0);
// Advance to next byte if current bit is 8!
if (++currentBit == 8)
{
currentByte++;
currentBit = 0;
}
return flag;
}
#endif
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?