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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
#include "fitz_base.h"
#include "fitz_stream.h"
struct fmt
{
char *buf;
int cap;
int len;
int indent;
int tight;
int col;
int sep;
int last;
};
static void fmtobj(struct fmt *fmt, fz_obj *obj);
static inline int iswhite(int ch)
{
return
ch == '\000' ||
ch == '\011' ||
ch == '\012' ||
ch == '\014' ||
ch == '\015' ||
ch == '\040';
}
static inline int isdelim(int ch)
{
return ch == '(' || ch == ')' ||
ch == '<' || ch == '>' ||
ch == '[' || ch == ']' ||
ch == '{' || ch == '}' ||
ch == '/' ||
ch == '%';
}
static inline void fmtputc(struct fmt *fmt, int c)
{
if (fmt->sep && !isdelim(fmt->last) && !isdelim(c)) {
fmt->sep = 0;
fmtputc(fmt, ' ');
}
fmt->sep = 0;
if (fmt->buf && fmt->len < fmt->cap)
fmt->buf[fmt->len] = c;
if (c == '\n')
fmt->col = 0;
else
fmt->col ++;
fmt->len ++;
fmt->last = c;
}
static void fmtindent(struct fmt *fmt)
{
int i = fmt->indent;
while (i--) {
fmtputc(fmt, ' ');
fmtputc(fmt, ' ');
}
}
static inline void fmtputs(struct fmt *fmt, char *s)
{
while (*s)
fmtputc(fmt, *s++);
}
static inline void fmtsep(struct fmt *fmt)
{
fmt->sep = 1;
}
static void fmtstr(struct fmt *fmt, fz_obj *obj)
{
int i;
int c;
fmtputc(fmt, '(');
for (i = 0; i < obj->u.s.len; i++)
{
c = (unsigned char) obj->u.s.buf[i];
if (c == '\n')
fmtputs(fmt, "\\n");
else if (c == '\r')
fmtputs(fmt, "\\r");
else if (c == '\t')
fmtputs(fmt, "\\t");
else if (c == '\b')
fmtputs(fmt, "\\b");
else if (c == '\f')
fmtputs(fmt, "\\f");
else if (c == '(')
fmtputs(fmt, "\\(");
else if (c == ')')
fmtputs(fmt, "\\)");
else if (c < 32 || c > 126) {
char buf[16];
fmtputc(fmt, '\\');
sprintf(buf, "%o", c);
fmtputs(fmt, buf);
}
else
fmtputc(fmt, c);
}
fmtputc(fmt, ')');
}
static void fmthex(struct fmt *fmt, fz_obj *obj)
{
int i;
int b;
int c;
fmtputc(fmt, '<');
for (i = 0; i < obj->u.s.len; i++) {
b = (unsigned char) obj->u.s.buf[i];
c = (b >> 4) & 0x0f;
fmtputc(fmt, c < 0xA ? c + '0' : c + 'A' - 0xA);
c = (b) & 0x0f;
fmtputc(fmt, c < 0xA ? c + '0' : c + 'A' - 0xA);
}
fmtputc(fmt, '>');
}
static void fmtname(struct fmt *fmt, fz_obj *obj)
{
unsigned char *s = (unsigned char *) fz_toname(obj);
int i, c;
fmtputc(fmt, '/');
for (i = 0; s[i]; i++)
{
if (isdelim(s[i]) || iswhite(s[i]) ||
s[i] == '#' || s[i] < 32 || s[i] > 127)
{
fmtputc(fmt, '#');
c = (s[i] >> 4) & 0xf;
fmtputc(fmt, c < 0xA ? c + '0' : c + 'A' - 0xA);
c = s[i] & 0xf;
fmtputc(fmt, c < 0xA ? c + '0' : c + 'A' - 0xA);
}
else
{
fmtputc(fmt, s[i]);
}
}
}
static void fmtarray(struct fmt *fmt, fz_obj *obj)
{
int i;
if (fmt->tight) {
fmtputc(fmt, '[');
for (i = 0; i < fz_arraylen(obj); i++) {
fmtobj(fmt, fz_arrayget(obj, i));
fmtsep(fmt);
}
fmtputc(fmt, ']');
}
else {
fmtputs(fmt, "[ ");
for (i = 0; i < fz_arraylen(obj); i++) {
if (fmt->col > 60) {
fmtputc(fmt, '\n');
fmtindent(fmt);
}
fmtobj(fmt, fz_arrayget(obj, i));
fmtputc(fmt, ' ');
}
fmtputc(fmt, ']');
fmtsep(fmt);
}
}
static void fmtdict(struct fmt *fmt, fz_obj *obj)
{
int i;
fz_obj *key, *val;
if (fmt->tight) {
fmtputs(fmt, "<<");
for (i = 0; i < fz_dictlen(obj); i++) {
fmtobj(fmt, fz_dictgetkey(obj, i));
fmtsep(fmt);
fmtobj(fmt, fz_dictgetval(obj, i));
fmtsep(fmt);
}
fmtputs(fmt, ">>");
}
else {
fmtputs(fmt, "<<\n");
fmt->indent ++;
for (i = 0; i < fz_dictlen(obj); i++) {
key = fz_dictgetkey(obj, i);
val = fz_dictgetval(obj, i);
fmtindent(fmt);
fmtobj(fmt, key);
fmtputc(fmt, ' ');
if (!fz_isindirect(val) && fz_isarray(val))
fmt->indent ++;
fmtobj(fmt, val);
fmtputc(fmt, '\n');
if (!fz_isindirect(val) && fz_isarray(val))
fmt->indent --;
}
fmt->indent --;
fmtindent(fmt);
fmtputs(fmt, ">>");
}
}
static void fmtobj(struct fmt *fmt, fz_obj *obj)
{
char buf[256];
if (!obj) {
fmtputs(fmt, "<nil>");
return;
}
switch (obj->kind)
{
case FZ_NULL:
fmtputs(fmt, "null");
break;
case FZ_BOOL:
fmtputs(fmt, fz_tobool(obj) ? "true" : "false");
break;
case FZ_INT:
sprintf(buf, "%d", fz_toint(obj));
fmtputs(fmt, buf);
break;
case FZ_REAL:
sprintf(buf, "%g", fz_toreal(obj));
if (strchr(buf, 'e')) /* bad news! */
sprintf(buf, fabs(fz_toreal(obj)) > 1 ? "%1.1f" : "%1.8f", fz_toreal(obj));
fmtputs(fmt, buf);
break;
case FZ_STRING:
{
int added = 0;
int i, c;
for (i = 0; i < obj->u.s.len; i++) {
c = (unsigned char)obj->u.s.buf[i];
if (strchr("()\\\n\r\t\b\f", c))
added ++;
else if (c < 8)
added ++;
else if (c < 32)
added += 2;
else if (c >= 127)
added += 3;
}
if (added < obj->u.s.len)
fmtstr(fmt, obj);
else
fmthex(fmt, obj);
}
break;
case FZ_NAME:
fmtname(fmt, obj);
break;
case FZ_ARRAY:
fmtarray(fmt, obj);
break;
case FZ_DICT:
fmtdict(fmt, obj);
break;
case FZ_INDIRECT:
sprintf(buf, "%d %d R", obj->u.r.num, obj->u.r.gen);
fmtputs(fmt, buf);
break;
default:
sprintf(buf, "<unknown object type %d>", obj->kind);
fmtputs(fmt, buf);
break;
}
}
int
fz_sprintobj(char *s, int n, fz_obj *obj, int tight)
{
struct fmt fmt;
fmt.indent = 0;
fmt.col = 0;
fmt.sep = 0;
fmt.last = 0;
fmt.tight = tight;
fmt.buf = s;
fmt.cap = n;
fmt.len = 0;
fmtobj(&fmt, obj);
if (fmt.buf && fmt.len < fmt.cap)
fmt.buf[fmt.len] = '\0';
return fmt.len;
}
int
fz_fprintobj(FILE *fp, fz_obj *obj, int tight)
{
char buf[1024];
char *ptr;
int n;
n = fz_sprintobj(nil, 0, obj, tight);
if ((n + 1) < sizeof buf)
{
fz_sprintobj(buf, sizeof buf, obj, tight);
fputs(buf, fp);
fputc('\n', fp);
}
else
{
ptr = fz_malloc(n + 1);
fz_sprintobj(ptr, n + 1, obj, tight);
fputs(ptr, fp);
fputc('\n', fp);
fz_free(ptr);
}
return n;
}
void
fz_debugobj(fz_obj *obj)
{
fz_fprintobj(stdout, obj, 0);
}
|