1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
#include <qpdf/ClosedFileInputSource.hh>
#include <qpdf/FileInputSource.hh>
ClosedFileInputSource::ClosedFileInputSource(char const* filename) :
filename(filename)
{
}
ClosedFileInputSource::~ClosedFileInputSource() // NOLINT (modernize-use-equals-default)
{
// Must be explicit and not inline -- see QPDF_DLL_CLASS in README-maintainer
}
void
ClosedFileInputSource::before()
{
if (nullptr == this->fis) {
this->fis = std::make_shared<FileInputSource>(this->filename.c_str());
this->fis->seek(this->offset, SEEK_SET);
this->fis->setLastOffset(this->last_offset);
}
}
void
ClosedFileInputSource::after()
{
this->last_offset = this->fis->getLastOffset();
this->offset = this->fis->tell();
if (this->stay_open) {
return;
}
this->fis = nullptr;
}
qpdf_offset_t
ClosedFileInputSource::findAndSkipNextEOL()
{
before();
qpdf_offset_t r = this->fis->findAndSkipNextEOL();
after();
return r;
}
std::string const&
ClosedFileInputSource::getName() const
{
return this->filename;
}
qpdf_offset_t
ClosedFileInputSource::tell()
{
before();
qpdf_offset_t r = this->fis->tell();
after();
return r;
}
void
ClosedFileInputSource::seek(qpdf_offset_t offset, int whence)
{
before();
this->fis->seek(offset, whence);
after();
}
void
ClosedFileInputSource::rewind()
{
this->offset = 0;
if (this->fis.get()) {
this->fis->rewind();
}
}
size_t
ClosedFileInputSource::read(char* buffer, size_t length)
{
before();
size_t r = this->fis->read(buffer, length);
after();
return r;
}
void
ClosedFileInputSource::unreadCh(char ch)
{
before();
this->fis->unreadCh(ch);
// Don't call after -- the file has to stay open after this operation.
}
void
ClosedFileInputSource::stayOpen(bool val)
{
this->stay_open = val;
if ((!val) && this->fis.get()) {
after();
}
}
|