2 * Copyright (c) 1996-2001, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-ccl.c,v 1.15 2001-11-13 23:00:43 adam Exp $
12 #include <yaz/yaz-ccl.h>
14 static Z_RPNStructure *ccl_rpn_structure (ODR o, struct ccl_rpn_node *p);
16 static Z_AttributesPlusTerm *ccl_rpn_term (ODR o, struct ccl_rpn_node *p)
18 struct ccl_rpn_attr *attr;
20 Z_AttributesPlusTerm *zapt;
23 Z_AttributeElement **elements;
25 zapt = (Z_AttributesPlusTerm *)odr_malloc (o, sizeof(*zapt));
27 term_octet = (Odr_oct *)odr_malloc (o, sizeof(*term_octet));
29 term = (Z_Term *)odr_malloc (o, sizeof(*term));
31 for (attr = p->u.t.attr_list; attr; attr = attr->next)
34 elements = (Z_AttributeElement**)odr_nullval();
38 elements = (Z_AttributeElement **)
39 odr_malloc (o, num*sizeof(*elements));
40 for (attr = p->u.t.attr_list; attr; attr = attr->next, i++)
42 elements[i] = (Z_AttributeElement *)
43 odr_malloc (o, sizeof(**elements));
44 elements[i]->attributeType =
45 (int *)odr_malloc(o, sizeof(int));
46 *elements[i]->attributeType = attr->type;
47 elements[i]->attributeSet = 0;
48 if (attr->set && *attr->set)
50 int value = oid_getvalbyname (attr->set);
52 if (value != VAL_NONE)
54 elements[i]->attributeSet =
55 yaz_oidval_to_z3950oid(o, CLASS_ATTSET, value);
58 elements[i]->which = Z_AttributeValue_numeric;
59 elements[i]->value.numeric =
60 (int *)odr_malloc (o, sizeof(int));
61 *elements[i]->value.numeric = attr->value;
64 zapt->attributes = (Z_AttributeList *)
65 odr_malloc (o, sizeof(*zapt->attributes));
66 zapt->attributes->num_attributes = num;
67 zapt->attributes->attributes = elements;
69 term->which = Z_Term_general;
70 term->u.general = term_octet;
71 term_octet->len = term_octet->size = strlen (p->u.t.term);
72 term_octet->buf = (unsigned char *)odr_malloc (o, term_octet->len+1);
73 strcpy ((char*) term_octet->buf, p->u.t.term);
77 static Z_Operand *ccl_rpn_simple (ODR o, struct ccl_rpn_node *p)
81 zo = (Z_Operand *)odr_malloc (o, sizeof(*zo));
86 zo->which = Z_Operand_APT;
87 zo->u.attributesPlusTerm = ccl_rpn_term (o, p);
90 zo->which = Z_Operand_resultSetId;
91 zo->u.resultSetId = odr_strdup (o, p->u.setname);
99 static Z_Complex *ccl_rpn_complex (ODR o, struct ccl_rpn_node *p)
104 zc = (Z_Complex *)odr_malloc (o, sizeof(*zc));
105 zo = (Z_Operator *)odr_malloc (o, sizeof(*zo));
111 zo->which = Z_Operator_and;
112 zo->u.and_not = odr_nullval();
115 zo->which = Z_Operator_or;
116 zo->u.and_not = odr_nullval();
119 zo->which = Z_Operator_and_not;
120 zo->u.and_not = odr_nullval();
123 zo->which = Z_Operator_prox;
124 zo->u.prox = (Z_ProximityOperator *)
125 odr_malloc (o, sizeof(*zo->u.prox));
126 zo->u.prox->exclusion = 0;
128 zo->u.prox->distance = (int *)
129 odr_malloc (o, sizeof(*zo->u.prox->distance));
130 *zo->u.prox->distance = 2;
132 zo->u.prox->ordered = (bool_t *)
133 odr_malloc (o, sizeof(*zo->u.prox->ordered));
134 *zo->u.prox->ordered = 0;
136 zo->u.prox->relationType = (int *)
137 odr_malloc (o, sizeof(*zo->u.prox->relationType));
138 *zo->u.prox->relationType = Z_ProximityOperator_Prox_lessThan;
139 zo->u.prox->which = Z_ProximityOperator_known;
140 zo->u.prox->u.known =
141 (Z_ProxUnit *) odr_malloc (o, sizeof(*zo->u.prox->u.known));
142 *zo->u.prox->u.known = Z_ProxUnit_word;
147 zc->s1 = ccl_rpn_structure (o, p->u.p[0]);
148 zc->s2 = ccl_rpn_structure (o, p->u.p[1]);
152 static Z_RPNStructure *ccl_rpn_structure (ODR o, struct ccl_rpn_node *p)
156 zs = (Z_RPNStructure *)odr_malloc (o, sizeof(*zs));
163 zs->which = Z_RPNStructure_complex;
164 zs->u.complex = ccl_rpn_complex (o, p);
168 zs->which = Z_RPNStructure_simple;
169 zs->u.simple = ccl_rpn_simple (o, p);
177 Z_RPNQuery *ccl_rpn_query (ODR o, struct ccl_rpn_node *p)
179 Z_RPNQuery *zq = (Z_RPNQuery *)odr_malloc (o, sizeof(*zq));
180 zq->attributeSetId = yaz_oidval_to_z3950oid (o, CLASS_ATTSET, VAL_BIB1);
181 zq->RPNStructure = ccl_rpn_structure (o, p);
185 Z_AttributesPlusTerm *ccl_scan_query (ODR o, struct ccl_rpn_node *p)
187 if (p->kind != CCL_RPN_TERM)
189 return ccl_rpn_term (o, p);
192 static void ccl_pquery_complex (WRBUF w, struct ccl_rpn_node *p)
197 wrbuf_puts (w, "@and ");
200 wrbuf_puts(w, "@or ");
203 wrbuf_puts(w, "@not ");
206 wrbuf_puts(w, "@prox 0 2 0 1 known 2 ");
209 wrbuf_puts(w, "@ bad op (unknown) ");
211 ccl_pquery(w, p->u.p[0]);
212 ccl_pquery(w, p->u.p[1]);
215 void ccl_pquery (WRBUF w, struct ccl_rpn_node *p)
217 struct ccl_rpn_attr *att;
226 ccl_pquery_complex (w, p);
229 wrbuf_puts (w, "@set ");
230 wrbuf_puts (w, p->u.setname);
234 for (att = p->u.t.attr_list; att; att = att->next)
237 wrbuf_puts (w, "@attr ");
240 wrbuf_puts (w, att->set);
243 sprintf(tmpattr, "%d=%d ", att->type, att->value);
244 wrbuf_puts (w, tmpattr);
246 for (cp = p->u.t.term; *cp; cp++)
248 if (*cp == ' ' || *cp == '\\')
249 wrbuf_putc (w, '\\');