[poppler] poppler/CurlPDFDocBuilder.cc poppler/CurlPDFDocBuilder.h poppler/LocalPDFDocBuilder.cc poppler/LocalPDFDocBuilder.h poppler/PDFDocBuilder.h poppler/PDFDoc.cc poppler/PDFDocFactory.cc poppler/PDFDocFactory.h poppler/PDFDoc.h poppler/StdinPDFDocBuilder.cc poppler/StdinPDFDocBuilder.h utils/pdfdetach.cc utils/pdfimages.cc utils/pdfinfo.cc utils/pdftocairo.cc utils/pdftohtml.cc utils/pdftoppm.cc utils/pdftops.cc utils/pdftotext.cc

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Fri Feb 12 13:15:24 UTC 2021


 poppler/CurlPDFDocBuilder.cc  |    5 +++--
 poppler/CurlPDFDocBuilder.h   |    3 ++-
 poppler/LocalPDFDocBuilder.cc |    7 ++++---
 poppler/LocalPDFDocBuilder.h  |    3 ++-
 poppler/PDFDoc.cc             |    6 ++++--
 poppler/PDFDoc.h              |    4 ++--
 poppler/PDFDocBuilder.h       |    5 ++++-
 poppler/PDFDocFactory.cc      |    4 ++--
 poppler/PDFDocFactory.h       |    6 ++++--
 poppler/StdinPDFDocBuilder.cc |    5 +++--
 poppler/StdinPDFDocBuilder.h  |    3 ++-
 utils/pdfdetach.cc            |    5 ++---
 utils/pdfimages.cc            |   30 +++++++++---------------------
 utils/pdfinfo.cc              |   11 +++++------
 utils/pdftocairo.cc           |   10 ++++------
 utils/pdftohtml.cc            |   11 ++++-------
 utils/pdftoppm.cc             |   39 +++++++++++++++------------------------
 utils/pdftops.cc              |   30 ++++++++++++++----------------
 utils/pdftotext.cc            |    9 ++++-----
 19 files changed, 89 insertions(+), 107 deletions(-)

New commits:
commit 33ae035d03e56e4f37ebb33ac109bbb71084dc93
Author: Oliver Sander <oliver.sander at tu-dresden.de>
Date:   Thu Feb 11 20:22:36 2021 +0100

    Make PDFDocBuilder return a std::unique_ptr
    
    This make the memory ownership cleaner, and allows to simplify
    a bit of error handling code in the `utils` directory.

diff --git a/poppler/CurlPDFDocBuilder.cc b/poppler/CurlPDFDocBuilder.cc
index 3a3f6c50..367a5b4f 100644
--- a/poppler/CurlPDFDocBuilder.cc
+++ b/poppler/CurlPDFDocBuilder.cc
@@ -6,6 +6,7 @@
 //
 // Copyright 2010 Hib Eris <hib at hiberis.nl>
 // Copyright 2010, 2017 Albert Astals Cid <aacid at kde.org>
+// Copyright 2021 Oliver Sander <oliver.sander at tu-dresden.de>
 //
 //========================================================================
 
@@ -21,7 +22,7 @@
 // CurlPDFDocBuilder
 //------------------------------------------------------------------------
 
