-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathquery.c
192 lines (167 loc) · 4.81 KB
/
query.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
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
#include "whistlepig.h"
#include "query.h"
static wp_query* wp_query_new() {
wp_query* ret = malloc(sizeof(wp_query));
ret->type = 0; // error
ret->field = ret->word = NULL;
ret->num_children = 0;
ret->children = ret->next = ret->last = NULL;
ret->search_data = NULL;
return ret;
}
static const char* identity(const char* field, const char* word) {
(void)field;
if(word) return strdup(word);
else return NULL;
}
wp_query* wp_query_clone(wp_query* other) {
return wp_query_substitute(other, identity);
}
wp_query* wp_query_substitute(wp_query* other, const char *(*substituter)(const char* field, const char* word)) {
wp_query* ret = malloc(sizeof(wp_query));
ret->type = other->type;
ret->num_children = other->num_children;
ret->search_data = NULL;
if(other->field) ret->field = strdup(other->field);
else ret->field = NULL;
if(other->field && other->word) ret->word = substituter(other->field, other->word);
else if(other->word) ret->word = strdup(other->word); // labels have this form
else ret->word = NULL;
ret->children = ret->next = ret->last = NULL; // set below
for(wp_query* child = other->children; child != NULL; child = child->next) {
wp_query* clone = wp_query_substitute(child, substituter);
if(ret->last == NULL) ret->children = ret->last = clone;
else {
ret->last->next = clone;
ret->last = clone;
}
}
return ret;
}
wp_query* wp_query_new_term(const char* field, const char* word) {
wp_query* ret = wp_query_new();
ret->type = WP_QUERY_TERM;
ret->field = field;
ret->word = word;
return ret;
}
wp_query* wp_query_new_label(const char* label) {
wp_query* ret = wp_query_new();
ret->type = WP_QUERY_LABEL;
ret->word = label;
ret->field = NULL;
return ret;
}
#define SIMPLE_QUERY_CONSTRUCTOR(name, type_name) \
wp_query* wp_query_new_##name() { \
wp_query* ret = wp_query_new(); \
ret->type = type_name; \
return ret; \
}
SIMPLE_QUERY_CONSTRUCTOR(conjunction, WP_QUERY_CONJ);
SIMPLE_QUERY_CONSTRUCTOR(disjunction, WP_QUERY_DISJ);
SIMPLE_QUERY_CONSTRUCTOR(phrase, WP_QUERY_PHRASE);
SIMPLE_QUERY_CONSTRUCTOR(negation, WP_QUERY_NEG);
SIMPLE_QUERY_CONSTRUCTOR(empty, WP_QUERY_EMPTY);
SIMPLE_QUERY_CONSTRUCTOR(every, WP_QUERY_EVERY);
wp_query* wp_query_add(wp_query* a, wp_query* b) {
if(a->type == WP_QUERY_EMPTY) {
wp_query_free(a);
return b;
}
else if(b->type == WP_QUERY_EMPTY) {
wp_query_free(b);
return a;
}
else {
a->num_children++;
if(a->last == NULL) a->children = a->last = b;
else {
a->last->next = b;
a->last = b;
}
return a;
}
}
void wp_query_free(wp_query* q) {
if(q->field) free((void*)q->field);
if(q->word) free((void*)q->word);
while(q->children) {
wp_query* b = q->children;
q->children = q->children->next;
wp_query_free(b);
}
free(q);
}
static int subquery_to_s(wp_query* q, size_t n, char* buf) {
char* orig_buf = buf;
for(wp_query* child = q->children; child != NULL; child = child->next) {
if((n - (buf - orig_buf)) < 1) break; // can we add a space?
buf += sprintf(buf, " ");
buf += wp_query_to_s(child, n - (buf - orig_buf), buf);
}
return (int)(buf - orig_buf);
}
#define min(a, b) (a < b ? a : b)
size_t wp_query_to_s(wp_query* q, size_t n, char* buf) {
size_t ret, term_n;
char* orig_buf = buf;
/* nodes without children */
switch(q->type) {
case WP_QUERY_TERM:
term_n = (size_t)snprintf(buf, n, "%s:\"%s\"", q->field, q->word);
ret = min(term_n, n);
break;
case WP_QUERY_LABEL:
term_n = (size_t)snprintf(buf, n, "~%s", q->word);
ret = min(term_n, n);
break;
case WP_QUERY_EMPTY:
term_n = (size_t)snprintf(buf, n, "<EMPTY>");
ret = min(term_n, n);
break;
case WP_QUERY_EVERY:
term_n = (size_t)snprintf(buf, n, "<EVERY>");
ret = min(term_n, n);
break;
/* nodes with children */
default:
switch(q->type) {
case WP_QUERY_CONJ:
if(n >= 4) { // "(AND"
buf += snprintf(buf, n, "(AND");
n -= 4;
}
break;
case WP_QUERY_DISJ:
if(n >= 3) { // "(OR"
buf += snprintf(buf, n, "(OR");
n -= 3;
}
break;
case WP_QUERY_PHRASE:
if(n >= 7) { // "(PHRASE"
buf += snprintf(buf, n, "(PHRASE");
n -= 7;
}
break;
case WP_QUERY_NEG:
if(n >= 4) {
buf += snprintf(buf, n, "(NOT");
n -= 4;
}
break;
}
int subq_size = subquery_to_s(q, n, buf);
n -= subq_size;
buf += subq_size;
if(n >= 1) buf += sprintf(buf, ")");
ret = buf - orig_buf;
}
return ret;
}
wp_query* wp_query_set_all_child_fields(wp_query* q, const char* field) {
if(q->type == WP_QUERY_TERM) q->field = field;
else for(wp_query* child = q->children; child != NULL; child = child->next) wp_query_set_all_child_fields(child, strdup(field));
return q;
}