2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/Pl_Buffer.hh>
|
2009-09-26 18:36:04 +00:00
|
|
|
#include <stdexcept>
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <assert.h>
|
2008-05-04 16:02:53 +00:00
|
|
|
#include <string.h>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
Pl_Buffer::Pl_Buffer(char const* identifier, Pipeline* next) :
|
|
|
|
Pipeline(identifier, next),
|
|
|
|
ready(false),
|
|
|
|
total_size(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Pl_Buffer::~Pl_Buffer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-06-20 15:20:57 +00:00
|
|
|
Pl_Buffer::write(unsigned char* buf, size_t len)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
Buffer* b = new Buffer(len);
|
|
|
|
memcpy(b->getBuffer(), buf, len);
|
|
|
|
this->data.push_back(b);
|
|
|
|
this->ready = false;
|
|
|
|
this->total_size += len;
|
|
|
|
|
|
|
|
if (getNext(true))
|
|
|
|
{
|
|
|
|
getNext()->write(buf, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pl_Buffer::finish()
|
|
|
|
{
|
|
|
|
this->ready = true;
|
|
|
|
if (getNext(true))
|
|
|
|
{
|
|
|
|
getNext()->finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Buffer*
|
|
|
|
Pl_Buffer::getBuffer()
|
|
|
|
{
|
|
|
|
if (! this->ready)
|
|
|
|
{
|
2009-09-26 18:36:04 +00:00
|
|
|
throw std::logic_error("Pl_Buffer::getBuffer() called when not ready");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Buffer* b = new Buffer(this->total_size);
|
|
|
|
unsigned char* p = b->getBuffer();
|
|
|
|
while (! this->data.empty())
|
|
|
|
{
|
2010-09-24 20:45:18 +00:00
|
|
|
PointerHolder<Buffer> bp = this->data.front();
|
2008-04-29 12:55:25 +00:00
|
|
|
this->data.pop_front();
|
|
|
|
size_t bytes = bp->getSize();
|
|
|
|
memcpy(p, bp->getBuffer(), bytes);
|
|
|
|
p += bytes;
|
|
|
|
this->total_size -= bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(this->total_size == 0);
|
|
|
|
this->ready = false;
|
|
|
|
|
|
|
|
return b;
|
|
|
|
}
|