-PDFDoc *CurlPDFDocBuilder::buildPDFDoc(const GooString &uri, GooString *ownerPassword, GooString *userPassword, void *guiDataA)
+std::unique_ptr<PDFDoc> CurlPDFDocBuilder::buildPDFDoc(const GooString &uri, GooString *ownerPassword, GooString *userPassword, void *guiDataA)
 {
     CachedFile *cachedFile = new CachedFile(new CurlCachedFileLoader(), uri.copy());
 
@@ -32,7 +33,7 @@ PDFDoc *CurlPDFDocBuilder::buildPDFDoc(const GooString &uri, GooString *ownerPas
 
     BaseStream *str = new CachedFileStream(cachedFile, 0, false, cachedFile->getLength(), Object(objNull));
 
-    return new PDFDoc(str, ownerPassword, userPassword, guiDataA);
+    return std::make_unique<PDFDoc>(str, ownerPassword, userPassword, guiDataA);
 }
 
 bool CurlPDFDocBuilder::supports(const GooString &uri)
diff --git a/poppler/CurlPDFDocBuilder.h b/poppler/CurlPDFDocBuilder.h
index 4a81f9f9..601f0304 100644
--- a/poppler/CurlPDFDocBuilder.h
+++ b/poppler/CurlPDFDocBuilder.h
@@ -6,6 +6,7 @@
 //
 // Copyright 2010 Hib Eris <hib at hiberis.nl>
 // Copyright 2010, 2018 Albert Astals Cid <aacid at kde.org>
+// Copyright 2021 Oliver Sander <oliver.sander at tu-dresden.de>
 //
 //========================================================================
 
@@ -24,7 +25,7 @@ class CurlPDFDocBuilder : public PDFDocBuilder
 {
 
 public:
-    PDFDoc *buildPDFDoc(const GooString &uri, GooString *ownerPassword = nullptr, GooString *userPassword = nullptr, void *guiDataA = nullptr) override;
+    std::unique_ptr<PDFDoc> buildPDFDoc(const GooString &uri, GooString *ownerPassword = nullptr, GooString *userPassword = nullptr, void *guiDataA = nullptr) override;
     bool supports(const GooString &uri) override;
 };
 
diff --git a/poppler/LocalPDFDocBuilder.cc b/poppler/LocalPDFDocBuilder.cc
index b76a83c8..9aab08eb 100644
--- a/poppler/LocalPDFDocBuilder.cc
+++ b/poppler/LocalPDFDocBuilder.cc
@@ -6,6 +6,7 @@
 //
 // Copyright 2010 Hib Eris <hib at hiberis.nl>
 // Copyright 2010 Albert Astals Cid <aacid at kde.org>
+// Copyright 2021 Oliver Sander <oliver.sander at tu-dresden.de>
 //
 //========================================================================
 
@@ -17,15 +18,15 @@
 // LocalPDFDocBuilder
 //------------------------------------------------------------------------
 
-PDFDoc *LocalPDFDocBuilder::buildPDFDoc(const GooString &uri, GooString *ownerPassword, GooString *userPassword, void *guiDataA)
+std::unique_ptr<PDFDoc> LocalPDFDocBuilder::buildPDFDoc(const GooString &uri, GooString *ownerPassword, GooString *userPassword, void *guiDataA)
 {
     if (uri.cmpN("file://", 7) == 0) {
         GooString *fileName = uri.copy();
         fileName->del(0, 7);
-        return new PDFDoc(fileName, ownerPassword, userPassword, guiDataA);
+        return std::make_unique<PDFDoc>(fileName, ownerPassword, userPassword, guiDataA);
     } else {
         GooString *fileName = uri.copy();
-        return new PDFDoc(fileName, ownerPassword, userPassword, guiDataA);
+        return std::make_unique<PDFDoc>(fileName, ownerPassword, userPassword, guiDataA);
     }
 }
 
diff --git a/poppler/LocalPDFDocBuilder.h b/poppler/LocalPDFDocBuilder.h
index ec3f6648..f82bdf63 100644
--- a/poppler/LocalPDFDocBuilder.h
+++ b/poppler/LocalPDFDocBuilder.h
@@ -6,6 +6,7 @@
 //
 // Copyright 2010 Hib Eris <hib at hiberis.nl>
 // Copyright 2010, 2018 Albert Astals Cid <aacid at kde.org>
+// Copyright 2021 Oliver Sander <oliver.sander at tu-dresden.de>
 //
 //========================================================================
 
@@ -24,7 +25,7 @@ class LocalPDFDocBuilder : public PDFDocBuilder
 {
 
 public:
-    PDFDoc *buildPDFDoc(const GooString &uri, GooString *ownerPassword = nullptr, GooString *userPassword = nullptr, void *guiDataA = nullptr) override;
+    std::unique_ptr<PDFDoc> buildPDFDoc(const GooString &uri, GooString *ownerPassword = nullptr, GooString *userPassword = nullptr, void *guiDataA = nullptr) override;
     bool supports(const GooString &uri) override;
 };
 
diff --git a/poppler/PDFDoc.cc b/poppler/PDFDoc.cc
index 6f7b8052..ca440ca1 100644
--- a/poppler/PDFDoc.cc
+++ b/poppler/PDFDoc.cc
@@ -46,6 +46,7 @@
 // Copyright (C) 2020 Nelson Benítez León <nbenitezl at gmail.com>
 // Copyright (C) 2020 Thorsten Behrens <Thorsten.Behrens at CIB.de>
 // Copyright (C) 2020 Adam Sampson <ats at offog.org>
+// Copyright (C) 2021 Oliver Sander <oliver.sander at tu-dresden.de>
 //
 // To see a description of the changes please see the Changelog file that
 // came with your tarball or type make ChangeLog if you are building from git
@@ -1937,13 +1938,14 @@ Outline *PDFDoc::getOutline()
     return outline;
 }
 
-PDFDoc *PDFDoc::ErrorPDFDoc(int errorCode, const GooString *fileNameA)
+std::unique_ptr<PDFDoc> PDFDoc::ErrorPDFDoc(int errorCode, const GooString *fileNameA)
 {
+    // We cannot call std::make_unique here because the PDFDoc constructor is private
     PDFDoc *doc = new PDFDoc();
     doc->errCode = errorCode;
     doc->fileName = fileNameA;
 
-    return doc;
+    return std::unique_ptr<PDFDoc>(doc);
 }
 
 long long PDFDoc::strToLongLong(const char *s)
diff --git a/poppler/PDFDoc.h b/poppler/PDFDoc.h
index 0faef013..5ab1fb21 100644
--- a/poppler/PDFDoc.h
+++ b/poppler/PDFDoc.h
@@ -32,7 +32,7 @@
 // Copyright (C) 2016 Jakub Alba <jakubalba at gmail.com>
 // Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, <info at kdab.com>. Work sponsored by the LiMux project of the city of Munich
 // Copyright (C) 2018 Evangelos Rigas <erigas at rnd2.org>
-// Copyright (C) 2020 Oliver Sander <oliver.sander at tu-dresden.de>
+// Copyright (C) 2020, 2021 Oliver Sander <oliver.sander at tu-dresden.de>
 // Copyright (C) 2020 Nelson Benítez León <nbenitezl at gmail.com>
 //
 // To see a description of the changes please see the Changelog file that
@@ -132,7 +132,7 @@ public:
     PDFDoc(const PDFDoc &) = delete;
     PDFDoc &operator=(const PDFDoc &) = delete;
 
-    static PDFDoc *ErrorPDFDoc(int errorCode, const GooString *fileNameA = nullptr);
+    static std::unique_ptr<PDFDoc> ErrorPDFDoc(int errorCode, const GooString *fileNameA = nullptr);
 
     // Was PDF document successfully opened?
     bool isOk() const { return ok; }
diff --git a/poppler/PDFDocBuilder.h b/poppler/PDFDocBuilder.h
index a0c19fc1..a9231f01 100644
--- a/poppler/PDFDocBuilder.h
+++ b/poppler/PDFDocBuilder.h
@@ -6,12 +6,15 @@
 //
 // Copyright 2010 Hib Eris <hib at hiberis.nl>
 // Copyright 2010, 2018, 2020 Albert Astals Cid <aacid at kde.org>
+// Copyright 2021 Oliver Sander <oliver.sander at tu-dresden.de>
 //
 //========================================================================
 
 #ifndef PDFDOCBUILDER_H
 #define PDFDOCBUILDER_H
 
+#include <memory>
+
 #include "PDFDoc.h"
 class GooString;
 
