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