aboutsummaryrefslogtreecommitdiffstats
path: root/examples/qpdfjob-save-attachment.cc
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2022-06-18 20:16:56 +0200
committerJay Berkenbilt <ejb@ql.org>2022-06-19 14:46:58 +0200
commit8e361d98f0bb23d58cbc773367ba76dffced7bdb (patch)
treec3c0aebbe88f51aa43c3eb8a586f8b795b8b5a45 /examples/qpdfjob-save-attachment.cc
parent8130d50e3b5aa0235a133c3c5a3018ac01afb5e1 (diff)
downloadqpdf-8e361d98f0bb23d58cbc773367ba76dffced7bdb.tar.zst
Add examples for output capture (fixes #691)
Diffstat (limited to 'examples/qpdfjob-save-attachment.cc')
-rw-r--r--examples/qpdfjob-save-attachment.cc51
1 files changed, 51 insertions, 0 deletions
diff --git a/examples/qpdfjob-save-attachment.cc b/examples/qpdfjob-save-attachment.cc
new file mode 100644
index 00000000..2c08620a
--- /dev/null
+++ b/examples/qpdfjob-save-attachment.cc
@@ -0,0 +1,51 @@
+#include <qpdf/Pl_StdioFile.hh>
+#include <qpdf/QPDFJob.hh>
+#include <qpdf/QPDFLogger.hh>
+#include <qpdf/QUtil.hh>
+
+// This example demonstrates how we can redirect where saved output
+// goes by calling the default logger's setSave method before running
+// something with QPDFJob. See qpdfjob-c-save-attachment.c for an
+// implementation that uses the C API.
+
+int
+main(int argc, char* argv[])
+{
+ auto whoami = QUtil::getWhoami(argv[0]);
+
+ if (argc != 4) {
+ std::cerr << "Usage: " << whoami << " file attachment-key outfile"
+ << std::endl;
+ exit(2);
+ }
+
+ char const* filename = argv[1];
+ char const* key = argv[2];
+ char const* outfilename = argv[3];
+ std::string attachment_arg = "--show-attachment=";
+ attachment_arg += key;
+
+ char const* j_argv[] = {
+ whoami,
+ filename,
+ attachment_arg.c_str(),
+ "--",
+ nullptr,
+ };
+
+ QUtil::FileCloser fc(QUtil::safe_fopen(outfilename, "wb"));
+ auto save = std::make_shared<Pl_StdioFile>("capture", fc.f);
+ QPDFLogger::defaultLogger()->setSave(save, false);
+
+ try {
+ QPDFJob j;
+ j.initializeFromArgv(j_argv);
+ j.run();
+ } catch (std::exception& e) {
+ std::cerr << whoami << ": " << e.what() << std::endl;
+ exit(2);
+ }
+
+ std::cout << whoami << ": wrote attachment to " << outfilename << std::endl;
+ return 0;
+}