diff options
author | Albert Astals Cid <aacid@kde.org> | 2020-07-03 23:51:42 +0200 |
---|---|---|
committer | Albert Astals Cid <aacid@kde.org> | 2020-07-03 23:51:42 +0200 |
commit | 814fbda28cc8a37fed3134c2db8da28f86fb5ee0 (patch) | |
tree | 77872b408199925ebba6a68b0dccaa0d29274c3f /utils/pdfseparate.cc | |
parent | 0d48722746b9702e219df58ad14cee6184a62bef (diff) |
Run clang-format
find . \( -name "*.cpp" -or -name "*.h" -or -name "*.c" -or -name "*.cc" \) -exec clang-format -i {} \;
If you reached this file doing a git blame, please see README.contributors (instructions added 2 commits in the future to this one)
Diffstat (limited to 'utils/pdfseparate.cc')
-rw-r--r-- | utils/pdfseparate.cc | 240 |
1 files changed, 113 insertions, 127 deletions
diff --git a/utils/pdfseparate.cc b/utils/pdfseparate.cc index 240022a6..7fc5d029 100644 --- a/utils/pdfseparate.cc +++ b/utils/pdfseparate.cc @@ -34,148 +34,134 @@ static int lastPage = 0; static bool printVersion = false; static bool printHelp = false; -static const ArgDesc argDesc[] = { - {"-f", argInt, &firstPage, 0, - "first page to extract"}, - {"-l", argInt, &lastPage, 0, - "last page to extract"}, - {"-v", argFlag, &printVersion, 0, - "print copyright and version info"}, - {"-h", argFlag, &printHelp, 0, - "print usage information"}, - {"-help", argFlag, &printHelp, 0, - "print usage information"}, - {"--help", argFlag, &printHelp, 0, - "print usage information"}, - {"-?", argFlag, &printHelp, 0, - "print usage information"}, - {} -}; +static const ArgDesc argDesc[] = { { "-f", argInt, &firstPage, 0, "first page to extract" }, + { "-l", argInt, &lastPage, 0, "last page to extract" }, + { "-v", argFlag, &printVersion, 0, "print copyright and version info" }, + { "-h", argFlag, &printHelp, 0, "print usage information" }, + { "-help", argFlag, &printHelp, 0, "print usage information" }, + { "--help", argFlag, &printHelp, 0, "print usage information" }, + { "-?", argFlag, &printHelp, 0, "print usage information" }, + {} }; -static bool extractPages (const char *srcFileName, const char *destFileName) { - char pathName[4096]; - GooString *gfileName = new GooString (srcFileName); - PDFDoc *doc = new PDFDoc (gfileName, nullptr, nullptr, nullptr); +static bool extractPages(const char *srcFileName, const char *destFileName) +{ + char pathName[4096]; + GooString *gfileName = new GooString(srcFileName); + PDFDoc *doc = new PDFDoc(gfileName, nullptr, nullptr, nullptr); - if (!doc->isOk()) { - error(errSyntaxError, -1, "Could not extract page(s) from damaged file ('{0:s}')", srcFileName); - delete doc; - return false; - } + if (!doc->isOk()) { + error(errSyntaxError, -1, "Could not extract page(s) from damaged file ('{0:s}')", srcFileName); + delete doc; + return false; + } - // destFileName can have multiple %% and one %d - // We use auxDestFileName to replace all the valid % appearances - // by 'A' (random char that is not %), if at the end of replacing - // any of the valid appearances there is still any % around, the - // pattern is wrong - if (firstPage == 0 && lastPage == 0) { - firstPage = 1; - lastPage = doc->getNumPages(); - } - if (lastPage == 0) - lastPage = doc->getNumPages(); - if (firstPage == 0) - firstPage = 1; - if (lastPage < firstPage) { - error(errCommandLine, -1, - "Wrong page range given: the first page ({0:d}) can not be after the last page ({1:d}).", - firstPage, lastPage); - delete doc; - return false; - } - bool foundmatch = false; - char *auxDestFileName = strdup(destFileName); - char *p = strstr(auxDestFileName, "%d"); - if (p != nullptr) { - foundmatch = true; - *p = 'A'; - } else { - char pattern[6]; - for (int i = 2; i < 10; i++) { - sprintf(pattern, "%%0%dd", i); - p = strstr(auxDestFileName, pattern); - if (p != nullptr) { - foundmatch = true; - *p = 'A'; - break; - } + // destFileName can have multiple %% and one %d + // We use auxDestFileName to replace all the valid % appearances + // by 'A' (random char that is not %), if at the end of replacing + // any of the valid appearances there is still any % around, the + // pattern is wrong + if (firstPage == 0 && lastPage == 0) { + firstPage = 1; + lastPage = doc->getNumPages(); + } + if (lastPage == 0) + lastPage = doc->getNumPages(); + if (firstPage == 0) + firstPage = 1; + if (lastPage < firstPage) { + error(errCommandLine, -1, "Wrong page range given: the first page ({0:d}) can not be after the last page ({1:d}).", firstPage, lastPage); + delete doc; + return false; + } + bool foundmatch = false; + char *auxDestFileName = strdup(destFileName); + char *p = strstr(auxDestFileName, "%d"); + if (p != nullptr) { + foundmatch = true; + *p = 'A'; + } else { + char pattern[6]; + for (int i = 2; i < 10; i++) { + sprintf(pattern, "%%0%dd", i); + p = strstr(auxDestFileName, pattern); + if (p != nullptr) { + foundmatch = true; + *p = 'A'; + break; + } + } + } + if (!foundmatch && firstPage != lastPage) { + error(errSyntaxError, -1, "'{0:s}' must contain '%d' (or any variant respecting printf format) if more than one page should be extracted, in order to print the page number", destFileName); + free(auxDestFileName); + delete doc; + return false; } - } - if (!foundmatch && firstPage != lastPage) { - error(errSyntaxError, -1, "'{0:s}' must contain '%d' (or any variant respecting printf format) if more than one page should be extracted, in order to print the page number", destFileName); - free(auxDestFileName); - delete doc; - return false; - } - // at this point auxDestFileName can only contain %% - p = strstr(auxDestFileName, "%%"); - while (p != nullptr) { - *p = 'A'; - *(p + 1) = 'A'; - p = strstr(p, "%%"); - } + // at this point auxDestFileName can only contain %% + p = strstr(auxDestFileName, "%%"); + while (p != nullptr) { + *p = 'A'; + *(p + 1) = 'A'; + p = strstr(p, "%%"); + } - // at this point any other % is wrong - p = strstr(auxDestFileName, "%"); - if (p != nullptr) { - error(errSyntaxError, -1, "'{0:s}' can only contain one '%d' pattern", destFileName); + // at this point any other % is wrong + p = strstr(auxDestFileName, "%"); + if (p != nullptr) { + error(errSyntaxError, -1, "'{0:s}' can only contain one '%d' pattern", destFileName); + free(auxDestFileName); + delete doc; + return false; + } free(auxDestFileName); - delete doc; - return false; - } - free(auxDestFileName); - - for (int pageNo = firstPage; pageNo <= lastPage; pageNo++) { - snprintf (pathName, sizeof (pathName) - 1, destFileName, pageNo); - GooString *gpageName = new GooString (pathName); - PDFDoc *pagedoc = new PDFDoc (new GooString (srcFileName), nullptr, nullptr, nullptr); - int errCode = pagedoc->savePageAs(gpageName, pageNo); - if ( errCode != errNone) { - delete gpageName; - delete doc; - delete pagedoc; - return false; + + for (int pageNo = firstPage; pageNo <= lastPage; pageNo++) { + snprintf(pathName, sizeof(pathName) - 1, destFileName, pageNo); + GooString *gpageName = new GooString(pathName); + PDFDoc *pagedoc = new PDFDoc(new GooString(srcFileName), nullptr, nullptr, nullptr); + int errCode = pagedoc->savePageAs(gpageName, pageNo); + if (errCode != errNone) { + delete gpageName; + delete doc; + delete pagedoc; + return false; + } + delete pagedoc; + delete gpageName; } - delete pagedoc; - delete gpageName; - } - delete doc; - return true; + delete doc; + return true; } -int -main (int argc, char *argv[]) +int main(int argc, char *argv[]) { - bool ok; - int exitCode; + bool ok; + int exitCode; - exitCode = 99; + exitCode = 99; - // parse args - Win32Console win32console(&argc, &argv); - ok = parseArgs (argDesc, &argc, argv); - if (!ok || argc != 3 || printVersion || printHelp) - { - fprintf (stderr, "pdfseparate version %s\n", PACKAGE_VERSION); - fprintf (stderr, "%s\n", popplerCopyright); - fprintf (stderr, "%s\n", xpdfCopyright); - if (!printVersion) - { - printUsage ("pdfseparate", "<PDF-sourcefile> <PDF-pattern-destfile>", - argDesc); - } - if (printVersion || printHelp) - exitCode = 0; - goto err0; + // parse args + Win32Console win32console(&argc, &argv); + ok = parseArgs(argDesc, &argc, argv); + if (!ok || argc != 3 || printVersion || printHelp) { + fprintf(stderr, "pdfseparate version %s\n", PACKAGE_VERSION); + fprintf(stderr, "%s\n", popplerCopyright); + fprintf(stderr, "%s\n", xpdfCopyright); + if (!printVersion) { + printUsage("pdfseparate", "<PDF-sourcefile> <PDF-pattern-destfile>", argDesc); + } + if (printVersion || printHelp) + exitCode = 0; + goto err0; + } + globalParams = std::make_unique<GlobalParams>(); + ok = extractPages(argv[1], argv[2]); + if (ok) { + exitCode = 0; } - globalParams = std::make_unique<GlobalParams>(); - ok = extractPages (argv[1], argv[2]); - if (ok) { - exitCode = 0; - } err0: - return exitCode; + return exitCode; } |