cql2xcql: option -s deals with CQL sortkeys
[yaz-moved-to-github.git] / util / cql2xcql.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 <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] [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
34     while ((ret = options("cn:s", argv, argc, &arg)) != YAZ_OPTIONS_EOF)
35     {
36         switch (ret)
37         {
38         case 0:
39             fname = arg;
40             break;
41         case 'c':
42             convert_to_ccl = 1;
43             break;
44         case 'n':
45             iterations = atoi(arg);
46             break;
47         case 's':
48             do_sortkeys = 1;
49             break;
50         default:
51             usage(prog);            
52         }
53     }
54
55     cp = cql_parser_create();
56     if (fname)
57     {
58         int i;
59         for (i = 0; i<iterations; i++)
60             r = cql_parser_string(cp, fname);
61     }
62     else
63         r = cql_parser_stdio(cp, stdin);
64     if (r)
65         fprintf (stderr, "Syntax error\n");
66     else
67     {
68         if (convert_to_ccl)
69         {
70             cql_to_ccl_stdio(cql_parser_result(cp), stdout);
71             putchar('\n');
72         }
73         else
74             cql_to_xml_stdio(cql_parser_result(cp), stdout);
75         if (do_sortkeys)
76         {
77             WRBUF w = wrbuf_alloc();
78             r = cql_sortby_to_sortkeys(cql_parser_result(cp), 
79                                        wrbuf_vp_puts, w);
80             if (r == 0)
81                 printf("sortkeys: %s\n", wrbuf_cstr(w));
82             else
83                 fprintf(stderr, "failed to generate sortkeys\n");
84             wrbuf_destroy(w);
85         }
86     }
87     cql_parser_destroy(cp);
88     return 0;
89 }
90 /*
91  * Local variables:
92  * c-basic-offset: 4
93  * c-file-style: "Stroustrup"
94  * indent-tabs-mode: nil
95  * End:
96  * vim: shiftwidth=4 tabstop=8 expandtab
97  */
98