Implemented local AttributeSet setting for CCL field maps.
[yaz-moved-to-github.git] / zutil / yaz-ccl.c
1 /*
2  * Copyright (c) 1996-2000, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: yaz-ccl.c,v $
7  * Revision 1.7  2000-11-16 09:58:02  adam
8  * Implemented local AttributeSet setting for CCL field maps.
9  *
10  * Revision 1.6  2000/02/02 15:13:23  adam
11  * Minor change.
12  *
13  * Revision 1.5  2000/01/31 13:15:22  adam
14  * Removed uses of assert(3). Cleanup of ODR. CCL parser update so
15  * that some characters are not surrounded by spaces in resulting term.
16  * ILL-code updates.
17  *
18  * Revision 1.4  1999/12/20 15:20:13  adam
19  * Implemented ccl_pquery to convert from CCL tree to prefix query.
20  *
21  * Revision 1.3  1999/11/30 13:47:12  adam
22  * Improved installation. Moved header files to include/yaz.
23  *
24  * Revision 1.2  1999/06/16 12:00:08  adam
25  * Added proximity.
26  *
27  * Revision 1.1  1999/06/08 10:12:43  adam
28  * Moved file to be part of zutil (instead of util).
29  *
30  * Revision 1.13  1998/03/31 15:13:20  adam
31  * Development towards compiled ASN.1.
32  *
33  * Revision 1.12  1998/02/11 11:53:36  adam
34  * Changed code so that it compiles as C++.
35  *
36  * Revision 1.11  1997/11/24 11:33:57  adam
37  * Using function odr_nullval() instead of global ODR_NULLVAL when
38  * appropriate.
39  *
40  * Revision 1.10  1997/09/29 08:58:25  adam
41  * Fixed conversion of trees so that true copy is made.
42  *
43  * Revision 1.9  1997/06/23 10:31:25  adam
44  * Added ODR argument to ccl_rpn_query and ccl_scan_query.
45  *
46  * Revision 1.8  1996/10/29 13:36:27  adam
47  * Added header.
48  *
49  */
50
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54
55 #include <yaz/yaz-ccl.h>
56
57 static Z_RPNStructure *ccl_rpn_structure (ODR o, struct ccl_rpn_node *p);
58
59 static Z_AttributesPlusTerm *ccl_rpn_term (ODR o, struct ccl_rpn_node *p)
60 {
61     struct ccl_rpn_attr *attr;
62     int num = 0;
63     Z_AttributesPlusTerm *zapt;
64     Odr_oct *term_octet;
65     Z_Term *term;
66     Z_AttributeElement **elements;
67
68     zapt = (Z_AttributesPlusTerm *)odr_malloc (o, sizeof(*zapt));
69
70     term_octet = (Odr_oct *)odr_malloc (o, sizeof(*term_octet));
71
72     term = (Z_Term *)odr_malloc (o, sizeof(*term));
73
74     for (attr = p->u.t.attr_list; attr; attr = attr->next)
75         num++;
76     if (!num)
77         elements = (Z_AttributeElement**)odr_nullval();
78     else
79     {
80         int i = 0;
81         elements = (Z_AttributeElement **)
82             odr_malloc (o, num*sizeof(*elements));
83         for (attr = p->u.t.attr_list; attr; attr = attr->next, i++)
84         {
85             elements[i] = (Z_AttributeElement *)
86                 odr_malloc (o, sizeof(**elements));
87             elements[i]->attributeType =
88                 (int *)odr_malloc(o, sizeof(int));
89             *elements[i]->attributeType = attr->type;
90             elements[i]->attributeSet = 0;
91             if (attr->set && *attr->set)
92             {
93                 int value = oid_getvalbyname (attr->set);
94
95                 if (value != VAL_NONE)
96                 {
97                     int oid[OID_SIZE];
98                     struct oident ident;
99
100                     ident.oclass = CLASS_ATTSET;
101                     ident.proto = PROTO_Z3950;
102                     ident.value = value;
103                     elements[i]->attributeSet =
104                         odr_oiddup (o, oid_ent_to_oid (&ident, oid));
105                 }
106             }
107             elements[i]->which = Z_AttributeValue_numeric;
108             elements[i]->value.numeric =
109                 (int *)odr_malloc (o, sizeof(int));
110             *elements[i]->value.numeric = attr->value;
111         }
112     }
113 #ifdef ASN_COMPILED
114     zapt->attributes = (Z_AttributeList *)
115         odr_malloc (o, sizeof(*zapt->attributes));
116     zapt->attributes->num_attributes = num;
117     zapt->attributes->attributes = elements;
118 #else
119     zapt->num_attributes = num;
120     zapt->attributeList = elements;
121 #endif    
122     zapt->term = term;
123     term->which = Z_Term_general;
124     term->u.general = term_octet;
125     term_octet->len = term_octet->size = strlen (p->u.t.term);
126     term_octet->buf = (unsigned char *)odr_malloc (o, term_octet->len+1);
127     strcpy ((char*) term_octet->buf, p->u.t.term);
128     return zapt;
129 }
130
131 static Z_Operand *ccl_rpn_simple (ODR o, struct ccl_rpn_node *p)
132 {
133     Z_Operand *zo;
134
135     zo = (Z_Operand *)odr_malloc (o, sizeof(*zo));
136
137     switch (p->kind)
138     {
139     case CCL_RPN_TERM:
140         zo->which = Z_Operand_APT;
141         zo->u.attributesPlusTerm = ccl_rpn_term (o, p);
142         break;
143     case CCL_RPN_SET:
144         zo->which = Z_Operand_resultSetId;
145         zo->u.resultSetId = p->u.setname;
146         break;
147     default:
148         return 0;
149     }
150     return zo;
151 }
152
153 static Z_Complex *ccl_rpn_complex (ODR o, struct ccl_rpn_node *p)
154 {
155     Z_Complex *zc;
156     Z_Operator *zo;
157
158     zc = (Z_Complex *)odr_malloc (o, sizeof(*zc));
159     zo = (Z_Operator *)odr_malloc (o, sizeof(*zo));
160
161     zc->roperator = zo;
162     switch (p->kind)
163     {
164     case CCL_RPN_AND:
165         zo->which = Z_Operator_and;
166         zo->u.and = odr_nullval();
167         break;
168     case CCL_RPN_OR:
169         zo->which = Z_Operator_or;
170         zo->u.and = odr_nullval();
171         break;
172     case CCL_RPN_NOT:
173         zo->which = Z_Operator_and_not;
174         zo->u.and = odr_nullval();
175         break;
176     case CCL_RPN_PROX:
177         zo->which = Z_Operator_prox;
178         zo->u.prox = (Z_ProximityOperator *)
179             odr_malloc (o, sizeof(*zo->u.prox));
180         zo->u.prox->exclusion = 0;
181
182         zo->u.prox->distance = (int *)
183             odr_malloc (o, sizeof(*zo->u.prox->distance));
184         *zo->u.prox->distance = 2;
185
186         zo->u.prox->ordered = (bool_t *)
187             odr_malloc (o, sizeof(*zo->u.prox->ordered));
188         *zo->u.prox->ordered = 0;
189
190         zo->u.prox->relationType = (int *)
191             odr_malloc (o, sizeof(*zo->u.prox->relationType));
192 #ifdef ASN_COMPILED
193         *zo->u.prox->relationType = Z_ProximityOperator_Prox_lessThan;
194         zo->u.prox->which = Z_ProximityOperator_known;
195         zo->u.prox->u.known = 
196             odr_malloc (o, sizeof(*zo->u.prox->u.known));
197         *zo->u.prox->u.known = Z_ProxUnit_word;
198 #else
199         *zo->u.prox->relationType = Z_Prox_lessThan;
200         zo->u.prox->which = Z_ProxCode_known;
201         zo->u.prox->proximityUnitCode = (int*)
202             odr_malloc (o, sizeof(*zo->u.prox->proximityUnitCode));
203         *zo->u.prox->proximityUnitCode = Z_ProxUnit_word;
204 #endif
205         break;
206     default:
207         return 0;
208     }
209     zc->s1 = ccl_rpn_structure (o, p->u.p[0]);
210     zc->s2 = ccl_rpn_structure (o, p->u.p[1]);
211     return zc;
212 }
213
214 static Z_RPNStructure *ccl_rpn_structure (ODR o, struct ccl_rpn_node *p)
215 {
216     Z_RPNStructure *zs;
217
218     zs = (Z_RPNStructure *)odr_malloc (o, sizeof(*zs));
219     switch (p->kind)
220     {
221     case CCL_RPN_AND:
222     case CCL_RPN_OR:
223     case CCL_RPN_NOT:
224     case CCL_RPN_PROX:
225         zs->which = Z_RPNStructure_complex;
226         zs->u.complex = ccl_rpn_complex (o, p);
227         break;
228     case CCL_RPN_TERM:
229     case CCL_RPN_SET:
230         zs->which = Z_RPNStructure_simple;
231         zs->u.simple = ccl_rpn_simple (o, p);
232         break;
233     default:
234         return 0;
235     }
236     return zs;
237 }
238
239 Z_RPNQuery *ccl_rpn_query (ODR o, struct ccl_rpn_node *p)
240 {
241     Z_RPNQuery *zq;
242
243     zq = (Z_RPNQuery *)odr_malloc (o, sizeof(*zq));
244     zq->attributeSetId = NULL;
245     zq->RPNStructure = ccl_rpn_structure (o, p);
246     return zq;
247 }
248
249 Z_AttributesPlusTerm *ccl_scan_query (ODR o, struct ccl_rpn_node *p)
250 {
251     if (p->kind != CCL_RPN_TERM)
252         return NULL;
253     return ccl_rpn_term (o, p);
254 }
255
256 static void ccl_pquery_complex (WRBUF w, struct ccl_rpn_node *p)
257 {
258     switch (p->kind)
259     {
260     case CCL_RPN_AND:
261         wrbuf_puts (w, "@and ");
262         break;
263     case CCL_RPN_OR:
264         wrbuf_puts(w, "@or ");
265         break;
266     case CCL_RPN_NOT:
267         wrbuf_puts(w, "@not ");
268         break;
269     case CCL_RPN_PROX:
270         wrbuf_puts(w, "@prox 0 2 0 1 known 2 ");
271         break;
272     default:
273         wrbuf_puts(w, "@ bad op (unknown) ");
274     };
275     ccl_pquery(w, p->u.p[0]);
276     ccl_pquery(w, p->u.p[1]);
277 }
278         
279 void ccl_pquery (WRBUF w, struct ccl_rpn_node *p)
280 {
281     struct ccl_rpn_attr *att;
282
283     switch (p->kind)
284     {
285     case CCL_RPN_AND:
286     case CCL_RPN_OR:
287     case CCL_RPN_NOT:
288     case CCL_RPN_PROX:
289         ccl_pquery_complex (w, p);
290         break;
291     case CCL_RPN_SET:
292         wrbuf_puts (w, "@set ");
293         wrbuf_puts (w, p->u.setname);
294         wrbuf_puts (w, " ");
295         break;
296     case CCL_RPN_TERM:
297         for (att = p->u.t.attr_list; att; att = att->next)
298         {
299             char tmpattr[128];
300             sprintf(tmpattr, "@attr %d=%d ", att->type, att->value);
301             wrbuf_puts (w, tmpattr);
302         }
303         wrbuf_puts (w, "{");
304         wrbuf_puts (w, p->u.t.term);
305         wrbuf_puts (w, "} ");
306         break;
307     }
308 }