diff options
-rw-r--r-- | include/mupdf/fitz/output.h | 10 | ||||
-rw-r--r-- | source/fitz/output.c | 43 |
2 files changed, 53 insertions, 0 deletions
diff --git a/include/mupdf/fitz/output.h b/include/mupdf/fitz/output.h index 6345e15e..a4f71100 100644 --- a/include/mupdf/fitz/output.h +++ b/include/mupdf/fitz/output.h @@ -28,6 +28,11 @@ struct fz_output_s fz_output *fz_new_output_with_file(fz_context *, FILE *); /* + fz_new_output_to_filename: Open an output stream to a filename. +*/ +fz_output *fz_new_output_to_filename(fz_context *, const char *filename); + +/* fz_new_output_with_buffer: Open an output stream onto a buffer. The stream does NOT take ownership of the buffer. @@ -50,6 +55,11 @@ int fz_puts(fz_output *, const char *); int fz_write(fz_output *out, const void *data, int len); /* + fz_putc: putc equivalent for output streams. +*/ +void fz_putc(fz_output *out, char c); + +/* fz_close_output: Close a previously opened fz_output stream. Note: whether or not this closes the underlying output method is 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) { |