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