diff options
author | Robin Watts <robin.watts@artifex.com> | 2016-11-11 16:05:30 +0000 |
---|---|---|
committer | Robin Watts <robin.watts@artifex.com> | 2016-11-14 16:09:39 +0000 |
commit | 37f95f87bdfb2e0b01d649afae5fffe60bf99d3a (patch) | |
tree | da3491c62d6af86c1a5bc4e523e8f9ebe19793d1 /source/fitz/buffer.c | |
parent | c0e1dfdab1a13def046e94d771f8a821ba2a10d9 (diff) | |
download | mupdf-37f95f87bdfb2e0b01d649afae5fffe60bf99d3a.tar.xz |
Make fz_buffer structure private to fitz.
Move the definition of the structure contents into new fitz-imp.h
file. Make all code outside of fitz access the buffer through the
defined API.
Add a convenience API for people that want to get buffers as
null terminated C strings.
Diffstat (limited to 'source/fitz/buffer.c')
-rw-r--r-- | source/fitz/buffer.c | 48 |
1 files changed, 41 insertions, 7 deletions
diff --git a/source/fitz/buffer.c b/source/fitz/buffer.c index 10c0c055..13aae983 100644 --- a/source/fitz/buffer.c +++ b/source/fitz/buffer.c @@ -1,4 +1,4 @@ -#include "mupdf/fitz.h" +#include "fitz-imp.h" fz_buffer * fz_new_buffer(fz_context *ctx, size_t size) @@ -30,12 +30,20 @@ fz_new_buffer_from_data(fz_context *ctx, unsigned char *data, size_t size) { fz_buffer *b; - b = fz_malloc_struct(ctx, fz_buffer); - b->refs = 1; - b->data = data; - b->cap = size; - b->len = size; - b->unused_bits = 0; + fz_try(ctx) + { + b = fz_malloc_struct(ctx, fz_buffer); + b->refs = 1; + b->data = data; + b->cap = size; + b->len = size; + b->unused_bits = 0; + } + fz_catch(ctx) + { + fz_free(ctx, data); + fz_rethrow(ctx); + } return b; } @@ -152,6 +160,32 @@ fz_buffer_storage(fz_context *ctx, fz_buffer *buf, unsigned char **datap) return (buf ? buf->len : 0); } +const char * +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); + + return (const char *)buf->data; +} + +size_t +fz_buffer_extract(fz_context *ctx, fz_buffer *buf, unsigned char **datap) +{ + size_t len = buf ? buf->len : 0; + *datap = (buf ? buf->data : NULL); + + if (buf) + { + buf->data = NULL; + buf->len = 0; + } + return len; +} + void fz_append_buffer(fz_context *ctx, fz_buffer *buf, fz_buffer *extra) { |