diff --git a/include/qpdf/QPDFJob.hh b/include/qpdf/QPDFJob.hh index cc079fc4..7d0bc3ee 100644 --- a/include/qpdf/QPDFJob.hh +++ b/include/qpdf/QPDFJob.hh @@ -310,7 +310,7 @@ class QPDFJob bool check_is_encrypted; bool check_requires_password; std::shared_ptr infilename; - char const* outfilename; + std::shared_ptr outfilename; // QXXXQ END-PUBLIC private: diff --git a/libqpdf/QPDFJob.cc b/libqpdf/QPDFJob.cc index 00546652..129436d6 100644 --- a/libqpdf/QPDFJob.cc +++ b/libqpdf/QPDFJob.cc @@ -414,7 +414,6 @@ QPDFJob::QPDFJob() : replace_input(false), check_is_encrypted(false), check_requires_password(false), - outfilename(0), m(new Members()) { } @@ -3257,7 +3256,8 @@ QPDFJob::setWriterOptions(QPDF& pdf, QPDFWriter& w) { w.registerProgressReporter( new ProgressReporter( - *(this->m->cout), this->m->message_prefix, o.outfilename)); + *(this->m->cout), this->m->message_prefix, + o.outfilename.get())); } } @@ -3268,27 +3268,27 @@ QPDFJob::doSplitPages(QPDF& pdf, bool& warnings) // Generate output file pattern std::string before; std::string after; - size_t len = strlen(o.outfilename); - char* num_spot = strstr(const_cast(o.outfilename), "%d"); + size_t len = strlen(o.outfilename.get()); + char* num_spot = strstr(const_cast(o.outfilename.get()), "%d"); if (num_spot != 0) { QTC::TC("qpdf", "qpdf split-pages %d"); - before = std::string(o.outfilename, - QIntC::to_size(num_spot - o.outfilename)); + before = std::string(o.outfilename.get(), + QIntC::to_size(num_spot - o.outfilename.get())); after = num_spot + 2; } else if ((len >= 4) && (QUtil::str_compare_nocase( - o.outfilename + len - 4, ".pdf") == 0)) + o.outfilename.get() + len - 4, ".pdf") == 0)) { QTC::TC("qpdf", "qpdf split-pages .pdf"); - before = std::string(o.outfilename, len - 4) + "-"; - after = o.outfilename + len - 4; + before = std::string(o.outfilename.get(), len - 4) + "-"; + after = o.outfilename.get() + len - 4; } else { QTC::TC("qpdf", "qpdf split-pages other"); - before = std::string(o.outfilename) + "-"; + before = std::string(o.outfilename.get()) + "-"; } if (shouldRemoveUnreferencedResources(pdf)) @@ -3385,24 +3385,25 @@ void QPDFJob::writeOutfile(QPDF& pdf) { QPDFJob& o = *this; // QXXXQ - std::string temp_out; + std::shared_ptr temp_out; if (o.replace_input) { // Append but don't prepend to the path to generate a // temporary name. This saves us from having to split the path // by directory and non-directory. - temp_out = std::string(o.infilename.get()) + ".~qpdf-temp#"; + temp_out = QUtil::make_shared_cstr( + std::string(o.infilename.get()) + ".~qpdf-temp#"); // o.outfilename will be restored to 0 before temp_out // goes out of scope. - o.outfilename = temp_out.c_str(); + o.outfilename = temp_out; } - else if (strcmp(o.outfilename, "-") == 0) + else if (strcmp(o.outfilename.get(), "-") == 0) { o.outfilename = 0; } { // Private scope so QPDFWriter will close the output file - QPDFWriter w(pdf, o.outfilename); + QPDFWriter w(pdf, o.outfilename.get()); setWriterOptions(pdf, w); w.write(); } @@ -3427,7 +3428,7 @@ QPDFJob::writeOutfile(QPDF& pdf) backup.append(1, '#'); } QUtil::rename_file(o.infilename.get(), backup.c_str()); - QUtil::rename_file(temp_out.c_str(), o.infilename.get()); + QUtil::rename_file(temp_out.get(), o.infilename.get()); if (warnings) { *(this->m->cerr) diff --git a/libqpdf/QPDFJob_argv.cc b/libqpdf/QPDFJob_argv.cc index 0fb7d408..9ad02a02 100644 --- a/libqpdf/QPDFJob_argv.cc +++ b/libqpdf/QPDFJob_argv.cc @@ -72,7 +72,7 @@ ArgParser::argPositional(char* arg) } else if (o.outfilename == 0) { - o.outfilename = arg; + o.outfilename = QUtil::make_shared_cstr(arg); } else { @@ -1494,7 +1494,7 @@ ArgParser::doFinalChecks() } if (o.require_outfile && o.outfilename && - (strcmp(o.outfilename, "-") == 0)) + (strcmp(o.outfilename.get(), "-") == 0)) { if (o.split_pages) { @@ -1514,7 +1514,7 @@ ArgParser::doFinalChecks() } if ((! o.split_pages) && - QUtil::same_file(o.infilename.get(), o.outfilename)) + QUtil::same_file(o.infilename.get(), o.outfilename.get())) { QTC::TC("qpdf", "qpdf same file error"); usage("input file and output file are the same;"