2
1
mirror of https://github.com/qpdf/qpdf.git synced 2025-01-03 15:17:29 +00:00

QPDFJob: convert outfilename to shared pointer

This commit is contained in:
Jay Berkenbilt 2022-01-22 18:30:39 -05:00
parent e5edfc786f
commit e4905983d2
3 changed files with 21 additions and 20 deletions

View File

@ -310,7 +310,7 @@ class QPDFJob
bool check_is_encrypted; bool check_is_encrypted;
bool check_requires_password; bool check_requires_password;
std::shared_ptr<char> infilename; std::shared_ptr<char> infilename;
char const* outfilename; std::shared_ptr<char> outfilename;
// QXXXQ END-PUBLIC // QXXXQ END-PUBLIC
private: private:

View File

@ -414,7 +414,6 @@ QPDFJob::QPDFJob() :
replace_input(false), replace_input(false),
check_is_encrypted(false), check_is_encrypted(false),
check_requires_password(false), check_requires_password(false),
outfilename(0),
m(new Members()) m(new Members())
{ {
} }
@ -3257,7 +3256,8 @@ QPDFJob::setWriterOptions(QPDF& pdf, QPDFWriter& w)
{ {
w.registerProgressReporter( w.registerProgressReporter(
new ProgressReporter( 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 // Generate output file pattern
std::string before; std::string before;
std::string after; std::string after;
size_t len = strlen(o.outfilename); size_t len = strlen(o.outfilename.get());
char* num_spot = strstr(const_cast<char*>(o.outfilename), "%d"); char* num_spot = strstr(const_cast<char*>(o.outfilename.get()), "%d");
if (num_spot != 0) if (num_spot != 0)
{ {
QTC::TC("qpdf", "qpdf split-pages %d"); QTC::TC("qpdf", "qpdf split-pages %d");
before = std::string(o.outfilename, before = std::string(o.outfilename.get(),
QIntC::to_size(num_spot - o.outfilename)); QIntC::to_size(num_spot - o.outfilename.get()));
after = num_spot + 2; after = num_spot + 2;
} }
else if ((len >= 4) && else if ((len >= 4) &&
(QUtil::str_compare_nocase( (QUtil::str_compare_nocase(
o.outfilename + len - 4, ".pdf") == 0)) o.outfilename.get() + len - 4, ".pdf") == 0))
{ {
QTC::TC("qpdf", "qpdf split-pages .pdf"); QTC::TC("qpdf", "qpdf split-pages .pdf");
before = std::string(o.outfilename, len - 4) + "-"; before = std::string(o.outfilename.get(), len - 4) + "-";
after = o.outfilename + len - 4; after = o.outfilename.get() + len - 4;
} }
else else
{ {
QTC::TC("qpdf", "qpdf split-pages other"); QTC::TC("qpdf", "qpdf split-pages other");
before = std::string(o.outfilename) + "-"; before = std::string(o.outfilename.get()) + "-";
} }
if (shouldRemoveUnreferencedResources(pdf)) if (shouldRemoveUnreferencedResources(pdf))
@ -3385,24 +3385,25 @@ void
QPDFJob::writeOutfile(QPDF& pdf) QPDFJob::writeOutfile(QPDF& pdf)
{ {
QPDFJob& o = *this; // QXXXQ QPDFJob& o = *this; // QXXXQ
std::string temp_out; std::shared_ptr<char> temp_out;
if (o.replace_input) if (o.replace_input)
{ {
// Append but don't prepend to the path to generate a // Append but don't prepend to the path to generate a
// temporary name. This saves us from having to split the path // temporary name. This saves us from having to split the path
// by directory and non-directory. // 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 // o.outfilename will be restored to 0 before temp_out
// goes out of scope. // 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; o.outfilename = 0;
} }
{ {
// Private scope so QPDFWriter will close the output file // Private scope so QPDFWriter will close the output file
QPDFWriter w(pdf, o.outfilename); QPDFWriter w(pdf, o.outfilename.get());
setWriterOptions(pdf, w); setWriterOptions(pdf, w);
w.write(); w.write();
} }
@ -3427,7 +3428,7 @@ QPDFJob::writeOutfile(QPDF& pdf)
backup.append(1, '#'); backup.append(1, '#');
} }
QUtil::rename_file(o.infilename.get(), backup.c_str()); 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) if (warnings)
{ {
*(this->m->cerr) *(this->m->cerr)

View File

@ -72,7 +72,7 @@ ArgParser::argPositional(char* arg)
} }
else if (o.outfilename == 0) else if (o.outfilename == 0)
{ {
o.outfilename = arg; o.outfilename = QUtil::make_shared_cstr(arg);
} }
else else
{ {
@ -1494,7 +1494,7 @@ ArgParser::doFinalChecks()
} }
if (o.require_outfile && o.outfilename && if (o.require_outfile && o.outfilename &&
(strcmp(o.outfilename, "-") == 0)) (strcmp(o.outfilename.get(), "-") == 0))
{ {
if (o.split_pages) if (o.split_pages)
{ {
@ -1514,7 +1514,7 @@ ArgParser::doFinalChecks()
} }
if ((! o.split_pages) && 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"); QTC::TC("qpdf", "qpdf same file error");
usage("input file and output file are the same;" usage("input file and output file are the same;"