-
Notifications
You must be signed in to change notification settings - Fork 0
/
print.c
155 lines (128 loc) · 3.02 KB
/
print.c
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
#include "print.h"
#include <stdlib.h>
#include <string.h>
#include "filter.h"
typedef struct _ctx ctx;
struct _ctx {
tpl_writefunc write;
void *out;
tpl_doc *tdoc;
tpl_doc *doc;
char *tsrc;
char *src;
};
static int xprintf(ctx *x, const char *fmt, ...);
static size_t printtext(ctx *x, asttext *el);
static size_t printfield(ctx *x, astfield *el);
static int xgetdef(ctx *x, char *name, size_t namelen, char **value, size_t *valuelen);
size_t xwrite(void *buf, size_t size, size_t nitems, ctx *x)
{
return x->write(buf, size, nitems, x->out);
}
int xprintf(ctx *x, const char *fmt, ...)
{
va_list ap;
char *buf = 0;
int len;
va_start(ap, fmt);
len = vasprintf(&buf, fmt, ap);
len = xwrite(buf, 1, len, x);
free(buf);
va_end(ap);
return len;
}
size_t tpl_doc_process(tpl_doc *tdoc, void *in, void *out)
{
int i;
size_t n;
ctx x = { 0 };
tpl_doc *doc;
astbody *body;
doc = tpl_doc_parse_stream(tdoc->t, tpl_ctx_get_readfunc(tdoc->t), in);
if(doc == NULL)
return 0;
x.write = tpl_ctx_get_writefunc(tdoc->t);
x.out = out;
x.tdoc = tdoc;
x.doc = doc;
x.tsrc = tdoc->ast->src;
x.src = doc->ast->src;
body = tdoc->ast->body;
if(body == 0)
return 0;
n = 0;
for(i = 0; i < body->num; i++)
switch(body->els[i]->type) {
case AST_EL_TEXT:
n += printtext(&x, &(body->els[i]->text));
break;
case AST_EL_FIELD:
n += printfield(&x, &(body->els[i]->field));
break;
}
return n;
}
size_t printtext(ctx *x, asttext *el)
{
return xwrite(x->tsrc + el->off, 1, el->len, x);
}
size_t printfield(ctx *x, astfield *el)
{
char *name;
size_t namelen;
char *value;
size_t valuelen;
char *filter;
size_t n;
name = x->tsrc + el->nameoff;
namelen = el->namelen;
if(!xgetdef(x, name, namelen, &value, &valuelen))
return 0;
if(!el->hasfilter)
return xwrite(value, 1, valuelen, x);
filter = malloc(el->filterlen + 1);
memcpy(filter, x->tsrc + el->filteroff, el->filterlen);
filter[el->filterlen] = '\0';
n = filter_buffer(filter, value, valuelen, (tpl_writefunc)xwrite, x);
free(filter);
return n;
}
int xgetdef(ctx *x, char *name, size_t namelen, char **value, size_t *valuelen)
{
int i;
astdefset *defset;
astdef *def;
if((*value = tpl_ctx_get_definition(x->doc->t, name, namelen)) != NULL) {
*valuelen = strlen(*value);
return 1;
}
if(namelen == 3)
if(name[0] == '.' && name[1] == '.' && name[2] == '.') {
*value = x->src + x->doc->ast->body->off;
*valuelen = x->doc->ast->body->len;
return 1;
}
defset = x->doc->ast->defset;
if(defset != NULL)
for(i = 0; i < defset->num; i++) {
def = defset->els[i];
if(def->namelen == namelen)
if(memcmp(x->src + def->nameoff, name, namelen) == 0) {
*value = x->doc->ast->src + def->valueoff;
*valuelen = def->valuelen;
return 1;
}
}
defset = x->tdoc->ast->defset;
if(defset != NULL)
for(i = 0; i < defset->num; i++) {
def = defset->els[i];
if(def->namelen == namelen)
if(memcmp(x->tsrc + def->nameoff, name, namelen) == 0) {
*value = x->tsrc + def->valueoff;
*valuelen = def->valuelen;
return 1;
}
}
return 0;
}