From bbcc85a9f746c161b2e23c6057e69ec7b967252b Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Wed, 28 Dec 2016 13:19:47 +0100 Subject: Add fz_terminate_buffer function. Non-destructively zero terminate a fz_buffer for use as a C string. --- source/fitz/buffer.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'source/fitz/buffer.c') diff --git a/source/fitz/buffer.c b/source/fitz/buffer.c index 13aae983..a2880659 100644 --- a/source/fitz/buffer.c +++ b/source/fitz/buffer.c @@ -152,6 +152,15 @@ fz_trim_buffer(fz_context *ctx, fz_buffer *buf) fz_resize_buffer(ctx, buf, buf->len); } +void +fz_terminate_buffer(fz_context *ctx, fz_buffer *buf) +{ + /* ensure that there is a zero-byte after the end of the data */ + if (buf->len + 1 > buf->cap) + fz_grow_buffer(ctx, buf); + buf->data[buf->len] = 0; +} + size_t fz_buffer_storage(fz_context *ctx, fz_buffer *buf, unsigned char **datap) { @@ -165,10 +174,7 @@ fz_string_from_buffer(fz_context *ctx, fz_buffer *buf) { if (!buf) return ""; - - if (buf->data[buf->len-1] != 0) - fz_write_buffer_byte(ctx, buf, 0); - + fz_terminate_buffer(ctx, buf); return (const char *)buf->data; } -- cgit v1.2.3