Merge branch 'master' into sru_2_0
[yaz-moved-to-github.git] / util / srwtst.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2013 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     size_t no;
24     Z_SOAP *soap_package = 0;
25     ODR decode, encode;
26     int debug = 0;
27
28     if (argc == 2 && !strcmp(argv[1], "debug"))
29         debug = 1;
30     no = fread(buf, 1, sizeof(buf), stdin);
31     if (no < 1 || no == sizeof(buf))
32     {
33         fprintf(stderr, "Bad file or too big\n");
34         exit (1);
35     }
36     decode = odr_createmem(ODR_DECODE);
37     encode = odr_createmem(ODR_ENCODE);
38     content_len = no;
39     z_soap_codec(decode, &soap_package,
40                  &content_buf, &content_len, h);
41     if (!soap_package)
42     {
43         fprintf(stderr, "Decoding seriously failed\n");
44         exit(1);
45     }
46     if (debug)
47     {
48         fprintf(stderr, "got NS = %s\n", soap_package->ns);
49         if (soap_package->which == Z_SOAP_generic &&
50             soap_package->u.generic->no == 0)
51         {
52             Z_SRW_PDU *sr = (Z_SRW_PDU *) soap_package->u.generic->p;
53             if (sr->which == Z_SRW_searchRetrieve_request)
54             {
55                 Z_SRW_searchRetrieveRequest *req = sr->u.request;
56                 fprintf(stderr, "%s: %s\n", req->queryType, req->query);
57             }
58             else if (sr->which == Z_SRW_searchRetrieve_response)
59             {
60                 Z_SRW_searchRetrieveResponse *res = sr->u.response;
61                 if (res->records && res->num_records)
62                 {
63                     int i;
64                     for (i = 0; i<res->num_records; i++)
65                     {
66                         fprintf (stderr, "%d\n", i);
67                         if (res->records[i].recordData_buf)
68                         {
69                             fprintf(stderr, "%.*s",
70                                     res->records[i].recordData_len,
71                                     res->records[i].recordData_buf);
72                         }
73                     }
74                 }
75             }
76
77         }
78     }
79     z_soap_codec(encode, &soap_package, &content_buf, &content_len, h);
80     if (content_buf && content_len)
81     {
82         printf("%.*s", content_len, content_buf);
83     }
84     else
85     {
86         fprintf(stderr, "No output!\n");
87         exit(1);
88     }
89     odr_destroy(decode);
90     odr_destroy(encode);
91     exit(0);
92 }
93 #else
94 int main(int argc, char **argv)
95 {
96     fprintf(stderr, "SOAP disabled\n");
97     exit(1);
98 }
99 #endif
100 /*
101  * Local variables:
102  * c-basic-offset: 4
103  * c-file-style: "Stroustrup"
104  * indent-tabs-mode: nil
105  * End:
106  * vim: shiftwidth=4 tabstop=8 expandtab
107  */
108