summaryrefslogtreecommitdiff
path: root/source/fitz
diff options
context:
space:
mode:
authorSebastian Rasmussen <sebras@gmail.com>2016-04-19 16:54:52 +0200
committerTor Andersson <tor.andersson@artifex.com>2016-05-13 11:42:00 +0200
commitb994d72069d761c8645cc6a0638bde21109c0b40 (patch)
tree2d95cbdb501d81b49210d6c115b51bb5fa9c62a7 /source/fitz
parente67981e781fd087d7cd19e2373b3e054375e82ad (diff)
downloadmupdf-b994d72069d761c8645cc6a0638bde21109c0b40.tar.xz
Introduce a general output context.
This makes it possible to redirect standard out and standard error output streams to output streams of your liking. This means that now you can, in gdb, type: (gdb) call pdf_print_obj(ctx, fz_stdout(ctx), obj, 0) (gdb) call fflush(0) or when dealing with an unresolved indirect reference: (gdb) call pdf_print_obj(ctx, fz_stdout(ctx), pdf_resolve_indirect(ctx, ref), 0) (gdb) call fflush(0)
Diffstat (limited to 'source/fitz')
-rw-r--r--source/fitz/context.c4
-rw-r--r--source/fitz/output.c92
2 files changed, 95 insertions, 1 deletions
diff --git a/source/fitz/context.c b/source/fitz/context.c
index bfc7a1cd..9a533cff 100644
--- a/source/fitz/context.c
+++ b/source/fitz/context.c
@@ -132,6 +132,7 @@ fz_drop_context(fz_context *ctx)
fz_drop_colorspace_context(ctx);
fz_drop_font_context(ctx);
fz_drop_id_context(ctx);
+ fz_drop_output_context(ctx);
if (ctx->warn)
{
@@ -222,6 +223,7 @@ fz_new_context_imp(const fz_alloc_context *alloc, const fz_locks_context *locks,
/* Now initialise sections that are shared */
fz_try(ctx)
{
+ fz_new_output_context(ctx);
fz_new_store_context(ctx, max_store);
fz_new_glyph_cache_context(ctx);
fz_new_colorspace_context(ctx);
@@ -266,6 +268,8 @@ fz_clone_context_internal(fz_context *ctx)
fz_copy_aa_context(new_ctx, ctx);
/* Keep thread lock checking happy by copying pointers first and locking under new context */
+ new_ctx->output = ctx->output;
+ new_ctx->output = fz_keep_output_context(new_ctx);
new_ctx->user = ctx->user;
new_ctx->store = ctx->store;
new_ctx->store = fz_keep_store_context(new_ctx);
diff --git a/source/fitz/output.c b/source/fitz/output.c
index 9ea2d169..07422955 100644
--- a/source/fitz/output.c
+++ b/source/fitz/output.c
@@ -1,5 +1,87 @@
#include "mupdf/fitz.h"
+struct fz_output_context_s
+{
+ int refs;
+ fz_output *out;
+ fz_output *err;
+};
+
+static void std_write(fz_context *ctx, void *opaque, const void *buffer, int count);
+
+static fz_output fz_stdout_global = {
+ &fz_stdout_global,
+ std_write,
+ NULL,
+ NULL,
+ NULL,
+};
+
+static fz_output fz_stderr_global = {
+ &fz_stderr_global,
+ std_write,
+ NULL,
+ NULL,
+ NULL,
+};
+
+void
+fz_new_output_context(fz_context *ctx)
+{
+ ctx->output = fz_malloc_struct(ctx, fz_output_context);
+ ctx->output->refs = 1;
+ ctx->output->out = &fz_stdout_global;
+ ctx->output->err = &fz_stderr_global;
+}
+
+fz_output_context *
+fz_keep_output_context(fz_context *ctx)
+{
+ if (!ctx)
+ return NULL;
+ return fz_keep_imp(ctx, ctx->output, &ctx->output->refs);
+}
+
+void
+fz_drop_output_context(fz_context *ctx)
+{
+ if (!ctx || !ctx->output)
+ return;
+
+ if (fz_drop_imp(ctx, ctx->output, &ctx->output->refs))
+ {
+ /* FIXME: should we flush here? closing the streams seems wrong */
+ fz_free(ctx, ctx->output);
+ ctx->output = NULL;
+ }
+}
+
+void
+fz_set_stdout(fz_context *ctx, fz_output *out)
+{
+ fz_drop_output(ctx, ctx->output->out);
+ ctx->output->out = out ? out : &fz_stdout_global;
+}
+
+void
+fz_set_stderr(fz_context *ctx, fz_output *err)
+{
+ fz_drop_output(ctx, ctx->output->err);
+ ctx->output->err = err ? err : &fz_stderr_global;
+}
+
+fz_output *
+fz_stdout(fz_context *ctx)
+{
+ return ctx->output->out;
+}
+
+fz_output *
+fz_stderr(fz_context *ctx)
+{
+ return ctx->output->err;
+}
+
static void
file_write(fz_context *ctx, void *opaque, const void *buffer, int count)
{
@@ -23,6 +105,13 @@ file_write(fz_context *ctx, void *opaque, const void *buffer, int count)
}
static void
+std_write(fz_context *ctx, void *opaque, const void *buffer, int count)
+{
+ FILE *f = opaque == &fz_stdout_global ? stdout : opaque == &fz_stderr_global ? stderr : NULL;
+ file_write(ctx, f, buffer, count);
+}
+
+static void
file_seek(fz_context *ctx, void *opaque, fz_off_t off, int whence)
{
FILE *file = opaque;
@@ -132,7 +221,8 @@ fz_drop_output(fz_context *ctx, fz_output *out)
if (!out) return;
if (out->close)
out->close(ctx, out->opaque);
- fz_free(ctx, out);
+ if (out->opaque != &fz_stdout_global && out->opaque != &fz_stderr_global)
+ fz_free(ctx, out);
}
void