Commit 0a52e60ececc3ed7430dacaf4e433f04d91efbce

Authored by Jay Berkenbilt
1 parent 0b1623d0

Use QUtil::path_basename

examples/pdf-attach-file.cc
@@ -60,16 +60,7 @@ static void process(char const* infilename, char const* password, @@ -60,16 +60,7 @@ static void process(char const* infilename, char const* password,
60 ">>")); 60 ">>"));
61 61
62 // Create a file spec. 62 // Create a file spec.
63 - std::string key(attachment);  
64 - size_t pos = key.find_last_of("/\\");  
65 - if (pos != std::string::npos)  
66 - {  
67 - key = key.substr(pos + 1);  
68 - }  
69 - if (key.empty())  
70 - {  
71 - throw std::runtime_error("can't get last path element of attachment");  
72 - } 63 + std::string key = QUtil::path_basename(attachment);
73 std::cout << whoami << ": attaching " << attachment << " as " << key 64 std::cout << whoami << ": attaching " << attachment << " as " << key
74 << std::endl; 65 << std::endl;
75 auto fs = QPDFFileSpecObjectHelper::createFileSpec(q, key, attachment); 66 auto fs = QPDFFileSpecObjectHelper::createFileSpec(q, key, attachment);
qpdf/qpdf.cc
@@ -2873,16 +2873,10 @@ ArgParser::argEndAddAttachment() @@ -2873,16 +2873,10 @@ ArgParser::argEndAddAttachment()
2873 { 2873 {
2874 usage("add attachment: no path specified"); 2874 usage("add attachment: no path specified");
2875 } 2875 }
2876 - std::string last_element = cur.path;  
2877 - size_t pathsep = cur.path.find_last_of("/\\");  
2878 - if (pathsep != std::string::npos) 2876 + std::string last_element = QUtil::path_basename(cur.path);
  2877 + if (last_element.empty())
2879 { 2878 {
2880 - last_element = cur.path.substr(pathsep + 1);  
2881 - if (last_element.empty())  
2882 - {  
2883 - usage("path for --add-attachment may not end"  
2884 - " with a path separator");  
2885 - } 2879 + usage("path for --add-attachment may not be empty");
2886 } 2880 }
2887 if (cur.filename.empty()) 2881 if (cur.filename.empty())
2888 { 2882 {
qpdf/qtest/qpdf.test
@@ -523,7 +523,7 @@ $td-&gt;runtest(&quot;page operations on form xobject&quot;, @@ -523,7 +523,7 @@ $td-&gt;runtest(&quot;page operations on form xobject&quot;,
523 show_ntests(); 523 show_ntests();
524 # ---------- 524 # ----------
525 $td->notify("--- File Attachments ---"); 525 $td->notify("--- File Attachments ---");
526 -$n_tests += 34; 526 +$n_tests += 33;
527 527
528 open(F, ">auto-txt") or die; 528 open(F, ">auto-txt") or die;
529 print F "from file"; 529 print F "from file";
@@ -598,12 +598,6 @@ $td-&gt;runtest(&quot;add attachment: bad mod date&quot;, @@ -598,12 +598,6 @@ $td-&gt;runtest(&quot;add attachment: bad mod date&quot;,
598 $td->NORMALIZE_NEWLINES); 598 $td->NORMALIZE_NEWLINES);
599 $td->runtest("add attachment: trailing slash", 599 $td->runtest("add attachment: trailing slash",
600 {$td->COMMAND => "qpdf minimal.pdf a.pdf" . 600 {$td->COMMAND => "qpdf minimal.pdf a.pdf" .
601 - " --add-attachment auto-txt/ --"},  
602 - {$td->REGEXP => ".*may not end with a path separator.*",  
603 - $td->EXIT_STATUS => 2},  
604 - $td->NORMALIZE_NEWLINES);  
605 -$td->runtest("add attachment: trailing slash",  
606 - {$td->COMMAND => "qpdf minimal.pdf a.pdf" .  
607 " --add-attachment --"}, 601 " --add-attachment --"},
608 {$td->REGEXP => ".*add attachment: no path specified.*", 602 {$td->REGEXP => ".*add attachment: no path specified.*",
609 $td->EXIT_STATUS => 2}, 603 $td->EXIT_STATUS => 2},