Slightly different format in generated C code
[yaz-moved-to-github.git] / util / yaz-xmlquery.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2011 Index Data
3  * See the file LICENSE for details.
4  */
5 #if HAVE_CONFIG_H
6 #include <config.h>
7 #endif
8
9 #include <stdlib.h>
10 #include <stdio.h>
11
12 #include <yaz/options.h>
13 #include <yaz/querytowrbuf.h>
14 #include <yaz/xmlquery.h>
15 #include <yaz/pquery.h>
16 #include <yaz/test.h>
17
18 #if YAZ_HAVE_XML2
19 #include <libxml/parser.h>
20 #endif
21
22 static char *prog = "yaz-xmlquery";
23
24 #if YAZ_HAVE_XML2
25 void pqftoxmlquery(const char *pqf)
26 {
27     YAZ_PQF_Parser parser = yaz_pqf_create();
28     ODR odr = odr_createmem(ODR_ENCODE);
29     Z_RPNQuery *rpn;
30
31     if (!parser)
32     {
33         fprintf(stderr, "%s: cannot create parser\n", prog);
34         exit(1);
35     }
36
37     if (!odr)
38     {
39         fprintf(stderr, "%s: cannot create parser\n", prog);
40         exit(1);
41     }
42
43     rpn = yaz_pqf_parse(parser, odr, pqf);
44
45     yaz_pqf_destroy(parser);
46
47     if (!rpn)
48     {
49         fprintf(stderr, "%s: pqf parse error for query %s\n", prog, pqf);
50         exit(2);
51     }
52     else
53     {
54         xmlDocPtr doc = 0;
55         
56         yaz_rpnquery2xml(rpn, &doc);
57         
58         if (!doc)
59         {
60             fprintf(stderr, "%s: yaz_rpnquery2xml failed for query %s\n",
61                     prog, pqf);
62             exit(3);
63         }
64         else
65         {
66             xmlChar *buf_out = 0;
67             int len_out = 0;
68
69             xmlDocDumpMemory(doc, &buf_out, &len_out);
70
71             if (!len_out || !buf_out)
72             {
73                 fprintf(stderr, "%s: xmlDocDumpMemory failed for query %s\n",
74                         prog, pqf);
75                 exit(4);
76             }
77             else
78             {
79                 if (fwrite(buf_out, len_out, 1, stdout) != 1)
80                 {
81                     fprintf(stderr, "%s: write failed\n", prog);
82                     exit(5);
83                 }
84             }
85             xmlFreeDoc(doc);
86         }
87     }    
88     odr_destroy(odr);
89 }
90
91
92 void xmlquerytopqf(const char *xmlstr)
93 {
94     xmlDocPtr doc;
95
96     doc = xmlParseMemory(xmlstr, strlen(xmlstr));
97     if (!doc)
98     {
99         fprintf(stderr, "%s: xml parse error for XML:\n%s\n", prog, xmlstr);
100         exit(1);
101     }
102     else
103     {
104         int error_code = 0;
105         const char *addinfo = 0;
106         Z_Query *query = 0;
107         ODR odr = odr_createmem(ODR_ENCODE);
108
109         const xmlNode *root_element = xmlDocGetRootElement(doc);
110         yaz_xml2query(root_element, &query, odr, &error_code, &addinfo);
111         if (error_code)
112         {
113             fprintf(stderr, "%s: yaz_xml2query failed code=%d addinfo=%s\n",
114                     prog, error_code, addinfo);
115             exit(1);
116         }
117         else if (!query)
118         {
119             fprintf(stderr, "%s: yaz_xml2query no query result\n",
120                     prog);
121             exit(1);
122         }
123         else
124         {
125             WRBUF w = wrbuf_alloc();
126             yaz_query_to_wrbuf(w, query);
127             printf("%s\n", wrbuf_cstr(w));
128             wrbuf_destroy(w);
129         }
130         odr_destroy(odr);
131         xmlFreeDoc(doc);
132     }
133 }
134
135 void xmlfiletopqf(const char *xmlfile)
136 {
137     long sz;
138     char *xmlstr;
139     FILE *f = fopen(xmlfile, "rb");
140     if (!f)
141     {
142         fprintf(stderr, "%s: cannot open %s\n", prog, xmlfile);
143         exit(1);
144     }
145     fseek(f, 0, SEEK_END);
146     sz = ftell(f);
147     if (sz <= 0 || sz >= 1<<18)
148     {
149         fprintf(stderr, "%s: bad size for file %s\n", prog, xmlfile);
150         exit(1);
151     }
152     rewind(f);
153     xmlstr = (char *) xmalloc(sz+1);
154     xmlstr[sz] = '\0';
155     if (fread(xmlstr, sz, 1, f) != 1)
156     {
157         fprintf(stderr, "%s: read failed for file %s\n", prog, xmlfile);
158         exit(1);
159     }
160     if (fclose(f))
161     {
162         fprintf(stderr, "%s: close failed for file %s\n", prog, xmlfile);
163         exit(1);
164     }
165     
166     xmlquerytopqf(xmlstr);
167     xfree(xmlstr);
168 }
169 #endif
170
171 void usage(void)
172 {
173     fprintf(stderr, "%s [-p pqf] [-x xmlfile]\n", prog);
174     fprintf(stderr, " -p pqf      reads pqf. write xml to stdout\n");
175     fprintf(stderr, " -x xmlfile  reads XML from file. write pqf to stdout\n");
176     exit(1);
177 }
178
179 int main (int argc, char **argv)
180 {
181 #if YAZ_HAVE_XML2
182     char *arg;
183     int r;
184     int active = 0;
185
186     while ((r = options("-p:x:", argv, argc, &arg)) != -2)
187     {
188         switch(r)
189         {
190         case 'p':
191             pqftoxmlquery(arg);
192             active = 1;
193             break;
194         case 'x':
195             xmlfiletopqf(arg);
196             active = 1;
197             break;
198         case 0:
199             break;
200         }
201     }
202     if (!active)
203     {
204         fprintf(stderr, "%s: nothing to do\n", prog);
205         usage();
206     }
207 #else
208     fprintf(stderr, "%s: XML support not enabled.\n", prog);
209     exit(1);
210 #endif
211     return 0;
212 }
213