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