diff options
author | Andreas Müller <andreas.mueller@ost.ch> | 2022-02-26 09:14:14 +0100 |
---|---|---|
committer | Andreas Müller <andreas.mueller@ost.ch> | 2022-02-26 09:14:14 +0100 |
commit | 11c94692a0e4936667ed1a646b3d7bcdb26ebdee (patch) | |
tree | 12bb205d9d069b0a37e3f0d8ce7b650967f5916f /vorlesungsnotizen/bin/numberpages.cpp | |
parent | changes (diff) | |
download | SeminarSpezielleFunktionen-11c94692a0e4936667ed1a646b3d7bcdb26ebdee.tar.gz SeminarSpezielleFunktionen-11c94692a0e4936667ed1a646b3d7bcdb26ebdee.zip |
add page numbering program
Diffstat (limited to '')
-rw-r--r-- | vorlesungsnotizen/bin/numberpages.cpp | 171 |
1 files changed, 171 insertions, 0 deletions
diff --git a/vorlesungsnotizen/bin/numberpages.cpp b/vorlesungsnotizen/bin/numberpages.cpp new file mode 100644 index 0000000..b990453 --- /dev/null +++ b/vorlesungsnotizen/bin/numberpages.cpp @@ -0,0 +1,171 @@ +/* + * numberpages.cpp -- driver program to number pages in a PDF, this was + * mainly necessary because the shell breaks blanks + * in the section option string + * + * (c) 2022 Prof Dr Andreas Müller, OST Ostschweizer Fachhochschule + */ +#include <cstdio> +#include <cstdlib> +#include <getopt.h> +#include <iostream> +#include <sstream> +#include <string> +#include <sys/stat.h> + +void usage(char *progname) { + std::cout << "usage: " << progname << " [ -f ] [ -s section ] <unnumbered.pdf> <numbered.pdf>" << std::endl; +} + +struct option longoptions[] = { +{ "debug", no_argument, NULL, 'd' }, +{ "section", required_argument, NULL, 's' }, +{ "force", no_argument, NULL, 'f' }, +{ NULL, no_argument, NULL, 0 } +}; + +int main(int argc, char *argv[]) { + bool force = false; + bool debug = false; + std::string section; + int c, longindex, rc; + while (EOF != (c = getopt_long(argc, argv, "dfhs:?", longoptions, &longindex))) { + switch (c) { + case 'd': + debug = true; + break; + case 'f': + force = true; + break; + case 'h': + case '?': + usage(argv[0]); + return EXIT_SUCCESS; + case 's': + section = std::string(optarg); + if (debug) { + std::cout << "section string: " << section; + std::cout << std::endl; + } + break; + } + } + + // remaining arguments are file names + if ((argc - optind) < 2) { + std::cerr << "not enough arguments" << std::endl; + usage(argv[0]); + return EXIT_FAILURE; + } + std::string unnumbered = std::string(argv[optind++]); + std::string numbered = std::string(argv[optind++]); + if (debug) { + std::cout << "numbering " << unnumbered; + std::cout << ", sending output to " << unnumbered; + std::cout << std::endl; + } + + // make sure input file does exist + struct stat sb; + rc = stat(unnumbered.c_str(), &sb); + if ((0 == rc) && S_ISREG(sb.st_mode)) { + if (debug) { + std::cout << "input file " << unnumbered << " found"; + std::cout << std::endl; + } + } else { + std::cerr << "cannot find " << unnumbered << std::endl; + return EXIT_FAILURE; + } + + // make sure the "unnumbered.pdf" file does not exist + rc = stat("unnumbered.pdf", &sb); + if (0 == rc) { + std::cerr << "file unnumbered.pdf exists, clean up first"; + std::cerr << std::endl; + return EXIT_FAILURE; + } + + // test for existence of output file + rc = stat(numbered.c_str(), &sb); + if ((!force) && (0 == rc)) { + std::cerr << "output file exists, use --force option"; + std::cerr << std::endl; + return EXIT_FAILURE; + } + + // make sure the numbered.tex is present + rc = stat("numbered.tex", &sb); + if ((0 != rc) || (!S_ISREG(sb.st_mode))) { + std::cerr << "file 'numbered.tex' is missing" << std::endl; + return EXIT_FAILURE; + } + + // remove the "numbered.pdf" if it exists + if (debug) { + std::cout << "removing numbered.pdf" << std::endl; + } + unlink("numbered.pdf"); + + // copy the file + { + std::ostringstream out; + out << "cp '" << unnumbered << "' unnumbered.pdf"; + out.flush(); + std::string copycommand = out.str(); + rc = system(copycommand.c_str()); + if (0 != rc) { + std::cerr << "could not copy " << unnumbered; + std::cerr << " to unnumbered.pdf: " << rc << std::endl; + return EXIT_FAILURE; + } + if (debug) { + std::cout << "file " << unnumbered << " copied to "; + std::cout << "unnumbered.pdf" << std::endl; + } + } + + // prepare command line + std::ostringstream out; + out << "pdflatex "; + if (section.size() > 0) { + out << "\\\\def\\\\sectionnumber{" << section << "} "; + out << "\\\\input{numbered.tex}"; + } else { + out << unnumbered; + } + if (debug) { + std::cout << "command line: " << out.str() << std::endl; + } + + // run the command + rc = system(out.str().c_str()); + if (0 != rc) { + std::cerr << "pdflatex failed: " << rc << std::endl; + return EXIT_FAILURE; + } + + // check for the existence of the output file + rc = stat("numbered.pdf", &sb); + if ((0 != rc) || (!S_ISREG(sb.st_mode))) { + std::cerr << "output file 'numbered.pdf' not found"; + std::cerr << std::endl; + return EXIT_FAILURE; + } + + // rename the output file + rc = rename("numbered.pdf", numbered.c_str()); + if (rc != 0) { + std::string error(strerror(errno)); + std::cerr << "cannot rename file to '" << numbered << ": "; + std::cerr << error << std::endl; + return EXIT_FAILURE; + } + + // perform some cleanup + unlink("unnumbered.pdf"); + unlink("numbered.log"); + unlink("numbered.aux"); + + return EXIT_SUCCESS; +} |