From 000e932a844ecc6cd59ae9cadc3c3150d4a6bf5b Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Thu, 16 Jun 2016 12:21:16 +0200 Subject: Add fz_format_output_path function. --- include/mupdf/fitz/string.h | 9 +++++++++ source/fitz/string.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) 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 @@ -62,6 +62,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. 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 * -- cgit v1.2.3