Ejemplo n.º 1
0
static void tst1(void)
{
    cql_transform_t ct = cql_transform_create();    

    YAZ_CHECK(compare(ct, "abc", "abc"));
    YAZ_CHECK(compare(ct, "\"a b c\"", "\"a b c\""));
    YAZ_CHECK(compare(ct, "@and a b", "a and b"));
    YAZ_CHECK(compare(ct, "@attr 1=field abc", "field=abc"));
    YAZ_CHECK(compare(ct, "@attr 1=4 abc", 0)); /* should fail */

    cql_transform_define_pattern(ct, "index.title", "1=4");
    YAZ_CHECK(compare(ct, "@attr 1=4 abc", "title=abc"));

    cql_transform_define_pattern(ct, "index.foo", "1=bar");
    YAZ_CHECK(compare(ct, "@attr 1=bar abc", "foo=abc"));

    cql_transform_close(ct);
}
Ejemplo n.º 2
0
cql_transform_t cql_transform_open_FILE(FILE *f)
{
    cql_transform_t ct = cql_transform_create();
    char line[1024];

    yaz_tok_cfg_single_tokens(ct->tok_cfg, "=");

    while (fgets(line, sizeof(line)-1, f))
    {
        yaz_tok_parse_t tp = yaz_tok_parse_buf(ct->tok_cfg, line);
        int t;
        wrbuf_rewind(ct->w);
        t = yaz_tok_move(tp);
        if (t == YAZ_TOK_STRING)
        {
            char * pattern = xstrdup(yaz_tok_parse_string(tp));
            t = yaz_tok_move(tp);
            if (t != '=')
            {
                yaz_tok_parse_destroy(tp);
                cql_transform_close(ct);
                return 0;
            }
            if (cql_transform_parse_tok_line(ct, pattern, tp))
            {
                yaz_tok_parse_destroy(tp);
                cql_transform_close(ct);
                return 0;
            }
            xfree(pattern);
        }
        else if (t != YAZ_TOK_EOF)
        {
            yaz_tok_parse_destroy(tp);
            cql_transform_close(ct);
            return 0;
        }
        yaz_tok_parse_destroy(tp);
    }
    return ct;
}
Ejemplo n.º 3
0
static void tst1(void)
{
    cql_transform_t ct = cql_transform_create();

    YAZ_CHECK(compare(ct, "abc", "abc"));
    YAZ_CHECK(compare(ct, "\"a b c\"", "\"a b c\""));
    YAZ_CHECK(compare(ct, "@and a b", "a and b"));
    YAZ_CHECK(compare(ct, "@or a @and b c", "a or (b and c)"));
    YAZ_CHECK(compare(ct, "@or @and a b @and c d", "(a and b) or (c and d)"));
    YAZ_CHECK(compare(ct, "@or @or a b @or c d", "(a or b) or (c or d)"));
    YAZ_CHECK(compare(ct, "@and @and a b @and c d", "(a and b) and (c and d)"));

    YAZ_CHECK(compare(ct, "@attr 1=field abc", "field=abc"));
    YAZ_CHECK(compare2(ct, "@attr 1=4 abc", 0, 114)); /* should fail */

    cql_transform_define_pattern(ct, "index.title", "1=4");
    YAZ_CHECK(compare(ct, "@attr 1=4 abc", "title=abc"));

    cql_transform_define_pattern(ct, "index.foo", "1=bar");
    YAZ_CHECK(compare(ct, "@attr 1=bar abc", "foo=abc"));

    cql_transform_close(ct);
}
Ejemplo n.º 4
0
int main(int argc, char **argv)
{
    cql_transform_t ct;
    int i, iterations = 1;
    char *query = 0;
    char *fname = 0;
    int reverse = 0;

    int ret;
    char *arg;

    while ((ret = options("n:r", argv, argc, &arg)) != -2)
    {
        switch (ret)
        {
        case 0:
            if (!fname)
                fname = arg;
            else
                query = arg;
            break;
        case 'r':
            reverse = 1;
            break;
        case 'n':
            iterations = atoi(arg);
            break;
        default:
            usage();
        }
    }
    if (!fname)
        usage();
    if (!strcmp(fname, "-"))
        ct = cql_transform_create();
    else
        ct = cql_transform_open_fname(fname);
    if (!ct)
    {
        fprintf(stderr, "failed to read properties %s\n", fname);
        exit(1);
    }

    if (reverse)
    {
        if (!query)
            usage();
        else
        {
            ODR odr = odr_createmem(ODR_ENCODE);
            YAZ_PQF_Parser pp = yaz_pqf_create();
            Z_RPNQuery *rpn = yaz_pqf_parse(pp, odr, query);
            if (!rpn)
            {
                fprintf(stderr, "PQF syntax error\n");
            }
            else 
            {
                int ret = cql_transform_rpn2cql_stream(ct, cql_fputs,
                                                       stdout, rpn);
                
                if (ret)
                {
                    const char *addinfo;
                    int r = cql_transform_error(ct, &addinfo);
                    printf("Transform error %d %s\n", r, addinfo ? addinfo : "");
                }
                else
                    printf("\n");
            }
            yaz_pqf_destroy(pp);
            odr_destroy(odr);
        }
    }
    else
    {
        CQL_parser cp = cql_parser_create();
        int r = 0;
        
        if (query)
        {
            for (i = 0; i<iterations; i++)
                r = cql_parser_string(cp, query);
        }
        else
            r = cql_parser_stdio(cp, stdin);
        
        if (r)
            fprintf(stderr, "Syntax error\n");
        else
        {
            r = cql_transform_FILE(ct, cql_parser_result(cp), stdout);
            printf("\n");
            if (r)
            {
                const char *addinfo;
                r = 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_parser_destroy(cp);
    }
    cql_transform_close(ct);
    return 0;
}