[poppler] glib/poppler-document.cc poppler/PDFDoc.cc poppler/PDFDoc.h qt5/src qt6/src

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Wed Dec 8 00:21:00 UTC 2021


 glib/poppler-document.cc    |   68 ++++++++++++++------------------------------
 poppler/PDFDoc.cc           |   31 +++++++-------------
 poppler/PDFDoc.h            |   21 ++++++-------
 qt5/src/poppler-document.cc |   40 ++++++++++++-------------
 qt6/src/poppler-document.cc |   40 ++++++++++++-------------
 5 files changed, 84 insertions(+), 116 deletions(-)

New commits:
commit c99378fe823256383810941123c1e1478369340a
Author: Albert Astals Cid <aacid at kde.org>
Date:   Wed Dec 8 01:07:14 2021 +0100

    PDFDoc::getDocInfoStringEntry return a unique_ptr
    
    Makes clear that the ownership is transferred

diff --git a/glib/poppler-document.cc b/glib/poppler-document.cc
index 7a000398..36c52be9 100644
--- a/glib/poppler-document.cc
+++ b/glib/poppler-document.cc
@@ -1273,11 +1273,8 @@ gchar *poppler_document_get_title(PopplerDocument *document)
 {
     g_return_val_if_fail(POPPLER_IS_DOCUMENT(document), NULL);
 
-    GooString *goo_title = document->doc->getDocInfoTitle();
-    gchar *utf8 = _poppler_goo_string_to_utf8(goo_title);
-    delete goo_title;
-
-    return utf8;
+    const std::unique_ptr<GooString> goo_title = document->doc->getDocInfoTitle();
+    return _poppler_goo_string_to_utf8(goo_title.get());
 }
 
 /**
@@ -1320,11 +1317,8 @@ gchar *poppler_document_get_author(PopplerDocument *document)
 {
     g_return_val_if_fail(POPPLER_IS_DOCUMENT(document), NULL);
 
-    GooString *goo_author = document->doc->getDocInfoAuthor();
-    gchar *utf8 = _poppler_goo_string_to_utf8(goo_author);
-    delete goo_author;
-
-    return utf8;
+    const std::unique_ptr<GooString> goo_author = document->doc->getDocInfoAuthor();
+    return _poppler_goo_string_to_utf8(goo_author.get());
 }
 
 /**
@@ -1367,11 +1361,8 @@ gchar *poppler_document_get_subject(PopplerDocument *document)
 {
     g_return_val_if_fail(POPPLER_IS_DOCUMENT(document), NULL);
 
-    GooString *goo_subject = document->doc->getDocInfoSubject();
-    gchar *utf8 = _poppler_goo_string_to_utf8(goo_subject);
-    delete goo_subject;
-
-    return utf8;
+    const std::unique_ptr<GooString> goo_subject = document->doc->getDocInfoSubject();
+    return _poppler_goo_string_to_utf8(goo_subject.get());
 }
 
 /**
@@ -1414,11 +1405,8 @@ gchar *poppler_document_get_keywords(PopplerDocument *document)
 {
     g_return_val_if_fail(POPPLER_IS_DOCUMENT(document), NULL);
 
-    GooString *goo_keywords = document->doc->getDocInfoKeywords();
-    gchar *utf8 = _poppler_goo_string_to_utf8(goo_keywords);
-    delete goo_keywords;
-
-    return utf8;
+    const std::unique_ptr<GooString> goo_keywords = document->doc->getDocInfoKeywords();
+    return _poppler_goo_string_to_utf8(goo_keywords.get());
 }
 
 /**
@@ -1463,11 +1451,8 @@ gchar *poppler_document_get_creator(PopplerDocument *document)
 {
     g_return_val_if_fail(POPPLER_IS_DOCUMENT(document), NULL);
 
-    GooString *goo_creator = document->doc->getDocInfoCreator();
-    gchar *utf8 = _poppler_goo_string_to_utf8(goo_creator);
-    delete goo_creator;
-
-    return utf8;
+    const std::unique_ptr<GooString> goo_creator = document->doc->getDocInfoCreator();
+    return _poppler_goo_string_to_utf8(goo_creator.get());
 }
 
 /**
@@ -1512,11 +1497,8 @@ gchar *poppler_document_get_producer(PopplerDocument *document)
 {
     g_return_val_if_fail(POPPLER_IS_DOCUMENT(document), NULL);
 
-    GooString *goo_producer = document->doc->getDocInfoProducer();
-    gchar *utf8 = _poppler_goo_string_to_utf8(goo_producer);
-    delete goo_producer;
-
-    return utf8;
+    const std::unique_ptr<GooString> goo_producer = document->doc->getDocInfoProducer();
+    return _poppler_goo_string_to_utf8(goo_producer.get());
 }
 
 /**
@@ -1558,14 +1540,13 @@ time_t poppler_document_get_creation_date(PopplerDocument *document)
 {
     g_return_val_if_fail(POPPLER_IS_DOCUMENT(document), (time_t)-1);
 
-    GooString *str = document->doc->getDocInfoCreatDate();
-    if (str == nullptr) {
+    const std::unique_ptr<GooString> str = document->doc->getDocInfoCreatDate();
+    if (!str) {
         return (time_t)-1;
     }
 
     time_t date;
-    gboolean success = _poppler_convert_pdf_date_to_gtime(str, &date);
-    delete str;
+    gboolean success = _poppler_convert_pdf_date_to_gtime(str.get(), &date);
 
     return (success) ? date : (time_t)-1;
 }
@@ -1646,14 +1627,13 @@ time_t poppler_document_get_modification_date(PopplerDocument *document)
 {
     g_return_val_if_fail(POPPLER_IS_DOCUMENT(document), (time_t)-1);
 
-    GooString *str = document->doc->getDocInfoModDate();
-    if (str == nullptr) {
+    const std::unique_ptr<GooString> str = document->doc->getDocInfoModDate();
+    if (!str) {
         return (time_t)-1;
     }
 
     time_t date;
-    gboolean success = _poppler_convert_pdf_date_to_gtime(str, &date);
-    delete str;
+    gboolean success = _poppler_convert_pdf_date_to_gtime(str.get(), &date);
 
     return (success) ? date : (time_t)-1;
 }
@@ -2014,7 +1994,7 @@ gchar *poppler_document_get_pdf_subtype_string(PopplerDocument *document)
 {
     g_return_val_if_fail(POPPLER_IS_DOCUMENT(document), NULL);
 
-    GooString *infostring;
+    std::unique_ptr<GooString> infostring;
 
     switch (document->doc->getPDFSubtype()) {
     case subtypePDFA:
@@ -2034,14 +2014,12 @@ gchar *poppler_document_get_pdf_subtype_string(PopplerDocument *document)
         break;
     case subtypeNone:
     case subtypeNull:
-    default:
-        infostring = nullptr;
+    default: {
+        /* nothing */
+    }
     }
 
