Use QUtil::path_basename

This commit is contained in:
Jay Berkenbilt 2021-02-18 08:03:32 -05:00
parent 0b1623d07d
commit 0a52e60ece
3 changed files with 5 additions and 26 deletions

View File

@ -60,16 +60,7 @@ static void process(char const* infilename, char const* password,
">>")); ">>"));
// Create a file spec. // Create a file spec.
std::string key(attachment); std::string key = QUtil::path_basename(attachment);
size_t pos = key.find_last_of("/\\");
if (pos != std::string::npos)
{
key = key.substr(pos + 1);
}
if (key.empty())
{
throw std::runtime_error("can't get last path element of attachment");
}
std::cout << whoami << ": attaching " << attachment << " as " << key std::cout << whoami << ": attaching " << attachment << " as " << key
<< std::endl; << std::endl;
auto fs = QPDFFileSpecObjectHelper::createFileSpec(q, key, attachment); auto fs = QPDFFileSpecObjectHelper::createFileSpec(q, key, attachment);

View File

@ -2873,16 +2873,10 @@ ArgParser::argEndAddAttachment()
{ {
usage("add attachment: no path specified"); usage("add attachment: no path specified");
} }
std::string last_element = cur.path; std::string last_element = QUtil::path_basename(cur.path);
size_t pathsep = cur.path.find_last_of("/\\"); if (last_element.empty())
if (pathsep != std::string::npos)
{ {
last_element = cur.path.substr(pathsep + 1); usage("path for --add-attachment may not be empty");
if (last_element.empty())
{
usage("path for --add-attachment may not end"
" with a path separator");
}
} }
if (cur.filename.empty()) if (cur.filename.empty())
{ {

View File

@ -523,7 +523,7 @@ $td->runtest("page operations on form xobject",
show_ntests(); show_ntests();
# ---------- # ----------
$td->notify("--- File Attachments ---"); $td->notify("--- File Attachments ---");
$n_tests += 34; $n_tests += 33;
open(F, ">auto-txt") or die; open(F, ">auto-txt") or die;
print F "from file"; print F "from file";
@ -596,12 +596,6 @@ $td->runtest("add attachment: bad mod date",
".*mime type should be specified as type/subtype.*", ".*mime type should be specified as type/subtype.*",
$td->EXIT_STATUS => 2}, $td->EXIT_STATUS => 2},
$td->NORMALIZE_NEWLINES); $td->NORMALIZE_NEWLINES);
$td->runtest("add attachment: trailing slash",
{$td->COMMAND => "qpdf minimal.pdf a.pdf" .
" --add-attachment auto-txt/ --"},
{$td->REGEXP => ".*may not end with a path separator.*",
$td->EXIT_STATUS => 2},
$td->NORMALIZE_NEWLINES);
$td->runtest("add attachment: trailing slash", $td->runtest("add attachment: trailing slash",
{$td->COMMAND => "qpdf minimal.pdf a.pdf" . {$td->COMMAND => "qpdf minimal.pdf a.pdf" .
" --add-attachment --"}, " --add-attachment --"},