2001-10-06 14:13:28 +00:00
|
|
|
#include "CStreamBuffer.h"
|
|
|
|
|
|
|
|
//
|
|
|
|
// CStreamBuffer
|
|
|
|
//
|
|
|
|
|
|
|
|
const UInt32 CStreamBuffer::kChunkSize = 4096;
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
CStreamBuffer::CStreamBuffer() :
|
2002-06-14 18:08:20 +00:00
|
|
|
m_size(0),
|
|
|
|
m_headUsed(0)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
CStreamBuffer::~CStreamBuffer()
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
const void*
|
2002-06-17 13:31:21 +00:00
|
|
|
CStreamBuffer::peek(UInt32 n)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
assert(n <= m_size);
|
|
|
|
|
|
|
|
// reserve space in first chunk
|
|
|
|
ChunkList::iterator head = m_chunks.begin();
|
2002-06-14 18:08:20 +00:00
|
|
|
head->reserve(n + m_headUsed);
|
2001-10-06 14:13:28 +00:00
|
|
|
|
|
|
|
// consolidate chunks into the first chunk until it has n bytes
|
|
|
|
ChunkList::iterator scan = head;
|
|
|
|
++scan;
|
2002-06-14 18:08:20 +00:00
|
|
|
while (head->size() - m_headUsed < n && scan != m_chunks.end()) {
|
2001-10-06 14:13:28 +00:00
|
|
|
head->insert(head->end(), scan->begin(), scan->end());
|
|
|
|
scan = m_chunks.erase(scan);
|
|
|
|
}
|
|
|
|
|
2002-06-14 18:08:20 +00:00
|
|
|
return reinterpret_cast<const void*>(head->begin() + m_headUsed);
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2002-06-17 13:31:21 +00:00
|
|
|
CStreamBuffer::pop(UInt32 n)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
2002-05-02 11:33:34 +00:00
|
|
|
// discard all chunks if n is greater than or equal to m_size
|
|
|
|
if (n >= m_size) {
|
2002-06-14 18:08:20 +00:00
|
|
|
m_size = 0;
|
|
|
|
m_headUsed = 0;
|
2002-05-02 11:33:34 +00:00
|
|
|
m_chunks.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update size
|
2001-10-06 14:13:28 +00:00
|
|
|
m_size -= n;
|
|
|
|
|
|
|
|
// discard chunks until more than n bytes would've been discarded
|
|
|
|
ChunkList::iterator scan = m_chunks.begin();
|
2002-05-02 11:33:34 +00:00
|
|
|
assert(scan != m_chunks.end());
|
2002-06-14 18:08:20 +00:00
|
|
|
while (scan->size() - m_headUsed <= n) {
|
|
|
|
n -= scan->size() - m_headUsed;
|
|
|
|
m_headUsed = 0;
|
|
|
|
scan = m_chunks.erase(scan);
|
2002-05-02 11:33:34 +00:00
|
|
|
assert(scan != m_chunks.end());
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
2002-05-02 11:33:34 +00:00
|
|
|
// remove left over bytes from the head chunk
|
2002-05-02 11:43:52 +00:00
|
|
|
if (n > 0) {
|
2002-06-14 18:08:20 +00:00
|
|
|
m_headUsed += n;
|
2002-05-02 11:43:52 +00:00
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
void
|
2002-06-17 13:31:21 +00:00
|
|
|
CStreamBuffer::write(const void* vdata, UInt32 n)
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
assert(vdata != NULL);
|
|
|
|
|
2002-05-02 11:33:34 +00:00
|
|
|
// ignore if no data, otherwise update size
|
2001-10-06 14:13:28 +00:00
|
|
|
if (n == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_size += n;
|
|
|
|
|
|
|
|
// cast data to bytes
|
|
|
|
const UInt8* data = reinterpret_cast<const UInt8*>(vdata);
|
|
|
|
|
|
|
|
// point to last chunk if it has space, otherwise append an empty chunk
|
|
|
|
ChunkList::iterator scan = m_chunks.end();
|
|
|
|
if (scan != m_chunks.begin()) {
|
|
|
|
--scan;
|
2002-06-10 22:06:45 +00:00
|
|
|
if (scan->size() >= kChunkSize) {
|
2001-10-06 14:13:28 +00:00
|
|
|
++scan;
|
2002-06-10 22:06:45 +00:00
|
|
|
}
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
if (scan == m_chunks.end()) {
|
2002-05-02 11:33:34 +00:00
|
|
|
scan = m_chunks.insert(scan, Chunk());
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// append data in chunks
|
|
|
|
while (n > 0) {
|
|
|
|
// choose number of bytes for next chunk
|
2002-05-02 11:33:34 +00:00
|
|
|
assert(scan->size() <= kChunkSize);
|
2001-10-06 14:13:28 +00:00
|
|
|
UInt32 count = kChunkSize - scan->size();
|
|
|
|
if (count > n)
|
|
|
|
count = n;
|
|
|
|
|
|
|
|
// transfer data
|
|
|
|
scan->insert(scan->end(), data, data + count);
|
|
|
|
n -= count;
|
|
|
|
data += count;
|
|
|
|
|
|
|
|
// append another empty chunk if we're not done yet
|
|
|
|
if (n > 0) {
|
2002-05-02 11:33:34 +00:00
|
|
|
++scan;
|
|
|
|
scan = m_chunks.insert(scan, Chunk());
|
2001-10-06 14:13:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-10 22:06:45 +00:00
|
|
|
UInt32
|
|
|
|
CStreamBuffer::getSize() const
|
2001-10-06 14:13:28 +00:00
|
|
|
{
|
|
|
|
return m_size;
|
|
|
|
}
|