b449a4b6e87005fb0a26cc0832ac68759fb30ef2
[yaz-moved-to-github.git] / util / cql2pqf.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/rpn2cql.h>
13 #include <yaz/pquery.h>
14 #include <yaz/options.h>
15
16 static void usage(void)
17 {
18     fprintf(stderr, "usage\n cql2pqf [-n <n>] [-r] <properties> [<query>]\n");
19     exit(1);
20 }
21
22 int main(int argc, char **argv)
23 {
24     cql_transform_t ct;
25     int i, iterations = 1;
26     char *query = 0;
27     char *fname = 0;
28     int reverse = 0;
29     int verbose = 1;
30
31     int ret;
32     char *arg;
33
34     while ((ret = options("n:rv", argv, argc, &arg)) != -2)
35     {
36         switch (ret)
37         {
38         case 0:
39             if (!fname)
40                 fname = arg;
41             else
42                 query = arg;
43             break;
44         case 'n':
45             iterations = atoi(arg);
46             break;
47         case 'r':
48             reverse = 1;
49             break;
50         case 'v':
51             verbose = 1;
52             break;
53         default:
54             usage();
55         }
56     }
57     if (!fname)
58         usage();
59     if (!strcmp(fname, "-"))
60         ct = cql_transform_create();
61     else
62         ct = cql_transform_open_fname(fname);
63     if (!ct)
64     {
65         fprintf(stderr, "failed to read properties %s\n", fname);
66         exit(1);
67     }
68
69     if (reverse)
70     {
71         if (!query)
72             usage();
73         else
74         {
75             ODR odr = odr_createmem(ODR_ENCODE);
76             YAZ_PQF_Parser pp = yaz_pqf_create();
77             Z_RPNQuery *rpn = yaz_pqf_parse(pp, odr, query);
78             if (!rpn)
79             {
80                 fprintf(stderr, "PQF syntax error\n");
81             }
82             else 
83             {
84                 int ret = cql_transform_rpn2cql_stream(ct, cql_fputs,
85                                                        stdout, rpn);
86                 
87                 if (ret)
88                 {
89                     const char *addinfo;
90                     int r = cql_transform_error(ct, &addinfo);
91                     printf("Transform error %d %s\n", r, addinfo ? addinfo : "");
92                 }
93                 else
94                     printf("\n");
95             }
96             yaz_pqf_destroy(pp);
97             odr_destroy(odr);
98         }
99     }
100     else
101     {
102         CQL_parser cp = cql_parser_create();
103         int r = 0;
104         
105         if (query)
106         {
107             if (verbose)
108                 printf("Parsing CQL %s\n", query);
109             for (i = 0; i<iterations; i++)
110                 r = cql_parser_string(cp, query);
111         }
112         else
113             r = cql_parser_stdio(cp, stdin);
114         
115         if (r)
116             fprintf(stderr, "Syntax error\n");
117         else
118         {
119             r = cql_transform_FILE(ct, cql_parser_result(cp), stdout);
120             printf("\n");
121             if (r)
122             {
123                 const char *addinfo;
124                 r = cql_transform_error(ct, &addinfo);
125                 printf("Transform error %d %s\n", r, addinfo ? addinfo : "");
126             }
127             else
128             {
129                 FILE *null = fopen("/dev/null", "w");
130                 for (i = 1; i<iterations; i++)
131                     cql_transform_FILE(ct, cql_parser_result(cp), null);
132                 fclose(null);
133             }
134         }
135         cql_parser_destroy(cp);
136     }
137     cql_transform_close(ct);
138     return 0;
139 }
140 /*
141  * Local variables:
142  * c-basic-offset: 4
143  * c-file-style: "Stroustrup"
144  * indent-tabs-mode: nil
145  * End:
146  * vim: shiftwidth=4 tabstop=8 expandtab
147  */
148