diff options
author | Robin Watts <robin.watts@artifex.com> | 2013-08-29 18:36:30 +0100 |
---|---|---|
committer | Robin Watts <robin.watts@artifex.com> | 2013-08-29 18:38:30 +0100 |
commit | 3d8bdbcf696cb9d9ab5ec385bc4e5756adf63757 (patch) | |
tree | bbd93a3d650196931e949483c2a79721ddd3a39d /source | |
parent | 6453f81a6f1c218cd118427bb8e23f2b2ede2f7d (diff) | |
download | mupdf-3d8bdbcf696cb9d9ab5ec385bc4e5756adf63757.tar.xz |
Add new fz_putc and fz_new_output_to_filename functions.
fz_putc; this fills a hole in our fz_output functions.
fz_new_output_to_filename: This saves people having to create
a FILE * just to pass to fz_new_output_with_file and then having
to remember to close the FILE *.
Diffstat (limited to 'source')
-rw-r--r-- | source/fitz/output.c | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/source/fitz/output.c b/source/fitz/output.c index f98b945e..e5c3db4e 100644 --- a/source/fitz/output.c +++ b/source/fitz/output.c @@ -16,6 +16,14 @@ file_write(fz_output *out, const void *buffer, int count) return fwrite(buffer, 1, count, file); } +static void +file_close(fz_output *out) +{ + FILE *file = (FILE *)out->opaque; + + fclose(file); +} + fz_output * fz_new_output_with_file(fz_context *ctx, FILE *file) { @@ -28,6 +36,34 @@ fz_new_output_with_file(fz_context *ctx, FILE *file) return out; } +fz_output * +fz_new_output_to_filename(fz_context *ctx, const char *filename) +{ + fz_output *out = NULL; + + FILE *file = fopen(filename, "wb"); + if (!file) + fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open file '%s': %s", filename, strerror(errno)); + + fz_var(ctx); + + fz_try(ctx) + { + out = fz_malloc_struct(ctx, fz_output); + out->ctx = ctx; + out->opaque = file; + out->printf = file_printf; + out->write = file_write; + out->close = file_close; + } + fz_catch(ctx) + { + fclose(file); + fz_rethrow(ctx); + } + return out; +} + void fz_close_output(fz_output *out) { @@ -62,6 +98,13 @@ fz_write(fz_output *out, const void *data, int len) return out->write(out, data, len); } +void +fz_putc(fz_output *out, char c) +{ + if (out) + (void)out->write(out, &c, 1); +} + int fz_puts(fz_output *out, const char *str) { |