summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Andersson <tor.andersson@artifex.com>2016-06-16 12:21:16 +0200
committerRobin Watts <robin.watts@artifex.com>2016-06-16 18:08:15 +0100
commit000e932a844ecc6cd59ae9cadc3c3150d4a6bf5b (patch)
tree6f746b61ed74038e208a0ca96adcb2e7e19009ff
parentb8f6455de4c500807bf85825d32a69a9c71efcd7 (diff)
downloadmupdf-000e932a844ecc6cd59ae9cadc3c3150d4a6bf5b.tar.xz
Add fz_format_output_path function.
-rw-r--r--include/mupdf/fitz/string.h9
-rw-r--r--source/fitz/string.c44
2 files changed, 53 insertions, 0 deletions
diff --git a/include/mupdf/fitz/string.h b/include/mupdf/fitz/string.h
index 7700b6c7..9f1f656b 100644
--- a/include/mupdf/fitz/string.h
+++ b/include/mupdf/fitz/string.h
@@ -63,6 +63,15 @@ void fz_dirname(char *dir, const char *path, int dirsize);
char *fz_urldecode(char *url);
/*
+ fz_format_output_path: create output file name using a template.
+ If the path contains %[0-9]*d, the first such pattern will be replaced
+ with the page number. If the template does not contain such a pattern, the page
+ number will be inserted before the file suffix. If the template does not have
+ a file suffix, the page number will be added to the end.
+*/
+void fz_format_output_path(fz_context *ctx, char *path, int size, const char *fmt, int page);
+
+/*
fz_cleanname: rewrite path to the shortest string that names the same path.
Eliminates multiple and trailing slashes, interprets "." and "..".
diff --git a/source/fitz/string.c b/source/fitz/string.c
index df171795..de9ee80f 100644
--- a/source/fitz/string.c
+++ b/source/fitz/string.c
@@ -142,6 +142,50 @@ fz_urldecode(char *url)
return url;
}
+void
+fz_format_output_path(fz_context *ctx, char *path, int size, const char *fmt, int page)
+{
+ const char *s, *p;
+ char num[40];
+ int i, n;
+ int z = 0;
+
+ for (i = 0; page; page /= 10)
+ num[i++] = '0' + page % 10;
+ num[i] = 0;
+
+ s = p = strchr(fmt, '%');
+ if (p)
+ {
+ ++p;
+ while (*p >= '0' && *p <= '9')
+ z = z * 10 + (*p++ - '0');
+ }
+ if (p && *p == 'd')
+ {
+ ++p;
+ }
+ else
+ {
+ s = p = strrchr(fmt, '.');
+ if (!p)
+ s = p = fmt + strlen(fmt);
+ }
+
+ if (z < 1)
+ z = 1;
+ while (i < z && i < sizeof num)
+ num[i++] = '0';
+ n = s - fmt;
+ if (n + i + strlen(p) >= size)
+ fz_throw(ctx, FZ_ERROR_GENERIC, "path name buffer overflow");
+ memcpy(path, fmt, n);
+ while (i > 0)
+ path[n++] = num[--i];
+ fz_strlcpy(path + n, p, size - n);
+
+}
+
#define SEP(x) ((x)=='/' || (x) == 0)
char *