Commit 43c404b45ab54583a33492adc93f54fa349c2f94

Authored by Tobias Hoffmann
Committed by Jay Berkenbilt
1 parent 75054c0b

Add QPDFObjectHandle::newStream(QPDF *, std::string const&)

This makes the code simpler than having to create a buffer of a fixed
size and copy the string to it.
examples/pdf-create.cc
@@ -65,10 +65,7 @@ static QPDFObjectHandle createPageContents(QPDF& pdf, std::string const& text) @@ -65,10 +65,7 @@ static QPDFObjectHandle createPageContents(QPDF& pdf, std::string const& text)
65 std::string contents = 65 std::string contents =
66 "BT /F1 24 Tf 72 720 Td (" + text + ") Tj ET\n" 66 "BT /F1 24 Tf 72 720 Td (" + text + ") Tj ET\n"
67 "q 144 0 0 144 234 324 cm /Im1 Do Q\n"; 67 "q 144 0 0 144 234 324 cm /Im1 Do Q\n";
68 - PointerHolder<Buffer> b = new Buffer(contents.length());  
69 - unsigned char* bp = b->getBuffer();  
70 - memcpy(bp, (char*)contents.c_str(), contents.length());  
71 - return QPDFObjectHandle::newStream(&pdf, b); 68 + return QPDFObjectHandle::newStream(&pdf, contents);
72 } 69 }
73 70
74 QPDFObjectHandle newName(std::string const& name) 71 QPDFObjectHandle newName(std::string const& name)
include/qpdf/QPDFObjectHandle.hh
@@ -142,6 +142,12 @@ class QPDFObjectHandle @@ -142,6 +142,12 @@ class QPDFObjectHandle
142 QPDF_DLL 142 QPDF_DLL
143 static QPDFObjectHandle newStream(QPDF* qpdf, PointerHolder<Buffer> data); 143 static QPDFObjectHandle newStream(QPDF* qpdf, PointerHolder<Buffer> data);
144 144
  145 + // Create new stream with data from string. This method will
  146 + // create a copy of the data rather than using the user-provided
  147 + // buffer as in the PointerHolder<Buffer> version of newStream.
  148 + QPDF_DLL
  149 + static QPDFObjectHandle newStream(QPDF* qpdf, std::string const& data);
  150 +
145 // Accessor methods. If an accessor method that is valid for only 151 // Accessor methods. If an accessor method that is valid for only
146 // a particular object type is called on an object of the wrong 152 // a particular object type is called on an object of the wrong
147 // type, an exception is thrown. 153 // type, an exception is thrown.
libqpdf/QPDFObjectHandle.cc
@@ -681,6 +681,15 @@ QPDFObjectHandle::newStream(QPDF* qpdf, PointerHolder&lt;Buffer&gt; data) @@ -681,6 +681,15 @@ QPDFObjectHandle::newStream(QPDF* qpdf, PointerHolder&lt;Buffer&gt; data)
681 } 681 }
682 682
683 QPDFObjectHandle 683 QPDFObjectHandle
  684 +QPDFObjectHandle::newStream(QPDF* qpdf, std::string const& data)
  685 +{
  686 + PointerHolder<Buffer> b = new Buffer(data.length());
  687 + unsigned char* bp = b->getBuffer();
  688 + memcpy(bp, (char*)data.c_str(), data.length());
  689 + return QPDFObjectHandle::newStream(qpdf, b);
  690 +}
  691 +
  692 +QPDFObjectHandle
684 QPDFObjectHandle::shallowCopy() 693 QPDFObjectHandle::shallowCopy()
685 { 694 {
686 assertInitialized(); 695 assertInitialized();
qpdf/pdf_from_scratch.cc
@@ -21,10 +21,7 @@ void usage() @@ -21,10 +21,7 @@ void usage()
21 static QPDFObjectHandle createPageContents(QPDF& pdf, std::string const& text) 21 static QPDFObjectHandle createPageContents(QPDF& pdf, std::string const& text)
22 { 22 {
23 std::string contents = "BT /F1 15 Tf 72 720 Td (" + text + ") Tj ET\n"; 23 std::string contents = "BT /F1 15 Tf 72 720 Td (" + text + ") Tj ET\n";
24 - PointerHolder<Buffer> b = new Buffer(contents.length());  
25 - unsigned char* bp = b->getBuffer();  
26 - memcpy(bp, (char*)contents.c_str(), contents.length());  
27 - return QPDFObjectHandle::newStream(&pdf, b); 24 + return QPDFObjectHandle::newStream(&pdf, contents);
28 } 25 }
29 26
30 QPDFObjectHandle newName(std::string const& name) 27 QPDFObjectHandle newName(std::string const& name)
qpdf/test_driver.cc
@@ -73,10 +73,7 @@ static void checkPageContents(QPDFObjectHandle page, @@ -73,10 +73,7 @@ static void checkPageContents(QPDFObjectHandle page,
73 static QPDFObjectHandle createPageContents(QPDF& pdf, std::string const& text) 73 static QPDFObjectHandle createPageContents(QPDF& pdf, std::string const& text)
74 { 74 {
75 std::string contents = "BT /F1 15 Tf 72 720 Td (" + text + ") Tj ET\n"; 75 std::string contents = "BT /F1 15 Tf 72 720 Td (" + text + ") Tj ET\n";
76 - PointerHolder<Buffer> b = new Buffer(contents.length());  
77 - unsigned char* bp = b->getBuffer();  
78 - memcpy(bp, (char*)contents.c_str(), contents.length());  
79 - return QPDFObjectHandle::newStream(&pdf, b); 76 + return QPDFObjectHandle::newStream(&pdf, contents);
80 } 77 }
81 78
82 void runtest(int n, char const* filename) 79 void runtest(int n, char const* filename)
qpdf/test_large_file.cc
@@ -173,11 +173,7 @@ std::string generate_page_contents(int pageno) @@ -173,11 +173,7 @@ std::string generate_page_contents(int pageno)
173 173
174 static QPDFObjectHandle create_page_contents(QPDF& pdf, int pageno) 174 static QPDFObjectHandle create_page_contents(QPDF& pdf, int pageno)
175 { 175 {
176 - std::string contents = generate_page_contents(pageno);  
177 - PointerHolder<Buffer> b = new Buffer(contents.length());  
178 - unsigned char* bp = b->getBuffer();  
179 - memcpy(bp, (char*)contents.c_str(), contents.length());  
180 - return QPDFObjectHandle::newStream(&pdf, b); 176 + return QPDFObjectHandle::newStream(&pdf, generate_page_contents(pageno));
181 } 177 }
182 178
183 QPDFObjectHandle newName(std::string const& name) 179 QPDFObjectHandle newName(std::string const& name)