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