From 8705e2e8fc1a9721b2438c09ba7e92ec673af19d Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Thu, 5 Jul 2012 21:22:27 -0400 Subject: Add QPDFWriter method to output to FILE* --- ChangeLog | 6 ++++++ include/qpdf/QPDFWriter.hh | 16 ++++++++++++++++ libqpdf/QPDFWriter.cc | 28 +++++++++++++++++++++++----- qpdf/test_driver.cc | 5 ++++- 4 files changed, 49 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 230a49a9..5e3092de 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2012-07-05 Jay Berkenbilt + + * Add QPDFWriter methods to write to an already open stdio FILE*. + Implementation and idea area based on contributions from Tobias + Hoffmann. + 2012-07-04 Jay Berkenbilt * Accept changes from Tobias Hoffmann: add public method diff --git a/include/qpdf/QPDFWriter.hh b/include/qpdf/QPDFWriter.hh index 25acbb14..2261e45b 100644 --- a/include/qpdf/QPDFWriter.hh +++ b/include/qpdf/QPDFWriter.hh @@ -49,6 +49,14 @@ class QPDFWriter // setOutputFilename() for details. QPDF_DLL QPDFWriter(QPDF& pdf, char const* filename); + + // Create a QPDFWriter object that writes its output to an already + // open FILE*. This is equivalent to calling the first + // constructor and then calling setOutputFile(). See + // setOutputFile() for details. + QPDF_DLL + QPDFWriter(QPDF& pdf, char const* description, FILE* file, bool close_file); + QPDF_DLL ~QPDFWriter(); @@ -66,6 +74,14 @@ class QPDFWriter QPDF_DLL void setOutputFilename(char const* filename); + // Write to the given FILE*, which must be opened by the caller. + // If close_file is true, QPDFWriter will close the file. + // Otherwise, the caller must close the file. The file does not + // need to be seekable; it will be written to in a single pass. + // It must be open in binary mode. + QPDF_DLL + void setOutputFile(char const* description, FILE* file, bool close_file); + // Indicate that QPDFWriter should create a memory buffer to // contain the final PDF file. Obtain the memory by calling // getBuffer(). diff --git a/libqpdf/QPDFWriter.cc b/libqpdf/QPDFWriter.cc index 564133e3..a7c78b19 100644 --- a/libqpdf/QPDFWriter.cc +++ b/libqpdf/QPDFWriter.cc @@ -34,6 +34,14 @@ QPDFWriter::QPDFWriter(QPDF& pdf, char const* filename) : setOutputFilename(filename); } +QPDFWriter::QPDFWriter(QPDF& pdf, char const* description, + FILE *file, bool close_file) : + pdf(pdf) +{ + init(); + setOutputFile(description, file, close_file); +} + void QPDFWriter::init() { @@ -79,21 +87,31 @@ QPDFWriter::~QPDFWriter() void QPDFWriter::setOutputFilename(char const* filename) { - this->filename = filename; + char const* description = filename; + FILE* f = 0; if (filename == 0) { - this->filename = "standard output"; + description = "standard output"; QTC::TC("qpdf", "QPDFWriter write to stdout"); - file = stdout; + f = stdout; QUtil::binary_stdout(); } else { QTC::TC("qpdf", "QPDFWriter write to file"); - file = QUtil::fopen_wrapper(std::string("open ") + filename, - fopen(filename, "wb+")); + f = QUtil::fopen_wrapper(std::string("open ") + filename, + fopen(filename, "wb+")); close_file = true; } + setOutputFile(description, f, close_file); +} + +void +QPDFWriter::setOutputFile(char const* description, FILE* file, bool close_file) +{ + this->filename = description; + this->file = file; + this->close_file = close_file; Pipeline* p = new Pl_StdioFile("qpdf output", file); to_delete.push_back(p); initializePipelineStack(p); diff --git a/qpdf/test_driver.cc b/qpdf/test_driver.cc index b72c5d20..89b422ac 100644 --- a/qpdf/test_driver.cc +++ b/qpdf/test_driver.cc @@ -728,7 +728,10 @@ void runtest(int n, char const* filename) checkPageContents(pages[11], "New page 11"); checkPageContents(pages[12], "New page 12"); - QPDFWriter w(pdf, "a.pdf"); + // Exercise writing to FILE* + FILE* out = QUtil::fopen_wrapper(std::string("open a.pdf"), + fopen("a.pdf", "wb")); + QPDFWriter w(pdf, "FILE* a.pdf", out, true); w.setStaticID(true); w.setStreamDataMode(qpdf_s_preserve); w.write(); -- cgit v1.2.3-54-g00ecf