implement methods to get dictionary and array contents as map and vector

This commit is contained in:
Jay Berkenbilt 2011-08-10 13:33:58 -04:00
parent d9ec2eb0f6
commit 655c55f848
8 changed files with 48 additions and 0 deletions

View File

@ -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

View File

@ -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)

View File

@ -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)
{

View File

@ -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)

View File

@ -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:

View File

@ -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&);

View File

@ -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

View File

@ -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);