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" ODR_INT_PRINTF "=" ODR_INT_PRINTF " ",
60 *element->attributeType, *element->value.numeric);
62 case Z_AttributeValue_complex:
63 wrbuf_printf(b,"@attr %s%s\""ODR_INT_PRINTF "=", setname, sep,
64 *element->attributeType);
65 for (i = 0; i<element->value.complex->num_list; i++)
69 if (element->value.complex->list[i]->which ==
70 Z_StringOrNumeric_string)
71 wrbuf_printf (b, "%s",
72 element->value.complex->list[i]->u.string);
73 else if (element->value.complex->list[i]->which ==
74 Z_StringOrNumeric_numeric)
75 wrbuf_printf (b, ODR_INT_PRINTF,
76 *element->value.complex->list[i]->u.numeric);
78 wrbuf_printf(b, "\" ");
81 wrbuf_printf (b, "@attr 1=unknown ");
85 static const char *complex_op_name(const Z_Operator *op)
93 case Z_Operator_and_not:
98 return "unknown complex operator";
102 static void yaz_apt_to_wrbuf(WRBUF b, const Z_AttributesPlusTerm *zapt)
104 int num_attributes = zapt->attributes->num_attributes;
106 for (i = 0; i<num_attributes; i++)
107 yaz_attribute_element_to_wrbuf(b,zapt->attributes->attributes[i]);
109 switch (zapt->term->which)
112 yaz_encode_pqf_term(b, (const char *)zapt->term->u.general->buf,
113 zapt->term->u.general->len);
115 case Z_Term_characterString:
116 wrbuf_printf(b, "@term string ");
117 yaz_encode_pqf_term(b, zapt->term->u.characterString,
118 strlen(zapt->term->u.characterString));
121 wrbuf_printf(b, "@term numeric " ODR_INT_PRINTF " ",
122 *zapt->term->u.numeric);
125 wrbuf_printf(b, "@term null x");
128 wrbuf_printf(b, "@term null unknown%d ", zapt->term->which);
132 static void yaz_rpnstructure_to_wrbuf(WRBUF b, const Z_RPNStructure *zs)
134 if (zs->which == Z_RPNStructure_complex)
136 Z_Operator *op = zs->u.complex->roperator;
137 wrbuf_printf(b, "@%s ", complex_op_name(op) );
138 if (op->which== Z_Operator_prox)
140 if (!op->u.prox->exclusion)
142 else if (*op->u.prox->exclusion)
147 wrbuf_printf(b, " " ODR_INT_PRINTF " %d "
148 ODR_INT_PRINTF " ", *op->u.prox->distance,
149 *op->u.prox->ordered,
150 *op->u.prox->relationType);
152 switch(op->u.prox->which)
154 case Z_ProximityOperator_known:
157 case Z_ProximityOperator_private:
161 wrbuf_printf(b, "%d", op->u.prox->which);
163 if (op->u.prox->u.known)
164 wrbuf_printf(b, " " ODR_INT_PRINTF " ", *op->u.prox->u.known);
166 wrbuf_printf(b, " 0 ");
168 yaz_rpnstructure_to_wrbuf(b,zs->u.complex->s1);
169 yaz_rpnstructure_to_wrbuf(b,zs->u.complex->s2);
171 else if (zs->which == Z_RPNStructure_simple)
173 if (zs->u.simple->which == Z_Operand_APT)
174 yaz_apt_to_wrbuf(b, zs->u.simple->u.attributesPlusTerm);
175 else if (zs->u.simple->which == Z_Operand_resultSetId)
177 wrbuf_printf(b, "@set ");
178 yaz_encode_pqf_term(b, zs->u.simple->u.resultSetId,
179 strlen(zs->u.simple->u.resultSetId));
182 wrbuf_printf (b, "(unknown simple structure)");
185 wrbuf_puts(b, "(unknown structure)");
188 void yaz_rpnquery_to_wrbuf(WRBUF b, const Z_RPNQuery *rpn)
190 if (rpn->attributeSetId)
192 char oid_name_str[OID_STR_MAX];
193 const char *oid_name = yaz_oid_to_string_buf(rpn->attributeSetId,
196 wrbuf_printf(b, "@attrset %s ", oid_name);
198 yaz_rpnstructure_to_wrbuf(b, rpn->RPNStructure);
202 void yaz_query_to_wrbuf(WRBUF b, const Z_Query *q)
209 case Z_Query_type_101:
210 wrbuf_printf(b,"RPN ");
211 yaz_rpnquery_to_wrbuf(b, q->u.type_1);
214 wrbuf_printf(b, "CCL %.*s", q->u.type_2->len, q->u.type_2->buf);
216 case Z_Query_type_100:
217 wrbuf_printf(b, "Z39.58 %.*s", q->u.type_100->len,
220 case Z_Query_type_104:
221 if (q->u.type_104->which == Z_External_CQL)
222 wrbuf_printf(b, "CQL %s", q->u.type_104->u.cql);
224 wrbuf_printf(b,"UNKNOWN type 104 query %d", q->u.type_104->which);
228 void yaz_scan_to_wrbuf(WRBUF b, const Z_AttributesPlusTerm *zapt,
229 const Odr_oid *attrbute_set)
231 /* should print attr set here */
232 wrbuf_printf(b, "RPN ");
233 yaz_apt_to_wrbuf(b, zapt);
236 void wrbuf_diags(WRBUF b, int num_diagnostics,Z_DiagRec **diags)
238 /* we only dump the first diag - that keeps the log cleaner. */
239 wrbuf_printf(b," ERROR ");
240 if (diags[0]->which != Z_DiagRec_defaultFormat)
241 wrbuf_printf(b,"(diag not in default format?)");
244 Z_DefaultDiagFormat *e=diags[0]->u.defaultFormat;
246 wrbuf_printf(b, ODR_INT_PRINTF " ",*e->condition);
248 wrbuf_printf(b, "?? ");
249 if ((e->which==Z_DefaultDiagFormat_v2Addinfo) && (e->u.v2Addinfo))
250 wrbuf_printf(b,"%s ",e->u.v2Addinfo);
251 else if ((e->which==Z_DefaultDiagFormat_v3Addinfo) && (e->u.v3Addinfo))
252 wrbuf_printf(b,"%s ",e->u.v3Addinfo);
259 * c-file-style: "Stroustrup"
260 * indent-tabs-mode: nil
262 * vim: shiftwidth=4 tabstop=8 expandtab