Code updates which makes things compile as C++. Mostly type casts were
[yaz-moved-to-github.git] / util / srwtst.c
1 /*
2  * Copyright (C) 1995-2007, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: srwtst.c,v 1.8 2007-05-06 20:12:20 adam Exp $
6  */
7
8 #include <stdlib.h>
9 #include <yaz/srw.h>
10
11 #if YAZ_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     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     ret = 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                 switch(req->query_type)
57                 {
58                 case Z_SRW_query_type_cql:
59                     fprintf(stderr, "CQL: %s\n", req->query.cql);
60                     break;
61                 case Z_SRW_query_type_xcql:
62                     fprintf(stderr, "XCQL\n");
63                     break;
64                 case Z_SRW_query_type_pqf:
65                     fprintf(stderr, "PQF: %s\n", req->query.pqf);
66                     break;
67                 }
68             }
69             else if (sr->which == Z_SRW_searchRetrieve_response)
70             {
71                 Z_SRW_searchRetrieveResponse *res = sr->u.response;
72                 if (res->records && res->num_records)
73                 {
74                     int i;
75                     for (i = 0; i<res->num_records; i++)
76                     {
77                         fprintf (stderr, "%d\n", i);
78                         if (res->records[i].recordData_buf)
79                             fwrite(res->records[i].recordData_buf, 1,
80                                    res->records[i].recordData_len, stderr);
81                     }
82                 }
83             }
84
85         }
86     }
87     ret = z_soap_codec(encode, &soap_package,
88                        &content_buf, &content_len, h);
89     if (content_buf && content_len)
90         fwrite (content_buf, content_len, 1, stdout);
91     else
92     {
93         fprintf(stderr, "No output!\n");
94         exit(1);
95     }
96     odr_destroy(decode);
97     odr_destroy(encode);
98     exit(0);
99 }
100 #else
101 int main(int argc, char **argv)
102 {
103     fprintf(stderr, "SOAP disabled\n");
104     exit(1);
105 }
106 #endif
107 /*
108  * Local variables:
109  * c-basic-offset: 4
110  * indent-tabs-mode: nil
111  * End:
112  * vim: shiftwidth=4 tabstop=8 expandtab
113  */
114