Commit 8705e2e8fc1a9721b2438c09ba7e92ec673af19d

Authored by Jay Berkenbilt
1 parent 3b5d72b9

Add QPDFWriter method to output to FILE*

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 2012-07-04 Jay Berkenbilt <ejb@ql.org> 7 2012-07-04 Jay Berkenbilt <ejb@ql.org>
2 8
3 * Accept changes from Tobias Hoffmann: add public method 9 * Accept changes from Tobias Hoffmann: add public method
include/qpdf/QPDFWriter.hh
@@ -49,6 +49,14 @@ class QPDFWriter @@ -49,6 +49,14 @@ class QPDFWriter
49 // setOutputFilename() for details. 49 // setOutputFilename() for details.
50 QPDF_DLL 50 QPDF_DLL
51 QPDFWriter(QPDF& pdf, char const* filename); 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 QPDF_DLL 60 QPDF_DLL
53 ~QPDFWriter(); 61 ~QPDFWriter();
54 62
@@ -66,6 +74,14 @@ class QPDFWriter @@ -66,6 +74,14 @@ class QPDFWriter
66 QPDF_DLL 74 QPDF_DLL
67 void setOutputFilename(char const* filename); 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 // Indicate that QPDFWriter should create a memory buffer to 85 // Indicate that QPDFWriter should create a memory buffer to
70 // contain the final PDF file. Obtain the memory by calling 86 // contain the final PDF file. Obtain the memory by calling
71 // getBuffer(). 87 // getBuffer().
libqpdf/QPDFWriter.cc
@@ -34,6 +34,14 @@ QPDFWriter::QPDFWriter(QPDF&amp; pdf, char const* filename) : @@ -34,6 +34,14 @@ QPDFWriter::QPDFWriter(QPDF&amp; pdf, char const* filename) :
34 setOutputFilename(filename); 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 void 45 void
38 QPDFWriter::init() 46 QPDFWriter::init()
39 { 47 {
@@ -79,21 +87,31 @@ QPDFWriter::~QPDFWriter() @@ -79,21 +87,31 @@ QPDFWriter::~QPDFWriter()
79 void 87 void
80 QPDFWriter::setOutputFilename(char const* filename) 88 QPDFWriter::setOutputFilename(char const* filename)
81 { 89 {
82 - this->filename = filename; 90 + char const* description = filename;
  91 + FILE* f = 0;
83 if (filename == 0) 92 if (filename == 0)
84 { 93 {
85 - this->filename = "standard output"; 94 + description = "standard output";
86 QTC::TC("qpdf", "QPDFWriter write to stdout"); 95 QTC::TC("qpdf", "QPDFWriter write to stdout");
87 - file = stdout; 96 + f = stdout;
88 QUtil::binary_stdout(); 97 QUtil::binary_stdout();
89 } 98 }
90 else 99 else
91 { 100 {
92 QTC::TC("qpdf", "QPDFWriter write to file"); 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 close_file = true; 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 Pipeline* p = new Pl_StdioFile("qpdf output", file); 115 Pipeline* p = new Pl_StdioFile("qpdf output", file);
98 to_delete.push_back(p); 116 to_delete.push_back(p);
99 initializePipelineStack(p); 117 initializePipelineStack(p);
qpdf/test_driver.cc
@@ -728,7 +728,10 @@ void runtest(int n, char const* filename) @@ -728,7 +728,10 @@ void runtest(int n, char const* filename)
728 checkPageContents(pages[11], "New page 11"); 728 checkPageContents(pages[11], "New page 11");
729 checkPageContents(pages[12], "New page 12"); 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 w.setStaticID(true); 735 w.setStaticID(true);
733 w.setStreamDataMode(qpdf_s_preserve); 736 w.setStreamDataMode(qpdf_s_preserve);
734 w.write(); 737 w.write();