@@ -35,7 +38,7 @@ public:
     // Builds a new PDFDoc. Returns a PDFDoc. You should check this PDFDoc
     // with PDFDoc::isOk() for failures.
     // The caller is responsible for deleting ownerPassword, userPassWord and guiData.
-    virtual PDFDoc *buildPDFDoc(const GooString &uri, GooString *ownerPassword = nullptr, GooString *userPassword = nullptr, void *guiDataA = nullptr) = 0;
+    virtual std::unique_ptr<PDFDoc> buildPDFDoc(const GooString &uri, GooString *ownerPassword = nullptr, GooString *userPassword = nullptr, void *guiDataA = nullptr) = 0;
 
     // Returns true if the builder supports building a PDFDoc from the URI.
     virtual bool supports(const GooString &uri) = 0;
diff --git a/poppler/PDFDocFactory.cc b/poppler/PDFDocFactory.cc
index 51440c9f..e4797074 100644
--- a/poppler/PDFDocFactory.cc
+++ b/poppler/PDFDocFactory.cc
@@ -8,7 +8,7 @@
 // Copyright 2010 Albert Astals Cid <aacid at kde.org>
 // Copyright 2017 Adrian Johnson <ajohnson at redneon.com>
 // Copyright 2018 Adam Reichold <adam.reichold at t-online.de>
-// Copyright 2019 Oliver Sander <oliver.sander at tu-dresden.de>
+// Copyright 2019, 2021 Oliver Sander <oliver.sander at tu-dresden.de>
 //
 //========================================================================
 
@@ -53,7 +53,7 @@ PDFDocFactory::~PDFDocFactory()
     }
 }
 
