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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
#include "mupdf/fitz.h"
struct fz_output_s
{
void *opaque;
int (*printf)(fz_context *, void *opaque, const char *, va_list ap);
int (*write)(fz_context *, void *opaque, const void *, int n);
void (*close)(fz_context *, void *opaque);
};
static int
file_printf(fz_context *ctx, void *opaque, const char *fmt, va_list ap)
{
FILE *file = opaque;
return fz_vfprintf(ctx, file, fmt, ap);
}
static int
file_write(fz_context *ctx, void *opaque, const void *buffer, int count)
{
FILE *file = opaque;
return fwrite(buffer, 1, count, file);
}
static void
file_close(fz_context *ctx, void *opaque)
{
FILE *file = opaque;
fclose(file);
}
fz_output *
fz_new_output_with_file(fz_context *ctx, FILE *file, int close)
{
fz_output *out = fz_malloc_struct(ctx, fz_output);
out->opaque = file;
out->printf = file_printf;
out->write = file_write;
out->close = close ? file_close : NULL;
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->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_drop_output(fz_context *ctx, fz_output *out)
{
if (!out)
return;
if (out->close)
out->close(ctx, out->opaque);
fz_free(ctx, out);
}
int
fz_printf(fz_context *ctx, fz_output *out, const char *fmt, ...)
{
int ret;
va_list ap;
if (!out)
return 0;
va_start(ap, fmt);
ret = out->printf(ctx, out->opaque, fmt, ap);
va_end(ap);
return ret;
}
int
fz_write(fz_context *ctx, fz_output *out, const void *data, int len)
{
if (!out)
return 0;
return out->write(ctx, out->opaque, data, len);
}
void
fz_putc(fz_context *ctx, fz_output *out, char c)
{
if (out)
(void)out->write(ctx, out->opaque, &c, 1);
}
int
fz_puts(fz_context *ctx, fz_output *out, const char *str)
{
if (!out)
return 0;
return out->write(ctx, out->opaque, str, strlen(str));
}
static int
buffer_printf(fz_context *ctx, void *opaque, const char *fmt, va_list list)
{
fz_buffer *buffer = opaque;
return fz_buffer_vprintf(ctx, buffer, fmt, list);
}
static int
buffer_write(fz_context *ctx, void *opaque, const void *data, int len)
{
fz_buffer *buffer = opaque;
fz_write_buffer(ctx, buffer, (unsigned char *)data, len);
return len;
}
static void
buffer_close(fz_context *ctx, void *opaque)
{
fz_buffer *buffer = opaque;
fz_drop_buffer(ctx, buffer);
}
fz_output *
fz_new_output_with_buffer(fz_context *ctx, fz_buffer *buf)
{
fz_output *out = fz_malloc_struct(ctx, fz_output);
out->opaque = fz_keep_buffer(ctx, buf);
out->printf = buffer_printf;
out->write = buffer_write;
out->close = buffer_close;
return out;
}
|