Update source headers for 2008. Omit CVS ID keyword subst.
[yaz-moved-to-github.git] / util / srwtst.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2008 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                             fwrite(res->records[i].recordData_buf, 1,
78                                    res->records[i].recordData_len, stderr);
79                     }
80                 }
81             }
82
83         }
84     }
85     ret = z_soap_codec(encode, &soap_package,
86                        &content_buf, &content_len, h);
87     if (content_buf && content_len)
88         fwrite (content_buf, content_len, 1, stdout);
89     else
90     {
91         fprintf(stderr, "No output!\n");
92         exit(1);
93     }
94     odr_destroy(decode);
95     odr_destroy(encode);
96     exit(0);
97 }
98 #else
99 int main(int argc, char **argv)
100 {
101     fprintf(stderr, "SOAP disabled\n");
102     exit(1);
103 }
104 #endif
105 /*
106  * Local variables:
107  * c-basic-offset: 4
108  * indent-tabs-mode: nil
109  * End:
110  * vim: shiftwidth=4 tabstop=8 expandtab
111  */
112