096b2c5aedfb93f968f7d1f833e4c5eee96d4461
[yaz-moved-to-github.git] / util / srwtst.c
1 /*
2  * Copyright (C) 1995-2005, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: srwtst.c,v 1.3 2005-01-17 13:09:43 adam Exp $
6  */
7
8 #include <stdlib.h>
9 #include <yaz/srw.h>
10
11 #if HAVE_XML2
12 Z_SOAP_Handler h[2] = {
13     {"http://www.loc.gov/zing/srw/v1.0/", 0, (Z_SOAP_fun) yaz_srw_codec},
14     {0, 0, 0}
15 };
16
17 int main(int argc, char **argv)
18 {
19     char buf[163840];
20     char *content_buf = buf;
21     int content_len;
22     int ret;
23     size_t no;
24     Z_SOAP *soap_package = 0;
25     ODR decode, encode;
26     int debug = 0;
27
28     nmem_init();
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 = 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                             fwrite(res->records[i].recordData_buf, 1,
81                                    res->records[i].recordData_len, stderr);
82                     }
83                 }
84             }
85
86         }
87     }
88     ret = z_soap_codec(encode, &soap_package,
89                        &content_buf, &content_len, h);
90     if (content_buf && content_len)
91         fwrite (content_buf, content_len, 1, stdout);
92     else
93     {
94         fprintf(stderr, "No output!\n");
95         exit(1);
96     }
97     odr_destroy(decode);
98     odr_destroy(encode);
99     nmem_exit();
100     exit(0);
101 }
102 #else
103 int main(int argc, char **argv)
104 {
105     fprintf(stderr, "SOAP disabled\n");
106     exit(1);
107 }
108 #endif