-    gchar *utf8 = _poppler_goo_string_to_utf8(infostring);
-    delete infostring;
-
-    return utf8;
+    return _poppler_goo_string_to_utf8(infostring.get());
 }
 
 /**
diff --git a/poppler/PDFDoc.cc b/poppler/PDFDoc.cc
index 270662a7..dc1f01b9 100644
--- a/poppler/PDFDoc.cc
+++ b/poppler/PDFDoc.cc
@@ -478,11 +478,10 @@ bool PDFDoc::checkEncryption(const GooString *ownerPassword, const GooString *us
     return ret;
 }
 
-static PDFSubtypePart pdfPartFromString(PDFSubtype subtype, GooString *pdfSubtypeVersion)
+static PDFSubtypePart pdfPartFromString(PDFSubtype subtype, const std::string &pdfsubver)
 {
     const std::regex regex("PDF/(?:A|X|VT|E|UA)-([[:digit:]])(?:[[:alpha:]]{1,2})?:?([[:digit:]]{4})?");
     std::smatch match;
-    const std::string &pdfsubver = pdfSubtypeVersion->toStr();
     PDFSubtypePart subtypePart = subtypePartNone;
 
     if (std::regex_search(pdfsubver, match, regex)) {
@@ -538,11 +537,10 @@ static PDFSubtypePart pdfPartFromString(PDFSubtype subtype, GooString *pdfSubtyp
     return subtypePart;
 }
 
-static PDFSubtypeConformance pdfConformanceFromString(GooString *pdfSubtypeVersion)
+static PDFSubtypeConformance pdfConformanceFromString(const std::string &pdfsubver)
 {
     const std::regex regex("PDF/(?:A|X|VT|E|UA)-[[:digit:]]([[:alpha:]]+)");
     std::smatch match;
-    const std::string &pdfsubver = pdfSubtypeVersion->toStr();
     PDFSubtypeConformance pdfConf = subtypeConfNone;
 
     // match contains the PDF conformance (A, B, G, N, P, PG or U)
@@ -579,7 +577,7 @@ void PDFDoc::extractPDFSubtype()
     pdfPart = subtypePartNull;
     pdfConformance = subtypeConfNull;
 
-    GooString *pdfSubtypeVersion = nullptr;
+    std::unique_ptr<GooString> pdfSubtypeVersion;
     // Find PDF InfoDict subtype key if any
     if ((pdfSubtypeVersion = getDocInfoStringEntry("GTS_PDFA1Version"))) {
         pdfSubtype = subtypePDFA;
@@ -599,12 +597,10 @@ void PDFDoc::extractPDFSubtype()
     }
 
     // Extract part from version string
-    pdfPart = pdfPartFromString(pdfSubtype, pdfSubtypeVersion);
+    pdfPart = pdfPartFromString(pdfSubtype, pdfSubtypeVersion->toStr());
 
     // Extract conformance from version string
-    pdfConformance = pdfConformanceFromString(pdfSubtypeVersion);
-
-    delete pdfSubtypeVersion;
+    pdfConformance = pdfConformanceFromString(pdfSubtypeVersion->toStr());
 }
 
 static void addSignatureFieldsToVector(FormField *ff, std::vector<FormFieldSignature *> &res)
@@ -781,24 +777,19 @@ void PDFDoc::setDocInfoStringEntry(const char *key, GooString *value)
     }
 }
 
-GooString *PDFDoc::getDocInfoStringEntry(const char *key)
+std::unique_ptr<GooString> PDFDoc::getDocInfoStringEntry(const char *key)
 {
     Object infoObj = getDocInfo();
     if (!infoObj.isDict()) {
-        return nullptr;
+        return {};
     }
 
-    Object entryObj = infoObj.dictLookup(key);
-
-    GooString *result;
-
-    if (entryObj.isString()) {
-        result = entryObj.getString()->copy();
-    } else {
-        result = nullptr;
+    const Object entryObj = infoObj.dictLookup(key);
+    if (!entryObj.isString()) {
+        return {};
     }
 
-    return result;
+    return std::unique_ptr<GooString>(entryObj.getString()->copy());
 }
 
 static bool get_id(const GooString *encodedidstring, GooString *id)
diff --git a/poppler/PDFDoc.h b/poppler/PDFDoc.h
index b0de8549..23f9cd28 100644
--- a/poppler/PDFDoc.h
+++ b/poppler/PDFDoc.h
@@ -267,17 +267,16 @@ public:
 
     // Get document's properties from document's Info dictionary.
     // Returns nullptr on fail.
-    // Returned GooStrings should be freed by the caller.
-    GooString *getDocInfoStringEntry(const char *key);
-
-    GooString *getDocInfoTitle() { return getDocInfoStringEntry("Title"); }
-    GooString *getDocInfoAuthor() { return getDocInfoStringEntry("Author"); }
-    GooString *getDocInfoSubject() { return getDocInfoStringEntry("Subject"); }
-    GooString *getDocInfoKeywords() { return getDocInfoStringEntry("Keywords"); }
-    GooString *getDocInfoCreator() { return getDocInfoStringEntry("Creator"); }
-    GooString *getDocInfoProducer() { return getDocInfoStringEntry("Producer"); }
-    GooString *getDocInfoCreatDate() { return getDocInfoStringEntry("CreationDate"); }
-    GooString *getDocInfoModDate() { return getDocInfoStringEntry("ModDate"); }
+    std::unique_ptr<GooString> getDocInfoStringEntry(const char *key);
+
+    std::unique_ptr<GooString> getDocInfoTitle() { return getDocInfoStringEntry("Title"); }
+    std::unique_ptr<GooString> getDocInfoAuthor() { return getDocInfoStringEntry("Author"); }
+    std::unique_ptr<GooString> getDocInfoSubject() { return getDocInfoStringEntry("Subject"); }
+    std::unique_ptr<GooString> getDocInfoKeywords() { return getDocInfoStringEntry("Keywords"); }
+    std::unique_ptr<GooString> getDocInfoCreator() { return getDocInfoStringEntry("Creator"); }
+    std::unique_ptr<GooString> getDocInfoProducer() { return getDocInfoStringEntry("Producer"); }
+    std::unique_ptr<GooString> getDocInfoCreatDate() { return getDocInfoStringEntry("CreationDate"); }
+    std::unique_ptr<GooString> getDocInfoModDate() { return getDocInfoStringEntry("ModDate"); }
 
     // Return the PDF subtype, part, and conformance
     PDFSubtype getPDFSubtype() const { return pdfSubtype; }
diff --git a/qt5/src/poppler-document.cc b/qt5/src/poppler-document.cc
index 4690f313..be4d7119 100644
--- a/qt5/src/poppler-document.cc
+++ b/qt5/src/poppler-document.cc
@@ -261,8 +261,8 @@ QString Document::info(const QString &type) const
         return QString();
     }
 
-    QScopedPointer<GooString> goo(m_doc->doc->getDocInfoStringEntry(type.toLatin1().constData()));
-    return UnicodeParsedString(goo.data());
+    std::unique_ptr<GooString> goo(m_doc->doc->getDocInfoStringEntry(type.toLatin1().constData()));
+    return UnicodeParsedString(goo.get());
 }
 
 bool Document::setInfo(const QString &key, const QString &val)
@@ -282,8 +282,8 @@ QString Document::title() const
         return QString();
     }
 
-    QScopedPointer<GooString> goo(m_doc->doc->getDocInfoTitle());
-    return UnicodeParsedString(goo.data());
+    std::unique_ptr<GooString> goo(m_doc->doc->getDocInfoTitle());
+    return UnicodeParsedString(goo.get());
 }
 
 bool Document::setTitle(const QString &val)
@@ -302,8 +302,8 @@ QString Document::author() const
         return QString();
     }
 
-    QScopedPointer<GooString> goo(m_doc->doc->getDocInfoAuthor());
-    return UnicodeParsedString(goo.data());
+    std::unique_ptr<GooString> goo(m_doc->doc->getDocInfoAuthor());
+    return UnicodeParsedString(goo.get());
 }
 
 bool Document::setAuthor(const QString &val)
@@ -322,8 +322,8 @@ QString Document::subject() const
         return QString();
     }
 
-    QScopedPointer<GooString> goo(m_doc->doc->getDocInfoSubject());
-    return UnicodeParsedString(goo.data());
+    std::unique_ptr<GooString> goo(m_doc->doc->getDocInfoSubject());
+    return UnicodeParsedString(goo.get());
 }
 
 bool Document::setSubject(const QString &val)
@@ -342,8 +342,8 @@ QString Document::keywords() const
         return QString();
     }
 
-    QScopedPointer<GooString> goo(m_doc->doc->getDocInfoKeywords());
-    return UnicodeParsedString(goo.data());
+    std::unique_ptr<GooString> goo(m_doc->doc->getDocInfoKeywords());
+    return UnicodeParsedString(goo.get());
 }
 
 bool Document::setKeywords(const QString &val)
@@ -362,8 +362,8 @@ QString Document::creator() const
         return QString();
     }
 
-    QScopedPointer<GooString> goo(m_doc->doc->getDocInfoCreator());
-    return UnicodeParsedString(goo.data());
+    std::unique_ptr<GooString> goo(m_doc->doc->getDocInfoCreator());
+    return UnicodeParsedString(goo.get());
 }
 
 bool Document::setCreator(const QString &val)
@@ -382,8 +382,8 @@ QString Document::producer() const
         return QString();
     }
 
-    QScopedPointer<GooString> goo(m_doc->doc->getDocInfoProducer());
-    return UnicodeParsedString(goo.data());
+    std::unique_ptr<GooString> goo(m_doc->doc->getDocInfoProducer());
+    return UnicodeParsedString(goo.get());
 }
 
 bool Document::setProducer(const QString &val)
@@ -436,8 +436,8 @@ QDateTime Document::date(const QString &type) const
         return QDateTime();
     }
 
-    QScopedPointer<GooString> goo(m_doc->doc->getDocInfoStringEntry(type.toLatin1().constData()));
-    QString str = UnicodeParsedString(goo.data());
+    std::unique_ptr<GooString> goo(m_doc->doc->getDocInfoStringEntry(type.toLatin1().constData()));
+    QString str = UnicodeParsedString(goo.get());
     return Poppler::convertDate(str.toLatin1().constData());
 }
 
@@ -457,8 +457,8 @@ QDateTime Document::creationDate() const
         return QDateTime();
     }
 
-    QScopedPointer<GooString> goo(m_doc->doc->getDocInfoCreatDate());
-    QString str = UnicodeParsedString(goo.data());
+    std::unique_ptr<GooString> goo(m_doc->doc->getDocInfoCreatDate());
+    QString str = UnicodeParsedString(goo.get());
     return Poppler::convertDate(str.toLatin1().constData());
 }
 
@@ -478,8 +478,8 @@ QDateTime Document::modificationDate() const
         return QDateTime();
     }
 
-    QScopedPointer<GooString> goo(m_doc->doc->getDocInfoModDate());
-    QString str = UnicodeParsedString(goo.data());
+    std::unique_ptr<GooString> goo(m_doc->doc->getDocInfoModDate());
+    QString str = UnicodeParsedString(goo.get());
     return Poppler::convertDate(str.toLatin1().constData());
 }
 
diff --git a/qt6/src/poppler-document.cc b/qt6/src/poppler-document.cc
index ac469637..41d3452f 100644
--- a/qt6/src/poppler-document.cc
+++ b/qt6/src/poppler-document.cc
@@ -261,8 +261,8 @@ QString Document::info(const QString &type) const
         return QString();
     }
 
-    QScopedPointer<GooString> goo(m_doc->doc->getDocInfoStringEntry(type.toLatin1().constData()));
-    return UnicodeParsedString(goo.data());
+    std::unique_ptr<GooString> goo(m_doc->doc->getDocInfoStringEntry(type.toLatin1().constData()));
+    return UnicodeParsedString(goo.get());
 }
 
 bool Document::setInfo(const QString &key, const QString &val)
@@ -282,8 +282,8 @@ QString Document::title() const
         return QString();
     }
 
-    QScopedPointer<GooString> goo(m_doc->doc->getDocInfoTitle());
-    return UnicodeParsedString(goo.data());
+    std::unique_ptr<GooString> goo(m_doc->doc->getDocInfoTitle());
+    return UnicodeParsedString(goo.get());
 }
 
 bool Document::setTitle(const QString &val)
@@ -302,8 +302,8 @@ QString Document::author() const
         return QString();
     }
 
-    QScopedPointer<GooString> goo(m_doc->doc->getDocInfoAuthor());
-    return UnicodeParsedString(goo.data());
+    std::unique_ptr<GooString> goo(m_doc->doc->getDocInfoAuthor());
+    return UnicodeParsedString(goo.get());
 }
 
 bool Document::setAuthor(const QString &val)
@@ -322,8 +322,8 @@ QString Document::subject() const
         return QString();
     }
 
-    QScopedPointer<GooString> goo(m_doc->doc->getDocInfoSubject());
-    return UnicodeParsedString(goo.data());
+    std::unique_ptr<GooString> goo(m_doc->doc->getDocInfoSubject());
+    return UnicodeParsedString(goo.get());
 }
 
 bool Document::setSubject(const QString &val)
@@ -342,8 +342,8 @@ QString Document::keywords() const
         return QString();
     }
 
-    QScopedPointer<GooString> goo(m_doc->doc->getDocInfoKeywords());
-    return UnicodeParsedString(goo.data());
+    std::unique_ptr<GooString> goo(m_doc->doc->getDocInfoKeywords());
+    return UnicodeParsedString(goo.get());
 }
 
 bool Document::setKeywords(const QString &val)
@@ -362,8 +362,8 @@ QString Document::creator() const
         return QString();
     }
 
-    QScopedPointer<GooString> goo(m_doc->doc->getDocInfoCreator());
-    return UnicodeParsedString(goo.data());
+    std::unique_ptr<GooString> goo(m_doc->doc->getDocInfoCreator());
+    return UnicodeParsedString(goo.get());
 }
 
 bool Document::setCreator(const QString &val)
@@ -382,8 +382,8 @@ QString Document::producer() const
         return QString();
     }
 
-    QScopedPointer<GooString> goo(m_doc->doc->getDocInfoProducer());
-    return UnicodeParsedString(goo.data());
+    std::unique_ptr<GooString> goo(m_doc->doc->getDocInfoProducer());
+    return UnicodeParsedString(goo.get());
 }
 
 bool Document::setProducer(const QString &val)
@@ -436,8 +436,8 @@ QDateTime Document::date(const QString &type) const
         return QDateTime();
     }
 
-    QScopedPointer<GooString> goo(m_doc->doc->getDocInfoStringEntry(type.toLatin1().constData()));
-    QString str = UnicodeParsedString(goo.data());
+    std::unique_ptr<GooString> goo(m_doc->doc->getDocInfoStringEntry(type.toLatin1().constData()));
+    QString str = UnicodeParsedString(goo.get());
     return Poppler::convertDate(str.toLatin1().constData());
 }
 
@@ -457,8 +457,8 @@ QDateTime Document::creationDate() const
         return QDateTime();
     }
 
-    QScopedPointer<GooString> goo(m_doc->doc->getDocInfoCreatDate());
-    QString str = UnicodeParsedString(goo.data());
+    std::unique_ptr<GooString> goo(m_doc->doc->getDocInfoCreatDate());
+    QString str = UnicodeParsedString(goo.get());
     return Poppler::convertDate(str.toLatin1().constData());
 }
 
@@ -478,8 +478,8 @@ QDateTime Document::modificationDate() const
         return QDateTime();
     }
 
-    QScopedPointer<GooString> goo(m_doc->doc->getDocInfoModDate());
-    QString str = UnicodeParsedString(goo.data());
+    std::unique_ptr<GooString> goo(m_doc->doc->getDocInfoModDate());
+    QString str = UnicodeParsedString(goo.get());
     return Poppler::convertDate(str.toLatin1().constData());
 }
 


More information about the poppler mailing list