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