2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-11-02 11:46:35 +00:00
qpdf/libqpdf/ClosedFileInputSource.cc

106 lines
1.9 KiB
C++
Raw Normal View History

#include <qpdf/ClosedFileInputSource.hh>
#include <qpdf/FileInputSource.hh>
2022-08-25 13:23:53 +00:00
ClosedFileInputSource::ClosedFileInputSource(char const* filename) :
filename(filename),
offset(0),
stay_open(false)
{
}
ClosedFileInputSource::~ClosedFileInputSource()
{
// Must be explicit and not inline -- see QPDF_DLL_CLASS in
// README-maintainer
}
void
ClosedFileInputSource::before()
{
2022-08-25 13:23:53 +00:00
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()
{
2022-08-25 13:23:53 +00:00
this->last_offset = this->fis->getLastOffset();
this->offset = this->fis->tell();
if (this->stay_open) {
return;
}
2022-08-25 13:23:53 +00:00
this->fis = nullptr;
}
qpdf_offset_t
ClosedFileInputSource::findAndSkipNextEOL()
{
before();
2022-08-25 13:23:53 +00:00
qpdf_offset_t r = this->fis->findAndSkipNextEOL();
after();
return r;
}
std::string const&
ClosedFileInputSource::getName() const
{
2022-08-25 13:23:53 +00:00
return this->filename;
}
qpdf_offset_t
ClosedFileInputSource::tell()
{
before();
2022-08-25 13:23:53 +00:00
qpdf_offset_t r = this->fis->tell();
after();
return r;
}
void
ClosedFileInputSource::seek(qpdf_offset_t offset, int whence)
{
before();
2022-08-25 13:23:53 +00:00
this->fis->seek(offset, whence);
after();
}
void
ClosedFileInputSource::rewind()
{
2022-08-25 13:23:53 +00:00
this->offset = 0;
if (this->fis.get()) {
this->fis->rewind();
}
}
size_t
ClosedFileInputSource::read(char* buffer, size_t length)
{
before();
2022-08-25 13:23:53 +00:00
size_t r = this->fis->read(buffer, length);
after();
return r;
}
void
ClosedFileInputSource::unreadCh(char ch)
{
before();
2022-08-25 13:23:53 +00:00
this->fis->unreadCh(ch);
// Don't call after -- the file has to stay open after this
// operation.
}
void
ClosedFileInputSource::stayOpen(bool val)
{
2022-08-25 13:23:53 +00:00
this->stay_open = val;
if ((!val) && this->fis.get()) {
after();
}
}