From 7e274313904e46cd3cf3f476ceb888ca687d1969 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Mon, 1 Sep 2014 17:23:40 +0200 Subject: Add fz_snprintf and use it for formatting floating point numbers. --- source/fitz/printf.c | 11 +++++++++++ source/pdf/pdf-lex.c | 6 +----- source/pdf/pdf-object.c | 14 ++++++-------- source/pdf/pdf-type3.c | 2 +- 4 files changed, 19 insertions(+), 14 deletions(-) (limited to 'source') diff --git a/source/fitz/printf.c b/source/fitz/printf.c index b4fe844a..8a429db6 100644 --- a/source/fitz/printf.c +++ b/source/fitz/printf.c @@ -252,3 +252,14 @@ fz_vfprintf(fz_context *ctx, FILE *file, const char *fmt, va_list old_args) return l; } + +int +fz_snprintf(char *buffer, int space, const char *fmt, ...) +{ + int n; + va_list ap; + va_start(ap, fmt); + n = fz_vsnprintf(buffer, space, fmt, ap); + va_end(ap); + return n; +} diff --git a/source/pdf/pdf-lex.c b/source/pdf/pdf-lex.c index b019b7b2..6a4033f9 100644 --- a/source/pdf/pdf-lex.c +++ b/source/pdf/pdf-lex.c @@ -606,11 +606,7 @@ void pdf_print_token(fz_context *ctx, fz_buffer *fzbuf, int tok, pdf_lexbuf *buf break; case PDF_TOK_REAL: { - char sbuf[256]; - sprintf(sbuf, "%g", buf->f); - if (strchr(sbuf, 'e')) /* bad news! */ - sprintf(sbuf, fabsf(buf->f) > 1 ? "%1.1f" : "%1.8f", buf->f); - fz_buffer_printf(ctx, fzbuf, "%s", sbuf); + fz_buffer_printf(ctx, fzbuf, "%g", buf->f); } break; default: diff --git a/source/pdf/pdf-object.c b/source/pdf/pdf-object.c index 1b7a5aa9..a3c44fe2 100644 --- a/source/pdf/pdf-object.c +++ b/source/pdf/pdf-object.c @@ -1518,10 +1518,10 @@ static void fmt_str(struct fmt *fmt, pdf_obj *obj) else if (c == '\\') fmt_puts(fmt, "\\\\"); else if (c < 32 || c >= 127) { - char buf[16]; fmt_putc(fmt, '\\'); - sprintf(buf, "%03o", c); - fmt_puts(fmt, buf); + fmt_putc(fmt, '0' + ((c / 64) & 7)); + fmt_putc(fmt, '0' + ((c / 8) & 7)); + fmt_putc(fmt, '0' + ((c) & 7)); } else fmt_putc(fmt, c); @@ -1645,7 +1645,7 @@ static void fmt_obj(struct fmt *fmt, pdf_obj *obj) fmt_puts(fmt, ""); else if (pdf_is_indirect(obj)) { - sprintf(buf, "%d %d R", pdf_to_num(obj), pdf_to_gen(obj)); + fz_snprintf(buf, sizeof buf, "%d %d R", pdf_to_num(obj), pdf_to_gen(obj)); fmt_puts(fmt, buf); } else if (pdf_is_null(obj)) @@ -1654,14 +1654,12 @@ static void fmt_obj(struct fmt *fmt, pdf_obj *obj) fmt_puts(fmt, pdf_to_bool(obj) ? "true" : "false"); else if (pdf_is_int(obj)) { - sprintf(buf, "%d", pdf_to_int(obj)); + fz_snprintf(buf, sizeof buf, "%d", pdf_to_int(obj)); fmt_puts(fmt, buf); } else if (pdf_is_real(obj)) { - sprintf(buf, "%1.9g", pdf_to_real(obj)); - if (strchr(buf, 'e')) /* bad news! */ - sprintf(buf, fabsf(pdf_to_real(obj)) > 1 ? "%1.1f" : "%1.8f", pdf_to_real(obj)); + fz_snprintf(buf, sizeof buf, "%g", pdf_to_real(obj)); fmt_puts(fmt, buf); } else if (pdf_is_string(obj)) diff --git a/source/pdf/pdf-type3.c b/source/pdf/pdf-type3.c index b216b950..3bb01a1c 100644 --- a/source/pdf/pdf-type3.c +++ b/source/pdf/pdf-type3.c @@ -48,7 +48,7 @@ pdf_load_type3_font(pdf_document *doc, pdf_obj *rdb, pdf_obj *dict) if (pdf_is_name(obj)) fz_strlcpy(buf, pdf_to_name(obj), sizeof buf); else - sprintf(buf, "Unnamed-T3"); + fz_strlcpy(buf, "Unnamed-T3", sizeof buf); fontdesc = pdf_new_font_desc(ctx); -- cgit v1.2.3