2 * Copyright (c) 1996-2001, Index Data.
3 * See the file LICENSE for details.
6 * Revision 1.13 2001-05-09 23:31:35 adam
7 * String attribute values for PQF. Proper C-backslash escaping for PQF.
9 * Revision 1.12 2001/03/07 13:24:40 adam
10 * Member and_not in Z_Operator is kept for backwards compatibility.
11 * Added support for definition of CCL operators in field spec file.
13 * Revision 1.11 2001/02/21 13:46:54 adam
16 * Revision 1.10 2001/02/20 11:23:50 adam
17 * Updated ccl_pquery to consider local attribute set too.
19 * Revision 1.9 2000/11/27 14:16:55 adam
20 * Fixed bug in ccl_rpn_simple regarding resultSetId's.
22 * Revision 1.8 2000/11/16 13:03:13 adam
23 * Function ccl_rpn_query sets attributeSet to Bib-1.
25 * Revision 1.7 2000/11/16 09:58:02 adam
26 * Implemented local AttributeSet setting for CCL field maps.
28 * Revision 1.6 2000/02/02 15:13:23 adam
31 * Revision 1.5 2000/01/31 13:15:22 adam
32 * Removed uses of assert(3). Cleanup of ODR. CCL parser update so
33 * that some characters are not surrounded by spaces in resulting term.
36 * Revision 1.4 1999/12/20 15:20:13 adam
37 * Implemented ccl_pquery to convert from CCL tree to prefix query.
39 * Revision 1.3 1999/11/30 13:47:12 adam
40 * Improved installation. Moved header files to include/yaz.
42 * Revision 1.2 1999/06/16 12:00:08 adam
45 * Revision 1.1 1999/06/08 10:12:43 adam
46 * Moved file to be part of zutil (instead of util).
48 * Revision 1.13 1998/03/31 15:13:20 adam
49 * Development towards compiled ASN.1.
51 * Revision 1.12 1998/02/11 11:53:36 adam
52 * Changed code so that it compiles as C++.
54 * Revision 1.11 1997/11/24 11:33:57 adam
55 * Using function odr_nullval() instead of global ODR_NULLVAL when
58 * Revision 1.10 1997/09/29 08:58:25 adam
59 * Fixed conversion of trees so that true copy is made.
61 * Revision 1.9 1997/06/23 10:31:25 adam
62 * Added ODR argument to ccl_rpn_query and ccl_scan_query.
64 * Revision 1.8 1996/10/29 13:36:27 adam
73 #include <yaz/yaz-ccl.h>
75 static Z_RPNStructure *ccl_rpn_structure (ODR o, struct ccl_rpn_node *p);
77 static Z_AttributesPlusTerm *ccl_rpn_term (ODR o, struct ccl_rpn_node *p)
79 struct ccl_rpn_attr *attr;
81 Z_AttributesPlusTerm *zapt;
84 Z_AttributeElement **elements;
86 zapt = (Z_AttributesPlusTerm *)odr_malloc (o, sizeof(*zapt));
88 term_octet = (Odr_oct *)odr_malloc (o, sizeof(*term_octet));
90 term = (Z_Term *)odr_malloc (o, sizeof(*term));
92 for (attr = p->u.t.attr_list; attr; attr = attr->next)
95 elements = (Z_AttributeElement**)odr_nullval();
99 elements = (Z_AttributeElement **)
100 odr_malloc (o, num*sizeof(*elements));
101 for (attr = p->u.t.attr_list; attr; attr = attr->next, i++)
103 elements[i] = (Z_AttributeElement *)
104 odr_malloc (o, sizeof(**elements));
105 elements[i]->attributeType =
106 (int *)odr_malloc(o, sizeof(int));
107 *elements[i]->attributeType = attr->type;
108 elements[i]->attributeSet = 0;
109 if (attr->set && *attr->set)
111 int value = oid_getvalbyname (attr->set);
113 if (value != VAL_NONE)
118 ident.oclass = CLASS_ATTSET;
119 ident.proto = PROTO_Z3950;
120 ident.value = (oid_value) value;
121 elements[i]->attributeSet =
122 odr_oiddup (o, oid_ent_to_oid (&ident, oid));
125 elements[i]->which = Z_AttributeValue_numeric;
126 elements[i]->value.numeric =
127 (int *)odr_malloc (o, sizeof(int));
128 *elements[i]->value.numeric = attr->value;
132 zapt->attributes = (Z_AttributeList *)
133 odr_malloc (o, sizeof(*zapt->attributes));
134 zapt->attributes->num_attributes = num;
135 zapt->attributes->attributes = elements;
137 zapt->num_attributes = num;
138 zapt->attributeList = elements;
141 term->which = Z_Term_general;
142 term->u.general = term_octet;
143 term_octet->len = term_octet->size = strlen (p->u.t.term);
144 term_octet->buf = (unsigned char *)odr_malloc (o, term_octet->len+1);
145 strcpy ((char*) term_octet->buf, p->u.t.term);
149 static Z_Operand *ccl_rpn_simple (ODR o, struct ccl_rpn_node *p)
153 zo = (Z_Operand *)odr_malloc (o, sizeof(*zo));
158 zo->which = Z_Operand_APT;
159 zo->u.attributesPlusTerm = ccl_rpn_term (o, p);
162 zo->which = Z_Operand_resultSetId;
163 zo->u.resultSetId = odr_strdup (o, p->u.setname);
171 static Z_Complex *ccl_rpn_complex (ODR o, struct ccl_rpn_node *p)
176 zc = (Z_Complex *)odr_malloc (o, sizeof(*zc));
177 zo = (Z_Operator *)odr_malloc (o, sizeof(*zo));
183 zo->which = Z_Operator_and;
184 zo->u.and_not = odr_nullval();
187 zo->which = Z_Operator_or;
188 zo->u.and_not = odr_nullval();
191 zo->which = Z_Operator_and_not;
192 zo->u.and_not = odr_nullval();
195 zo->which = Z_Operator_prox;
196 zo->u.prox = (Z_ProximityOperator *)
197 odr_malloc (o, sizeof(*zo->u.prox));
198 zo->u.prox->exclusion = 0;
200 zo->u.prox->distance = (int *)
201 odr_malloc (o, sizeof(*zo->u.prox->distance));
202 *zo->u.prox->distance = 2;
204 zo->u.prox->ordered = (bool_t *)
205 odr_malloc (o, sizeof(*zo->u.prox->ordered));
206 *zo->u.prox->ordered = 0;
208 zo->u.prox->relationType = (int *)
209 odr_malloc (o, sizeof(*zo->u.prox->relationType));
211 *zo->u.prox->relationType = Z_ProximityOperator_Prox_lessThan;
212 zo->u.prox->which = Z_ProximityOperator_known;
213 zo->u.prox->u.known =
214 (Z_ProxUnit *) odr_malloc (o, sizeof(*zo->u.prox->u.known));
215 *zo->u.prox->u.known = Z_ProxUnit_word;
217 *zo->u.prox->relationType = Z_Prox_lessThan;
218 zo->u.prox->which = Z_ProxCode_known;
219 zo->u.prox->proximityUnitCode = (int*)
220 odr_malloc (o, sizeof(*zo->u.prox->proximityUnitCode));
221 *zo->u.prox->proximityUnitCode = Z_ProxUnit_word;
227 zc->s1 = ccl_rpn_structure (o, p->u.p[0]);
228 zc->s2 = ccl_rpn_structure (o, p->u.p[1]);
232 static Z_RPNStructure *ccl_rpn_structure (ODR o, struct ccl_rpn_node *p)
236 zs = (Z_RPNStructure *)odr_malloc (o, sizeof(*zs));
243 zs->which = Z_RPNStructure_complex;
244 zs->u.complex = ccl_rpn_complex (o, p);
248 zs->which = Z_RPNStructure_simple;
249 zs->u.simple = ccl_rpn_simple (o, p);
257 Z_RPNQuery *ccl_rpn_query (ODR o, struct ccl_rpn_node *p)
262 bib1.proto = PROTO_Z3950;
263 bib1.oclass = CLASS_ATTSET;
264 bib1.value = VAL_BIB1;
266 zq = (Z_RPNQuery *)odr_malloc (o, sizeof(*zq));
267 zq->attributeSetId = odr_oiddup (o, oid_ent_to_oid (&bib1, oid));
268 zq->RPNStructure = ccl_rpn_structure (o, p);
272 Z_AttributesPlusTerm *ccl_scan_query (ODR o, struct ccl_rpn_node *p)
274 if (p->kind != CCL_RPN_TERM)
276 return ccl_rpn_term (o, p);
279 static void ccl_pquery_complex (WRBUF w, struct ccl_rpn_node *p)
284 wrbuf_puts (w, "@and ");
287 wrbuf_puts(w, "@or ");
290 wrbuf_puts(w, "@not ");
293 wrbuf_puts(w, "@prox 0 2 0 1 known 2 ");
296 wrbuf_puts(w, "@ bad op (unknown) ");
298 ccl_pquery(w, p->u.p[0]);
299 ccl_pquery(w, p->u.p[1]);
302 void ccl_pquery (WRBUF w, struct ccl_rpn_node *p)
304 struct ccl_rpn_attr *att;
313 ccl_pquery_complex (w, p);
316 wrbuf_puts (w, "@set ");
317 wrbuf_puts (w, p->u.setname);
321 for (att = p->u.t.attr_list; att; att = att->next)
324 wrbuf_puts (w, "@attr ");
327 wrbuf_puts (w, att->set);
330 sprintf(tmpattr, "%d=%d ", att->type, att->value);
331 wrbuf_puts (w, tmpattr);
333 for (cp = p->u.t.term; *cp; cp++)
335 if (*cp == ' ' || *cp == '\\')
336 wrbuf_putc (w, '\\');