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