mirror of https://github.com/PrimeDecomp/prime.git
parent
7ec4707661
commit
85173e2bd8
|
@ -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;
|
||||||
|
|
|
@ -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) {
|
while (curLen != 0) {
|
||||||
uint count = mBufLen - mPosition;
|
uint count = mBufLen - mPosition;
|
||||||
|
uint offset = curLen;
|
||||||
if (curLen < count) {
|
if (curLen < count) {
|
||||||
count = curLen;
|
count = curLen;
|
||||||
}
|
}
|
||||||
if (count == 0) {
|
if (count != 0) {
|
||||||
DoFlush();
|
memcpy((uchar*)mBufPtr + mPosition, (uchar*)ptr + offset, count);
|
||||||
} else {
|
|
||||||
memcpy((uchar*)mBufPtr + mPosition, (uchar*)ptr + (len - curLen), count);
|
|
||||||
mPosition += count;
|
mPosition += count;
|
||||||
curLen -= 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue