aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2011-08-10 19:33:58 +0200
committerJay Berkenbilt <ejb@ql.org>2011-08-10 19:33:58 +0200
commit655c55f84830190f9fa4777c615b8a622254648a (patch)
tree85b7c79d5a30f08bcd1f40f466fae74b3e3c22ac
parentd9ec2eb0f63f35d61d68475ca03f99af76d0b3d9 (diff)
downloadqpdf-655c55f84830190f9fa4777c615b8a622254648a.tar.zst
implement methods to get dictionary and array contents as map and vector
-rw-r--r--include/qpdf/QPDFObjectHandle.hh4
-rw-r--r--libqpdf/QPDFObjectHandle.cc14
-rw-r--r--libqpdf/QPDF_Array.cc6
-rw-r--r--libqpdf/QPDF_Dictionary.cc7
-rw-r--r--libqpdf/qpdf/QPDF_Array.hh1
-rw-r--r--libqpdf/qpdf/QPDF_Dictionary.hh1
-rw-r--r--qpdf/qtest/qpdf/test14.out1
-rw-r--r--qpdf/test_driver.cc14
8 files changed, 48 insertions, 0 deletions
diff --git a/include/qpdf/QPDFObjectHandle.hh b/include/qpdf/QPDFObjectHandle.hh
index 3e4f633a..9873da7b 100644
--- a/include/qpdf/QPDFObjectHandle.hh
+++ b/include/qpdf/QPDFObjectHandle.hh
@@ -172,6 +172,8 @@ class QPDFObjectHandle
int getArrayNItems();
QPDF_DLL
QPDFObjectHandle getArrayItem(int n);
+ QPDF_DLL
+ std::vector<QPDFObjectHandle> getArrayAsVector();
// Methods for dictionary objects
QPDF_DLL
@@ -180,6 +182,8 @@ class QPDFObjectHandle
QPDFObjectHandle getKey(std::string const&);
QPDF_DLL
std::set<std::string> getKeys();
+ QPDF_DLL
+ std::map<std::string, QPDFObjectHandle> getDictAsMap();
// Methods for name and array objects
QPDF_DLL
diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc
index b877f3dd..8567eef9 100644
--- a/libqpdf/QPDFObjectHandle.cc
+++ b/libqpdf/QPDFObjectHandle.cc
@@ -246,6 +246,13 @@ QPDFObjectHandle::getArrayItem(int n)
return dynamic_cast<QPDF_Array*>(obj.getPointer())->getItem(n);
}
+std::vector<QPDFObjectHandle>
+QPDFObjectHandle::getArrayAsVector()
+{
+ assertType("Array", isArray());
+ return dynamic_cast<QPDF_Array*>(obj.getPointer())->getAsVector();
+}
+
// Array mutators
void
@@ -278,6 +285,13 @@ QPDFObjectHandle::getKeys()
return dynamic_cast<QPDF_Dictionary*>(obj.getPointer())->getKeys();
}
+std::map<std::string, QPDFObjectHandle>
+QPDFObjectHandle::getDictAsMap()
+{
+ assertType("Dictionary", isDictionary());
+ return dynamic_cast<QPDF_Dictionary*>(obj.getPointer())->getAsMap();
+}
+
// Array and Name accessors
bool
QPDFObjectHandle::isOrHasName(std::string const& value)
diff --git a/libqpdf/QPDF_Array.cc b/libqpdf/QPDF_Array.cc
index ae70bd67..ab61f307 100644
--- a/libqpdf/QPDF_Array.cc
+++ b/libqpdf/QPDF_Array.cc
@@ -51,6 +51,12 @@ QPDF_Array::getItem(int n) const
return this->items[n];
}
+std::vector<QPDFObjectHandle> const&
+QPDF_Array::getAsVector() const
+{
+ return this->items;
+}
+
void
QPDF_Array::setItem(int n, QPDFObjectHandle const& oh)
{
diff --git a/libqpdf/QPDF_Dictionary.cc b/libqpdf/QPDF_Dictionary.cc
index 838a37e6..3be138a4 100644
--- a/libqpdf/QPDF_Dictionary.cc
+++ b/libqpdf/QPDF_Dictionary.cc
@@ -78,6 +78,13 @@ QPDF_Dictionary::getKeys()
return result;
}
+std::map<std::string, QPDFObjectHandle> const&
+QPDF_Dictionary::getAsMap() const
+{
+
+ return this->items;
+}
+
void
QPDF_Dictionary::replaceKey(std::string const& key,
QPDFObjectHandle const& value)
diff --git a/libqpdf/qpdf/QPDF_Array.hh b/libqpdf/qpdf/QPDF_Array.hh
index 490df723..a57834c8 100644
--- a/libqpdf/qpdf/QPDF_Array.hh
+++ b/libqpdf/qpdf/QPDF_Array.hh
@@ -14,6 +14,7 @@ class QPDF_Array: public QPDFObject
virtual std::string unparse();
int getNItems() const;
QPDFObjectHandle getItem(int n) const;
+ std::vector<QPDFObjectHandle> const& getAsVector() const;
void setItem(int, QPDFObjectHandle const&);
protected:
diff --git a/libqpdf/qpdf/QPDF_Dictionary.hh b/libqpdf/qpdf/QPDF_Dictionary.hh
index e84b549e..af9f7f09 100644
--- a/libqpdf/qpdf/QPDF_Dictionary.hh
+++ b/libqpdf/qpdf/QPDF_Dictionary.hh
@@ -21,6 +21,7 @@ class QPDF_Dictionary: public QPDFObject
bool hasKey(std::string const&);
QPDFObjectHandle getKey(std::string const&);
std::set<std::string> getKeys();
+ std::map<std::string, QPDFObjectHandle> const& getAsMap() const;
// Replace value of key, adding it if it does not exist
void replaceKey(std::string const& key, QPDFObjectHandle const&);
diff --git a/qpdf/qtest/qpdf/test14.out b/qpdf/qtest/qpdf/test14.out
index 5b68d9f6..65b640b9 100644
--- a/qpdf/qtest/qpdf/test14.out
+++ b/qpdf/qtest/qpdf/test14.out
@@ -3,4 +3,5 @@ old dict: 1
old dict: 1
new dict: 2
swapped array: /Array
+array and dictionary contents are correct
test 14 done
diff --git a/qpdf/test_driver.cc b/qpdf/test_driver.cc
index 17451dc7..d551d6f2 100644
--- a/qpdf/test_driver.cc
+++ b/qpdf/test_driver.cc
@@ -570,6 +570,20 @@ void runtest(int n, char const* filename)
std::cout << "swapped array: " << qdict.getArrayItem(0).getName()
<< std::endl;
+ // Exercise getAsMap and getAsArray
+ std::vector<QPDFObjectHandle> array_elements =
+ qdict.getArrayAsVector();
+ std::map<std::string, QPDFObjectHandle> dict_items =
+ qarray.getDictAsMap();
+ if ((array_elements.size() == 1) &&
+ (array_elements[0].getName() == "/Array") &&
+ (dict_items.size() == 1) &&
+ (dict_items["/NewDict"].getIntValue() == 2))
+ {
+ std::cout << "array and dictionary contents are correct"
+ << std::endl;
+ }
+
QPDFWriter w(pdf, "a.pdf");
w.setStaticID(true);
w.setStreamDataMode(qpdf_s_preserve);