-PDFDoc *PDFDocFactory::createPDFDoc(const GooString &uri, GooString *ownerPassword, GooString *userPassword, void *guiDataA)
+std::unique_ptr<PDFDoc> PDFDocFactory::createPDFDoc(const GooString &uri, GooString *ownerPassword, GooString *userPassword, void *guiDataA)
 {
     for (int i = builders->size() - 1; i >= 0; i--) {
         PDFDocBuilder *builder = (*builders)[i];
diff --git a/poppler/PDFDocFactory.h b/poppler/PDFDocFactory.h
index 3dd92394..92a66d7e 100644
--- a/poppler/PDFDocFactory.h
+++ b/poppler/PDFDocFactory.h
@@ -6,13 +6,15 @@
 //
 // Copyright 2010 Hib Eris <hib at hiberis.nl>
 // Copyright 2010, 2018 Albert Astals Cid <aacid at kde.org>
-// Copyright 2019 Oliver Sander <oliver.sander at tu-dresden.de>
+// Copyright 2019, 2021 Oliver Sander <oliver.sander at tu-dresden.de>
 //
 //========================================================================
 
 #ifndef PDFDOCFACTORY_H
 #define PDFDOCFACTORY_H
 
+#include <memory>
+
 #include "PDFDoc.h"
 
 class GooString;
@@ -43,7 +45,7 @@ public:
     // Create a PDFDoc. Returns a PDFDoc. You should check this PDFDoc
     // with PDFDoc::isOk() for failures.
     // The caller is responsible for deleting ownerPassword, userPassWord and guiData.
-    PDFDoc *createPDFDoc(const GooString &uri, GooString *ownerPassword = nullptr, GooString *userPassword = nullptr, void *guiDataA = nullptr);
+    std::unique_ptr<PDFDoc> createPDFDoc(const GooString &uri, GooString *ownerPassword = nullptr, GooString *userPassword = nullptr, void *guiDataA = nullptr);
 
     // Extend supported URIs with the ones from the PDFDocBuilder.
     void registerPDFDocBuilder(PDFDocBuilder *pdfDocBuilder);
diff --git a/poppler/StdinPDFDocBuilder.cc b/poppler/StdinPDFDocBuilder.cc
index 9d7142b4..b84420b2 100644
--- a/poppler/StdinPDFDocBuilder.cc
+++ b/poppler/StdinPDFDocBuilder.cc
@@ -6,6 +6,7 @@
 //
 // Copyright 2010 Hib Eris <hib at hiberis.nl>
 // Copyright 2010, 2017 Albert Astals Cid <aacid at kde.org>
+// Copyright 2021 Oliver Sander <oliver.sander at tu-dresden.de>
 //
 //========================================================================
 
@@ -19,10 +20,10 @@
 // StdinPDFDocBuilder
 //------------------------------------------------------------------------
 
-PDFDoc *StdinPDFDocBuilder::buildPDFDoc(const GooString &uri, GooString *ownerPassword, GooString *userPassword, void *guiDataA)
+std::unique_ptr<PDFDoc> StdinPDFDocBuilder::buildPDFDoc(const GooString &uri, GooString *ownerPassword, GooString *userPassword, void *guiDataA)
 {
     CachedFile *cachedFile = new CachedFile(new StdinCacheLoader(), nullptr);
-    return new PDFDoc(new CachedFileStream(cachedFile, 0, false, cachedFile->getLength(), Object(objNull)), ownerPassword, userPassword);
+    return std::make_unique<PDFDoc>(new CachedFileStream(cachedFile, 0, false, cachedFile->getLength(), Object(objNull)), ownerPassword, userPassword);
 }
 
 bool StdinPDFDocBuilder::supports(const GooString &uri)
diff --git a/poppler/StdinPDFDocBuilder.h b/poppler/StdinPDFDocBuilder.h
index 33fbcf0c..a4bbf8e4 100644
--- a/poppler/StdinPDFDocBuilder.h
+++ b/poppler/StdinPDFDocBuilder.h
@@ -6,6 +6,7 @@
 //
 // Copyright 2010 Hib Eris <hib at hiberis.nl>
 // Copyright 2010, 2018 Albert Astals Cid <aacid at kde.org>
+// Copyright 2021 Oliver Sander <oliver.sander at tu-dresden.de>
 //
 //========================================================================
 
@@ -24,7 +25,7 @@ class StdinPDFDocBuilder : public PDFDocBuilder
 {
 
 public:
-    PDFDoc *buildPDFDoc(const GooString &uri, GooString *ownerPassword = nullptr, GooString *userPassword = nullptr, void *guiDataA = nullptr) override;
+    std::unique_ptr<PDFDoc> buildPDFDoc(const GooString &uri, GooString *ownerPassword = nullptr, GooString *userPassword = nullptr, void *guiDataA = nullptr) override;
     bool supports(const GooString &uri) override;
 };
 
diff --git a/utils/pdfdetach.cc b/utils/pdfdetach.cc
index 78846ea4..f5576b38 100644
--- a/utils/pdfdetach.cc
+++ b/utils/pdfdetach.cc
@@ -18,7 +18,7 @@
 // Copyright (C) 2014, 2017 Adrian Johnson <ajohnson at redneon.com>
 // Copyright (C) 2018, 2020 Albert Astals Cid <aacid at kde.org>
 // Copyright (C) 2018 Adam Reichold <adam.reichold at t-online.de>
-// Copyright (C) 2019 Oliver Sander <oliver.sander at tu-dresden.de>
+// Copyright (C) 2019, 2021 Oliver Sander <oliver.sander at tu-dresden.de>
 // Copyright (C) 2020 <r.coeffier at bee-buzziness.com>
 //
 // To see a description of the changes please see the Changelog file that
@@ -72,10 +72,10 @@ static const ArgDesc argDesc[] = { { "-list", argFlag, &doList, 0, "list all emb
 
 int main(int argc, char *argv[])
 {
+    std::unique_ptr<PDFDoc> doc;
     GooString *fileName;
     const UnicodeMap *uMap;
     GooString *ownerPW, *userPW;
-    PDFDoc *doc;
     char uBuf[8];
     char path[1024];
     char *p;
@@ -329,7 +329,6 @@ int main(int argc, char *argv[])
 err2:
     for (auto &file : embeddedFiles)
         delete file;
-    delete doc;
 err0:
 
     return exitCode;
diff --git a/utils/pdfimages.cc b/utils/pdfimages.cc
index 46cebf98..2a524fa8 100644
--- a/utils/pdfimages.cc
+++ b/utils/pdfimages.cc
@@ -21,7 +21,7 @@
 // Copyright (C) 2012, 2013, 2017 Adrian Johnson <ajohnson at redneon.com>
 // Copyright (C) 2013 Suzuki Toshiya <mpsuzuki at hiroshima-u.ac.jp>
 // Copyright (C) 2018 Adam Reichold <adam.reichold at t-online.de>
-// Copyright (C) 2019 Oliver Sander <oliver.sander at tu-dresden.de>
+// Copyright (C) 2019, 2021 Oliver Sander <oliver.sander at tu-dresden.de>
 // Copyright (C) 2019 Hartmut Goebel <h.goebel at crazy-compilers.com>
 //
 // To see a description of the changes please see the Changelog file that
@@ -96,16 +96,13 @@ static const ArgDesc argDesc[] = { { "-f", argInt, &firstPage, 0, "first page to
 
 int main(int argc, char *argv[])
 {
-    PDFDoc *doc;
     GooString *fileName;
     char *imgRoot = nullptr;
     GooString *ownerPW, *userPW;
     ImageOutputDev *imgOut;
     bool ok;
-    int exitCode;
 
     Win32Console win32Console(&argc, &argv);
-    exitCode = 99;
 
     // parse args
     ok = parseArgs(argDesc, &argc, argv);
@@ -117,8 +114,8 @@ int main(int argc, char *argv[])
             printUsage("pdfimages", "<PDF-file> <image-root>", argDesc);
         }
         if (printVersion || printHelp)
-            exitCode = 0;
-        goto err0;
+            return 0;
+        return 99;
     }
     fileName = new GooString(argv[1]);
     if (!listImages)
@@ -146,7 +143,7 @@ int main(int argc, char *argv[])
         fileName = new GooString("fd://0");
     }
 
-    doc = PDFDocFactory().createPDFDoc(*fileName, ownerPW, userPW);
+    std::unique_ptr<PDFDoc> doc = PDFDocFactory().createPDFDoc(*fileName, ownerPW, userPW);
     delete fileName;
 
     if (userPW) {
@@ -156,16 +153,14 @@ int main(int argc, char *argv[])
         delete ownerPW;
     }
     if (!doc->isOk()) {
-        exitCode = 1;
-        goto err1;
+        return 1;
     }
 
     // check for copy permission
 #ifdef ENFORCE_PERMISSIONS
     if (!doc->okToCopy()) {
         error(errNotAllowed, -1, "Copying of images from this document is not allowed.");
-        exitCode = 3;
-        goto err1;
+        return 3;
     }
 #endif
 
@@ -174,13 +169,13 @@ int main(int argc, char *argv[])
         firstPage = 1;
     if (firstPage > doc->getNumPages()) {
         error(errCommandLine, -1, "Wrong page range given: the first page ({0:d}) can not be larger then the number of pages in the document ({1:d}).", firstPage, doc->getNumPages());
-        goto err1;
+        return 99;
     }
     if (lastPage < 1 || lastPage > doc->getNumPages())
         lastPage = doc->getNumPages();
     if (lastPage < firstPage) {
         error(errCommandLine, -1, "Wrong page range given: the first page ({0:d}) can not be after the last page ({1:d}).", firstPage, lastPage);
-        goto err1;
+        return 99;
     }
 
     // write image files
@@ -205,12 +200,5 @@ int main(int argc, char *argv[])
     }
     delete imgOut;
 
-    exitCode = 0;
-
-    // clean up
-err1:
-    delete doc;
-err0:
-
-    return exitCode;
+    return 0;
 }
diff --git a/utils/pdfinfo.cc b/utils/pdfinfo.cc
index 6180dbea..cdc88d32 100644
--- a/utils/pdfinfo.cc
+++ b/utils/pdfinfo.cc
@@ -26,7 +26,7 @@
 // Copyright (C) 2018 Adam Reichold <adam.reichold at t-online.de>
 // Copyright (C) 2018 Evangelos Rigas <erigas at rnd2.org>
 // Copyright (C) 2019 Christian Persch <chpe at src.gnome.org>
-// Copyright (C) 2019, 2020 Oliver Sander <oliver.sander at tu-dresden.de>
+// Copyright (C) 2019-2021 Oliver Sander <oliver.sander at tu-dresden.de>
 // Copyright (C) 2019 Thomas Fischer <fischer at unix-ag.uni-kl.de>
 //
 // To see a description of the changes please see the Changelog file that
@@ -807,7 +807,7 @@ static void printInfo(PDFDoc *doc, const UnicodeMap *uMap, long long filesize, b
 
 int main(int argc, char *argv[])
 {
-    PDFDoc *doc;
+    std::unique_ptr<PDFDoc> doc;
     GooString *fileName;
     GooString *ownerPW, *userPW;
     const UnicodeMap *uMap;
@@ -915,7 +915,7 @@ int main(int argc, char *argv[])
         }
     } else if (printJS) {
         // print javascript
-        JSInfo jsInfo(doc, firstPage - 1);
+        JSInfo jsInfo(doc.get(), firstPage - 1);
         jsInfo.scanJS(lastPage - firstPage + 1, stdout, uMap);
     } else if (printStructure || printStructureText) {
         // print structure
@@ -926,7 +926,7 @@ int main(int argc, char *argv[])
             }
         }
     } else if (printDests) {
-        printDestinations(doc, uMap);
+        printDestinations(doc.get(), uMap);
     } else {
         // print info
         long long filesize = 0;
@@ -941,13 +941,12 @@ int main(int argc, char *argv[])
         if (multiPage == false)
             lastPage = 1;
 
-        printInfo(doc, uMap, filesize, multiPage);
+        printInfo(doc.get(), uMap, filesize, multiPage);
     }
     exitCode = 0;
 
     // clean up
 err2:
-    delete doc;
     delete fileName;
 err1:
 err0:
diff --git a/utils/pdftocairo.cc b/utils/pdftocairo.cc
index 1a0785d2..8cdb13d2 100644
--- a/utils/pdftocairo.cc
+++ b/utils/pdftocairo.cc
@@ -34,7 +34,7 @@
 // Copyright (C) 2018 Adam Reichold <adam.reichold at t-online.de>
 // Copyright (C) 2019, 2020 Oliver Sander <oliver.sander at tu-dresden.de>
 // Copyright (C) 2019 Kris Jurka <jurka at ejurka.com>
-// Copyright (C) 2020 Oliver Sander <oliver.sander at tu-dresden.de>
+// Copyright (C) 2020, 2021 Oliver Sander <oliver.sander at tu-dresden.de>
 // Copyright (C) 2020 Philipp Knechtges <philipp-dev at knechtges.com>
 // Copyright (C) 2020 Salvo Miosi <salvo.ilmiosi at gmail.com>
 //
@@ -855,7 +855,6 @@ static void checkInvalidImageOption(bool option, const char *option_name)
 
 int main(int argc, char *argv[])
 {
-    PDFDoc *doc;
     GooString *fileName = nullptr;
     GooString *outputName = nullptr;
     GooString *outputFileName = nullptr;
@@ -1061,7 +1060,7 @@ int main(int argc, char *argv[])
     }
 #endif
 
-    doc = PDFDocFactory().createPDFDoc(*fileName, ownerPW, userPW);
+    std::unique_ptr<PDFDoc> doc = PDFDocFactory().createPDFDoc(*fileName, ownerPW, userPW);
     if (!doc->isOk()) {
         fprintf(stderr, "Error opening PDF file.\n");
         exit(1);
@@ -1126,7 +1125,7 @@ int main(int argc, char *argv[])
 #ifdef USE_CMS
     cairoOut->setDisplayProfile(profile);
 #endif
-    cairoOut->startDoc(doc);
+    cairoOut->startDoc(doc.get());
     if (sz != 0)
         crop_w = crop_h = sz;
     pg_num_len = numberOfCharacters(doc->getNumPages());
@@ -1181,14 +1180,13 @@ int main(int argc, char *argv[])
         if (pg == firstPage)
             beginDocument(fileName, outputFileName, output_w, output_h);
         beginPage(&output_w, &output_h);
-        renderPage(doc, cairoOut, pg, pg_w, pg_h, output_w, output_h);
+        renderPage(doc.get(), cairoOut, pg, pg_w, pg_h, output_w, output_h);
         endPage(imageFileName);
     }
     endDocument();
 
     // clean up
     delete cairoOut;
-    delete doc;
     if (fileName)
         delete fileName;
     if (outputName)
diff --git a/utils/pdftohtml.cc b/utils/pdftohtml.cc
index 23e8cef4..483b211d 100644
--- a/utils/pdftohtml.cc
+++ b/utils/pdftohtml.cc
@@ -28,7 +28,7 @@
 // Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, <info at kdab.com>. Work sponsored by the LiMux project of the city of Munich
 // Copyright (C) 2018 Thibaut Brard <thibaut.brard at gmail.com>
 // Copyright (C) 2018 Adam Reichold <adam.reichold at t-online.de>
-// Copyright (C) 2019 Oliver Sander <oliver.sander at tu-dresden.de>
+// Copyright (C) 2019, 2021 Oliver Sander <oliver.sander at tu-dresden.de>
 //
 // To see a description of the changes please see the Changelog file that
 // came with your tarball or type make ChangeLog if you are building from git
@@ -160,7 +160,7 @@ SplashOutputDevNoText::~SplashOutputDevNoText() = default;
 
 int main(int argc, char *argv[])
 {
-    PDFDoc *doc = nullptr;
+    std::unique_ptr<PDFDoc> doc;
     GooString *fileName = nullptr;
     GooString *docTitle = nullptr;
     GooString *author = nullptr, *keywords = nullptr, *subject = nullptr, *date = nullptr;
@@ -362,7 +362,7 @@ int main(int argc, char *argv[])
         SplashImageFileFormat format = strcmp(extension, "jpg") ? splashFormatPng : splashFormatJpeg;
 
         splashOut = new SplashOutputDevNoText(splashModeRGB8, 4, false, color);
-        splashOut->startDoc(doc);
+        splashOut->startDoc(doc.get());
 
         for (int pg = firstPage; pg <= lastPage; ++pg) {
             InMemoryFile imf;
@@ -392,14 +392,13 @@ int main(int argc, char *argv[])
         delete htmlOut;
         delete htmlFileName;
         delete fileName;
-        delete doc;
         return -1;
 #endif
     }
 
     if (htmlOut->isOk()) {
         doc->displayPages(htmlOut, firstPage, lastPage, 72 * scale, 72 * scale, 0, true, false, false);
-        htmlOut->dumpDocOutline(doc);
+        htmlOut->dumpDocOutline(doc.get());
     }
 
     delete htmlOut;
@@ -408,8 +407,6 @@ int main(int argc, char *argv[])
 
     // clean up
 error:
-    if (doc)
-        delete doc;
     delete fileName;
 
     if (htmlFileName)
diff --git a/utils/pdftoppm.cc b/utils/pdftoppm.cc
index 5d7c5389..9ad62fdd 100644
--- a/utils/pdftoppm.cc
+++ b/utils/pdftoppm.cc
@@ -29,7 +29,7 @@
 // Copyright (C) 2015 William Bader <williambader at hotmail.com>
 // Copyright (C) 2018 Martin Packman <gzlist at googlemail.com>
 // Copyright (C) 2019 Yves-Gaël Chény <gitlab at r0b0t.fr>
-// Copyright (C) 2019, 2020 Oliver Sander <oliver.sander at tu-dresden.de>
+// Copyright (C) 2019-2021 Oliver Sander <oliver.sander at tu-dresden.de>
 // Copyright (C) 2019 <corentinf at free.fr>
 // Copyright (C) 2019 Kris Jurka <jurka at ejurka.com>
 // Copyright (C) 2019 Sébastien Berthier <s.berthier at bee-buzziness.com>
@@ -381,7 +381,6 @@ static void processPageJobs()
 
 int main(int argc, char *argv[])
 {
-    PDFDoc *doc;
     GooString *fileName = nullptr;
     char *ppmRoot = nullptr;
     char *ppmFile;
@@ -421,7 +420,7 @@ int main(int argc, char *argv[])
         }
         if (printVersion || printHelp)
             exitCode = 0;
-        goto err0;
+        return exitCode;
     }
     if (argc > 1)
         fileName = new GooString(argv[1]);
@@ -484,7 +483,7 @@ int main(int argc, char *argv[])
         delete fileName;
         fileName = new GooString("fd://0");
     }
