aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2021-02-18 14:03:32 +0100
committerJay Berkenbilt <ejb@ql.org>2021-02-18 15:59:03 +0100
commit0a52e60ececc3ed7430dacaf4e433f04d91efbce (patch)
tree1699bca5335b0a19934f149d5adce4af2bcbbc25
parent0b1623d07db963ecf3789aba7163321812cba88e (diff)
downloadqpdf-0a52e60ececc3ed7430dacaf4e433f04d91efbce.tar.zst
Use QUtil::path_basename
-rw-r--r--examples/pdf-attach-file.cc11
-rw-r--r--qpdf/qpdf.cc12
-rw-r--r--qpdf/qtest/qpdf.test8
3 files changed, 5 insertions, 26 deletions
diff --git a/examples/pdf-attach-file.cc b/examples/pdf-attach-file.cc
index 903f30eb..0c046000 100644
--- a/examples/pdf-attach-file.cc
+++ b/examples/pdf-attach-file.cc
@@ -60,16 +60,7 @@ static void process(char const* infilename, char const* password,
">>"));
// Create a file spec.
- std::string key(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::string key = QUtil::path_basename(attachment);
std::cout << whoami << ": attaching " << attachment << " as " << key
<< std::endl;
auto fs = QPDFFileSpecObjectHelper::createFileSpec(q, key, attachment);
diff --git a/qpdf/qpdf.cc b/qpdf/qpdf.cc
index 8de2ab8b..0c0b344f 100644
--- a/qpdf/qpdf.cc
+++ b/qpdf/qpdf.cc
@@ -2873,16 +2873,10 @@ ArgParser::argEndAddAttachment()
{
usage("add attachment: no path specified");
}
- std::string last_element = cur.path;
- size_t pathsep = cur.path.find_last_of("/\\");
- if (pathsep != std::string::npos)
+ std::string last_element = QUtil::path_basename(cur.path);
+ if (last_element.empty())
{
- last_element = cur.path.substr(pathsep + 1);
- if (last_element.empty())
- {
- usage("path for --add-attachment may not end"
- " with a path separator");
- }
+ usage("path for --add-attachment may not be empty");
}
if (cur.filename.empty())
{
diff --git a/qpdf/qtest/qpdf.test b/qpdf/qtest/qpdf.test
index c033e7ef..4ed3ac33 100644
--- a/qpdf/qtest/qpdf.test
+++ b/qpdf/qtest/qpdf.test
@@ -523,7 +523,7 @@ $td->runtest("page operations on form xobject",
show_ntests();
# ----------
$td->notify("--- File Attachments ---");
-$n_tests += 34;
+$n_tests += 33;
open(F, ">auto-txt") or die;
print F "from file";
@@ -598,12 +598,6 @@ $td->runtest("add attachment: bad mod date",
$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->COMMAND => "qpdf minimal.pdf a.pdf" .
" --add-attachment --"},
{$td->REGEXP => ".*add attachment: no path specified.*",
$td->EXIT_STATUS => 2},