File: srwtst.c

package info (click to toggle)
yaz 2.1.18-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 11,988 kB
  • ctags: 11,045
  • sloc: xml: 109,719; ansic: 41,566; sh: 10,625; makefile: 1,115; tcl: 380; yacc: 288
file content (116 lines) | stat: -rw-r--r-- 3,180 bytes parent folder | download
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
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * Copyright (C) 1995-2005, Index Data ApS
 * See the file LICENSE for details.
 *
 * $Id: srwtst.c,v 1.4 2005/06/25 15:46:07 adam Exp $
 */

#include <stdlib.h>
#include <yaz/srw.h>

#if HAVE_XML2
Z_SOAP_Handler h[2] = {
    {"http://www.loc.gov/zing/srw/v1.0/", 0, (Z_SOAP_fun) yaz_srw_codec},
    {0, 0, 0}
};

int main(int argc, char **argv)
{
    char buf[163840];
    char *content_buf = buf;
    int content_len;
    int ret;
    size_t no;
    Z_SOAP *soap_package = 0;
    ODR decode, encode;
    int debug = 0;

    nmem_init();
    if (argc == 2 && !strcmp(argv[1], "debug"))
        debug = 1;
    no = fread(buf, 1, sizeof(buf), stdin);
    if (no < 1 || no == sizeof(buf))
    {
        fprintf(stderr, "Bad file or too big\n");
        exit (1);
    }
    decode = odr_createmem(ODR_DECODE);
    encode = odr_createmem(ODR_ENCODE);
    content_len = no;
    ret = z_soap_codec(decode, &soap_package, 
                       &content_buf, &content_len, h);
    if (!soap_package)
    {
        fprintf(stderr, "Decoding seriously failed\n");
        exit(1);
    }
    if (debug)
    {
        fprintf(stderr, "got NS = %s\n", soap_package->ns);
        if (soap_package->which == Z_SOAP_generic &&
            soap_package->u.generic->no == 0)
        {
            Z_SRW_PDU *sr = soap_package->u.generic->p;
            if (sr->which == Z_SRW_searchRetrieve_request)
            { 
                Z_SRW_searchRetrieveRequest *req = sr->u.request;
                switch(req->query_type)
                {
                case Z_SRW_query_type_cql:
                    fprintf(stderr, "CQL: %s\n", req->query.cql);
                    break;
                case Z_SRW_query_type_xcql:
                    fprintf(stderr, "XCQL\n");
                    break;
                case Z_SRW_query_type_pqf:
                    fprintf(stderr, "PQF: %s\n", req->query.pqf);
                    break;
                }
            }
            else if (sr->which == Z_SRW_searchRetrieve_response)
            {
                Z_SRW_searchRetrieveResponse *res = sr->u.response;
                if (res->records && res->num_records)
                {
                    int i;
                    for (i = 0; i<res->num_records; i++)
                    {
                        fprintf (stderr, "%d\n", i);
                        if (res->records[i].recordData_buf)
                            fwrite(res->records[i].recordData_buf, 1,
                                   res->records[i].recordData_len, stderr);
                    }
                }
            }

        }
    }
    ret = z_soap_codec(encode, &soap_package,
                       &content_buf, &content_len, h);
    if (content_buf && content_len)
        fwrite (content_buf, content_len, 1, stdout);
    else
    {
        fprintf(stderr, "No output!\n");
        exit(1);
    }
    odr_destroy(decode);
    odr_destroy(encode);
    nmem_exit();
    exit(0);
}
#else
int main(int argc, char **argv)
{
    fprintf(stderr, "SOAP disabled\n");
    exit(1);
}
#endif
/*
 * Local variables:
 * c-basic-offset: 4
 * indent-tabs-mode: nil
 * End:
 * vim: shiftwidth=4 tabstop=8 expandtab
 */