New OID database - with public definitions in oid_db.h. Removed old OID
[yaz-moved-to-github.git] / src / querytowrbuf.c
1 /*
2  * Copyright (C) 1995-2007, Index Data ApS
3  * All rights reserved.
4  *
5  * $Id: querytowrbuf.c,v 1.9 2007-04-12 13:52:57 adam Exp $
6  */
7
8 /** \file querytowrbuf.c
9     \brief Query to WRBUF (to strings)
10  */
11
12 #include <stdio.h>
13 #include <assert.h>
14
15 #include <yaz/logrpn.h>
16 #include <yaz/querytowrbuf.h>
17 #include <yaz/oid_db.h>
18
19 static void yaz_term_to_wrbuf(WRBUF b, const char *term, int len)
20 {
21     int i;
22     for (i = 0; i < len; i++)
23         if (strchr(" \"{", term[i]))
24             break;
25     if (i == len && i)
26         wrbuf_printf(b, "%.*s ", len, term);
27     else
28     {
29         wrbuf_putc(b, '"');
30         for (i = 0; i<len; i++)
31         {
32             if (term[i] == '"')
33                 wrbuf_putc(b, '\\');
34             wrbuf_putc(b, term[i]);
35         }
36         wrbuf_printf(b, "\" ");
37     }
38 }
39
40 static void yaz_attribute_element_to_wrbuf(WRBUF b,
41                                            const Z_AttributeElement *element)
42 {
43     int i;
44     char oid_name_str[OID_STR_MAX];
45     const char *setname = 0;
46     char *sep = " "; /* optional space after attrset name */
47     if (element->attributeSet)
48     {
49         setname = yaz_oid_to_string_buf(element->attributeSet, 
50                                         0, oid_name_str);
51     }
52     if (!setname)
53     {
54         setname = "";
55         sep = "";
56     }
57     switch (element->which) 
58     {
59     case Z_AttributeValue_numeric:
60         wrbuf_printf(b,"@attr %s%s%d=%d ", setname, sep,
61                      *element->attributeType, *element->value.numeric);
62         break;
63     case Z_AttributeValue_complex:
64         wrbuf_printf(b,"@attr %s%s\"%d=", setname, sep,
65                      *element->attributeType);
66         for (i = 0; i<element->value.complex->num_list; i++)
67         {
68             if (i)
69                 wrbuf_printf(b,",");
70             if (element->value.complex->list[i]->which ==
71                 Z_StringOrNumeric_string)
72                 wrbuf_printf (b, "%s",
73                               element->value.complex->list[i]->u.string);
74             else if (element->value.complex->list[i]->which ==
75                      Z_StringOrNumeric_numeric)
76                 wrbuf_printf (b, "%d", 
77                               *element->value.complex->list[i]->u.numeric);
78         }
79         wrbuf_printf(b, "\" ");
80         break;
81     default:
82         wrbuf_printf (b, "@attr 1=unknown ");
83     }
84 }
85
86 static const char *complex_op_name(const Z_Operator *op)
87 {
88     switch (op->which)
89     {
90     case Z_Operator_and:
91         return "and";
92     case Z_Operator_or:
93         return "or";
94     case Z_Operator_and_not:
95         return "not";
96     case Z_Operator_prox:
97         return "prox";
98     default:
99         return "unknown complex operator";
100     }
101 }
102
103 static void yaz_apt_to_wrbuf(WRBUF b, const Z_AttributesPlusTerm *zapt)
104 {
105     int num_attributes = zapt->attributes->num_attributes;
106     int i;
107     for (i = 0; i<num_attributes; i++)
108         yaz_attribute_element_to_wrbuf(b,zapt->attributes->attributes[i]);
109     
110     switch (zapt->term->which)
111     {
112     case Z_Term_general:
113         yaz_term_to_wrbuf(b, (const char *)zapt->term->u.general->buf,
114                           zapt->term->u.general->len);
115         break;
116     case Z_Term_characterString:
117         wrbuf_printf(b, "@term string ");
118         yaz_term_to_wrbuf(b, zapt->term->u.characterString,
119                           strlen(zapt->term->u.characterString));
120         break;
121     case Z_Term_numeric:
122         wrbuf_printf(b, "@term numeric %d ", *zapt->term->u.numeric);
123         break;
124     case Z_Term_null:
125         wrbuf_printf(b, "@term null x");
126         break;
127     default:
128         wrbuf_printf(b, "@term null unknown%d ", zapt->term->which);
129     }
130 }
131
132 static void yaz_rpnstructure_to_wrbuf(WRBUF b, const Z_RPNStructure *zs)
133 {
134     if (zs->which == Z_RPNStructure_complex)
135     {
136         Z_Operator *op = zs->u.complex->roperator;
137         wrbuf_printf(b, "@%s ", complex_op_name(op) );
138         if (op->which== Z_Operator_prox)
139         {
140             if (!op->u.prox->exclusion)
141                 wrbuf_putc(b, 'n');
142             else if (*op->u.prox->exclusion)
143                 wrbuf_putc(b, '1');
144             else
145                 wrbuf_putc(b, '0');
146
147             wrbuf_printf(b, " %d %d %d ", *op->u.prox->distance,
148                          *op->u.prox->ordered,
149                          *op->u.prox->relationType);
150
151             switch(op->u.prox->which)
152             {
153             case Z_ProximityOperator_known:
154                 wrbuf_putc(b, 'k');
155                 break;
156             case Z_ProximityOperator_private:
157                 wrbuf_putc(b, 'p');
158                 break;
159             default:
160                 wrbuf_printf(b, "%d", op->u.prox->which);
161             }
162             if (op->u.prox->u.known)
163                 wrbuf_printf(b, " %d ", *op->u.prox->u.known);
164             else
165                 wrbuf_printf(b, " 0 ");
166         }
167         yaz_rpnstructure_to_wrbuf(b,zs->u.complex->s1);
168         yaz_rpnstructure_to_wrbuf(b,zs->u.complex->s2);
169     }
170     else if (zs->which == Z_RPNStructure_simple)
171     {
172         if (zs->u.simple->which == Z_Operand_APT)
173             yaz_apt_to_wrbuf(b, zs->u.simple->u.attributesPlusTerm);
174         else if (zs->u.simple->which == Z_Operand_resultSetId)
175         {
176             wrbuf_printf(b, "@set ");
177             yaz_term_to_wrbuf(b, zs->u.simple->u.resultSetId,
178                               strlen(zs->u.simple->u.resultSetId));
179         }
180         else
181             wrbuf_printf (b, "(unknown simple structure)");
182     }
183     else
184         wrbuf_puts(b, "(unknown structure)");
185 }
186
187 void yaz_rpnquery_to_wrbuf(WRBUF b, const Z_RPNQuery *rpn)
188 {
189     if (rpn->attributeSetId)
190     {
191         char oid_name_str[OID_STR_MAX];
192         const char *oid_name = yaz_oid_to_string_buf(rpn->attributeSetId,
193                                                      0, oid_name_str);
194         if (oid_name)
195             wrbuf_printf(b, "@attrset %s ", oid_name);
196     } 
197     yaz_rpnstructure_to_wrbuf(b, rpn->RPNStructure);
198     wrbuf_chop_right(b);
199 }
200
201 void yaz_query_to_wrbuf(WRBUF b, const Z_Query *q)
202 {
203     assert(q);
204     assert(b);
205     switch (q->which)
206     {
207     case Z_Query_type_1: 
208     case Z_Query_type_101:
209         wrbuf_printf(b,"RPN ");
210         yaz_rpnquery_to_wrbuf(b, q->u.type_1);
211         break;
212     case Z_Query_type_2:
213         wrbuf_printf(b, "CCL %.*s", q->u.type_2->len, q->u.type_2->buf);
214         break;
215     case Z_Query_type_100:
216         wrbuf_printf(b, "Z39.58 %.*s", q->u.type_100->len,
217                      q->u.type_100->buf);
218         break;
219     case Z_Query_type_104:
220         if (q->u.type_104->which == Z_External_CQL)
221             wrbuf_printf(b, "CQL %s", q->u.type_104->u.cql);
222         else
223             wrbuf_printf(b,"UNKNOWN type 104 query %d", q->u.type_104->which);
224     }
225 }
226
227 void yaz_scan_to_wrbuf(WRBUF b, const Z_AttributesPlusTerm *zapt,
228                        const int *attrbute_set)
229 {
230     /* should print attr set here */
231     wrbuf_printf(b, "RPN ");
232     yaz_apt_to_wrbuf(b, zapt);
233 }
234
235 /*
236  * Local variables:
237  * c-basic-offset: 4
238  * indent-tabs-mode: nil
239  * End:
240  * vim: shiftwidth=4 tabstop=8 expandtab
241  */
242