aboutsummaryrefslogtreecommitdiffstats
path: root/examples/pdf-mod-info.cc
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2022-04-02 23:14:10 +0200
committerJay Berkenbilt <ejb@ql.org>2022-04-04 14:10:40 +0200
commit12f1eb15ca3fed6310402847559a7c99d3c77847 (patch)
tree8935675b623c6f3b4914b8b44f7fa5f2816a9241 /examples/pdf-mod-info.cc
parentf20fa61eb4c323eb1642c69c236b3d9a1f8b2cdb (diff)
downloadqpdf-12f1eb15ca3fed6310402847559a7c99d3c77847.tar.zst
Programmatically apply new formatting to code
Run this: for i in **/*.cc **/*.c **/*.h **/*.hh; do clang-format < $i >| $i.new && mv $i.new $i done
Diffstat (limited to 'examples/pdf-mod-info.cc')
-rw-r--r--examples/pdf-mod-info.cc149
1 files changed, 56 insertions, 93 deletions
diff --git a/examples/pdf-mod-info.cc b/examples/pdf-mod-info.cc
index 4ee4cdd0..8666474f 100644
--- a/examples/pdf-mod-info.cc
+++ b/examples/pdf-mod-info.cc
@@ -1,49 +1,43 @@
// Author: Vitaliy Pavlyuk
#include <qpdf/QPDF.hh>
-#include <qpdf/QPDFWriter.hh>
#include <qpdf/QPDFObjectHandle.hh>
-#include <qpdf/QUtil.hh>
+#include <qpdf/QPDFWriter.hh>
#include <qpdf/QTC.hh>
+#include <qpdf/QUtil.hh>
#include <iostream>
-#include <string.h>
-#include <stdlib.h>
#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
static char const* version = "1.1";
static char const* whoami = 0;
-void usage()
+void
+usage()
{
- std::cerr
- << "Usage: " << whoami
- << " --in in_file [--out out_file] [--key key [--val val]?]+\n"
- << "Modifies/Adds/Removes PDF /Info entries in the in_file\n"
- << "and stores the result in out_file\n"
- << "Special mode: " << whoami << " --dump file\n"
- << "dumps all /Info entries to stdout\n";
+ std::cerr << "Usage: " << whoami
+ << " --in in_file [--out out_file] [--key key [--val val]?]+\n"
+ << "Modifies/Adds/Removes PDF /Info entries in the in_file\n"
+ << "and stores the result in out_file\n"
+ << "Special mode: " << whoami << " --dump file\n"
+ << "dumps all /Info entries to stdout\n";
exit(2);
}
-void dumpInfoDict(QPDF& pdf,
- std::ostream& os = std::cout,
- std::string const& sep = ":\t")
+void
+dumpInfoDict(
+ QPDF& pdf, std::ostream& os = std::cout, std::string const& sep = ":\t")
{
QPDFObjectHandle trailer = pdf.getTrailer();
- if (trailer.hasKey("/Info"))
- {
- for (auto& it: trailer.getKey("/Info").ditems())
- {
+ if (trailer.hasKey("/Info")) {
+ for (auto& it : trailer.getKey("/Info").ditems()) {
std::string val;
- if (it.second.isString())
- {
+ if (it.second.isString()) {
val = it.second.getStringValue();
- }
- else if (it.second.isName())
- {
+ } else if (it.second.isName()) {
val = it.second.getName();
- }
- else // according to PDF Spec 1.5, shouldn't happen
+ } else // according to PDF Spec 1.5, shouldn't happen
{
val = it.second.unparseResolved();
}
@@ -52,36 +46,32 @@ void dumpInfoDict(QPDF& pdf,
}
}
-void pdfDumpInfoDict(char const* fname)
+void
+pdfDumpInfoDict(char const* fname)
{
- try
- {
+ try {
QPDF pdf;
pdf.processFile(fname);
dumpInfoDict(pdf);
- }
- catch (std::exception& e)
- {
+ } catch (std::exception& e) {
std::cerr << e.what() << std::endl;
exit(2);
}
}
-int main(int argc, char* argv[])
+int
+main(int argc, char* argv[])
{
-
bool static_id = false;
std::map<std::string, std::string> Keys;
whoami = QUtil::getWhoami(argv[0]);
- if ((argc == 2) && (! strcmp(argv[1], "--version")) )
- {
+ if ((argc == 2) && (!strcmp(argv[1], "--version"))) {
std::cout << whoami << " version " << version << std::endl;
exit(0);
}
- if ((argc == 3) && (! strcmp(argv[1], "--dump")))
- {
+ if ((argc == 3) && (!strcmp(argv[1], "--dump"))) {
QTC::TC("examples", "pdf-mod-info --dump");
pdfDumpInfoDict(argv[2]);
exit(0);
@@ -91,59 +81,43 @@ int main(int argc, char* argv[])
char* fl_out = 0;
std::string cur_key;
- for (int i = 1; i < argc; ++i)
- {
- if ((! strcmp(argv[i], "--in")) && (++i < argc))
- {
+ for (int i = 1; i < argc; ++i) {
+ if ((!strcmp(argv[i], "--in")) && (++i < argc)) {
fl_in = argv[i];
- }
- else if ((! strcmp(argv[i], "--out")) && (++i < argc))
- {
+ } else if ((!strcmp(argv[i], "--out")) && (++i < argc)) {
fl_out = argv[i];
- }
- else if (! strcmp(argv[i], "--static-id")) // don't document
+ } else if (!strcmp(argv[i], "--static-id")) // don't document
{
static_id = true; // this should be used in test suites only
- }
- else if ((! strcmp(argv[i], "--key")) && (++i < argc))
- {
+ } else if ((!strcmp(argv[i], "--key")) && (++i < argc)) {
QTC::TC("examples", "pdf-mod-info -key");
cur_key = argv[i];
- if (! ((cur_key.length() > 0) && (cur_key.at(0) == '/')))
- {
+ if (!((cur_key.length() > 0) && (cur_key.at(0) == '/'))) {
cur_key = "/" + cur_key;
}
Keys[cur_key] = "";
- }
- else if ((! strcmp(argv[i], "--val")) && (++i < argc))
- {
- if (cur_key.empty())
- {
+ } else if ((!strcmp(argv[i], "--val")) && (++i < argc)) {
+ if (cur_key.empty()) {
QTC::TC("examples", "pdf-mod-info usage wrong val");
usage();
}
QTC::TC("examples", "pdf-mod-info -val");
Keys[cur_key] = argv[i];
cur_key.clear();
- }
- else
- {
+ } else {
QTC::TC("examples", "pdf-mod-info usage junk");
usage();
}
}
- if (! fl_in)
- {
+ if (!fl_in) {
QTC::TC("examples", "pdf-mod-info no in file");
usage();
}
- if (! fl_out)
- {
+ if (!fl_out) {
QTC::TC("examples", "pdf-mod-info in-place");
fl_out = fl_in;
}
- if (Keys.size() == 0)
- {
+ if (Keys.size() == 0) {
QTC::TC("examples", "pdf-mod-info no keys");
usage();
}
@@ -151,8 +125,7 @@ int main(int argc, char* argv[])
std::string fl_tmp = fl_out;
fl_tmp += ".tmp";
- try
- {
+ try {
QPDF file;
file.processFile(fl_in);
@@ -160,28 +133,22 @@ int main(int argc, char* argv[])
QPDFObjectHandle fileinfo;
for (std::map<std::string, std::string>::const_iterator it =
- Keys.begin(); Keys.end() != it; ++it)
- {
- if (! fileinfo.isInitialized())
- {
- if (filetrailer.hasKey("/Info"))
- {
+ Keys.begin();
+ Keys.end() != it;
+ ++it) {
+ if (!fileinfo.isInitialized()) {
+ if (filetrailer.hasKey("/Info")) {
QTC::TC("examples", "pdf-mod-info has info");
fileinfo = filetrailer.getKey("/Info");
- }
- else
- {
+ } else {
QTC::TC("examples", "pdf-mod-info file no info");
fileinfo = QPDFObjectHandle::newDictionary();
filetrailer.replaceKey("/Info", fileinfo);
}
}
- if (it->second == "")
- {
+ if (it->second == "") {
fileinfo.removeKey(it->first);
- }
- else
- {
+ } else {
QPDFObjectHandle elt = fileinfo.newString(it->second);
elt.makeDirect();
fileinfo.replaceKey(it->first, elt);
@@ -192,21 +159,17 @@ int main(int argc, char* argv[])
w.setLinearization(true);
w.setStaticID(static_id); // for testing only
w.write();
- }
- catch (std::exception& e)
- {
+ } catch (std::exception& e) {
std::cerr << e.what() << std::endl;
exit(2);
}
- try
- {
- (void) remove(fl_out);
- QUtil::os_wrapper("rename " + fl_tmp + " " + std::string(fl_out),
- rename(fl_tmp.c_str(), fl_out));
- }
- catch (std::exception& e)
- {
+ try {
+ (void)remove(fl_out);
+ QUtil::os_wrapper(
+ "rename " + fl_tmp + " " + std::string(fl_out),
+ rename(fl_tmp.c_str(), fl_out));
+ } catch (std::exception& e) {
std::cerr << e.what() << std::endl;
exit(2);
}