cql2xcql: option -c makes conversion to CCL.
[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/options.h>
14
15 static void usage(const char *prog)
16 {
17     fprintf(stderr, "%s: [-c] [-n iterations] [infile]\n", prog);
18     exit(1);
19 }
20
21 int main(int argc, char **argv)
22 {
23     CQL_parser cp;
24     int r = 0;
25     const char *fname = 0;
26     int iterations = 1;
27     int ret;
28     int convert_to_ccl = 0;
29     char *arg;
30     char *prog = argv[0];
31
32     while ((ret = options("cn:", argv, argc, &arg)) != -2)
33     {
34         switch (ret)
35         {
36         case 0:
37             fname = arg;
38             break;
39         case 'c':
40             convert_to_ccl = 1;
41             break;
42         case 'n':
43             iterations = atoi(arg);
44             break;
45         default:
46             usage(prog);            
47         }
48     }
49
50     cp = cql_parser_create();
51     if (fname)
52     {
53         int i;
54         for (i = 0; i<iterations; i++)
55             r = cql_parser_string(cp, fname);
56     }
57     else
58         r = cql_parser_stdio(cp, stdin);
59     if (r)
60         fprintf (stderr, "Syntax error\n");
61     else
62     {
63         if (convert_to_ccl)
64         {
65             cql_to_ccl_stdio(cql_parser_result(cp), stdout);
66             putchar('\n');
67         }
68         else
69             cql_to_xml_stdio(cql_parser_result(cp), stdout);
70     }
71     cql_parser_destroy(cp);
72     return 0;
73 }
74 /*
75  * Local variables:
76  * c-basic-offset: 4
77  * c-file-style: "Stroustrup"
78  * indent-tabs-mode: nil
79  * End:
80  * vim: shiftwidth=4 tabstop=8 expandtab
81  */
82