From dc5c8b82eb79d0ae5b92600a52162b9c0e352d6c Mon Sep 17 00:00:00 2001 From: m-holger Date: Thu, 25 Aug 2022 12:42:14 +0100 Subject: [PATCH] Remove FileInputSource::Members --- include/qpdf/FileInputSource.hh | 21 ++------- libqpdf/FileInputSource.cc | 84 +++++++++++++++------------------ 2 files changed, 41 insertions(+), 64 deletions(-) diff --git a/include/qpdf/FileInputSource.hh b/include/qpdf/FileInputSource.hh index f1e7edf4..9e0d57fb 100644 --- a/include/qpdf/FileInputSource.hh +++ b/include/qpdf/FileInputSource.hh @@ -58,24 +58,9 @@ class QPDF_DLL_CLASS FileInputSource: public InputSource FileInputSource(FileInputSource const&) = delete; FileInputSource& operator=(FileInputSource const&) = delete; - class QPDF_DLL_PRIVATE Members - { - friend class FileInputSource; - - public: - QPDF_DLL - ~Members(); - - private: - Members(bool close_file); - Members(Members const&) = delete; - - bool close_file; - std::string filename; - FILE* file; - }; - - std::shared_ptr m; + bool close_file; + std::string filename; + FILE* file; }; #endif // QPDF_FILEINPUTSOURCE_HH diff --git a/libqpdf/FileInputSource.cc b/libqpdf/FileInputSource.cc index ab88d302..1a0c2653 100644 --- a/libqpdf/FileInputSource.cc +++ b/libqpdf/FileInputSource.cc @@ -5,58 +5,50 @@ #include #include -FileInputSource::Members::Members(bool close_file) : - close_file(close_file), +FileInputSource::FileInputSource() : + close_file(false), file(nullptr) { } -FileInputSource::Members::~Members() -{ - if (this->file && this->close_file) { - fclose(this->file); - } -} - -FileInputSource::FileInputSource() : - m(new Members(false)) -{ -} - FileInputSource::FileInputSource(char const* filename) : - m(new Members(false)) + close_file(true), + filename(filename), + file(QUtil::safe_fopen(filename, "rb")) { - setFilename(filename); } FileInputSource::FileInputSource( char const* description, FILE* filep, bool close_file) : - m(new Members(false)) + close_file(close_file), + filename(description), + file(filep) { - setFile(description, filep, close_file); -} - -void -FileInputSource::setFilename(char const* filename) -{ - this->m = std::shared_ptr(new Members(true)); - this->m->filename = filename; - this->m->file = QUtil::safe_fopen(filename, "rb"); -} - -void -FileInputSource::setFile(char const* description, FILE* filep, bool close_file) -{ - this->m = std::shared_ptr(new Members(close_file)); - this->m->filename = description; - this->m->file = filep; - this->seek(0, SEEK_SET); } FileInputSource::~FileInputSource() { // Must be explicit and not inline -- see QPDF_DLL_CLASS in // README-maintainer + if (this->file && this->close_file) { + fclose(this->file); + } +} + +void +FileInputSource::setFilename(char const* filename) +{ + this->close_file = true; + this->filename = filename; + this->file = QUtil::safe_fopen(filename, "rb"); +} + +void +FileInputSource::setFile(char const* description, FILE* filep, bool close_file) +{ + this->filename = description; + this->file = filep; + this->seek(0, SEEK_SET); } qpdf_offset_t @@ -66,7 +58,7 @@ FileInputSource::findAndSkipNextEOL() bool done = false; char buf[10240]; while (!done) { - qpdf_offset_t cur_offset = QUtil::tell(this->m->file); + qpdf_offset_t cur_offset = QUtil::tell(this->file); size_t len = this->read(buf, sizeof(buf)); if (len == 0) { done = true; @@ -98,41 +90,41 @@ FileInputSource::findAndSkipNextEOL() std::string const& FileInputSource::getName() const { - return this->m->filename; + return this->filename; } qpdf_offset_t FileInputSource::tell() { - return QUtil::tell(this->m->file); + return QUtil::tell(this->file); } void FileInputSource::seek(qpdf_offset_t offset, int whence) { QUtil::os_wrapper( - (std::string("seek to ") + this->m->filename + ", offset " + + (std::string("seek to ") + this->filename + ", offset " + QUtil::int_to_string(offset) + " (" + QUtil::int_to_string(whence) + ")"), - QUtil::seek(this->m->file, offset, whence)); + QUtil::seek(this->file, offset, whence)); } void FileInputSource::rewind() { - ::rewind(this->m->file); + ::rewind(this->file); } size_t FileInputSource::read(char* buffer, size_t length) { this->last_offset = this->tell(); - size_t len = fread(buffer, 1, length, this->m->file); + size_t len = fread(buffer, 1, length, this->file); if (len == 0) { - if (ferror(this->m->file)) { + if (ferror(this->file)) { throw QPDFExc( qpdf_e_system, - this->m->filename, + this->filename, "", this->last_offset, (std::string("read ") + QUtil::uint_to_string(length) + @@ -149,6 +141,6 @@ void FileInputSource::unreadCh(char ch) { QUtil::os_wrapper( - this->m->filename + ": unread character", - ungetc(static_cast(ch), this->m->file)); + this->filename + ": unread character", + ungetc(static_cast(ch), this->file)); }