1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2009 Index Data
3 * See the file LICENSE for details.
6 /** \file querytowrbuf.c
7 \brief Convert Z39.50 Z_Query to PQF (as WRBUF string)
13 #include <yaz/logrpn.h>
14 #include <yaz/querytowrbuf.h>
15 #include <yaz/oid_db.h>
17 void yaz_encode_pqf_term(WRBUF b, const char *term, int len)
20 for (i = 0; i < len; i++)
21 if (strchr(" \"{", term[i]))
24 wrbuf_printf(b, "%.*s ", len, term);
28 for (i = 0; i<len; i++)
32 wrbuf_putc(b, term[i]);
34 wrbuf_printf(b, "\" ");
38 static void yaz_attribute_element_to_wrbuf(WRBUF b,
39 const Z_AttributeElement *element)
42 char oid_name_str[OID_STR_MAX];
43 const char *setname = 0;
44 char *sep = " "; /* optional space after attrset name */
45 if (element->attributeSet)
47 setname = yaz_oid_to_string_buf(element->attributeSet,
55 switch (element->which)
57 case Z_AttributeValue_numeric:
58 wrbuf_printf(b,"@attr %s%s%d=%d ", setname, sep,
59 *element->attributeType, *element->value.numeric);
61 case Z_AttributeValue_complex:
62 wrbuf_printf(b,"@attr %s%s\"%d=", setname, sep,
63 *element->attributeType);
64 for (i = 0; i<element->value.complex->num_list; i++)
68 if (element->value.complex->list[i]->which ==
69 Z_StringOrNumeric_string)
70 wrbuf_printf (b, "%s",
71 element->value.complex->list[i]->u.string);
72 else if (element->value.complex->list[i]->which ==
73 Z_StringOrNumeric_numeric)
74 wrbuf_printf (b, "%d",
75 *element->value.complex->list[i]->u.numeric);
77 wrbuf_printf(b, "\" ");
80 wrbuf_printf (b, "@attr 1=unknown ");
84 static const char *complex_op_name(const Z_Operator *op)
92 case Z_Operator_and_not:
97 return "unknown complex operator";
101 static void yaz_apt_to_wrbuf(WRBUF b, const Z_AttributesPlusTerm *zapt)
103 int num_attributes = zapt->attributes->num_attributes;
105 for (i = 0; i<num_attributes; i++)
106 yaz_attribute_element_to_wrbuf(b,zapt->attributes->attributes[i]);
108 switch (zapt->term->which)
111 yaz_encode_pqf_term(b, (const char *)zapt->term->u.general->buf,
112 zapt->term->u.general->len);
114 case Z_Term_characterString:
115 wrbuf_printf(b, "@term string ");
116 yaz_encode_pqf_term(b, zapt->term->u.characterString,
117 strlen(zapt->term->u.characterString));
120 wrbuf_printf(b, "@term numeric %d ", *zapt->term->u.numeric);
123 wrbuf_printf(b, "@term null x");
126 wrbuf_printf(b, "@term null unknown%d ", zapt->term->which);
130 static void yaz_rpnstructure_to_wrbuf(WRBUF b, const Z_RPNStructure *zs)
132 if (zs->which == Z_RPNStructure_complex)
134 Z_Operator *op = zs->u.complex->roperator;
135 wrbuf_printf(b, "@%s ", complex_op_name(op) );
136 if (op->which== Z_Operator_prox)
138 if (!op->u.prox->exclusion)
140 else if (*op->u.prox->exclusion)
145 wrbuf_printf(b, " %d %d %d ", *op->u.prox->distance,
146 *op->u.prox->ordered,
147 *op->u.prox->relationType);
149 switch(op->u.prox->which)
151 case Z_ProximityOperator_known:
154 case Z_ProximityOperator_private:
158 wrbuf_printf(b, "%d", op->u.prox->which);
160 if (op->u.prox->u.known)
161 wrbuf_printf(b, " %d ", *op->u.prox->u.known);
163 wrbuf_printf(b, " 0 ");
165 yaz_rpnstructure_to_wrbuf(b,zs->u.complex->s1);
166 yaz_rpnstructure_to_wrbuf(b,zs->u.complex->s2);
168 else if (zs->which == Z_RPNStructure_simple)
170 if (zs->u.simple->which == Z_Operand_APT)
171 yaz_apt_to_wrbuf(b, zs->u.simple->u.attributesPlusTerm);
172 else if (zs->u.simple->which == Z_Operand_resultSetId)
174 wrbuf_printf(b, "@set ");
175 yaz_encode_pqf_term(b, zs->u.simple->u.resultSetId,
176 strlen(zs->u.simple->u.resultSetId));
179 wrbuf_printf (b, "(unknown simple structure)");
182 wrbuf_puts(b, "(unknown structure)");
185 void yaz_rpnquery_to_wrbuf(WRBUF b, const Z_RPNQuery *rpn)
187 if (rpn->attributeSetId)
189 char oid_name_str[OID_STR_MAX];
190 const char *oid_name = yaz_oid_to_string_buf(rpn->attributeSetId,
193 wrbuf_printf(b, "@attrset %s ", oid_name);
195 yaz_rpnstructure_to_wrbuf(b, rpn->RPNStructure);
199 void yaz_query_to_wrbuf(WRBUF b, const Z_Query *q)
206 case Z_Query_type_101:
207 wrbuf_printf(b,"RPN ");
208 yaz_rpnquery_to_wrbuf(b, q->u.type_1);
211 wrbuf_printf(b, "CCL %.*s", q->u.type_2->len, q->u.type_2->buf);
213 case Z_Query_type_100:
214 wrbuf_printf(b, "Z39.58 %.*s", q->u.type_100->len,
217 case Z_Query_type_104:
218 if (q->u.type_104->which == Z_External_CQL)
219 wrbuf_printf(b, "CQL %s", q->u.type_104->u.cql);
221 wrbuf_printf(b,"UNKNOWN type 104 query %d", q->u.type_104->which);
225 void yaz_scan_to_wrbuf(WRBUF b, const Z_AttributesPlusTerm *zapt,
226 const Odr_oid *attrbute_set)
228 /* should print attr set here */
229 wrbuf_printf(b, "RPN ");
230 yaz_apt_to_wrbuf(b, zapt);
233 void wrbuf_diags(WRBUF b, int num_diagnostics,Z_DiagRec **diags)
235 /* we only dump the first diag - that keeps the log cleaner. */
236 wrbuf_printf(b," ERROR ");
237 if (diags[0]->which != Z_DiagRec_defaultFormat)
238 wrbuf_printf(b,"(diag not in default format?)");
241 Z_DefaultDiagFormat *e=diags[0]->u.defaultFormat;
243 wrbuf_printf(b, "%d ",*e->condition);
245 wrbuf_printf(b, "?? ");
246 if ((e->which==Z_DefaultDiagFormat_v2Addinfo) && (e->u.v2Addinfo))
247 wrbuf_printf(b,"%s ",e->u.v2Addinfo);
248 else if ((e->which==Z_DefaultDiagFormat_v3Addinfo) && (e->u.v3Addinfo))
249 wrbuf_printf(b,"%s ",e->u.v3Addinfo);
256 * c-file-style: "Stroustrup"
257 * indent-tabs-mode: nil
259 * vim: shiftwidth=4 tabstop=8 expandtab