[poppler] cpp/poppler-document.cpp glib/poppler-document.cc poppler/Form.cc poppler/PDFDoc.cc poppler/PDFDoc.h qt5/tests qt6/tests test/pdf-fullrewrite.cc utils/pdfattach.cc utils/pdfseparate.cc

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Tue Feb 15 16:04:21 UTC 2022


 cpp/poppler-document.cpp             |    4 ++--
 glib/poppler-document.cc             |    6 ++----
 poppler/Form.cc                      |    2 +-
 poppler/PDFDoc.cc                    |   20 ++++++++++----------
 poppler/PDFDoc.h                     |    6 +++---
 qt5/tests/check_internal_outline.cpp |   16 ++++++++--------
 qt6/tests/check_internal_outline.cpp |   16 ++++++++--------
 test/pdf-fullrewrite.cc              |    3 ++-
 utils/pdfattach.cc                   |    4 ++--
 utils/pdfseparate.cc                 |    7 ++-----
 10 files changed, 40 insertions(+), 44 deletions(-)

New commits:
commit 47256c3c2905ade19f21224cb48ff5bb7de43a03
Author: Albert Astals Cid <aacid at kde.org>
Date:   Tue Feb 15 16:44:20 2022 +0100

    Change PDFDoc::save from pointer to reference
    
    Makes it clear it's not going to be destructed and that it can't be null

diff --git a/cpp/poppler-document.cpp b/cpp/poppler-document.cpp
index c4f52277..2dd47921 100644
--- a/cpp/poppler-document.cpp
+++ b/cpp/poppler-document.cpp
@@ -1025,7 +1025,7 @@ bool document::save(const std::string &file_name) const
     }
 
     GooString fname(file_name.c_str());
-    return d->doc->saveAs(&fname) == errNone;
+    return d->doc->saveAs(fname) == errNone;
 }
 
 /**
@@ -1040,7 +1040,7 @@ bool document::save_a_copy(const std::string &file_name) const
     }
 
     GooString fname(file_name.c_str());
-    return d->doc->saveWithoutChangesAs(&fname) == errNone;
+    return d->doc->saveWithoutChangesAs(fname) == errNone;
 }
 
 /**
diff --git a/glib/poppler-document.cc b/glib/poppler-document.cc
index b6b455e2..29172833 100644
--- a/glib/poppler-document.cc
+++ b/glib/poppler-document.cc
@@ -582,13 +582,12 @@ gboolean poppler_document_save(PopplerDocument *document, const char *uri, GErro
 
     filename = g_filename_from_uri(uri, nullptr, error);
     if (filename != nullptr) {
-        GooString *fname = new GooString(filename);
+        GooString fname(filename);
         int err_code;
         g_free(filename);
 
         err_code = document->doc->saveAs(fname);
         retval = handle_save_error(err_code, error);
-        delete fname;
     }
 
     return retval;
@@ -617,13 +616,12 @@ gboolean poppler_document_save_a_copy(PopplerDocument *document, const char *uri
 
     filename = g_filename_from_uri(uri, nullptr, error);
     if (filename != nullptr) {
-        GooString *fname = new GooString(filename);
+        GooString fname(filename);
         int err_code;
         g_free(filename);
 
         err_code = document->doc->saveWithoutChangesAs(fname);
         retval = handle_save_error(err_code, error);
-        delete fname;
     }
 
     return retval;
diff --git a/poppler/Form.cc b/poppler/Form.cc
index c3624240..d0f4bcc8 100644
--- a/poppler/Form.cc
+++ b/poppler/Form.cc
@@ -605,7 +605,7 @@ bool FormWidgetSignature::signDocument(const char *saveFilename, const char *cer
 
     // Incremental save to avoid breaking any existing signatures
     GooString *fname = new GooString(saveFilename);
-    if (doc->saveAs(fname, writeForceIncremental) != errNone) {
+    if (doc->saveAs(*fname, writeForceIncremental) != errNone) {
         fprintf(stderr, "signDocument: error saving to file \"%s\"\n", saveFilename);
         delete fname;
         return false;
diff --git a/poppler/PDFDoc.cc b/poppler/PDFDoc.cc
index 3b382a5c..44163450 100644
--- a/poppler/PDFDoc.cc
+++ b/poppler/PDFDoc.cc
@@ -802,7 +802,7 @@ Hints *PDFDoc::getHints()
     return hints;
 }
 
-int PDFDoc::savePageAs(const GooString *name, int pageNo)
+int PDFDoc::savePageAs(const GooString &name, int pageNo)
 {
     FILE *f;
     OutStream *outStr;
@@ -834,8 +834,8 @@ int PDFDoc::savePageAs(const GooString *name, int pageNo)
     Ref *refPage = getCatalog()->getPageRef(pageNo);
     Object page = getXRef()->fetch(*refPage);
 
-    if (!(f = openFile(name->c_str(), "wb"))) {
-        error(errIO, -1, "Couldn't open file '{0:t}'", name);
+    if (!(f = openFile(name.c_str(), "wb"))) {
+        error(errIO, -1, "Couldn't open file '{0:t}'", &name);
         return errOpenFile;
     }
     outStr = new FileOutStream(f, 0);
@@ -946,7 +946,7 @@ int PDFDoc::savePageAs(const GooString *name, int pageNo)
     Ref ref;
     ref.num = rootNum;
     ref.gen = 0;
-    Object trailerDict = createTrailerDict(rootNum + 3, false, 0, &ref, getXRef(), name->c_str(), uxrefOffset);
+    Object trailerDict = createTrailerDict(rootNum + 3, false, 0, &ref, getXRef(), name.c_str(), uxrefOffset);
     writeXRefTableTrailer(std::move(trailerDict), yRef, false /* do not write unnecessary entries */, uxrefOffset, outStr, getXRef());
 
     outStr->close();
