Updated.
[yaz-moved-to-github.git] / util / yaz-ccl.c
1 /*
2  * Copyright (c) 1996-1997, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: yaz-ccl.c,v $
7  * Revision 1.11  1997-11-24 11:33:57  adam
8  * Using function odr_nullval() instead of global ODR_NULLVAL when
9  * appropriate.
10  *
11  * Revision 1.10  1997/09/29 08:58:25  adam
12  * Fixed conversion of trees so that true copy is made.
13  *
14  * Revision 1.9  1997/06/23 10:31:25  adam
15  * Added ODR argument to ccl_rpn_query and ccl_scan_query.
16  *
17  * Revision 1.8  1996/10/29 13:36:27  adam
18  * Added header.
19  *
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <assert.h>
26
27 #include <yaz-ccl.h>
28
29 static Z_RPNStructure *ccl_rpn_structure (ODR o, struct ccl_rpn_node *p);
30
31 static Z_AttributesPlusTerm *ccl_rpn_term (ODR o, struct ccl_rpn_node *p)
32 {
33     struct ccl_rpn_attr *attr;
34     int num = 0;
35     Z_AttributesPlusTerm *zapt;
36     Odr_oct *term_octet;
37     Z_Term *term;
38
39     zapt = odr_malloc (o, sizeof(*zapt));
40     assert (zapt);
41
42     term_octet = odr_malloc (o, sizeof(*term_octet));
43     assert (term_octet);
44
45     term = odr_malloc (o, sizeof(*term));
46     assert(term);
47
48     for (attr = p->u.t.attr_list; attr; attr = attr->next)
49         num++;
50     zapt->num_attributes = num;
51     if (num)
52     {
53         int i = 0;
54         zapt->attributeList = odr_malloc (o, num*sizeof(*zapt->attributeList));
55         assert (zapt->attributeList);
56         for (attr = p->u.t.attr_list; attr; attr = attr->next, i++)
57         {
58             zapt->attributeList[i] =
59                 odr_malloc (o, sizeof(**zapt->attributeList));
60             assert (zapt->attributeList[i]);
61             zapt->attributeList[i]->attributeType =
62                 odr_malloc(o, sizeof(int));
63             *zapt->attributeList[i]->attributeType = attr->type;
64             zapt->attributeList[i]->attributeSet = 0;
65             zapt->attributeList[i]->which = Z_AttributeValue_numeric;
66             zapt->attributeList[i]->value.numeric =
67                 odr_malloc (o, sizeof(int));
68             *zapt->attributeList[i]->value.numeric = attr->value;
69         }
70     }
71     else
72         zapt->attributeList = odr_nullval();
73     
74     zapt->term = term;
75     term->which = Z_Term_general;
76     term->u.general = term_octet;
77     term_octet->len = term_octet->size = strlen (p->u.t.term);
78     term_octet->buf = odr_malloc (o, term_octet->len+1);
79     strcpy ((char*) term_octet->buf, p->u.t.term);
80     return zapt;
81 }
82
83 static Z_Operand *ccl_rpn_simple (ODR o, struct ccl_rpn_node *p)
84 {
85     Z_Operand *zo;
86
87     zo = odr_malloc (o, sizeof(*zo));
88     assert (zo);
89
90     switch (p->kind)
91     {
92     case CCL_RPN_TERM:
93         zo->which = Z_Operand_APT;
94         zo->u.attributesPlusTerm = ccl_rpn_term (o, p);
95         break;
96     case CCL_RPN_SET:
97         zo->which = Z_Operand_resultSetId;
98         zo->u.resultSetId = p->u.setname;
99         break;
100     default:
101         assert (0);
102     }
103     return zo;
104 }
105
106 static Z_Complex *ccl_rpn_complex (ODR o, struct ccl_rpn_node *p)
107 {
108     Z_Complex *zc;
109     Z_Operator *zo;
110
111     zc = odr_malloc (o, sizeof(*zc));
112     assert (zc);
113     zo = odr_malloc (o, sizeof(*zo));
114     assert (zo);
115
116     zc->roperator = zo;
117     switch (p->kind)
118     {
119     case CCL_RPN_AND:
120         zo->which = Z_Operator_and;
121         zo->u.and = odr_nullval();
122         break;
123     case CCL_RPN_OR:
124         zo->which = Z_Operator_or;
125         zo->u.and = odr_nullval();
126         break;
127     case CCL_RPN_NOT:
128         zo->which = Z_Operator_and_not;
129         zo->u.and = odr_nullval();
130         break;
131     default:
132         assert (0);
133     }
134     zc->s1 = ccl_rpn_structure (o, p->u.p[0]);
135     zc->s2 = ccl_rpn_structure (o, p->u.p[1]);
136     return zc;
137 }
138
139 static Z_RPNStructure *ccl_rpn_structure (ODR o, struct ccl_rpn_node *p)
140 {
141     Z_RPNStructure *zs;
142
143     zs = odr_malloc (o, sizeof(*zs));
144     assert (zs);
145     switch (p->kind)
146     {
147     case CCL_RPN_AND:
148     case CCL_RPN_OR:
149     case CCL_RPN_NOT:
150     case CCL_RPN_PROX:
151         zs->which = Z_RPNStructure_complex;
152         zs->u.complex = ccl_rpn_complex (o, p);
153         break;
154     case CCL_RPN_TERM:
155     case CCL_RPN_SET:
156         zs->which = Z_RPNStructure_simple;
157         zs->u.simple = ccl_rpn_simple (o, p);
158         break;
159     default:
160         assert (0);
161     }
162     return zs;
163 }
164
165 Z_RPNQuery *ccl_rpn_query (ODR o, struct ccl_rpn_node *p)
166 {
167     Z_RPNQuery *zq;
168
169     zq = odr_malloc (o, sizeof(*zq));
170     assert (zq);
171     zq->attributeSetId = NULL;
172     zq->RPNStructure = ccl_rpn_structure (o, p);
173     return zq;
174 }
175
176 Z_AttributesPlusTerm *ccl_scan_query (ODR o, struct ccl_rpn_node *p)
177 {
178     if (p->kind != CCL_RPN_TERM)
179         return NULL;
180     return ccl_rpn_term (o, p);
181 }