diff options
author | Tor Andersson <tor.andersson@artifex.com> | 2014-03-19 12:47:32 +0100 |
---|---|---|
committer | Tor Andersson <tor.andersson@artifex.com> | 2014-03-19 13:20:37 +0100 |
commit | 8dc107d0449fa29b50414d3155a4ac3d108be0a6 (patch) | |
tree | 2034fd3f202395e1857c292f724d56130bcbb500 | |
parent | 30bcbe60b93a0e4a697871f69e8367476796f27c (diff) | |
download | mupdf-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.
-rw-r--r-- | include/mupdf/fitz/output.h | 1 | ||||
-rw-r--r-- | source/fitz/printf.c | 39 |
2 files changed, 40 insertions, 0 deletions
diff --git a/include/mupdf/fitz/output.h b/include/mupdf/fitz/output.h index 01d4d227..9c0c9dd0 100644 --- a/include/mupdf/fitz/output.h +++ b/include/mupdf/fitz/output.h @@ -97,6 +97,7 @@ fz_write_byte(fz_output *out, int x) see fz_ftoa for specifics. %C outputs a utf8 encoded int. %M outputs a fz_matrix*. %R outputs a fz_rect*. %P outputs a fz_point*. + %q and %( output escaped strings in C/PDF syntax. */ int fz_vsnprintf(char *buffer, int space, const char *fmt, va_list args); 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); |