@@ -958,14 +958,14 @@ int PDFDoc::savePageAs(const GooString *name, int pageNo)
     return errNone;
 }
 
-int PDFDoc::saveAs(const GooString *name, PDFWriteMode mode)
+int PDFDoc::saveAs(const GooString &name, PDFWriteMode mode)
 {
     FILE *f;
     OutStream *outStr;
     int res;
 
-    if (!(f = openFile(name->c_str(), "wb"))) {
-        error(errIO, -1, "Couldn't open file '{0:t}'", name);
+    if (!(f = openFile(name.c_str(), "wb"))) {
+        error(errIO, -1, "Couldn't open file '{0:t}'", &name);
         return errOpenFile;
     }
     outStr = new FileOutStream(f, 0);
@@ -992,14 +992,14 @@ int PDFDoc::saveAs(OutStream *outStr, PDFWriteMode mode)
     return errNone;
 }
 
-int PDFDoc::saveWithoutChangesAs(const GooString *name)
+int PDFDoc::saveWithoutChangesAs(const GooString &name)
 {
     FILE *f;
     OutStream *outStr;
     int res;
 
-    if (!(f = openFile(name->c_str(), "wb"))) {
-        error(errIO, -1, "Couldn't open file '{0:t}'", name);
+    if (!(f = openFile(name.c_str(), "wb"))) {
+        error(errIO, -1, "Couldn't open file '{0:t}'", &name);
         return errOpenFile;
     }
 
diff --git a/poppler/PDFDoc.h b/poppler/PDFDoc.h
index d54bf7d9..3128674a 100644
--- a/poppler/PDFDoc.h
+++ b/poppler/PDFDoc.h
@@ -302,13 +302,13 @@ public:
     bool getID(GooString *permanent_id, GooString *update_id) const;
 
     // Save one page with another name.
-    int savePageAs(const GooString *name, int pageNo);
+    int savePageAs(const GooString &name, int pageNo);
     // Save this file with another name.
-    int saveAs(const GooString *name, PDFWriteMode mode = writeStandard);
+    int saveAs(const GooString &name, PDFWriteMode mode = writeStandard);
     // Save this file in the given output stream.
     int saveAs(OutStream *outStr, PDFWriteMode mode = writeStandard);
     // Save this file with another name without saving changes
-    int saveWithoutChangesAs(const GooString *name);
+    int saveWithoutChangesAs(const GooString &name);
     // Save this file in the given output stream without saving changes
     int saveWithoutChangesAs(OutStream *outStr);
 
diff --git a/qt5/tests/check_internal_outline.cpp b/qt5/tests/check_internal_outline.cpp
index 02342de5..c12b6041 100644
--- a/qt5/tests/check_internal_outline.cpp
+++ b/qt5/tests/check_internal_outline.cpp
@@ -40,7 +40,7 @@ void TestInternalOutline::testCreateOutline()
     outlineItems = outline->getItems();
     // no items will result in a nullptr rather than a 0 length list
     QVERIFY(outlineItems == nullptr);
-    doc->saveAs(&gooTempFileName);
+    doc->saveAs(gooTempFileName);
 
     /******************************************************/
 
@@ -87,7 +87,7 @@ void TestInternalOutline::testSetOutline()
             { { "1", 1, { { "1.1", 1, {} }, { "1.2", 2, {} }, { "1.3", 3, { { "1.3.1", 1, {} }, { "1.3.2", 2, {} }, { "1.3.3", 3, {} }, { "1.3.4", 4, {} } } }, { "1.4", 4, {} } } }, { "2", 2, {} }, { "3", 3, {} }, { "4", 4, {} } });
     outlineItems = outline->getItems();
     QVERIFY(outlineItems != nullptr);
-    doc->saveAs(&gooTempFileName);
+    doc->saveAs(gooTempFileName);
     outline = nullptr;
 
     /******************************************************/
@@ -175,7 +175,7 @@ void TestInternalOutline::testInsertChild()
 
     // create an outline and save the file
     outline->setOutline({});
-    doc->saveAs(&gooTempFileName);
+    doc->saveAs(gooTempFileName);
     outline = nullptr;
 
     /******************************************************/
@@ -207,7 +207,7 @@ void TestInternalOutline::testInsertChild()
     outlineItems->at(1)->insertChild("2.3", 2, 2);
 
     // save the file
-    doc->saveAs(&gooTempFileName2);
+    doc->saveAs(gooTempFileName2);
     outline = nullptr;
 
     /******************************************************/
@@ -282,7 +282,7 @@ void TestInternalOutline::testRemoveChild()
                           { "4", 4, {} } });
     outlineItems = outline->getItems();
     QVERIFY(outlineItems != nullptr);
