Support read/write MARCXML collections.
[yaz-moved-to-github.git] / util / cql2pqf.c
index 516391f..4faab79 100644 (file)
@@ -1,5 +1,5 @@
-/* $Id: cql2pqf.c,v 1.2 2003-12-18 16:45:19 mike Exp $
-   Copyright (C) 2002-2003
+/* $Id: cql2pqf.c,v 1.9 2007-01-03 08:42:16 adam Exp $
+   Copyright (C) 1995-2007, Index Data ApS
    Index Data Aps
 
 This file is part of the YAZ toolkit.
@@ -11,32 +11,57 @@ See the file LICENSE.
 #include <stdio.h>
 
 #include <yaz/cql.h>
+#include <yaz/options.h>
+
+static void usage(void)
+{
+    fprintf (stderr, "usage\n cql2pqf [-n <n>] <properties> [<query>]\n");
+    exit (1);
+}
 
 int main(int argc, char **argv)
 {
     cql_transform_t ct;
-    int r;
-    int i, it = 1;
+    int r = 0;
+    int i, iterations = 1;
     CQL_parser cp = cql_parser_create();
+    char *query = 0;
+    char *fname = 0;
 
-    if (argc < 2)
+    int ret;
+    char *arg;
+
+    while ((ret = options("n:", argv, argc, &arg)) != -2)
     {
-        fprintf (stderr, "usage\n cqltransform <properties> [<query>] [iterations]\n");
-        exit (1);
+        switch (ret)
+        {
+        case 0:
+            if (!fname)
+                fname = arg;
+            else
+                query = arg;
+            break;
+        case 'n':
+            iterations = atoi(arg);
+            break;
+        default:
+            usage();
+        }
     }
-    ct = cql_transform_open_fname(argv[1]);
+    if (!fname)
+        usage();
+    ct = cql_transform_open_fname(fname);
     if (!ct)
     {
-        fprintf (stderr, "failed to read properties %s\n", argv[1]);
+        fprintf (stderr, "failed to read properties %s\n", fname);
         exit (1);
     }
-    if (argc >= 4)
-        it = atoi(argv[3]);
 
-    for (i = 0; i<it; i++)
+    if (query)
     {
-    if (argc >= 3)
-        r = cql_parser_string(cp, argv[2]);
+        for (i = 0; i<iterations; i++)
+            r = cql_parser_string(cp, query);
+    }
     else
         r = cql_parser_stdio(cp, stdin);
 
@@ -45,16 +70,30 @@ int main(int argc, char **argv)
     else
     {
         r = cql_transform_FILE(ct, cql_parser_result(cp), stdout);
-       printf("\n");
+        printf("\n");
         if (r)
         {
             const char *addinfo;
             cql_transform_error(ct, &addinfo);
             printf ("Transform error %d %s\n", r, addinfo ? addinfo : "");
         }
-    }
+        else
+        {
+            FILE *null = fopen("/dev/null", "w");
+            for (i = 1; i<iterations; i++)
+                cql_transform_FILE(ct, cql_parser_result(cp), null);
+            fclose(null);
+        }
     }
     cql_transform_close(ct);
     cql_parser_destroy(cp);
     return 0;
 }
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
+