summaryrefslogtreecommitdiff
path: root/source/fitz
diff options
context:
space:
mode:
authorTor Andersson <tor.andersson@artifex.com>2014-03-19 12:47:32 +0100
committerTor Andersson <tor.andersson@artifex.com>2014-03-19 13:20:37 +0100
commit8dc107d0449fa29b50414d3155a4ac3d108be0a6 (patch)
tree2034fd3f202395e1857c292f724d56130bcbb500 /source/fitz
parent30bcbe60b93a0e4a697871f69e8367476796f27c (diff)
downloadmupdf-8dc107d0449fa29b50414d3155a4ac3d108be0a6.tar.xz
Add %q and %( formatting to fz_printf to print escaped strings.
%q escapes using C syntax and wraps the string in double quotes. %( escapes using PS/PDF syntax and wraps the string in parens.
Diffstat (limited to 'source/fitz')
-rw-r--r--source/fitz/printf.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/source/fitz/printf.c b/source/fitz/printf.c
index ef70aca1..c6cb90a9 100644
--- a/source/fitz/printf.c
+++ b/source/fitz/printf.c
@@ -123,6 +123,35 @@ static void fmtint(struct fmtbuf *out, int value, int z, int base)
fmtputc(out, buf[--i]);
}
+static void fmtquote(struct fmtbuf *out, const char *s, int sq, int eq)
+{
+ int c;
+ fmtputc(out, sq);
+ while ((c = *s++)) {
+ switch (c) {
+ default:
+ if (c < 32 || c > 127) {
+ fmtputc(out, '\\');
+ fmtputc(out, '0' + ((c >> 6) & 7));
+ fmtputc(out, '0' + ((c >> 3) & 7));
+ fmtputc(out, '0' + ((c) & 7));
+ } else {
+ if (c == sq || c == eq)
+ fmtputc(out, '\\');
+ fmtputc(out, c);
+ }
+ break;
+ case '\\': fmtputc(out, '\\'); fmtputc(out, '\\'); break;
+ case '\b': fmtputc(out, '\\'); fmtputc(out, 'b'); break;
+ case '\f': fmtputc(out, '\\'); fmtputc(out, 'f'); break;
+ case '\n': fmtputc(out, '\\'); fmtputc(out, 'n'); break;
+ case '\r': fmtputc(out, '\\'); fmtputc(out, 'r'); break;
+ case '\t': fmtputc(out, '\\'); fmtputc(out, 't'); break;
+ }
+ }
+ fmtputc(out, eq);
+}
+
int
fz_vsnprintf(char *buffer, int space, const char *fmt, va_list args)
{
@@ -217,6 +246,16 @@ fz_vsnprintf(char *buffer, int space, const char *fmt, va_list args)
while ((c = *s++))
fmtputc(&out, c);
break;
+ case 'q':
+ s = va_arg(args, char*);
+ if (!s) s = "";
+ fmtquote(&out, s, '"', '"');
+ break;
+ case '(':
+ s = va_arg(args, char*);
+ if (!s) s = "";
+ fmtquote(&out, s, '(', ')');
+ break;
}
} else {
fmtputc(&out, c);