1 /* $Id: xcqlutil.c,v 1.1 2003-01-06 08:20:27 adam Exp $
2 Copyright (C) 2002-2003
5 This file is part of the YAZ toolkit.
15 static void pr_n(const char *buf,
16 void (*pr)(const char *buf, void *client_data),
17 void *client_data, int n)
21 (*pr)(" ", client_data);
22 (*pr)(buf, client_data);
25 static void pr_cdata(const char *buf,
26 void (*pr)(const char *buf, void *client_data),
29 const char *src = buf;
36 (*pr)("&", client_data);
39 (*pr)("<", client_data);
42 (*pr)(">", client_data);
47 (*pr)(bf, client_data);
53 static void prefixes(struct cql_node *cn,
54 void (*pr)(const char *buf, void *client_data),
55 void *client_data, int level)
59 pr_n("<prefixes>\n", pr, client_data, level);
60 for (; cn; cn = cn->u.mod.next)
62 pr_n("<prefix>\n", pr, client_data, level+2);
65 pr_n("<name>", pr, client_data, level+4);
66 pr_cdata(cn->u.mod.name, pr, client_data);
67 pr_n("</name>\n", pr, client_data, 0);
71 pr_n("<identifier>", pr, client_data, level+4);
72 pr_cdata(cn->u.mod.value, pr, client_data);
73 pr_n("</identifier>\n", pr, client_data, 0);
75 pr_n("</prefix>\n", pr, client_data, level+2);
77 pr_n("</prefixes>\n", pr, client_data, level);
81 static void cql_to_xml_r(struct cql_node *cn,
82 void (*pr)(const char *buf, void *client_data),
83 void *client_data, int level)
90 pr_n("<searchClause>\n", pr, client_data, level);
91 prefixes(cn->u.st.prefixes, pr, client_data, level+2);
94 pr_n("<index>", pr, client_data, level+2);
95 pr_cdata(cn->u.st.index, pr, client_data);
96 pr_n("</index>\n", pr, client_data, 0);
98 if (cn->u.st.relation)
100 struct cql_node *m = cn->u.st.modifiers;
101 pr_n("<relation>\n", pr, client_data, level+2);
102 pr_n("<value>", pr, client_data, level+4);
103 pr_cdata(cn->u.st.relation, pr, client_data);
104 pr_n("</value>\n", pr, client_data, 0);
107 pr_n("<modifiers>\n", pr, client_data, level+4);
108 for (; m; m = m->u.mod.next)
110 pr_n("<modifier><value>", pr, client_data, level+6);
111 pr_cdata(m->u.mod.value, pr, client_data);
112 pr_n("</value></modifier>\n", pr, client_data, 0);
114 pr_n("</modifiers>\n", pr, client_data, level+4);
116 pr_n("</relation>\n", pr, client_data, level+2);
120 pr_n("<term>", pr, client_data, level+2);
121 pr_cdata(cn->u.st.term, pr, client_data);
122 pr_n("</term>\n", pr, client_data, 0);
124 pr_n("</searchClause>\n", pr, client_data, level);
127 pr_n("<triple>\n", pr, client_data, level);
128 prefixes(cn->u.st.prefixes, pr, client_data, level+2);
129 if (cn->u.bool.value)
131 struct cql_node *m = cn->u.bool.modifiers;
132 pr_n("<boolean>\n", pr, client_data, level+2);
134 pr_n("<value>", pr, client_data, level+4);
135 pr_cdata(cn->u.bool.value, pr, client_data);
136 pr_n("</value>\n", pr, client_data, 0);
140 pr_n("<modifiers>\n", pr, client_data, level+4);
141 for (; m; m = m->u.mod.next)
143 pr_n("<modifier><type>", pr, client_data, level+6);
144 pr_cdata(m->u.mod.name, pr, client_data);
145 pr_n("</type>", pr, client_data, 0);
148 pr_n("<value>", pr, client_data, 0);
149 pr_cdata(m->u.mod.value, pr, client_data);
150 pr_n("</value>", pr, client_data, 0);
152 pr_n("</modifier>\n", pr, client_data, 0);
154 pr_n("</modifiers>\n", pr, client_data, level+4);
156 pr_n("</boolean>\n", pr, client_data, level+2);
160 printf ("%*s<leftOperand>\n", level+2, "");
161 cql_to_xml_r(cn->u.bool.left, pr, client_data, level+4);
162 printf ("%*s</leftOperand>\n", level+2, "");
164 if (cn->u.bool.right)
166 printf ("%*s<rightOperand>\n", level+2, "");
167 cql_to_xml_r(cn->u.bool.right, pr, client_data, level+4);
168 printf ("%*s</rightOperand>\n", level+2, "");
170 pr_n("</triple>\n", pr, client_data, level);
174 void cql_to_xml(struct cql_node *cn,
175 void (*pr)(const char *buf, void *client_data),
178 cql_to_xml_r(cn, pr, client_data, 0);
181 void cql_to_xml_stdio(struct cql_node *cn, FILE *f)
183 cql_to_xml(cn, cql_fputs, f);
186 void cql_buf_write_handler (const char *b, void *client_data)
188 struct cql_buf_write_info *info = client_data;
190 if (info->off < 0 || (info->off + l >= info->max))
195 memcpy (info->buf + info->off, b, l);
199 int cql_to_xml_buf(struct cql_node *cn, char *out, int max)
201 struct cql_buf_write_info info;
205 cql_to_xml(cn, cql_buf_write_handler, &info);
207 info.buf[info.off] = '\0';