2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-05-29 00:10:54 +00:00
qpdf/libqpdf/Buffer.cc
Jay Berkenbilt 9f444ffef3 add QPDF::processMemoryFile and API additions to support it
git-svn-id: svn+q:///qpdf/trunk@1034 71b93d88-0707-0410-a8cf-f5a4172ac649
2010-10-01 10:20:38 +00:00

95 lines
1.2 KiB
C++

#include <qpdf/Buffer.hh>
#include <string.h>
Buffer::Buffer()
{
init(0, 0, true);
}
Buffer::Buffer(unsigned long size)
{
init(size, 0, true);
}
Buffer::Buffer(unsigned char* buf, unsigned long size)
{
init(size, buf, false);
}
Buffer::Buffer(Buffer const& rhs)
{
init(0, 0, true);
copy(rhs);
}
Buffer&
Buffer::operator=(Buffer const& rhs)
{
copy(rhs);
return *this;
}
Buffer::~Buffer()
{
destroy();
}
void
Buffer::init(unsigned long size, unsigned char* buf, bool own_memory)
{
this->own_memory = own_memory;
this->size = size;
if (own_memory)
{
this->buf = (size ? new unsigned char[size] : 0);
}
else
{
this->buf = buf;
}
}
void
Buffer::copy(Buffer const& rhs)
{
if (this != &rhs)
{
this->destroy();
this->init(rhs.size, 0, true);
if (this->size)
{
memcpy(this->buf, rhs.buf, this->size);
}
}
}
void
Buffer::destroy()
{
if (this->own_memory)
{
delete [] this->buf;
}
this->size = 0;
this->buf = 0;
}
unsigned long
Buffer::getSize() const
{
return this->size;
}
unsigned char const*
Buffer::getBuffer() const
{
return this->buf;
}
unsigned char*
Buffer::getBuffer()
{
return this->buf;
}