Commit e5f3910c3ee1f6b779455fb1516ab3c985383d8e

Authored by Jay Berkenbilt
1 parent e2596359

Add new FileInputSource constructors

ChangeLog
1 1 2022-05-04 Jay Berkenbilt <ejb@ql.org>
2 2  
  3 + * FileInputSource has new constructors that eliminate the need to
  4 + call setFilename or setFile in most cases.
  5 +
3 6 * Enhance JSON by adding a write method that takes a Pipeline* and
4 7 depth, and add several helper methods to make it easier to write
5 8 large amounts of JSON incrementally without having to have the
... ...
include/qpdf/FileInputSource.hh
... ... @@ -30,6 +30,10 @@ class QPDF_DLL_CLASS FileInputSource: public InputSource
30 30 QPDF_DLL
31 31 FileInputSource();
32 32 QPDF_DLL
  33 + FileInputSource(char const* filename);
  34 + QPDF_DLL
  35 + FileInputSource(char const* description, FILE* filep, bool close_file);
  36 + QPDF_DLL
33 37 void setFilename(char const* filename);
34 38 QPDF_DLL
35 39 void setFile(char const* description, FILE* filep, bool close_file);
... ...
libqpdf/ClosedFileInputSource.cc
... ... @@ -24,8 +24,8 @@ void
24 24 ClosedFileInputSource::before()
25 25 {
26 26 if (0 == this->m->fis.get()) {
27   - this->m->fis = std::make_shared<FileInputSource>();
28   - this->m->fis->setFilename(this->m->filename.c_str());
  27 + this->m->fis =
  28 + std::make_shared<FileInputSource>(this->m->filename.c_str());
29 29 this->m->fis->seek(this->m->offset, SEEK_SET);
30 30 this->m->fis->setLastOffset(this->last_offset);
31 31 }
... ...
libqpdf/FileInputSource.cc
... ... @@ -23,6 +23,19 @@ FileInputSource::FileInputSource() :
23 23 {
24 24 }
25 25  
  26 +FileInputSource::FileInputSource(char const* filename) :
  27 + m(new Members(false))
  28 +{
  29 + setFilename(filename);
  30 +}
  31 +
  32 +FileInputSource::FileInputSource(
  33 + char const* description, FILE* filep, bool close_file) :
  34 + m(new Members(false))
  35 +{
  36 + setFile(description, filep, close_file);
  37 +}
  38 +
26 39 void
27 40 FileInputSource::setFilename(char const* filename)
28 41 {
... ...
libqpdf/QPDF.cc
... ... @@ -270,8 +270,7 @@ QPDF::~QPDF()
270 270 void
271 271 QPDF::processFile(char const* filename, char const* password)
272 272 {
273   - FileInputSource* fi = new FileInputSource();
274   - fi->setFilename(filename);
  273 + FileInputSource* fi = new FileInputSource(filename);
275 274 processInputSource(std::shared_ptr<InputSource>(fi), password);
276 275 }
277 276  
... ... @@ -279,8 +278,7 @@ void
279 278 QPDF::processFile(
280 279 char const* description, FILE* filep, bool close_file, char const* password)
281 280 {
282   - FileInputSource* fi = new FileInputSource();
283   - fi->setFile(description, filep, close_file);
  281 + FileInputSource* fi = new FileInputSource(description, filep, close_file);
284 282 processInputSource(std::shared_ptr<InputSource>(fi), password);
285 283 }
286 284  
... ...
libqpdf/QPDFJob.cc
... ... @@ -2445,9 +2445,9 @@ QPDFJob::handlePageSpecs(
2445 2445 cis->stayOpen(true);
2446 2446 } else {
2447 2447 QTC::TC("qpdf", "QPDFJob keep files open y");
2448   - FileInputSource* fis = new FileInputSource();
  2448 + FileInputSource* fis =
  2449 + new FileInputSource(page_spec.filename.c_str());
2449 2450 is = std::shared_ptr<InputSource>(fis);
2450   - fis->setFilename(page_spec.filename.c_str());
2451 2451 }
2452 2452 std::shared_ptr<QPDF> qpdf_ph =
2453 2453 processInputSource(is, password, true);
... ...
libtests/closed_file_input_source.cc
... ... @@ -73,8 +73,7 @@ main()
73 73 do_tests(&cf2);
74 74 cf2.stayOpen(false);
75 75 std::cout << "testing with FileInputSource\n";
76   - FileInputSource f;
77   - f.setFilename("input");
  76 + FileInputSource f("input");
78 77 do_tests(&f);
79 78 std::cout << "all assertions passed" << std::endl;
80 79 return 0;
... ...
qpdf/test_tokenizer.cc
... ... @@ -191,8 +191,7 @@ process(char const* filename, bool include_ignorable, size_t max_len)
191 191 std::shared_ptr<InputSource> is;
192 192  
193 193 // Tokenize file, skipping streams
194   - FileInputSource* fis = new FileInputSource();
195   - fis->setFilename(filename);
  194 + FileInputSource* fis = new FileInputSource(filename);
196 195 is = std::shared_ptr<InputSource>(fis);
197 196 dump_tokens(is, "FILE", max_len, include_ignorable, true, false);
198 197  
... ...