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