Commit 4325e6e4a79da854157c1ef3b95fe2c925f68194
Committed by
Jay Berkenbilt
1 parent
585ecf17
Refactor QPDFObjectHandle::copyObject and rename to makeDirect
Showing
2 changed files
with
18 additions
and
26 deletions
include/qpdf/QPDFObjectHandle.hh
| @@ -1628,7 +1628,7 @@ class QPDFObjectHandle | @@ -1628,7 +1628,7 @@ class QPDFObjectHandle | ||
| 1628 | void objectWarning(std::string const& warning); | 1628 | void objectWarning(std::string const& warning); |
| 1629 | void assertType(char const* type_name, bool istype); | 1629 | void assertType(char const* type_name, bool istype); |
| 1630 | bool dereference(); | 1630 | bool dereference(); |
| 1631 | - void copyObject(std::set<QPDFObjGen>& visited, bool stop_at_streams); | 1631 | + void makeDirect(std::set<QPDFObjGen>& visited, bool stop_at_streams); |
| 1632 | void disconnect(); | 1632 | void disconnect(); |
| 1633 | void setParsedOffset(qpdf_offset_t offset); | 1633 | void setParsedOffset(qpdf_offset_t offset); |
| 1634 | void parseContentStream_internal( | 1634 | void parseContentStream_internal( |
libqpdf/QPDFObjectHandle.cc
| @@ -2218,19 +2218,10 @@ QPDFObjectHandle::unsafeShallowCopy() | @@ -2218,19 +2218,10 @@ QPDFObjectHandle::unsafeShallowCopy() | ||
| 2218 | } | 2218 | } |
| 2219 | 2219 | ||
| 2220 | void | 2220 | void |
| 2221 | -QPDFObjectHandle::copyObject( | 2221 | +QPDFObjectHandle::makeDirect( |
| 2222 | std::set<QPDFObjGen>& visited, bool stop_at_streams) | 2222 | std::set<QPDFObjGen>& visited, bool stop_at_streams) |
| 2223 | { | 2223 | { |
| 2224 | assertInitialized(); | 2224 | assertInitialized(); |
| 2225 | - if (isStream()) { | ||
| 2226 | - QTC::TC( | ||
| 2227 | - "qpdf", "QPDFObjectHandle copy stream", stop_at_streams ? 0 : 1); | ||
| 2228 | - if (stop_at_streams) { | ||
| 2229 | - return; | ||
| 2230 | - } | ||
| 2231 | - throw std::runtime_error( | ||
| 2232 | - "attempt to make a stream into a direct object"); | ||
| 2233 | - } | ||
| 2234 | 2225 | ||
| 2235 | auto cur_og = getObjGen(); | 2226 | auto cur_og = getObjGen(); |
| 2236 | if (cur_og.getObj() != 0) { | 2227 | if (cur_og.getObj() != 0) { |
| @@ -2243,40 +2234,41 @@ QPDFObjectHandle::copyObject( | @@ -2243,40 +2234,41 @@ QPDFObjectHandle::copyObject( | ||
| 2243 | visited.insert(cur_og); | 2234 | visited.insert(cur_og); |
| 2244 | } | 2235 | } |
| 2245 | 2236 | ||
| 2246 | - if (isReserved()) { | ||
| 2247 | - throw std::logic_error("QPDFObjectHandle: attempting to make a" | ||
| 2248 | - " reserved object handle direct"); | ||
| 2249 | - } | ||
| 2250 | - | ||
| 2251 | - std::shared_ptr<QPDFObject> new_obj; | ||
| 2252 | - | ||
| 2253 | if (isBool() || isInteger() || isName() || isNull() || isReal() || | 2237 | if (isBool() || isInteger() || isName() || isNull() || isReal() || |
| 2254 | isString()) { | 2238 | isString()) { |
| 2255 | - new_obj = obj->copy(true); | 2239 | + this->obj = obj->copy(true); |
| 2256 | } else if (isArray()) { | 2240 | } else if (isArray()) { |
| 2257 | std::vector<QPDFObjectHandle> items; | 2241 | std::vector<QPDFObjectHandle> items; |
| 2258 | auto array = asArray(); | 2242 | auto array = asArray(); |
| 2259 | int n = array->getNItems(); | 2243 | int n = array->getNItems(); |
| 2260 | for (int i = 0; i < n; ++i) { | 2244 | for (int i = 0; i < n; ++i) { |
| 2261 | items.push_back(array->getItem(i)); | 2245 | items.push_back(array->getItem(i)); |
| 2262 | - items.back().copyObject(visited, stop_at_streams); | 2246 | + items.back().makeDirect(visited, stop_at_streams); |
| 2263 | } | 2247 | } |
| 2264 | - new_obj = QPDF_Array::create(items); | 2248 | + this->obj = QPDF_Array::create(items); |
| 2265 | } else if (isDictionary()) { | 2249 | } else if (isDictionary()) { |
| 2266 | std::map<std::string, QPDFObjectHandle> items; | 2250 | std::map<std::string, QPDFObjectHandle> items; |
| 2267 | auto dict = asDictionary(); | 2251 | auto dict = asDictionary(); |
| 2268 | for (auto const& key: getKeys()) { | 2252 | for (auto const& key: getKeys()) { |
| 2269 | items[key] = dict->getKey(key); | 2253 | items[key] = dict->getKey(key); |
| 2270 | - items[key].copyObject(visited, stop_at_streams); | 2254 | + items[key].makeDirect(visited, stop_at_streams); |
| 2271 | } | 2255 | } |
| 2272 | - new_obj = QPDF_Dictionary::create(items); | 2256 | + this->obj = QPDF_Dictionary::create(items); |
| 2257 | + } else if (isStream()) { | ||
| 2258 | + QTC::TC( | ||
| 2259 | + "qpdf", "QPDFObjectHandle copy stream", stop_at_streams ? 0 : 1); | ||
| 2260 | + if (!stop_at_streams) { | ||
| 2261 | + throw std::runtime_error( | ||
| 2262 | + "attempt to make a stream into a direct object"); | ||
| 2263 | + } | ||
| 2264 | + } else if (isReserved()) { | ||
| 2265 | + throw std::logic_error("QPDFObjectHandle: attempting to make a" | ||
| 2266 | + " reserved object handle direct"); | ||
| 2273 | } else { | 2267 | } else { |
| 2274 | throw std::logic_error("QPDFObjectHandle::makeDirectInternal: " | 2268 | throw std::logic_error("QPDFObjectHandle::makeDirectInternal: " |
| 2275 | "unknown object type"); | 2269 | "unknown object type"); |
| 2276 | } | 2270 | } |
| 2277 | 2271 | ||
| 2278 | - this->obj = new_obj; | ||
| 2279 | - | ||
| 2280 | if (cur_og.getObj()) { | 2272 | if (cur_og.getObj()) { |
| 2281 | visited.erase(cur_og); | 2273 | visited.erase(cur_og); |
| 2282 | } | 2274 | } |
| @@ -2304,7 +2296,7 @@ void | @@ -2304,7 +2296,7 @@ void | ||
| 2304 | QPDFObjectHandle::makeDirect(bool allow_streams) | 2296 | QPDFObjectHandle::makeDirect(bool allow_streams) |
| 2305 | { | 2297 | { |
| 2306 | std::set<QPDFObjGen> visited; | 2298 | std::set<QPDFObjGen> visited; |
| 2307 | - copyObject(visited, allow_streams); | 2299 | + makeDirect(visited, allow_streams); |
| 2308 | } | 2300 | } |
| 2309 | 2301 | ||
| 2310 | void | 2302 | void |