yaz-marcdump: fix leaks for MARCXML parsing YAZ-694
[yaz-moved-to-github.git] / util / cql2xcql.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2013 Index Data
3  * See the file LICENSE for details.
4  */
5 #if HAVE_CONFIG_H
6 #include <config.h>
7 #endif
8
9 #include <stdio.h>
10 #include <stdlib.h>
11
12 #include <yaz/cql.h>
13 #include <yaz/wrbuf.h>
14 #include <yaz/options.h>
15
16 static void usage(const char *prog)
17 {
18     fprintf(stderr, "%s: [-c] [-n iterations] [-s] [-S] [infile]\n", prog);
19     exit(1);
20 }
21
22 int main(int argc, char **argv)
23 {
24     CQL_parser cp;
25     int r = 0;
26     const char *fname = 0;
27     int iterations = 1;
28     int ret;
29     int convert_to_ccl = 0;
30     char *arg;
31     char *prog = argv[0];
32     int do_sortkeys = 0;
33     int do_strict = 0;
34
35     while ((ret = options("cn:sS", argv, argc, &arg)) != YAZ_OPTIONS_EOF)
36     {
37         switch (ret)
38         {
39         case 0:
40             fname = arg;
41             break;
42         case 'c':
43             convert_to_ccl = 1;
44             break;
45         case 'n':
46             iterations = atoi(arg);
47             break;
48         case 's':
49             do_sortkeys = 1;
50             break;
51         case 'S':
52             do_strict = 1;
53             break;
54         default:
55             usage(prog);
56         }
57     }
58
59     cp = cql_parser_create();
60     cql_parser_strict(cp, do_strict);
61     if (fname)
62     {
63         int i;
64         for (i = 0; i<iterations; i++)
65             r = cql_parser_string(cp, fname);
66     }
67     else
68         r = cql_parser_stdio(cp, stdin);
69     if (r)
70         fprintf (stderr, "Syntax error\n");
71     else
72     {
73         if (convert_to_ccl)
74         {
75             cql_to_ccl_stdio(cql_parser_result(cp), stdout);
76             putchar('\n');
77         }
78         else
79             cql_to_xml_stdio(cql_parser_result(cp), stdout);
80         if (do_sortkeys)
81         {
82             WRBUF w = wrbuf_alloc();
83             r = cql_sortby_to_sortkeys(cql_parser_result(cp),
84                                        wrbuf_vp_puts, w);
85             if (r == 0)
86                 printf("sortkeys: %s\n", wrbuf_cstr(w));
87             else
88                 fprintf(stderr, "failed to generate sortkeys\n");
89             wrbuf_destroy(w);
90         }
91     }
92     cql_parser_destroy(cp);
93     return 0;
94 }
95 /*
96  * Local variables:
97  * c-basic-offset: 4
98  * c-file-style: "Stroustrup"
99  * indent-tabs-mode: nil
100  * End:
101  * vim: shiftwidth=4 tabstop=8 expandtab
102  */
103