1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 Index Data
3 * See the file LICENSE for details.
5 /** \file querytowrbuf.c
6 \brief Convert Z39.50 Z_Query to PQF (as WRBUF string)
15 #include <yaz/logrpn.h>
16 #include <yaz/querytowrbuf.h>
17 #include <yaz/oid_db.h>
19 void yaz_encode_pqf_term(WRBUF b, const char *term, int len)
22 for (i = 0; i < len; i++)
23 if (strchr(" \"{", term[i]))
26 wrbuf_printf(b, "%.*s ", len, term);
30 for (i = 0; i<len; i++)
34 wrbuf_putc(b, term[i]);
36 wrbuf_printf(b, "\" ");
40 static void yaz_attribute_element_to_wrbuf(WRBUF b,
41 const Z_AttributeElement *element)
45 wrbuf_puts(b, "@attr ");
46 if (element->attributeSet)
48 char oid_name_str[OID_STR_MAX];
49 const char *setname = yaz_oid_to_string_buf(element->attributeSet,
53 wrbuf_puts(b, setname);
57 wrbuf_printf(b, ODR_INT_PRINTF "=", *element->attributeType);
58 switch (element->which)
60 case Z_AttributeValue_numeric:
61 wrbuf_printf(b, ODR_INT_PRINTF, *element->value.numeric);
63 case Z_AttributeValue_complex:
64 for (i = 0; i<element->value.complex->num_list; i++)
68 if (element->value.complex->list[i]->which ==
69 Z_StringOrNumeric_string)
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, ODR_INT_PRINTF,
75 *element->value.complex->list[i]->u.numeric);
79 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 " ODR_INT_PRINTF " ",
121 *zapt->term->u.numeric);
124 wrbuf_printf(b, "@term null x");
127 wrbuf_printf(b, "@term null unknown%d ", zapt->term->which);
131 static void yaz_rpnstructure_to_wrbuf(WRBUF b, const Z_RPNStructure *zs)
133 if (zs->which == Z_RPNStructure_complex)
135 Z_Operator *op = zs->u.complex->roperator;
136 wrbuf_printf(b, "@%s ", complex_op_name(op) );
137 if (op->which== Z_Operator_prox)
139 if (!op->u.prox->exclusion)
141 else if (*op->u.prox->exclusion)
146 wrbuf_printf(b, " " ODR_INT_PRINTF " %d "
147 ODR_INT_PRINTF " ", *op->u.prox->distance,
148 *op->u.prox->ordered,
149 *op->u.prox->relationType);
151 switch(op->u.prox->which)
153 case Z_ProximityOperator_known:
156 case Z_ProximityOperator_private:
160 wrbuf_printf(b, "%d", op->u.prox->which);
162 if (op->u.prox->u.known)
163 wrbuf_printf(b, " " ODR_INT_PRINTF " ", *op->u.prox->u.known);
165 wrbuf_printf(b, " 0 ");
167 yaz_rpnstructure_to_wrbuf(b,zs->u.complex->s1);
168 yaz_rpnstructure_to_wrbuf(b,zs->u.complex->s2);
170 else if (zs->which == Z_RPNStructure_simple)
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)
176 wrbuf_printf(b, "@set ");
177 yaz_encode_pqf_term(b, zs->u.simple->u.resultSetId,
178 strlen(zs->u.simple->u.resultSetId));
181 wrbuf_printf (b, "(unknown simple structure)");
184 wrbuf_puts(b, "(unknown structure)");
187 void yaz_rpnquery_to_wrbuf(WRBUF b, const Z_RPNQuery *rpn)
189 if (rpn->attributeSetId)
191 char oid_name_str[OID_STR_MAX];
192 const char *oid_name = yaz_oid_to_string_buf(rpn->attributeSetId,
195 wrbuf_printf(b, "@attrset %s ", oid_name);
197 yaz_rpnstructure_to_wrbuf(b, rpn->RPNStructure);
201 void yaz_query_to_wrbuf(WRBUF b, const Z_Query *q)
208 case Z_Query_type_101:
209 wrbuf_printf(b,"RPN ");
210 yaz_rpnquery_to_wrbuf(b, q->u.type_1);
213 wrbuf_printf(b, "CCL %.*s", q->u.type_2->len, q->u.type_2->buf);
215 case Z_Query_type_100:
216 wrbuf_printf(b, "Z39.58 %.*s", q->u.type_100->len,
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);
223 wrbuf_printf(b,"UNKNOWN type 104 query %d", q->u.type_104->which);
227 void yaz_scan_to_wrbuf(WRBUF b, const Z_AttributesPlusTerm *zapt,
228 const Odr_oid *attrbute_set)
230 /* should print attr set here */
231 wrbuf_printf(b, "RPN ");
232 yaz_apt_to_wrbuf(b, zapt);
235 void wrbuf_diags(WRBUF b, int num_diagnostics, Z_DiagRec **diags)
237 /* we only dump the first diag - that keeps the log cleaner. */
238 wrbuf_printf(b," ERROR ");
239 if (diags[0]->which != Z_DiagRec_defaultFormat)
240 wrbuf_printf(b,"(diag not in default format?)");
243 Z_DefaultDiagFormat *e=diags[0]->u.defaultFormat;
245 wrbuf_printf(b, ODR_INT_PRINTF " ",*e->condition);
247 wrbuf_printf(b, "?? ");
248 if ((e->which==Z_DefaultDiagFormat_v2Addinfo) && (e->u.v2Addinfo))
249 wrbuf_printf(b,"%s ",e->u.v2Addinfo);
250 else if ((e->which==Z_DefaultDiagFormat_v3Addinfo) && (e->u.v3Addinfo))
251 wrbuf_printf(b,"%s ",e->u.v3Addinfo);
258 * c-file-style: "Stroustrup"
259 * indent-tabs-mode: nil
261 * vim: shiftwidth=4 tabstop=8 expandtab