Commit a03c6863ae4e008275cea9074b9177df7a83b665

Authored by m-holger
1 parent 3d019121

Move QPDFObjectHandle dictionary methods implementation to QPDF_Dictionary.cc

libqpdf/QPDFObjectHandle.cc
@@ -974,7 +974,7 @@ QPDFObjectHandle::eraseItemAndGetOld(int at) @@ -974,7 +974,7 @@ QPDFObjectHandle::eraseItemAndGetOld(int at)
974 return result; 974 return result;
975 } 975 }
976 976
977 -// Dictionary accessors 977 +// Dictionary accessors are in QPDF_Dictionary.cc
978 978
979 QPDFObjectHandle::QPDFDictItems 979 QPDFObjectHandle::QPDFDictItems
980 QPDFObjectHandle::ditems() 980 QPDFObjectHandle::ditems()
@@ -982,59 +982,6 @@ QPDFObjectHandle::ditems() @@ -982,59 +982,6 @@ QPDFObjectHandle::ditems()
982 return {*this}; 982 return {*this};
983 } 983 }
984 984
985 -bool  
986 -QPDFObjectHandle::hasKey(std::string const& key) const  
987 -{  
988 - auto dict = as_dictionary(strict);  
989 - if (dict) {  
990 - return dict.hasKey(key);  
991 - } else {  
992 - typeWarning("dictionary", "returning false for a key containment request");  
993 - QTC::TC("qpdf", "QPDFObjectHandle dictionary false for hasKey");  
994 - return false;  
995 - }  
996 -}  
997 -  
998 -QPDFObjectHandle  
999 -QPDFObjectHandle::getKey(std::string const& key) const  
1000 -{  
1001 - if (auto dict = as_dictionary(strict)) {  
1002 - return dict.getKey(key);  
1003 - }  
1004 - typeWarning("dictionary", "returning null for attempted key retrieval");  
1005 - QTC::TC("qpdf", "QPDFObjectHandle dictionary null for getKey");  
1006 - static auto constexpr msg = " -> null returned from getting key $VD from non-Dictionary"sv;  
1007 - return QPDF_Null::create(obj, msg, "");  
1008 -}  
1009 -  
1010 -QPDFObjectHandle  
1011 -QPDFObjectHandle::getKeyIfDict(std::string const& key) const  
1012 -{  
1013 - return isNull() ? newNull() : getKey(key);  
1014 -}  
1015 -  
1016 -std::set<std::string>  
1017 -QPDFObjectHandle::getKeys() const  
1018 -{  
1019 - if (auto dict = as_dictionary(strict)) {  
1020 - return dict.getKeys();  
1021 - }  
1022 - typeWarning("dictionary", "treating as empty");  
1023 - QTC::TC("qpdf", "QPDFObjectHandle dictionary empty set for getKeys");  
1024 - return {};  
1025 -}  
1026 -  
1027 -std::map<std::string, QPDFObjectHandle>  
1028 -QPDFObjectHandle::getDictAsMap() const  
1029 -{  
1030 - if (auto dict = as_dictionary(strict)) {  
1031 - return dict.getAsMap();  
1032 - }  
1033 - typeWarning("dictionary", "treating as empty");  
1034 - QTC::TC("qpdf", "QPDFObjectHandle dictionary empty map for asMap");  
1035 - return {};  
1036 -}  
1037 -  
1038 // Array and Name accessors 985 // Array and Name accessors
1039 986
1040 bool 987 bool
@@ -1208,56 +1155,7 @@ QPDFObjectHandle::getUniqueResourceName( @@ -1208,56 +1155,7 @@ QPDFObjectHandle::getUniqueResourceName(
1208 " QPDFObjectHandle::getUniqueResourceName"); 1155 " QPDFObjectHandle::getUniqueResourceName");
1209 } 1156 }
1210 1157
1211 -// Dictionary mutators  
1212 -  
1213 -void  
1214 -QPDFObjectHandle::replaceKey(std::string const& key, QPDFObjectHandle const& value)  
1215 -{  
1216 - if (auto dict = as_dictionary(strict)) {  
1217 - checkOwnership(value);  
1218 - dict.replaceKey(key, value);  
1219 - return;  
1220 - }  
1221 - typeWarning("dictionary", "ignoring key replacement request");  
1222 - QTC::TC("qpdf", "QPDFObjectHandle dictionary ignoring replaceKey");  
1223 -}  
1224 -  
1225 -QPDFObjectHandle  
1226 -QPDFObjectHandle::replaceKeyAndGetNew(std::string const& key, QPDFObjectHandle const& value)  
1227 -{  
1228 - replaceKey(key, value);  
1229 - return value;  
1230 -}  
1231 -  
1232 -QPDFObjectHandle  
1233 -QPDFObjectHandle::replaceKeyAndGetOld(std::string const& key, QPDFObjectHandle const& value)  
1234 -{  
1235 - QPDFObjectHandle old = removeKeyAndGetOld(key);  
1236 - replaceKey(key, value);  
1237 - return old;  
1238 -}  
1239 -  
1240 -void  
1241 -QPDFObjectHandle::removeKey(std::string const& key)  
1242 -{  
1243 - if (auto dict = as_dictionary(strict)) {  
1244 - dict.removeKey(key);  
1245 - return;  
1246 - }  
1247 - typeWarning("dictionary", "ignoring key removal request");  
1248 - QTC::TC("qpdf", "QPDFObjectHandle dictionary ignoring removeKey");  
1249 -}  
1250 -  
1251 -QPDFObjectHandle  
1252 -QPDFObjectHandle::removeKeyAndGetOld(std::string const& key)  
1253 -{  
1254 - auto result = QPDFObjectHandle::newNull();  
1255 - if (auto dict = as_dictionary(strict)) {  
1256 - result = dict.getKey(key);  
1257 - }  
1258 - removeKey(key);  
1259 - return result;  
1260 -} 1158 +// Dictionary mutators are in QPDF_Dictionary.cc
1261 1159
1262 // Stream accessors 1160 // Stream accessors
1263 1161
@@ -2346,19 +2244,6 @@ QPDFObjectHandle::isImage(bool exclude_imagemask) const @@ -2346,19 +2244,6 @@ QPDFObjectHandle::isImage(bool exclude_imagemask) const
2346 } 2244 }
2347 2245
2348 void 2246 void
2349 -QPDFObjectHandle::checkOwnership(QPDFObjectHandle const& item) const  
2350 -{  
2351 - auto qpdf = getOwningQPDF();  
2352 - auto item_qpdf = item.getOwningQPDF();  
2353 - if ((qpdf != nullptr) && (item_qpdf != nullptr) && (qpdf != item_qpdf)) {  
2354 - QTC::TC("qpdf", "QPDFObjectHandle check ownership");  
2355 - throw std::logic_error(  
2356 - "Attempting to add an object from a different QPDF. Use "  
2357 - "QPDF::copyForeignObject to add objects from another file.");  
2358 - }  
2359 -}  
2360 -  
2361 -void  
2362 QPDFObjectHandle::assertPageObject() const 2247 QPDFObjectHandle::assertPageObject() const
2363 { 2248 {
2364 if (!isPageObject()) { 2249 if (!isPageObject()) {
libqpdf/QPDF_Dictionary.cc
@@ -169,3 +169,118 @@ BaseDictionary::replaceKey(std::string const&amp; key, QPDFObjectHandle value) @@ -169,3 +169,118 @@ BaseDictionary::replaceKey(std::string const&amp; key, QPDFObjectHandle value)
169 d->items[key] = value; 169 d->items[key] = value;
170 } 170 }
171 } 171 }
  172 +
  173 +void
  174 +QPDFObjectHandle::checkOwnership(QPDFObjectHandle const& item) const
  175 +{
  176 + auto qpdf = getOwningQPDF();
  177 + auto item_qpdf = item.getOwningQPDF();
  178 + if (qpdf && item_qpdf && qpdf != item_qpdf) {
  179 + QTC::TC("qpdf", "QPDFObjectHandle check ownership");
  180 + throw std::logic_error(
  181 + "Attempting to add an object from a different QPDF. Use "
  182 + "QPDF::copyForeignObject to add objects from another file.");
  183 + }
  184 +}
  185 +
  186 +bool
  187 +QPDFObjectHandle::hasKey(std::string const& key) const
  188 +{
  189 + auto dict = as_dictionary(strict);
  190 + if (dict) {
  191 + return dict.hasKey(key);
  192 + } else {
  193 + typeWarning("dictionary", "returning false for a key containment request");
  194 + QTC::TC("qpdf", "QPDFObjectHandle dictionary false for hasKey");
  195 + return false;
  196 + }
  197 +}
  198 +
  199 +QPDFObjectHandle
  200 +QPDFObjectHandle::getKey(std::string const& key) const
  201 +{
  202 + if (auto dict = as_dictionary(strict)) {
  203 + return dict.getKey(key);
  204 + }
  205 + typeWarning("dictionary", "returning null for attempted key retrieval");
  206 + QTC::TC("qpdf", "QPDFObjectHandle dictionary null for getKey");
  207 + static auto constexpr msg = " -> null returned from getting key $VD from non-Dictionary"sv;
  208 + return QPDF_Null::create(obj, msg, "");
  209 +}
  210 +
  211 +QPDFObjectHandle
  212 +QPDFObjectHandle::getKeyIfDict(std::string const& key) const
  213 +{
  214 + return isNull() ? newNull() : getKey(key);
  215 +}
  216 +
  217 +std::set<std::string>
  218 +QPDFObjectHandle::getKeys() const
  219 +{
  220 + if (auto dict = as_dictionary(strict)) {
  221 + return dict.getKeys();
  222 + }
  223 + typeWarning("dictionary", "treating as empty");
  224 + QTC::TC("qpdf", "QPDFObjectHandle dictionary empty set for getKeys");
  225 + return {};
  226 +}
  227 +
  228 +std::map<std::string, QPDFObjectHandle>
  229 +QPDFObjectHandle::getDictAsMap() const
  230 +{
  231 + if (auto dict = as_dictionary(strict)) {
  232 + return dict.getAsMap();
  233 + }
  234 + typeWarning("dictionary", "treating as empty");
  235 + QTC::TC("qpdf", "QPDFObjectHandle dictionary empty map for asMap");
  236 + return {};
  237 +}
  238 +
  239 +void
  240 +QPDFObjectHandle::replaceKey(std::string const& key, QPDFObjectHandle const& value)
  241 +{
  242 + if (auto dict = as_dictionary(strict)) {
  243 + checkOwnership(value);
  244 + dict.replaceKey(key, value);
  245 + return;
  246 + }
  247 + typeWarning("dictionary", "ignoring key replacement request");
  248 + QTC::TC("qpdf", "QPDFObjectHandle dictionary ignoring replaceKey");
  249 +}
  250 +
  251 +QPDFObjectHandle
  252 +QPDFObjectHandle::replaceKeyAndGetNew(std::string const& key, QPDFObjectHandle const& value)
  253 +{
  254 + replaceKey(key, value);
  255 + return value;
  256 +}
  257 +
  258 +QPDFObjectHandle
  259 +QPDFObjectHandle::replaceKeyAndGetOld(std::string const& key, QPDFObjectHandle const& value)
  260 +{
  261 + QPDFObjectHandle old = removeKeyAndGetOld(key);
  262 + replaceKey(key, value);
  263 + return old;
  264 +}
  265 +
  266 +void
  267 +QPDFObjectHandle::removeKey(std::string const& key)
  268 +{
  269 + if (auto dict = as_dictionary(strict)) {
  270 + dict.removeKey(key);
  271 + return;
  272 + }
  273 + typeWarning("dictionary", "ignoring key removal request");
  274 + QTC::TC("qpdf", "QPDFObjectHandle dictionary ignoring removeKey");
  275 +}
  276 +
  277 +QPDFObjectHandle
  278 +QPDFObjectHandle::removeKeyAndGetOld(std::string const& key)
  279 +{
  280 + auto result = QPDFObjectHandle::newNull();
  281 + if (auto dict = as_dictionary(strict)) {
  282 + result = dict.getKey(key);
  283 + }
  284 + removeKey(key);
  285 + return result;
  286 +}