More COutputStream

This commit is contained in:
Phillip Stephens 2022-10-11 11:38:08 -07:00
parent a02037e266
commit f27c87a5b5
2 changed files with 42 additions and 16 deletions

View File

@ -3,6 +3,7 @@
#include "types.h" #include "types.h"
class COutputStream; class COutputStream;
template < typename T > template < typename T >
@ -16,6 +17,7 @@ class COutputStream {
public: public:
COutputStream(int len); COutputStream(int len);
virtual ~COutputStream(); virtual ~COutputStream();
virtual void Write(const void* ptr, u32 len);
void WriteBits(int val, int bitCount); void WriteBits(int val, int bitCount);
void FlushShiftRegister(); void FlushShiftRegister();
@ -46,6 +48,7 @@ public:
++mPosition; ++mPosition;
} }
private: private:
uint mPosition; uint mPosition;
uint mBufLen; uint mBufLen;

View File

@ -22,29 +22,52 @@ void COutputStream::DoPut(const void* ptr, size_t len) {
if (len == 0) { if (len == 0) {
return; return;
} }
uint curLen = len;
mNumWrites += len; mNumWrites += len;
uint curLen = len;
if (mBufLen <= len + mPosition) { if (mBufLen <= len + mPosition) {
memcpy((uchar*)mBufPtr + mPosition, ptr, len); memcpy((uchar*)mBufPtr + mPosition, ptr, len);
mPosition += len; mPosition += len;
} else { return;
while (curLen != 0) { }
uint count = mBufLen - mPosition;
if (curLen < count) { while (curLen != 0) {
count = curLen; uint count = mBufLen - mPosition;
} uint offset = curLen;
if (count == 0) { if (curLen < count) {
DoFlush(); count = curLen;
} else { }
memcpy((uchar*)mBufPtr + mPosition, (uchar*)ptr + (len - curLen), count); if (count != 0) {
mPosition += count; memcpy((uchar*)mBufPtr + mPosition, (uchar*)ptr + offset, count);
curLen -= count; mPosition += count;
} curLen -= count;
} else {
DoFlush();
} }
} }
} }
void COutputStream::Flush() {} void COutputStream::Flush() {
FlushShiftRegister();
DoFlush();
}
void COutputStream::DoFlush() {} void COutputStream::DoFlush() {
if (mPosition != 0) {
Write(mBufPtr, mPosition);
mPosition = 0;
}
}
static inline u32 min_containing_bytes(u32 v) {
v = 32 - v;
return (v / 8) + ((v % 8) != 0);
}
void COutputStream::FlushShiftRegister() {
if (mShiftRegisterOffset < 32) {
DoPut(&mShiftRegister, min_containing_bytes(mShiftRegisterOffset));
mShiftRegister = 0;
mShiftRegisterOffset = 32;
}
}