mirror of
https://github.com/qpdf/qpdf.git
synced 2025-01-31 02:48:31 +00:00
Refactor Pl_Buffer
Base implementation of the buffer on std::basic_string<unsigned char>.
This commit is contained in:
parent
ae800361fe
commit
44a7aff56f
@ -35,7 +35,6 @@
|
||||
|
||||
#include <qpdf/Buffer.hh>
|
||||
#include <qpdf/Pipeline.hh>
|
||||
#include <qpdf/PointerHolder.hh>
|
||||
|
||||
#include <memory>
|
||||
|
||||
@ -80,12 +79,11 @@ class QPDF_DLL_CLASS Pl_Buffer: public Pipeline
|
||||
~Members() = default;
|
||||
|
||||
private:
|
||||
Members();
|
||||
Members() = default;
|
||||
Members(Members const&) = delete;
|
||||
|
||||
bool ready;
|
||||
std::shared_ptr<Buffer> data;
|
||||
size_t total_size;
|
||||
bool ready{true};
|
||||
std::basic_string<unsigned char> data;
|
||||
};
|
||||
|
||||
std::shared_ptr<Members> m;
|
||||
|
@ -5,12 +5,6 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
Pl_Buffer::Members::Members() :
|
||||
ready(true),
|
||||
total_size(0)
|
||||
{
|
||||
}
|
||||
|
||||
Pl_Buffer::Pl_Buffer(char const* identifier, Pipeline* next) :
|
||||
Pipeline(identifier, next),
|
||||
m(new Members())
|
||||
@ -26,21 +20,7 @@ Pl_Buffer::~Pl_Buffer()
|
||||
void
|
||||
Pl_Buffer::write(unsigned char const* buf, size_t len)
|
||||
{
|
||||
if (this->m->data == nullptr) {
|
||||
this->m->data = std::make_shared<Buffer>(len);
|
||||
}
|
||||
size_t cur_size = this->m->data->getSize();
|
||||
size_t left = cur_size - this->m->total_size;
|
||||
if (left < len) {
|
||||
size_t new_size = std::max(this->m->total_size + len, 2 * cur_size);
|
||||
auto b = std::make_shared<Buffer>(new_size);
|
||||
memcpy(b->getBuffer(), this->m->data->getBuffer(), this->m->total_size);
|
||||
this->m->data = b;
|
||||
}
|
||||
if (len) {
|
||||
memcpy(this->m->data->getBuffer() + this->m->total_size, buf, len);
|
||||
this->m->total_size += len;
|
||||
}
|
||||
this->m->data.append(buf, len);
|
||||
this->m->ready = false;
|
||||
|
||||
if (getNext(true)) {
|
||||
@ -64,12 +44,13 @@ Pl_Buffer::getBuffer()
|
||||
throw std::logic_error("Pl_Buffer::getBuffer() called when not ready");
|
||||
}
|
||||
|
||||
Buffer* b = new Buffer(this->m->total_size);
|
||||
if (this->m->total_size > 0) {
|
||||
auto size = this->m->data.length();
|
||||
Buffer* b = new Buffer(size);
|
||||
if (size > 0) {
|
||||
unsigned char* p = b->getBuffer();
|
||||
memcpy(p, this->m->data->getBuffer(), this->m->total_size);
|
||||
memcpy(p, this->m->data.data(), size);
|
||||
}
|
||||
this->m = std::shared_ptr<Members>(new Members());
|
||||
this->m->data.clear();
|
||||
return b;
|
||||
}
|
||||
|
||||
@ -86,13 +67,13 @@ Pl_Buffer::getMallocBuffer(unsigned char** buf, size_t* len)
|
||||
throw std::logic_error(
|
||||
"Pl_Buffer::getMallocBuffer() called when not ready");
|
||||
}
|
||||
|
||||
*len = this->m->total_size;
|
||||
if (this->m->total_size > 0) {
|
||||
*buf = reinterpret_cast<unsigned char*>(malloc(this->m->total_size));
|
||||
memcpy(*buf, this->m->data->getBuffer(), this->m->total_size);
|
||||
auto size = this->m->data.length();
|
||||
*len = size;
|
||||
if (size > 0) {
|
||||
*buf = reinterpret_cast<unsigned char*>(malloc(size));
|
||||
memcpy(*buf, this->m->data.data(), size);
|
||||
} else {
|
||||
*buf = nullptr;
|
||||
}
|
||||
this->m = std::shared_ptr<Members>(new Members());
|
||||
this->m->data.clear();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user