Commit 2c7b583b3aaed1902ca3cca2387414391fc3fe65

Authored by Jay Berkenbilt
1 parent ac56e013

QPDFJob: move input/output handling into config

include/qpdf/QPDFJob.hh
... ... @@ -244,6 +244,15 @@ class QPDFJob
244 244 friend class QPDFJob;
245 245 public:
246 246 QPDF_DLL
  247 + Config& inputFile(char const* filename);
  248 + QPDF_DLL
  249 + Config& emptyInput();
  250 + QPDF_DLL
  251 + Config& outputFile(char const* filename);
  252 + QPDF_DLL
  253 + Config& replaceInput();
  254 +
  255 + QPDF_DLL
247 256 std::shared_ptr<CopyAttConfig> copyAttachmentsFrom();
248 257 QPDF_DLL
249 258 std::shared_ptr<AttConfig> addAttachment();
... ...
libqpdf/QPDFJob_argv.cc
... ... @@ -37,7 +37,6 @@ namespace
37 37 void initOptionTables();
38 38  
39 39 QPDFArgParser ap;
40   - QPDFJob& o;
41 40 std::shared_ptr<QPDFJob::Config> c_main;
42 41 std::shared_ptr<QPDFJob::CopyAttConfig> c_copy_att;
43 42 std::shared_ptr<QPDFJob::AttConfig> c_att;
... ... @@ -46,15 +45,18 @@ namespace
46 45 std::shared_ptr<QPDFJob::EncConfig> c_enc;
47 46 std::vector<char*> accumulated_args; // points to member in ap
48 47 char* pages_password;
  48 + bool gave_input;
  49 + bool gave_output;
49 50 };
50 51 }
51 52  
52 53 ArgParser::ArgParser(QPDFArgParser& ap,
53 54 std::shared_ptr<QPDFJob::Config> c_main, QPDFJob& o) :
54 55 ap(ap),
55   - o(o),
56 56 c_main(c_main),
57   - pages_password(nullptr)
  57 + pages_password(nullptr),
  58 + gave_input(false),
  59 + gave_output(false)
58 60 {
59 61 initOptionTables();
60 62 }
... ... @@ -73,13 +75,15 @@ ArgParser::initOptionTables()
73 75 void
74 76 ArgParser::argPositional(char* arg)
75 77 {
76   - if (o.infilename == 0)
  78 + if (! this->gave_input)
77 79 {
78   - o.infilename = QUtil::make_shared_cstr(arg);
  80 + c_main->inputFile(arg);
  81 + this->gave_input = true;
79 82 }
80   - else if (o.outfilename == 0)
  83 + else if (! this->gave_output)
81 84 {
82   - o.outfilename = QUtil::make_shared_cstr(arg);
  85 + c_main->outputFile(arg);
  86 + this->gave_output = true;
83 87 }
84 88 else
85 89 {
... ... @@ -90,13 +94,15 @@ ArgParser::argPositional(char* arg)
90 94 void
91 95 ArgParser::argEmpty()
92 96 {
93   - o.infilename = QUtil::make_shared_cstr("");
  97 + c_main->emptyInput();
  98 + this->gave_input = true;
94 99 }
95 100  
96 101 void
97 102 ArgParser::argReplaceInput()
98 103 {
99   - o.replace_input = true;
  104 + c_main->replaceInput();
  105 + this->gave_output = true;
100 106 }
101 107  
102 108 void
... ...
libqpdf/QPDFJob_config.cc
... ... @@ -9,6 +9,72 @@ static void usage(std::string const&amp; msg)
9 9 }
10 10  
11 11 QPDFJob::Config&
  12 +QPDFJob::Config::inputFile(char const* filename)
  13 +{
  14 + if (o.infilename == 0)
  15 + {
  16 + o.infilename = QUtil::make_shared_cstr(filename);
  17 + }
  18 + else
  19 + {
  20 + usage("input file has already been given");
  21 + }
  22 + return *this;
  23 +}
  24 +
  25 +QPDFJob::Config&
  26 +QPDFJob::Config::emptyInput()
  27 +{
  28 + if (o.infilename == 0)
  29 + {
  30 + // QXXXQ decide whether to fix this or just leave the comment:
  31 + // Various places in QPDFJob.cc know that the empty string for
  32 + // infile means empty. This means that passing "" as the
  33 + // argument to inputFile, or equivalently using "" as a
  34 + // positional command-line argument would be the same as
  35 + // --empty. This probably isn't worth blocking or coding
  36 + // around, but it would be better if we had a tighter way of
  37 + // knowing that the input file is empty.
  38 + o.infilename = QUtil::make_shared_cstr("");
  39 + }
  40 + else
  41 + {
  42 + usage("empty input can't be used"
  43 + " since input file has already been given");
  44 + }
  45 + return *this;
  46 +}
  47 +
  48 +QPDFJob::Config&
  49 +QPDFJob::Config::outputFile(char const* filename)
  50 +{
  51 + if ((o.outfilename == 0) && (! o.replace_input))
  52 + {
  53 + o.outfilename = QUtil::make_shared_cstr(filename);
  54 + }
  55 + else
  56 + {
  57 + usage("output file has already been given");
  58 + }
  59 + return *this;
  60 +}
  61 +
  62 +QPDFJob::Config&
  63 +QPDFJob::Config::replaceInput()
  64 +{
  65 + if ((o.outfilename == 0) && (! o.replace_input))
  66 + {
  67 + o.replace_input = true;
  68 + }
  69 + else
  70 + {
  71 + usage("replace-input can't be used"
  72 + " since output file has already been given");
  73 + }
  74 + return *this;
  75 +}
  76 +
  77 +QPDFJob::Config&
12 78 QPDFJob::Config::allowWeakCrypto()
13 79 {
14 80 o.allow_weak_crypto = true;
... ...