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