-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml_parser_document_search.c
More file actions
247 lines (201 loc) · 5.06 KB
/
Copy pathhtml_parser_document_search.c
File metadata and controls
247 lines (201 loc) · 5.06 KB
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
#include <string.h>
#include "html_parser_assert.h"
#include "html_parser_mem.h"
#include "html_parser_types.h"
#include "html_parser_text.h"
#include "html_parser_document_stack.h"
#include "html_parser_document_node.h"
#include "html_parser_document_tree.h"
#include "html_parser_document_search.h"
#define T Match_T
struct T {
int count;
struct m {
Node_T *p; /* parent */
Node_T *n; /* match */
struct m *link;
} *el;
};
/* static function prototypes */
static T new_match(void);
static T tag_search(Doc_tree_T *tr, const char *tname,
const char *aname, const char *aval, int sub_on);
static int tag_match(T *resp, Node_T *curr, Node_T *target, const char *tname,
const char *aname, const char *aval);
static void cont_match(T *resp, Node_T *curr, Node_T *target, const char *str);
static void map(T *resp, void apply(struct m **, void *cl), void *cl);
static void results_print(struct m**, void *cl);
int Doc_search_result_size(T r)
{
assert(r);
return r->count;
}
/* wrappers */
T Doc_search_tag(Doc_tree_T *tr, const char *tname,
const char *aname, const char *aval)
{
return tag_search(tr, tname, aname, aval, 0);
}
T Doc_search_sub_tree(Doc_tree_T *tr, const char *tname,
const char *aname, const char *aval)
{
return tag_search(tr, tname, aname, aval, 1);
}
T Doc_search_content(Doc_tree_T *tr, const char *str)
{
Node_T *curr, *tmp; /* tmp is node of interest */
T res = new_match();
Stack_T stk = Stack_stack();
Stack_push(stk, Doc_tree_root(tr));
while (!Stack_empty(stk)) {
curr = Stack_pop(stk);
/* sibling */
if (Node_has_sibling(curr)) {
tmp = Node_sibling(curr);
if (C_CNTND == Node_type(*tmp)) /* check type here */
cont_match(&res, curr, tmp, str);
Stack_push(stk, tmp);
}
/* child */
if (Node_has_child(curr)) {
tmp = Node_child(curr);
if (C_CNTND == Node_type(*tmp))
cont_match(&res, curr, tmp, str);
Stack_push(stk, tmp);
}
}
return res;
}
void Doc_search_free(T *r)
{
struct m *tmp = (*r)->el;
while (tmp != NULL) {
(*r)->el = tmp->link;
FREE(tmp);
}
FREE(*r);
}
static T new_match(void)
{
T new_match;
NEW(new_match);
new_match->count = 0;
new_match->el = NULL;
return new_match;
}
static T tag_search(Doc_tree_T *tr, const char *tname,
const char *aname, const char *aval, int sub_on)
{
Node_T *curr, *tmp; /* tmp is node of interest */
T res = new_match();
Stack_T stk = Stack_stack();
Stack_push(stk, Doc_tree_root(tr));
while (!Stack_empty(stk)) {
curr = Stack_pop(stk);
/* sibling */
if (Node_has_sibling(curr)) {
tmp = Node_sibling(curr);
if (C_TAGND == Node_type(*tmp)) { /* check type here */
tag_match(&res, curr, tmp, tname, aname, aval);
Stack_push(stk, tmp);
}
}
/* child */
if (Node_has_child(curr)) {
tmp = Node_child(curr);
if (C_TAGND == Node_type(*tmp)) {
if (tag_match(&res, curr, tmp, tname, aname, aval) &&
sub_on) {
continue;
}
Stack_push(stk, tmp);
}
}
}
return res;
}
const char *Doc_search_result_print(T *r)
{
Text_T output = Text_box("", 0);
Fmt_register('T', Text_fmt);
map(r, results_print, (void *) &output);
return Fmt_string("%T\n", &output);
}
/* this is nasty */
static int tag_match(T *resp, Node_T *curr, Node_T *target, const char *tname,
const char *aname, const char *aval)
{
Attr_list_T *alist;
struct m *match;
/* search: as soon as there's a match add the node and return */
if (tname && (strcmp(tname, Node_name(*target)) == 0))
goto matched;
alist = Node_attr_list(*target);
if (alist && Attr_list_search(alist, aname, aval))
goto matched;
return 0; /* no match */
matched:
/* create match */
NEW(match);
match->p = curr;
match->n = target;
match->link = NULL;
/* add match to resp */
if (Doc_search_result_size(*resp)) {
struct m* tmp = (*resp)->el;
while (tmp->link != NULL)
tmp = tmp->link;
tmp->link = match;
} else {
(*resp)->el = match;
}
(*resp)->count++;
return 1;
}
static void cont_match(T *resp, Node_T *curr, Node_T *target, const char *str)
{
char *txt = (char *) Node_print(target);
/* search */
if (strstr(txt, str)) {
/* create match */
struct m *match;
NEW(match);
match->p = curr;
match->n = target;
match->link = NULL;
/* add match to resp */
if (Doc_search_result_size(*resp)) {
struct m* tmp = (*resp)->el;
while (tmp->link != NULL)
tmp = tmp->link;
tmp->link = match;
} else {
(*resp)->el = match;
}
(*resp)->count++;
}
FREE(txt);
}
static void map(T *resp, void apply(struct m **, void *cl), void *cl)
{
struct m *tmp;
assert(resp && *resp);
assert(apply);
tmp = (*resp)->el;
while (tmp != NULL) {
apply(&tmp, cl);
tmp = tmp->link;
}
}
static void results_print(struct m **rpp, void *cl)
{
Text_T *output = (Text_T *) cl;
Node_T *p = (*rpp)->p;
Node_T *n = (*rpp)->n;
*output = Text_cat(*output, Text_put("predecessor:\n"));
*output = Text_cat(*output, Text_put(Node_print(p)));
*output = Text_cat(*output, Text_put("\n"));
*output = Text_cat(*output, Text_put("match:\n"));
*output = Text_cat(*output, Text_put(Node_print(n)));
*output = Text_cat(*output, Text_put("\n"));
}