diff options
-rw-r--r-- | examples/pdf-create.cc | 5 | ||||
-rw-r--r-- | include/qpdf/QPDFObjectHandle.hh | 6 | ||||
-rw-r--r-- | libqpdf/QPDFObjectHandle.cc | 9 | ||||
-rw-r--r-- | qpdf/pdf_from_scratch.cc | 5 | ||||
-rw-r--r-- | qpdf/test_driver.cc | 5 | ||||
-rw-r--r-- | qpdf/test_large_file.cc | 6 |
6 files changed, 19 insertions, 17 deletions
diff --git a/examples/pdf-create.cc b/examples/pdf-create.cc index e1a3ddd6..e1d75759 100644 --- a/examples/pdf-create.cc +++ b/examples/pdf-create.cc @@ -65,10 +65,7 @@ static QPDFObjectHandle createPageContents(QPDF& pdf, std::string const& text) std::string contents = "BT /F1 24 Tf 72 720 Td (" + text + ") Tj ET\n" "q 144 0 0 144 234 324 cm /Im1 Do Q\n"; - PointerHolder<Buffer> b = new Buffer(contents.length()); - unsigned char* bp = b->getBuffer(); - memcpy(bp, (char*)contents.c_str(), contents.length()); - return QPDFObjectHandle::newStream(&pdf, b); + return QPDFObjectHandle::newStream(&pdf, contents); } QPDFObjectHandle newName(std::string const& name) diff --git a/include/qpdf/QPDFObjectHandle.hh b/include/qpdf/QPDFObjectHandle.hh index 350381b6..8cfe56f3 100644 --- a/include/qpdf/QPDFObjectHandle.hh +++ b/include/qpdf/QPDFObjectHandle.hh @@ -142,6 +142,12 @@ class QPDFObjectHandle QPDF_DLL static QPDFObjectHandle newStream(QPDF* qpdf, PointerHolder<Buffer> data); + // Create new stream with data from string. This method will + // create a copy of the data rather than using the user-provided + // buffer as in the PointerHolder<Buffer> version of newStream. + QPDF_DLL + static QPDFObjectHandle newStream(QPDF* qpdf, std::string const& data); + // Accessor methods. If an accessor method that is valid for only // a particular object type is called on an object of the wrong // type, an exception is thrown. diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc index b1f7df2c..139ca02b 100644 --- a/libqpdf/QPDFObjectHandle.cc +++ b/libqpdf/QPDFObjectHandle.cc @@ -681,6 +681,15 @@ QPDFObjectHandle::newStream(QPDF* qpdf, PointerHolder<Buffer> data) } QPDFObjectHandle +QPDFObjectHandle::newStream(QPDF* qpdf, std::string const& data) +{ + PointerHolder<Buffer> b = new Buffer(data.length()); + unsigned char* bp = b->getBuffer(); + memcpy(bp, (char*)data.c_str(), data.length()); + return QPDFObjectHandle::newStream(qpdf, b); +} + +QPDFObjectHandle QPDFObjectHandle::shallowCopy() { assertInitialized(); diff --git a/qpdf/pdf_from_scratch.cc b/qpdf/pdf_from_scratch.cc index 2d7d8a3d..2f853c24 100644 --- a/qpdf/pdf_from_scratch.cc +++ b/qpdf/pdf_from_scratch.cc @@ -21,10 +21,7 @@ void usage() static QPDFObjectHandle createPageContents(QPDF& pdf, std::string const& text) { std::string contents = "BT /F1 15 Tf 72 720 Td (" + text + ") Tj ET\n"; - PointerHolder<Buffer> b = new Buffer(contents.length()); - unsigned char* bp = b->getBuffer(); - memcpy(bp, (char*)contents.c_str(), contents.length()); - return QPDFObjectHandle::newStream(&pdf, b); + return QPDFObjectHandle::newStream(&pdf, contents); } QPDFObjectHandle newName(std::string const& name) diff --git a/qpdf/test_driver.cc b/qpdf/test_driver.cc index 6f5414e1..e9c63d8f 100644 --- a/qpdf/test_driver.cc +++ b/qpdf/test_driver.cc @@ -73,10 +73,7 @@ static void checkPageContents(QPDFObjectHandle page, static QPDFObjectHandle createPageContents(QPDF& pdf, std::string const& text) { std::string contents = "BT /F1 15 Tf 72 720 Td (" + text + ") Tj ET\n"; - PointerHolder<Buffer> b = new Buffer(contents.length()); - unsigned char* bp = b->getBuffer(); - memcpy(bp, (char*)contents.c_str(), contents.length()); - return QPDFObjectHandle::newStream(&pdf, b); + return QPDFObjectHandle::newStream(&pdf, contents); } void runtest(int n, char const* filename) diff --git a/qpdf/test_large_file.cc b/qpdf/test_large_file.cc index ec346514..fc2c7bbc 100644 --- a/qpdf/test_large_file.cc +++ b/qpdf/test_large_file.cc @@ -173,11 +173,7 @@ std::string generate_page_contents(int pageno) static QPDFObjectHandle create_page_contents(QPDF& pdf, int pageno) { - std::string contents = generate_page_contents(pageno); - PointerHolder<Buffer> b = new Buffer(contents.length()); - unsigned char* bp = b->getBuffer(); - memcpy(bp, (char*)contents.c_str(), contents.length()); - return QPDFObjectHandle::newStream(&pdf, b); + return QPDFObjectHandle::newStream(&pdf, generate_page_contents(pageno)); } QPDFObjectHandle newName(std::string const& name) |