-    doc->saveAs(&gooTempFileName);
+    doc->saveAs(gooTempFileName);
     outline = nullptr;
 
     /******************************************************/
@@ -303,7 +303,7 @@ void TestInternalOutline::testRemoveChild()
     outline->getItems()->at(1)->removeChild(0);
 
     // save the file
-    doc->saveAs(&gooTempFileName2);
+    doc->saveAs(gooTempFileName2);
     outline = nullptr;
 
     /******************************************************/
@@ -376,7 +376,7 @@ void TestInternalOutline::testSetTitleAndSetPageDest()
                           { "4", 4, {} } });
     outlineItems = outline->getItems();
     QVERIFY(outlineItems != nullptr);
-    doc->saveAs(&gooTempFileName);
+    doc->saveAs(gooTempFileName);
 
     outline = nullptr;
 
@@ -407,7 +407,7 @@ void TestInternalOutline::testSetTitleAndSetPageDest()
     }
 
     // save the file
-    doc->saveAs(&gooTempFileName2);
+    doc->saveAs(gooTempFileName2);
     outline = nullptr;
     item = nullptr;
 
diff --git a/qt6/tests/check_internal_outline.cpp b/qt6/tests/check_internal_outline.cpp
index 02342de5..c12b6041 100644
--- a/qt6/tests/check_internal_outline.cpp
+++ b/qt6/tests/check_internal_outline.cpp
@@ -40,7 +40,7 @@ void TestInternalOutline::testCreateOutline()
     outlineItems = outline->getItems();
     // no items will result in a nullptr rather than a 0 length list
     QVERIFY(outlineItems == nullptr);
-    doc->saveAs(&gooTempFileName);
+    doc->saveAs(gooTempFileName);
 
     /******************************************************/
 
@@ -87,7 +87,7 @@ void TestInternalOutline::testSetOutline()
             { { "1", 1, { { "1.1", 1, {} }, { "1.2", 2, {} }, { "1.3", 3, { { "1.3.1", 1, {} }, { "1.3.2", 2, {} }, { "1.3.3", 3, {} }, { "1.3.4", 4, {} } } }, { "1.4", 4, {} } } }, { "2", 2, {} }, { "3", 3, {} }, { "4", 4, {} } });
     outlineItems = outline->getItems();
     QVERIFY(outlineItems != nullptr);
-    doc->saveAs(&gooTempFileName);
+    doc->saveAs(gooTempFileName);
     outline = nullptr;
 
     /******************************************************/
@@ -175,7 +175,7 @@ void TestInternalOutline::testInsertChild()
 
     // create an outline and save the file
     outline->setOutline({});
-    doc->saveAs(&gooTempFileName);
+    doc->saveAs(gooTempFileName);
     outline = nullptr;
 
     /******************************************************/
@@ -207,7 +207,7 @@ void TestInternalOutline::testInsertChild()
     outlineItems->at(1)->insertChild("2.3", 2, 2);
 
     // save the file
-    doc->saveAs(&gooTempFileName2);
+    doc->saveAs(gooTempFileName2);
     outline = nullptr;
 
     /******************************************************/
@@ -282,7 +282,7 @@ void TestInternalOutline::testRemoveChild()
                           { "4", 4, {} } });
     outlineItems = outline->getItems();
     QVERIFY(outlineItems != nullptr);
-    doc->saveAs(&gooTempFileName);
+    doc->saveAs(gooTempFileName);
     outline = nullptr;
 
     /******************************************************/
@@ -303,7 +303,7 @@ void TestInternalOutline::testRemoveChild()
     outline->getItems()->at(1)->removeChild(0);
 
     // save the file
