summaryrefslogtreecommitdiff
path: root/source/fitz/buffer.c
diff options
context:
space:
mode:
authorTor Andersson <tor.andersson@artifex.com>2016-12-28 13:19:47 +0100
committerTor Andersson <tor.andersson@artifex.com>2017-01-09 13:21:40 +0100
commitbbcc85a9f746c161b2e23c6057e69ec7b967252b (patch)
tree8ead60bd762cc0cbf945c002c769e74699594c0b /source/fitz/buffer.c
parente9667e7f8ab7c154d8932916a22c33cf2bad0445 (diff)
downloadmupdf-bbcc85a9f746c161b2e23c6057e69ec7b967252b.tar.xz
Add fz_terminate_buffer function.
Non-destructively zero terminate a fz_buffer for use as a C string.
Diffstat (limited to 'source/fitz/buffer.c')
-rw-r--r--source/fitz/buffer.c14
1 files changed, 10 insertions, 4 deletions
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;
}