From e013fd7d0f5ae8f5c994aad4d02f5169946f1a26 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Wed, 15 Mar 2017 16:27:15 +0100 Subject: Simplify string formatter API. Emit characters with callbacks so we don't need to do two passes using vsnprintf to count, format, and copy the result. --- source/fitz/output.c | 37 +++++++++---------------------------- 1 file changed, 9 insertions(+), 28 deletions(-) (limited to 'source/fitz/output.c') diff --git a/source/fitz/output.c b/source/fitz/output.c index bd4dba66..961bafe5 100644 --- a/source/fitz/output.c +++ b/source/fitz/output.c @@ -266,36 +266,17 @@ fz_tell_output(fz_context *ctx, fz_output *out) return out->tell(ctx, out->state); } -void -fz_write_vprintf(fz_context *ctx, fz_output *out, const char *fmt, va_list old_args) +static void +fz_write_emit(fz_context *ctx, void *out, int c) { - char buffer[256], *p = buffer; - size_t len; - va_list args; + fz_write_byte(ctx, out, c); +} +void +fz_write_vprintf(fz_context *ctx, fz_output *out, const char *fmt, va_list args) +{ if (!out) return; - - /* First try using our fixed size buffer */ - va_copy(args, old_args); - len = fz_vsnprintf(buffer, sizeof buffer, fmt, args); - va_copy_end(args); - - /* If that failed, allocate a big enough buffer */ - if (len > sizeof buffer) - { - p = fz_malloc(ctx, len); - va_copy(args, old_args); - fz_vsnprintf(p, len, fmt, args); - va_copy_end(args); - } - - fz_try(ctx) - out->write(ctx, out->state, p, len); - fz_always(ctx) - if (p != buffer) - fz_free(ctx, p); - fz_catch(ctx) - fz_rethrow(ctx); + fz_format_string(ctx, out, fz_write_emit, fmt, args); } void @@ -304,7 +285,7 @@ fz_write_printf(fz_context *ctx, fz_output *out, const char *fmt, ...) va_list args; if (!out) return; va_start(args, fmt); - fz_write_vprintf(ctx, out, fmt, args); + fz_format_string(ctx, out, fz_write_emit, fmt, args); va_end(args); } -- cgit v1.2.3