-    doc->saveAs(&gooTempFileName2);
+    doc->saveAs(gooTempFileName2);
     outline = nullptr;
 
     /******************************************************/
@@ -376,7 +376,7 @@ void TestInternalOutline::testSetTitleAndSetPageDest()
                           { "4", 4, {} } });
     outlineItems = outline->getItems();
     QVERIFY(outlineItems != nullptr);
-    doc->saveAs(&gooTempFileName);
+    doc->saveAs(gooTempFileName);
 
     outline = nullptr;
 
@@ -407,7 +407,7 @@ void TestInternalOutline::testSetTitleAndSetPageDest()
     }
 
     // save the file
-    doc->saveAs(&gooTempFileName2);
+    doc->saveAs(gooTempFileName2);
     outline = nullptr;
     item = nullptr;
 
diff --git a/test/pdf-fullrewrite.cc b/test/pdf-fullrewrite.cc
index 968f9569..1afc7e16 100644
--- a/test/pdf-fullrewrite.cc
+++ b/test/pdf-fullrewrite.cc
@@ -4,6 +4,7 @@
 //
 // Copyright 2007 Julien Rebetez
 // Copyright 2012 Fabio D'Urso
+// Copyright 2022 Albert Astals Cid <aacid at kde.org>
 //
 //========================================================================
 
@@ -74,7 +75,7 @@ int main(int argc, char *argv[])
     }
 
     // save it back (in rewrite or incremental update mode)
-    if (doc->saveAs(outputName, forceIncremental ? writeForceIncremental : writeForceRewrite) != 0) {
+    if (doc->saveAs(*outputName, forceIncremental ? writeForceIncremental : writeForceRewrite) != 0) {
         fprintf(stderr, "Error saving document\n");
         res = 1;
         goto done;
diff --git a/utils/pdfattach.cc b/utils/pdfattach.cc
index d5b6f1b2..b839622f 100644
--- a/utils/pdfattach.cc
+++ b/utils/pdfattach.cc
@@ -4,7 +4,7 @@
 //
 // This file is licensed under the GPLv2 or later
 //
-// Copyright (C) 2019-2021 Albert Astals Cid <aacid at kde.org>
+// Copyright (C) 2019-2022 Albert Astals Cid <aacid at kde.org>
 // Copyright (C) 2019 Oliver Sander <oliver.sander at tu-dresden.de>
 //
 // To see a description of the changes please see the Changelog file that
@@ -95,7 +95,7 @@ int main(int argc, char *argv[])
     doc->getCatalog()->addEmbeddedFile(attachFile.get(), attachFileName);
 
     const GooString outputPdfFilePath(argv[3]);
-    const int saveResult = doc->saveAs(&outputPdfFilePath);
+    const int saveResult = doc->saveAs(outputPdfFilePath);
     if (saveResult != errNone) {
         fprintf(stderr, "Couldn't save the file properly.\n");
         return 5;
diff --git a/utils/pdfseparate.cc b/utils/pdfseparate.cc
index afe29cef..c0c84bb4 100644
--- a/utils/pdfseparate.cc
+++ b/utils/pdfseparate.cc
@@ -5,7 +5,7 @@
 // This file is licensed under the GPLv2 or later
 //
 // Copyright (C) 2011, 2012, 2015 Thomas Freitag <Thomas.Freitag at alfa.de>
-// Copyright (C) 2012-2014, 2017, 2018, 2021 Albert Astals Cid <aacid at kde.org>
+// Copyright (C) 2012-2014, 2017, 2018, 2021, 2022 Albert Astals Cid <aacid at kde.org>
 // Copyright (C) 2013, 2016 Pino Toscano <pino at kde.org>
 // Copyright (C) 2013 Daniel Kahn Gillmor <dkg at fifthhorseman.net>
 // Copyright (C) 2013 Suzuki Toshiya <mpsuzuki at hiroshima-u.ac.jp>
@@ -118,17 +118,14 @@ static bool extractPages(const char *srcFileName, const char *destFileName)
 
     for (int pageNo = firstPage; pageNo <= lastPage; pageNo++) {
         snprintf(pathName, sizeof(pathName) - 1, destFileName, pageNo);
-        GooString *gpageName = new GooString(pathName);
         PDFDoc *pagedoc = new PDFDoc(new GooString(srcFileName), nullptr, nullptr, nullptr);
-        int errCode = pagedoc->savePageAs(gpageName, pageNo);
+        int errCode = pagedoc->savePageAs(GooString(pathName), pageNo);
         if (errCode != errNone) {
-            delete gpageName;
             delete doc;
             delete pagedoc;
             return false;
         }
         delete pagedoc;
-        delete gpageName;
     }
     delete doc;
     return true;


More information about the poppler mailing list