2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-11-16 01:27:07 +00:00
qpdf/libqpdf/Pl_QPDFTokenizer.cc
2022-11-20 11:55:02 -05:00

76 lines
2.0 KiB
C++

#include <qpdf/Pl_QPDFTokenizer.hh>
#include <qpdf/BufferInputSource.hh>
#include <qpdf/QTC.hh>
#include <qpdf/QUtil.hh>
#include <stdexcept>
#include <string.h>
Pl_QPDFTokenizer::Members::Members() :
filter(nullptr),
buf("tokenizer buffer")
{
}
Pl_QPDFTokenizer::Pl_QPDFTokenizer(
char const* identifier,
QPDFObjectHandle::TokenFilter* filter,
Pipeline* next) :
Pipeline(identifier, next),
m(new Members)
{
m->filter = filter;
QPDFObjectHandle::TokenFilter::PipelineAccessor::setPipeline(
m->filter, next);
m->tokenizer.allowEOF();
m->tokenizer.includeIgnorable();
}
Pl_QPDFTokenizer::~Pl_QPDFTokenizer()
{
// Must be explicit and not inline -- see QPDF_DLL_CLASS in
// README-maintainer
}
void
Pl_QPDFTokenizer::write(unsigned char const* data, size_t len)
{
this->m->buf.write(data, len);
}
void
Pl_QPDFTokenizer::finish()
{
this->m->buf.finish();
auto input = std::shared_ptr<InputSource>(
// line-break
new BufferInputSource(
"tokenizer data", this->m->buf.getBuffer(), true));
while (true) {
QPDFTokenizer::Token token = this->m->tokenizer.readToken(
input, "offset " + std::to_string(input->tell()), true);
this->m->filter->handleToken(token);
if (token.getType() == QPDFTokenizer::tt_eof) {
break;
} else if (token.isWord("ID")) {
// Read the space after the ID.
char ch = ' ';
input->read(&ch, 1);
this->m->filter->handleToken(
// line-break
QPDFTokenizer::Token(
QPDFTokenizer::tt_space, std::string(1, ch)));
QTC::TC("qpdf", "Pl_QPDFTokenizer found ID");
this->m->tokenizer.expectInlineImage(input);
}
}
this->m->filter->handleEOF();
QPDFObjectHandle::TokenFilter::PipelineAccessor::setPipeline(
m->filter, nullptr);
Pipeline* next = this->getNext(true);
if (next) {
next->finish();
}
}