2 * Copyright (c) 1996-2001, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.12 2001-03-07 13:24:40 adam
8 * Member and_not in Z_Operator is kept for backwards compatibility.
9 * Added support for definition of CCL operators in field spec file.
11 * Revision 1.11 2001/02/21 13:46:54 adam
14 * Revision 1.10 2001/02/20 11:23:50 adam
15 * Updated ccl_pquery to consider local attribute set too.
17 * Revision 1.9 2000/11/27 14:16:55 adam
18 * Fixed bug in ccl_rpn_simple regarding resultSetId's.
20 * Revision 1.8 2000/11/16 13:03:13 adam
21 * Function ccl_rpn_query sets attributeSet to Bib-1.
23 * Revision 1.7 2000/11/16 09:58:02 adam
24 * Implemented local AttributeSet setting for CCL field maps.
26 * Revision 1.6 2000/02/02 15:13:23 adam
29 * Revision 1.5 2000/01/31 13:15:22 adam
30 * Removed uses of assert(3). Cleanup of ODR. CCL parser update so
31 * that some characters are not surrounded by spaces in resulting term.
34 * Revision 1.4 1999/12/20 15:20:13 adam
35 * Implemented ccl_pquery to convert from CCL tree to prefix query.
37 * Revision 1.3 1999/11/30 13:47:12 adam
38 * Improved installation. Moved header files to include/yaz.
40 * Revision 1.2 1999/06/16 12:00:08 adam
43 * Revision 1.1 1999/06/08 10:12:43 adam
44 * Moved file to be part of zutil (instead of util).
46 * Revision 1.13 1998/03/31 15:13:20 adam
47 * Development towards compiled ASN.1.
49 * Revision 1.12 1998/02/11 11:53:36 adam
50 * Changed code so that it compiles as C++.
52 * Revision 1.11 1997/11/24 11:33:57 adam
53 * Using function odr_nullval() instead of global ODR_NULLVAL when
56 * Revision 1.10 1997/09/29 08:58:25 adam
57 * Fixed conversion of trees so that true copy is made.
59 * Revision 1.9 1997/06/23 10:31:25 adam
60 * Added ODR argument to ccl_rpn_query and ccl_scan_query.
62 * Revision 1.8 1996/10/29 13:36:27 adam
71 #include <yaz/yaz-ccl.h>
73 static Z_RPNStructure *ccl_rpn_structure (ODR o, struct ccl_rpn_node *p);
75 static Z_AttributesPlusTerm *ccl_rpn_term (ODR o, struct ccl_rpn_node *p)
77 struct ccl_rpn_attr *attr;
79 Z_AttributesPlusTerm *zapt;
82 Z_AttributeElement **elements;
84 zapt = (Z_AttributesPlusTerm *)odr_malloc (o, sizeof(*zapt));
86 term_octet = (Odr_oct *)odr_malloc (o, sizeof(*term_octet));
88 term = (Z_Term *)odr_malloc (o, sizeof(*term));
90 for (attr = p->u.t.attr_list; attr; attr = attr->next)
93 elements = (Z_AttributeElement**)odr_nullval();
97 elements = (Z_AttributeElement **)
98 odr_malloc (o, num*sizeof(*elements));
99 for (attr = p->u.t.attr_list; attr; attr = attr->next, i++)
101 elements[i] = (Z_AttributeElement *)
102 odr_malloc (o, sizeof(**elements));
103 elements[i]->attributeType =
104 (int *)odr_malloc(o, sizeof(int));
105 *elements[i]->attributeType = attr->type;
106 elements[i]->attributeSet = 0;
107 if (attr->set && *attr->set)
109 int value = oid_getvalbyname (attr->set);
111 if (value != VAL_NONE)
116 ident.oclass = CLASS_ATTSET;
117 ident.proto = PROTO_Z3950;
118 ident.value = (oid_value) value;
119 elements[i]->attributeSet =
120 odr_oiddup (o, oid_ent_to_oid (&ident, oid));
123 elements[i]->which = Z_AttributeValue_numeric;
124 elements[i]->value.numeric =
125 (int *)odr_malloc (o, sizeof(int));
126 *elements[i]->value.numeric = attr->value;
130 zapt->attributes = (Z_AttributeList *)
131 odr_malloc (o, sizeof(*zapt->attributes));
132 zapt->attributes->num_attributes = num;
133 zapt->attributes->attributes = elements;
135 zapt->num_attributes = num;
136 zapt->attributeList = elements;
139 term->which = Z_Term_general;
140 term->u.general = term_octet;
141 term_octet->len = term_octet->size = strlen (p->u.t.term);
142 term_octet->buf = (unsigned char *)odr_malloc (o, term_octet->len+1);
143 strcpy ((char*) term_octet->buf, p->u.t.term);
147 static Z_Operand *ccl_rpn_simple (ODR o, struct ccl_rpn_node *p)
151 zo = (Z_Operand *)odr_malloc (o, sizeof(*zo));
156 zo->which = Z_Operand_APT;
157 zo->u.attributesPlusTerm = ccl_rpn_term (o, p);
160 zo->which = Z_Operand_resultSetId;
161 zo->u.resultSetId = odr_strdup (o, p->u.setname);
169 static Z_Complex *ccl_rpn_complex (ODR o, struct ccl_rpn_node *p)
174 zc = (Z_Complex *)odr_malloc (o, sizeof(*zc));
175 zo = (Z_Operator *)odr_malloc (o, sizeof(*zo));
181 zo->which = Z_Operator_and;
182 zo->u.and_not = odr_nullval();
185 zo->which = Z_Operator_or;
186 zo->u.and_not = odr_nullval();
189 zo->which = Z_Operator_and_not;
190 zo->u.and_not = odr_nullval();
193 zo->which = Z_Operator_prox;
194 zo->u.prox = (Z_ProximityOperator *)
195 odr_malloc (o, sizeof(*zo->u.prox));
196 zo->u.prox->exclusion = 0;
198 zo->u.prox->distance = (int *)
199 odr_malloc (o, sizeof(*zo->u.prox->distance));
200 *zo->u.prox->distance = 2;
202 zo->u.prox->ordered = (bool_t *)
203 odr_malloc (o, sizeof(*zo->u.prox->ordered));
204 *zo->u.prox->ordered = 0;
206 zo->u.prox->relationType = (int *)
207 odr_malloc (o, sizeof(*zo->u.prox->relationType));
209 *zo->u.prox->relationType = Z_ProximityOperator_Prox_lessThan;
210 zo->u.prox->which = Z_ProximityOperator_known;
211 zo->u.prox->u.known =
212 (Z_ProxUnit *) odr_malloc (o, sizeof(*zo->u.prox->u.known));
213 *zo->u.prox->u.known = Z_ProxUnit_word;
215 *zo->u.prox->relationType = Z_Prox_lessThan;
216 zo->u.prox->which = Z_ProxCode_known;
217 zo->u.prox->proximityUnitCode = (int*)
218 odr_malloc (o, sizeof(*zo->u.prox->proximityUnitCode));
219 *zo->u.prox->proximityUnitCode = Z_ProxUnit_word;
225 zc->s1 = ccl_rpn_structure (o, p->u.p[0]);
226 zc->s2 = ccl_rpn_structure (o, p->u.p[1]);
230 static Z_RPNStructure *ccl_rpn_structure (ODR o, struct ccl_rpn_node *p)
234 zs = (Z_RPNStructure *)odr_malloc (o, sizeof(*zs));
241 zs->which = Z_RPNStructure_complex;
242 zs->u.complex = ccl_rpn_complex (o, p);
246 zs->which = Z_RPNStructure_simple;
247 zs->u.simple = ccl_rpn_simple (o, p);
255 Z_RPNQuery *ccl_rpn_query (ODR o, struct ccl_rpn_node *p)
260 bib1.proto = PROTO_Z3950;
261 bib1.oclass = CLASS_ATTSET;
262 bib1.value = VAL_BIB1;
264 zq = (Z_RPNQuery *)odr_malloc (o, sizeof(*zq));
265 zq->attributeSetId = odr_oiddup (o, oid_ent_to_oid (&bib1, oid));
266 zq->RPNStructure = ccl_rpn_structure (o, p);
270 Z_AttributesPlusTerm *ccl_scan_query (ODR o, struct ccl_rpn_node *p)
272 if (p->kind != CCL_RPN_TERM)
274 return ccl_rpn_term (o, p);
277 static void ccl_pquery_complex (WRBUF w, struct ccl_rpn_node *p)
282 wrbuf_puts (w, "@and ");
285 wrbuf_puts(w, "@or ");
288 wrbuf_puts(w, "@not ");
291 wrbuf_puts(w, "@prox 0 2 0 1 known 2 ");
294 wrbuf_puts(w, "@ bad op (unknown) ");
296 ccl_pquery(w, p->u.p[0]);
297 ccl_pquery(w, p->u.p[1]);
300 void ccl_pquery (WRBUF w, struct ccl_rpn_node *p)
302 struct ccl_rpn_attr *att;
310 ccl_pquery_complex (w, p);
313 wrbuf_puts (w, "@set ");
314 wrbuf_puts (w, p->u.setname);
318 for (att = p->u.t.attr_list; att; att = att->next)
321 wrbuf_puts (w, "@attr ");
324 wrbuf_puts (w, att->set);
327 sprintf(tmpattr, "%d=%d ", att->type, att->value);
328 wrbuf_puts (w, tmpattr);
331 wrbuf_puts (w, p->u.t.term);
332 wrbuf_puts (w, "} ");