Old Z39.50 codecs gone. Added ZOOM. WRBUF MARC display util.
[yaz-moved-to-github.git] / zoom / zoomtst3.c
1 /*
2  * $Id: zoomtst3.c,v 1.1 2001-10-23 21:00:20 adam Exp $
3  *
4  * Asynchronous multi-target client doing search and piggyback retrieval
5  */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 #include <yaz/xmalloc.h>
11
12 #include <yaz/zoom.h>
13
14 int main(int argc, char **argv)
15 {
16     int i;
17     int no = argc-2;
18     Z3950_connection z[500]; /* allow at most 500 connections */
19     Z3950_resultset r[500];  /* and result sets .. */
20     Z3950_options o = Z3950_options_create ();
21
22     if (argc < 3)
23     {
24         fprintf (stderr, "usage:\n%s target1 target2 ... targetN query\n",
25                  *argv);
26         exit (1);
27     }
28     if (no > 500)
29         no = 500;
30
31     /* async mode */
32     Z3950_options_set (o, "async", "1");
33
34     /* get first 10 records of result set (using piggyback) */
35     Z3950_options_set (o, "count", "10");
36
37     /* preferred record syntax */
38     Z3950_options_set (o, "preferredRecordSyntax", "usmarc");
39     Z3950_options_set (o, "elementSetName", "F");
40
41     /* connect to all */
42     for (i = 0; i<no; i++)
43     {
44         /* create connection - pass options (they are the same for all) */
45         z[i] = Z3950_connection_create (o);
46
47         /* connect and init */
48         Z3950_connection_connect (z[i], argv[1+i], 0);
49     }
50     /* search all */
51     for (i = 0; i<no; i++)
52         r[i] = Z3950_connection_search_pqf (z[i], argv[argc-1]);
53
54     /* network I/O. pass number of connections and array of connections */
55     while (Z3950_event (no, z))
56         ;
57     
58     /* no more to be done. Inspect results */
59     for (i = 0; i<no; i++)
60     {
61         int error;
62         const char *errmsg, *addinfo;
63         /* display errors if any */
64         if ((error = Z3950_connection_error(z[i], &errmsg, &addinfo)))
65             fprintf (stderr, "%s error: %s (%d) %s\n", argv[i+1], errmsg,
66                      error, addinfo);
67         else
68         {
69             /* OK, no major errors. Look at the result count */
70             int pos;
71             printf ("%s: %d hits\n", argv[i+1], Z3950_resultset_size(r[i]));
72             /* go through all records at target */
73             for (pos = 0; pos < 10; pos++)
74             {
75                 int len; /* length of buffer rec */
76                 const char *rec =
77                     Z3950_resultset_get (r[i], pos, "render", &len);
78                 /* if rec is non-null, we got a record for display */
79                 if (rec)
80                 {
81                     printf ("%d\n", pos+1);
82                     if (rec)
83                         fwrite (rec, 1, len, stdout);
84                     putchar ('\n');
85                 }
86             }
87         }
88     }
89     /* destroy and exit */
90     for (i = 0; i<no; i++)
91     {
92         Z3950_resultset_destroy (r[i]);
93         Z3950_connection_destroy (z[i]);
94     }
95     Z3950_options_destroy(o);
96     exit (0);
97 }