-    doc = PDFDocFactory().createPDFDoc(*fileName, ownerPW, userPW);
+    std::unique_ptr<PDFDoc> doc(PDFDocFactory().createPDFDoc(*fileName, ownerPW, userPW));
     delete fileName;
 
     if (userPW) {
@@ -494,8 +493,7 @@ int main(int argc, char *argv[])
         delete ownerPW;
     }
     if (!doc->isOk()) {
-        exitCode = 1;
-        goto err1;
+        return 1;
     }
 
     // get page range
@@ -507,7 +505,7 @@ int main(int argc, char *argv[])
         lastPage = doc->getNumPages();
     if (lastPage < firstPage) {
         fprintf(stderr, "Wrong page range given: the first page (%d) can not be after the last page (%d).\n", firstPage, lastPage);
-        goto err1;
+        return exitCode;
     }
 
     // If our page range selection and document size indicate we're only
@@ -515,7 +513,7 @@ int main(int argc, char *argv[])
     // filter out that single page.
     if (firstPage == lastPage && ((printOnlyEven && firstPage % 2 == 1) || (printOnlyOdd && firstPage % 2 == 0))) {
         fprintf(stderr, "Invalid even/odd page selection, no pages match criteria.\n");
-        goto err1;
+        return exitCode;
     }
 
     if (singleFile && firstPage < lastPage) {
@@ -540,12 +538,12 @@ int main(int argc, char *argv[])
         displayprofile = make_GfxLCMSProfilePtr(cmsOpenProfileFromFile(displayprofilename.c_str(), "r"));
         if (!displayprofile) {
             fprintf(stderr, "Could not open the ICC profile \"%s\".\n", displayprofilename.c_str());
-            goto err1;
+            return exitCode;
         }
         if (!cmsIsIntentSupported(displayprofile.get(), INTENT_RELATIVE_COLORIMETRIC, LCMS_USED_AS_OUTPUT) && !cmsIsIntentSupported(displayprofile.get(), INTENT_ABSOLUTE_COLORIMETRIC, LCMS_USED_AS_OUTPUT)
             && !cmsIsIntentSupported(displayprofile.get(), INTENT_SATURATION, LCMS_USED_AS_OUTPUT) && !cmsIsIntentSupported(displayprofile.get(), INTENT_PERCEPTUAL, LCMS_USED_AS_OUTPUT)) {
             fprintf(stderr, "ICC profile \"%s\" is not an output profile.\n", displayprofilename.c_str());
-            goto err1;
+            return exitCode;
         }
         profilecolorspace = cmsGetColorSpace(displayprofile.get());
         // Note: In contrast to pdftops we do not fail if a non-matching ICC profile is supplied.
@@ -568,19 +566,19 @@ int main(int argc, char *argv[])
     if (!defaultgrayprofilename.toStr().empty()) {
         defaultgrayprofile = make_GfxLCMSProfilePtr(cmsOpenProfileFromFile(defaultgrayprofilename.c_str(), "r"));
         if (!checkICCProfile(defaultgrayprofile, defaultgrayprofilename.c_str(), LCMS_USED_AS_INPUT, cmsSigGrayData)) {
-            goto err1;
+            return exitCode;
         }
     }
     if (!defaultrgbprofilename.toStr().empty()) {
         defaultrgbprofile = make_GfxLCMSProfilePtr(cmsOpenProfileFromFile(defaultrgbprofilename.c_str(), "r"));
         if (!checkICCProfile(defaultrgbprofile, defaultrgbprofilename.c_str(), LCMS_USED_AS_INPUT, cmsSigRgbData)) {
-            goto err1;
+            return exitCode;
         }
     }
     if (!defaultcmykprofilename.toStr().empty()) {
         defaultcmykprofile = make_GfxLCMSProfilePtr(cmsOpenProfileFromFile(defaultcmykprofilename.c_str(), "r"));
         if (!checkICCProfile(defaultcmykprofile, defaultcmykprofilename.c_str(), LCMS_USED_AS_INPUT, cmsSigCmykData)) {
-            goto err1;
+            return exitCode;
         }
     }
 #endif
@@ -598,7 +596,7 @@ int main(int argc, char *argv[])
     splashOut->setDefaultRGBProfile(defaultrgbprofile);
     splashOut->setDefaultCMYKProfile(defaultcmykprofile);
 #    endif
-    splashOut->startDoc(doc);
+    splashOut->startDoc(doc.get());
 
 #endif // UTILS_USE_PTHREADS
 
@@ -656,13 +654,13 @@ int main(int argc, char *argv[])
         }
 #ifndef UTILS_USE_PTHREADS
         // process job in main thread
