Commit 2a82f6e1e05b5791c264efd8f70a20aeadca7501
1 parent
c8183607
Add method to get count of objects in QPDF
Showing
3 changed files
with
26 additions
and
9 deletions
ChangeLog
| 1 | 2018-06-22 Jay Berkenbilt <ejb@ql.org> | 1 | 2018-06-22 Jay Berkenbilt <ejb@ql.org> |
| 2 | 2 | ||
| 3 | + * Add new method QPDF::getObjectCount(). This gives an approximate | ||
| 4 | + (upper bound) account of objects in the QPDF object. | ||
| 5 | + | ||
| 3 | * Don't leave files open when merging. This makes it possible | 6 | * Don't leave files open when merging. This makes it possible |
| 4 | merge more files at once than the operating system's open file | 7 | merge more files at once than the operating system's open file |
| 5 | limit. Fixes #154. | 8 | limit. Fixes #154. |
include/qpdf/QPDF.hh
| @@ -431,6 +431,12 @@ class QPDF | @@ -431,6 +431,12 @@ class QPDF | ||
| 431 | QPDF_DLL | 431 | QPDF_DLL |
| 432 | void showXRefTable(); | 432 | void showXRefTable(); |
| 433 | 433 | ||
| 434 | + // Return the approximate number of indirect objects. It is | ||
| 435 | + // approximate because not all objects in the file are preserved | ||
| 436 | + // in all cases. | ||
| 437 | + QPDF_DLL | ||
| 438 | + size_t getObjectCount(); | ||
| 439 | + | ||
| 434 | // Returns a list of indirect objects for every object in the xref | 440 | // Returns a list of indirect objects for every object in the xref |
| 435 | // table. Useful for discovering objects that are not otherwise | 441 | // table. Useful for discovering objects that are not otherwise |
| 436 | // referenced. | 442 | // referenced. |
libqpdf/QPDF.cc
| @@ -1218,6 +1218,22 @@ QPDF::showXRefTable() | @@ -1218,6 +1218,22 @@ QPDF::showXRefTable() | ||
| 1218 | } | 1218 | } |
| 1219 | } | 1219 | } |
| 1220 | 1220 | ||
| 1221 | +size_t | ||
| 1222 | +QPDF::getObjectCount() | ||
| 1223 | +{ | ||
| 1224 | + // This method returns the next available indirect object number. | ||
| 1225 | + // makeIndirectObject uses it for this purpose. | ||
| 1226 | + QPDFObjGen o1(0, 0); | ||
| 1227 | + if (! this->m->obj_cache.empty()) | ||
| 1228 | + { | ||
| 1229 | + o1 = (*(this->m->obj_cache.rbegin())).first; | ||
| 1230 | + } | ||
| 1231 | + QPDFObjGen o2 = (*(this->m->xref_table.rbegin())).first; | ||
| 1232 | + QTC::TC("qpdf", "QPDF indirect last obj from xref", | ||
| 1233 | + (o2.getObj() > o1.getObj()) ? 1 : 0); | ||
| 1234 | + return std::max(o1.getObj(), o2.getObj()); | ||
| 1235 | +} | ||
| 1236 | + | ||
| 1221 | std::vector<QPDFObjectHandle> | 1237 | std::vector<QPDFObjectHandle> |
| 1222 | QPDF::getAllObjects() | 1238 | QPDF::getAllObjects() |
| 1223 | { | 1239 | { |
| @@ -1904,15 +1920,7 @@ QPDF::resolveObjectsInStream(int obj_stream_number) | @@ -1904,15 +1920,7 @@ QPDF::resolveObjectsInStream(int obj_stream_number) | ||
| 1904 | QPDFObjectHandle | 1920 | QPDFObjectHandle |
| 1905 | QPDF::makeIndirectObject(QPDFObjectHandle oh) | 1921 | QPDF::makeIndirectObject(QPDFObjectHandle oh) |
| 1906 | { | 1922 | { |
| 1907 | - QPDFObjGen o1(0, 0); | ||
| 1908 | - if (! this->m->obj_cache.empty()) | ||
| 1909 | - { | ||
| 1910 | - o1 = (*(this->m->obj_cache.rbegin())).first; | ||
| 1911 | - } | ||
| 1912 | - QPDFObjGen o2 = (*(this->m->xref_table.rbegin())).first; | ||
| 1913 | - QTC::TC("qpdf", "QPDF indirect last obj from xref", | ||
| 1914 | - (o2.getObj() > o1.getObj()) ? 1 : 0); | ||
| 1915 | - int max_objid = std::max(o1.getObj(), o2.getObj()); | 1923 | + int max_objid = getObjectCount(); |
| 1916 | QPDFObjGen next(max_objid + 1, 0); | 1924 | QPDFObjGen next(max_objid + 1, 0); |
| 1917 | this->m->obj_cache[next] = | 1925 | this->m->obj_cache[next] = |
| 1918 | ObjCache(QPDFObjectHandle::ObjAccessor::getObject(oh), -1, -1); | 1926 | ObjCache(QPDFObjectHandle::ObjAccessor::getObject(oh), -1, -1); |