summaryrefslogtreecommitdiff
path: root/fitz/stm_output.c
blob: f438a5b155d0f4b726aae81aef524e7ba88cc63d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "fitz-internal.h"

static int
file_printf(fz_output *out, const char *fmt, va_list ap)
{
	FILE *file = (FILE *)out->opaque;

	return vfprintf(file, fmt, ap);
}

fz_output *
fz_new_output_file(fz_context *ctx, FILE *file)
{
	fz_output *out = fz_malloc_struct(ctx, fz_output);
	out->ctx = ctx;
	out->opaque = file;
	out->printf = file_printf;
	out->close = NULL;
	return out;
}

void
fz_close_output(fz_output *out)
{
	if (!out)
		return;
	if (out->close)
		out->close(out);
	fz_free(out->ctx, out);
}

int
fz_printf(fz_output *out, const char *fmt, ...)
{
	int ret;
	va_list ap;

	if (!out)
		return 0;

	va_start(ap, fmt);
	ret = out->printf(out, fmt, ap);
	va_end(ap);

	return ret;
}

static int
buffer_printf(fz_output *out, const char *fmt, va_list list)
{
	fz_buffer *buffer = (fz_buffer *)out->opaque;

	return fz_buffer_vprintf(out->ctx, buffer, fmt, list);
}

fz_output *
fz_new_output_buffer(fz_context *ctx, fz_buffer *buf)
{
	fz_output *out = fz_malloc_struct(ctx, fz_output);
	out->ctx = ctx;
	out->opaque = buf;
	out->printf = buffer_printf;
	out->close = NULL;
	return out;
}