-        savePageSlice(doc, splashOut, pg, param_x, param_y, param_w, param_h, pg_w, pg_h, ppmFile);
+        savePageSlice(doc.get(), splashOut, pg, param_x, param_y, param_w, param_h, pg_w, pg_h, ppmFile);
 
         delete[] ppmFile;
 #else
 
         // queue job for worker threads
-        PageJob pageJob = { .doc = doc,
+        PageJob pageJob = { .doc = doc.get(),
                             .pg = pg,
 
                             .pg_w = pg_w,
@@ -701,12 +699,5 @@ int main(int argc, char *argv[])
 
 #endif // UTILS_USE_PTHREADS
 
-    exitCode = 0;
-
-    // clean up
-err1:
-    delete doc;
-err0:
-
-    return exitCode;
+    return 0;
 }
diff --git a/utils/pdftops.cc b/utils/pdftops.cc
index 597a7937..e77edde3 100644
--- a/utils/pdftops.cc
+++ b/utils/pdftops.cc
@@ -25,7 +25,7 @@
 // Copyright (C) 2013 Suzuki Toshiya <mpsuzuki at hiroshima-u.ac.jp>
 // Copyright (C) 2014, 2017 Adrian Johnson <ajohnson at redneon.com>
 // Copyright (C) 2018 Adam Reichold <adam.reichold at t-online.de>
-// Copyright (C) 2019 Oliver Sander <oliver.sander at tu-dresden.de>
+// Copyright (C) 2019, 2021 Oliver Sander <oliver.sander at tu-dresden.de>
 // Copyright (C) 2020 Philipp Knechtges <philipp-dev at knechtges.com>
 //
 // To see a description of the changes please see the Changelog file that
@@ -199,7 +199,7 @@ static const ArgDesc argDesc[] = { { "-f", argInt, &firstPage, 0, "first page to
 
 int main(int argc, char *argv[])
 {
-    PDFDoc *doc;
+    std::unique_ptr<PDFDoc> doc;
     GooString *fileName;
     GooString *psFileName;
     PSLevel level;
@@ -301,7 +301,7 @@ int main(int argc, char *argv[])
             processcolorformatspecified = true;
         } else {
             fprintf(stderr, "Error: Unknown process color format \"%s\".\n", processcolorformatname.c_str());
-            goto err05;
+            goto err1;
         }
     }
 
@@ -310,12 +310,12 @@ int main(int argc, char *argv[])
         processcolorprofile = make_GfxLCMSProfilePtr(cmsOpenProfileFromFile(processcolorprofilename.c_str(), "r"));
         if (!processcolorprofile) {
             fprintf(stderr, "Error: Could not open the ICC profile \"%s\".\n", processcolorprofilename.c_str());
-            goto err05;
+            goto err1;
         }
         if (!cmsIsIntentSupported(processcolorprofile.get(), INTENT_RELATIVE_COLORIMETRIC, LCMS_USED_AS_OUTPUT) && !cmsIsIntentSupported(processcolorprofile.get(), INTENT_ABSOLUTE_COLORIMETRIC, LCMS_USED_AS_OUTPUT)
             && !cmsIsIntentSupported(processcolorprofile.get(), INTENT_SATURATION, LCMS_USED_AS_OUTPUT) && !cmsIsIntentSupported(processcolorprofile.get(), INTENT_PERCEPTUAL, LCMS_USED_AS_OUTPUT)) {
             fprintf(stderr, "Error: ICC profile \"%s\" is not an output profile.\n", processcolorprofilename.c_str());
-            goto err05;
+            goto err1;
         }
         profilecolorspace = cmsGetColorSpace(processcolorprofile.get());
         if (profilecolorspace == cmsSigCmykData) {
@@ -324,7 +324,7 @@ int main(int argc, char *argv[])
                 processcolorformatspecified = true;
             } else if (processcolorformat != splashModeCMYK8) {
                 fprintf(stderr, "Error: Supplied ICC profile \"%s\" is a CMYK profile, but process color format is not CMYK8.\n", processcolorprofilename.c_str());
-                goto err05;
+                goto err1;
             }
         } else if (profilecolorspace == cmsSigGrayData) {
             if (!processcolorformatspecified) {
@@ -332,7 +332,7 @@ int main(int argc, char *argv[])
                 processcolorformatspecified = true;
             } else if (processcolorformat != splashModeMono8) {
                 fprintf(stderr, "Error: Supplied ICC profile \"%s\" is a monochrome profile, but process color format is not monochrome.\n", processcolorprofilename.c_str());
-                goto err05;
+                goto err1;
             }
         } else if (profilecolorspace == cmsSigRgbData) {
             if (!processcolorformatspecified) {
@@ -340,7 +340,7 @@ int main(int argc, char *argv[])
                 processcolorformatspecified = true;
             } else if (processcolorformat != splashModeRGB8) {
                 fprintf(stderr, "Error: Supplied ICC profile \"%s\" is a RGB profile, but process color format is not RGB.\n", processcolorprofilename.c_str());
-                goto err05;
+                goto err1;
             }
         }
     }
