summaryrefslogtreecommitdiff
path: root/utils
diff options
context:
space:
mode:
authorAlbert Astals Cid <aacid@kde.org>2013-10-24 01:03:02 +0200
committerAlbert Astals Cid <aacid@kde.org>2013-10-24 01:04:48 +0200
commit9f09b9596f1fc52481914019d68c8f9b85b5c438 (patch)
treee26b22b483c3cc1e8096f0db5c5ce38d280bd463 /utils
parentbd893d4a543a6cc3a525655f37def38440944f28 (diff)
parent61f79b8447c3ac8ab5a26e79e0c28053ffdccf75 (diff)
Merge remote-tracking branch 'origin/poppler-0.24'
Diffstat (limited to 'utils')
-rw-r--r--utils/pdfseparate.cc60
1 files changed, 44 insertions, 16 deletions
diff --git a/utils/pdfseparate.cc b/utils/pdfseparate.cc
index 2844dc52..924e5c7d 100644
--- a/utils/pdfseparate.cc
+++ b/utils/pdfseparate.cc
@@ -21,6 +21,7 @@
#include "PDFDoc.h"
#include "ErrorCodes.h"
#include "GlobalParams.h"
+#include <ctype.h>
static int firstPage = 0;
static int lastPage = 0;
@@ -55,6 +56,12 @@ bool extractPages (const char *srcFileName, const char *destFileName) {
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
+ char *auxDestFileName = strdup(destFileName);
if (firstPage == 0 && lastPage == 0) {
firstPage = 1;
lastPage = doc->getNumPages();
@@ -63,25 +70,46 @@ bool extractPages (const char *srcFileName, const char *destFileName) {
lastPage = doc->getNumPages();
if (firstPage == 0)
firstPage = 1;
- if (firstPage != lastPage) {
- bool foundmatch = false;
- if (strstr(destFileName, "%d") != NULL) {
- foundmatch = true;
- } else {
- char pattern[5];
- for (int i = 2; i < 10; i++) {
- sprintf(pattern, "%%0%dd", i);
- if (strstr(destFileName, pattern) != NULL) {
- foundmatch = true;
- break;
- }
+ bool foundmatch = false;
+ char *p = strstr(auxDestFileName, "%d");
+ if (p != NULL) {
+ foundmatch = true;
+ *p = 'A';
+ } else {
+ char pattern[5];
+ for (int i = 2; i < 10; i++) {
+ sprintf(pattern, "%%0%dd", i);
+ p = strstr(auxDestFileName, pattern);
+ if (p != NULL) {
+ foundmatch = true;
+ *p = 'A';
+ break;
}
}
- if (!foundmatch) {
- error(errSyntaxError, -1, "'{0:s}' must contain '%%d' if more than one page should be extracted", destFileName);
- return false;
- }
}
+ if (!foundmatch && firstPage != lastPage) {
+ error(errSyntaxError, -1, "'{0:s}' must contain '%%d' if more than one page should be extracted", destFileName);
+ free(auxDestFileName);
+ return false;
+ }
+
+ // at this point auxDestFileName can only contain %%
+ p = strstr(auxDestFileName, "%%");
+ while (p != NULL) {
+ *p = 'A';
+ *(p + 1) = 'A';
+ p = strstr(p, "%%");
+ }
+
+ // at this point any other % is wrong
+ p = strstr(auxDestFileName, "%");
+ if (p != NULL) {
+ error(errSyntaxError, -1, "'{0:s}' can only contain one '%d' pattern", destFileName);
+ free(auxDestFileName);
+ return false;
+ }
+ free(auxDestFileName);
+
for (int pageNo = firstPage; pageNo <= lastPage; pageNo++) {
snprintf (pathName, sizeof (pathName) - 1, destFileName, pageNo);
GooString *gpageName = new GooString (pathName);