Commit 8705e2e8fc1a9721b2438c09ba7e92ec673af19d
1 parent
3b5d72b9
Add QPDFWriter method to output to FILE*
Showing
4 changed files
with
49 additions
and
6 deletions
ChangeLog
| 1 | +2012-07-05 Jay Berkenbilt <ejb@ql.org> | |
| 2 | + | |
| 3 | + * Add QPDFWriter methods to write to an already open stdio FILE*. | |
| 4 | + Implementation and idea area based on contributions from Tobias | |
| 5 | + Hoffmann. | |
| 6 | + | |
| 1 | 7 | 2012-07-04 Jay Berkenbilt <ejb@ql.org> |
| 2 | 8 | |
| 3 | 9 | * Accept changes from Tobias Hoffmann: add public method | ... | ... |
include/qpdf/QPDFWriter.hh
| ... | ... | @@ -49,6 +49,14 @@ class QPDFWriter |
| 49 | 49 | // setOutputFilename() for details. |
| 50 | 50 | QPDF_DLL |
| 51 | 51 | QPDFWriter(QPDF& pdf, char const* filename); |
| 52 | + | |
| 53 | + // Create a QPDFWriter object that writes its output to an already | |
| 54 | + // open FILE*. This is equivalent to calling the first | |
| 55 | + // constructor and then calling setOutputFile(). See | |
| 56 | + // setOutputFile() for details. | |
| 57 | + QPDF_DLL | |
| 58 | + QPDFWriter(QPDF& pdf, char const* description, FILE* file, bool close_file); | |
| 59 | + | |
| 52 | 60 | QPDF_DLL |
| 53 | 61 | ~QPDFWriter(); |
| 54 | 62 | |
| ... | ... | @@ -66,6 +74,14 @@ class QPDFWriter |
| 66 | 74 | QPDF_DLL |
| 67 | 75 | void setOutputFilename(char const* filename); |
| 68 | 76 | |
| 77 | + // Write to the given FILE*, which must be opened by the caller. | |
| 78 | + // If close_file is true, QPDFWriter will close the file. | |
| 79 | + // Otherwise, the caller must close the file. The file does not | |
| 80 | + // need to be seekable; it will be written to in a single pass. | |
| 81 | + // It must be open in binary mode. | |
| 82 | + QPDF_DLL | |
| 83 | + void setOutputFile(char const* description, FILE* file, bool close_file); | |
| 84 | + | |
| 69 | 85 | // Indicate that QPDFWriter should create a memory buffer to |
| 70 | 86 | // contain the final PDF file. Obtain the memory by calling |
| 71 | 87 | // getBuffer(). | ... | ... |
libqpdf/QPDFWriter.cc
| ... | ... | @@ -34,6 +34,14 @@ QPDFWriter::QPDFWriter(QPDF& pdf, char const* filename) : |
| 34 | 34 | setOutputFilename(filename); |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | +QPDFWriter::QPDFWriter(QPDF& pdf, char const* description, | |
| 38 | + FILE *file, bool close_file) : | |
| 39 | + pdf(pdf) | |
| 40 | +{ | |
| 41 | + init(); | |
| 42 | + setOutputFile(description, file, close_file); | |
| 43 | +} | |
| 44 | + | |
| 37 | 45 | void |
| 38 | 46 | QPDFWriter::init() |
| 39 | 47 | { |
| ... | ... | @@ -79,21 +87,31 @@ QPDFWriter::~QPDFWriter() |
| 79 | 87 | void |
| 80 | 88 | QPDFWriter::setOutputFilename(char const* filename) |
| 81 | 89 | { |
| 82 | - this->filename = filename; | |
| 90 | + char const* description = filename; | |
| 91 | + FILE* f = 0; | |
| 83 | 92 | if (filename == 0) |
| 84 | 93 | { |
| 85 | - this->filename = "standard output"; | |
| 94 | + description = "standard output"; | |
| 86 | 95 | QTC::TC("qpdf", "QPDFWriter write to stdout"); |
| 87 | - file = stdout; | |
| 96 | + f = stdout; | |
| 88 | 97 | QUtil::binary_stdout(); |
| 89 | 98 | } |
| 90 | 99 | else |
| 91 | 100 | { |
| 92 | 101 | QTC::TC("qpdf", "QPDFWriter write to file"); |
| 93 | - file = QUtil::fopen_wrapper(std::string("open ") + filename, | |
| 94 | - fopen(filename, "wb+")); | |
| 102 | + f = QUtil::fopen_wrapper(std::string("open ") + filename, | |
| 103 | + fopen(filename, "wb+")); | |
| 95 | 104 | close_file = true; |
| 96 | 105 | } |
| 106 | + setOutputFile(description, f, close_file); | |
| 107 | +} | |
| 108 | + | |
| 109 | +void | |
| 110 | +QPDFWriter::setOutputFile(char const* description, FILE* file, bool close_file) | |
| 111 | +{ | |
| 112 | + this->filename = description; | |
| 113 | + this->file = file; | |
| 114 | + this->close_file = close_file; | |
| 97 | 115 | Pipeline* p = new Pl_StdioFile("qpdf output", file); |
| 98 | 116 | to_delete.push_back(p); |
| 99 | 117 | initializePipelineStack(p); | ... | ... |
qpdf/test_driver.cc
| ... | ... | @@ -728,7 +728,10 @@ void runtest(int n, char const* filename) |
| 728 | 728 | checkPageContents(pages[11], "New page 11"); |
| 729 | 729 | checkPageContents(pages[12], "New page 12"); |
| 730 | 730 | |
| 731 | - QPDFWriter w(pdf, "a.pdf"); | |
| 731 | + // Exercise writing to FILE* | |
| 732 | + FILE* out = QUtil::fopen_wrapper(std::string("open a.pdf"), | |
| 733 | + fopen("a.pdf", "wb")); | |
| 734 | + QPDFWriter w(pdf, "FILE* a.pdf", out, true); | |
| 732 | 735 | w.setStaticID(true); |
| 733 | 736 | w.setStreamDataMode(qpdf_s_preserve); |
| 734 | 737 | w.write(); | ... | ... |