@@ -349,10 +349,10 @@ int main(int argc, char *argv[])
     if (processcolorformatspecified) {
         if (level1 && processcolorformat != splashModeMono8) {
             fprintf(stderr, "Error: Setting -level1 requires -processcolorformat MONO8");
-            goto err05;
+            goto err1;
         } else if ((level1Sep || level2Sep || level3Sep || overprint) && processcolorformat != splashModeCMYK8) {
             fprintf(stderr, "Error: Setting -level1sep/-level2sep/-level3sep/-overprint requires -processcolorformat CMYK8");
-            goto err05;
+            goto err1;
         }
     }
 #endif
@@ -361,19 +361,19 @@ int main(int argc, char *argv[])
     if (!defaultgrayprofilename.toStr().empty()) {
         defaultgrayprofile = make_GfxLCMSProfilePtr(cmsOpenProfileFromFile(defaultgrayprofilename.c_str(), "r"));
         if (!checkICCProfile(defaultgrayprofile, defaultgrayprofilename.c_str(), LCMS_USED_AS_INPUT, cmsSigGrayData)) {
-            goto err05;
+            goto err1;
         }
     }
     if (!defaultrgbprofilename.toStr().empty()) {
         defaultrgbprofile = make_GfxLCMSProfilePtr(cmsOpenProfileFromFile(defaultrgbprofilename.c_str(), "r"));
         if (!checkICCProfile(defaultrgbprofile, defaultrgbprofilename.c_str(), LCMS_USED_AS_INPUT, cmsSigRgbData)) {
-            goto err05;
+            goto err1;
         }
     }
     if (!defaultcmykprofilename.toStr().empty()) {
         defaultcmykprofile = make_GfxLCMSProfilePtr(cmsOpenProfileFromFile(defaultcmykprofilename.c_str(), "r"));
         if (!checkICCProfile(defaultcmykprofile, defaultcmykprofilename.c_str(), LCMS_USED_AS_INPUT, cmsSigCmykData)) {
-            goto err05;
+            goto err1;
         }
     }
 #endif
