Merge branch 'master' of ssh://git.indexdata.com/home/git/pub/yaz
[yaz-moved-to-github.git] / util / srwtst.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 Index Data
3  * See the file LICENSE for details.
4  */
5 #if HAVE_CONFIG_H
6 #include <config.h>
7 #endif
8
9 #include <stdlib.h>
10 #include <yaz/srw.h>
11
12 #if YAZ_HAVE_XML2
13 Z_SOAP_Handler h[2] = {
14     {"http://www.loc.gov/zing/srw/v1.0/", 0, (Z_SOAP_fun) yaz_srw_codec},
15     {0, 0, 0}
16 };
17
18 int main(int argc, char **argv)
19 {
20     char buf[163840];
21     char *content_buf = buf;
22     int content_len;
23     int ret;
24     size_t no;
25     Z_SOAP *soap_package = 0;
26     ODR decode, encode;
27     int debug = 0;
28
29     if (argc == 2 && !strcmp(argv[1], "debug"))
30         debug = 1;
31     no = fread(buf, 1, sizeof(buf), stdin);
32     if (no < 1 || no == sizeof(buf))
33     {
34         fprintf(stderr, "Bad file or too big\n");
35         exit (1);
36     }
37     decode = odr_createmem(ODR_DECODE);
38     encode = odr_createmem(ODR_ENCODE);
39     content_len = no;
40     ret = z_soap_codec(decode, &soap_package, 
41                        &content_buf, &content_len, h);
42     if (!soap_package)
43     {
44         fprintf(stderr, "Decoding seriously failed\n");
45         exit(1);
46     }
47     if (debug)
48     {
49         fprintf(stderr, "got NS = %s\n", soap_package->ns);
50         if (soap_package->which == Z_SOAP_generic &&
51             soap_package->u.generic->no == 0)
52         {
53             Z_SRW_PDU *sr = (Z_SRW_PDU *) soap_package->u.generic->p;
54             if (sr->which == Z_SRW_searchRetrieve_request)
55             { 
56                 Z_SRW_searchRetrieveRequest *req = sr->u.request;
57                 switch(req->query_type)
58                 {
59                 case Z_SRW_query_type_cql:
60                     fprintf(stderr, "CQL: %s\n", req->query.cql);
61                     break;
62                 case Z_SRW_query_type_xcql:
63                     fprintf(stderr, "XCQL\n");
64                     break;
65                 case Z_SRW_query_type_pqf:
66                     fprintf(stderr, "PQF: %s\n", req->query.pqf);
67                     break;
68                 }
69             }
70             else if (sr->which == Z_SRW_searchRetrieve_response)
71             {
72                 Z_SRW_searchRetrieveResponse *res = sr->u.response;
73                 if (res->records && res->num_records)
74                 {
75                     int i;
76                     for (i = 0; i<res->num_records; i++)
77                     {
78                         fprintf (stderr, "%d\n", i);
79                         if (res->records[i].recordData_buf)
80                         {
81                             fprintf(stderr, "%.*s",
82                                     res->records[i].recordData_len,
83                                     res->records[i].recordData_buf);
84                         }
85                     }
86                 }
87             }
88
89         }
90     }
91     ret = z_soap_codec(encode, &soap_package,
92                        &content_buf, &content_len, h);
93     if (content_buf && content_len)
94     {
95         printf("%.*s", content_len, content_buf);
96     }
97     else
98     {
99         fprintf(stderr, "No output!\n");
100         exit(1);
101     }
102     odr_destroy(decode);
103     odr_destroy(encode);
104     exit(0);
105 }
106 #else
107 int main(int argc, char **argv)
108 {
109     fprintf(stderr, "SOAP disabled\n");
110     exit(1);
111 }
112 #endif
113 /*
114  * Local variables:
115  * c-basic-offset: 4
116  * c-file-style: "Stroustrup"
117  * indent-tabs-mode: nil
118  * End:
119  * vim: shiftwidth=4 tabstop=8 expandtab
120  */
121