File: cql2xcql.c

package info (click to toggle)
yaz 5.27.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 16,184 kB
  • sloc: xml: 123,414; ansic: 72,530; sh: 5,007; tcl: 2,169; makefile: 1,321; yacc: 382
file content (103 lines) | stat: -rw-r--r-- 2,272 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* This file is part of the YAZ toolkit.
 * Copyright (C) Index Data
 * See the file LICENSE for details.
 */
#if HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>

#include <yaz/cql.h>
#include <yaz/wrbuf.h>
#include <yaz/options.h>

static void usage(const char *prog)
{
    fprintf(stderr, "%s: [-c] [-n iterations] [-s] [-S] [infile]\n", prog);
    exit(1);
}

int main(int argc, char **argv)
{
    CQL_parser cp;
    int r = 0;
    const char *fname = 0;
    int iterations = 1;
    int ret;
    int convert_to_ccl = 0;
    char *arg;
    char *prog = argv[0];
    int do_sortkeys = 0;
    int do_strict = 0;

    while ((ret = options("cn:sS", argv, argc, &arg)) != YAZ_OPTIONS_EOF)
    {
        switch (ret)
        {
        case 0:
            fname = arg;
            break;
        case 'c':
            convert_to_ccl = 1;
            break;
        case 'n':
            iterations = atoi(arg);
            break;
        case 's':
            do_sortkeys = 1;
            break;
        case 'S':
            do_strict = 1;
            break;
        default:
            usage(prog);
        }
    }

    cp = cql_parser_create();
    cql_parser_strict(cp, do_strict);
    if (fname)
    {
        int i;
        for (i = 0; i<iterations; i++)
            r = cql_parser_string(cp, fname);
    }
    else
        r = cql_parser_stdio(cp, stdin);
    if (r)
        fprintf (stderr, "Syntax error\n");
    else
    {
        if (convert_to_ccl)
        {
            cql_to_ccl_stdio(cql_parser_result(cp), stdout);
            putchar('\n');
        }
        else
            cql_to_xml_stdio(cql_parser_result(cp), stdout);
        if (do_sortkeys)
        {
            WRBUF w = wrbuf_alloc();
            r = cql_sortby_to_sortkeys(cql_parser_result(cp),
                                       wrbuf_vp_puts, w);
            if (r == 0)
                printf("sortkeys: %s\n", wrbuf_cstr(w));
            else
                fprintf(stderr, "failed to generate sortkeys\n");
            wrbuf_destroy(w);
        }
    }
    cql_parser_destroy(cp);
    return 0;
}
/*
 * Local variables:
 * c-basic-offset: 4
 * c-file-style: "Stroustrup"
 * indent-tabs-mode: nil
 * End:
 * vim: shiftwidth=4 tabstop=8 expandtab
 */