@@ -455,7 +455,7 @@ int main(int argc, char *argv[])
     }
 
     // write PostScript file
-    psOut = new PSOutputDev(psFileName->c_str(), doc, nullptr, pages, mode, paperWidth, paperHeight, noCrop, duplex, /*imgLLXA*/ 0, /*imgLLYA*/ 0,
+    psOut = new PSOutputDev(psFileName->c_str(), doc.get(), nullptr, pages, mode, paperWidth, paperHeight, noCrop, duplex, /*imgLLXA*/ 0, /*imgLLYA*/ 0,
                             /*imgURXA*/ 0, /*imgURYA*/ 0, psRasterizeWhenNeeded, /*manualCtrlA*/ false, /*customCodeCbkA*/ nullptr, /*customCodeCbkDataA*/ nullptr, level);
     if (noCenter) {
         psOut->setPSCenter(false);
@@ -527,8 +527,6 @@ int main(int argc, char *argv[])
 err2:
     delete psFileName;
 err1:
-    delete doc;
-err05:
     delete fileName;
 err0:
 
diff --git a/utils/pdftotext.cc b/utils/pdftotext.cc
index eb75b9e7..71ebcc29 100644
--- a/utils/pdftotext.cc
+++ b/utils/pdftotext.cc
@@ -30,7 +30,7 @@
 // Copyright (C) 2018 Adam Reichold <adam.reichold at t-online.de>
 // Copyright (C) 2018 Sanchit Anand <sanxchit at gmail.com>
 // Copyright (C) 2019 Dan Shea <dan.shea at logical-innovations.com>
-// Copyright (C) 2019 Oliver Sander <oliver.sander at tu-dresden.de>
+// Copyright (C) 2019, 2021 Oliver Sander <oliver.sander at tu-dresden.de>
 // Copyright (C) 2021 William Bader <williambader at hotmail.com>
 //
 // To see a description of the changes please see the Changelog file that
@@ -155,7 +155,7 @@ static std::string myXmlTokenReplace(const char *inString)
 
 int main(int argc, char *argv[])
 {
-    PDFDoc *doc;
+    std::unique_ptr<PDFDoc> doc;
     GooString *fileName;
     GooString *textFileName;
     GooString *ownerPW, *userPW;
@@ -346,9 +346,9 @@ int main(int argc, char *argv[])
                 textOut->setTextPageBreaks(false);
             }
             if (bboxLayout) {
-                printDocBBox(f, doc, textOut, firstPage, lastPage);
+                printDocBBox(f, doc.get(), textOut, firstPage, lastPage);
             } else {
-                printWordBBox(f, doc, textOut, firstPage, lastPage);
+                printWordBBox(f, doc.get(), textOut, firstPage, lastPage);
             }
         }
         if (f != stdout) {
@@ -404,7 +404,6 @@ int main(int argc, char *argv[])
 err3:
     delete textFileName;
 err2:
-    delete doc;
     delete fileName;
 err1:
 err0:


More information about the poppler mailing list