Old Z39.50 codecs gone. Added ZOOM. WRBUF MARC display util.
[yaz-moved-to-github.git] / zoom / zoomtst5.c
1 /*
2  * $Id: zoomtst5.c,v 1.1 2001-10-23 21:00:20 adam Exp $
3  *
4  * Asynchronous multi-target client doing search, sort and present
5  */
6
7 #include <stdio.h>
8 #include <yaz/nmem.h>
9 #include <yaz/xmalloc.h>
10
11 #include <yaz/zoom.h>
12
13 const char *my_callback (void *handle, const char *name)
14 {
15     if (!strcmp (name, "async"))
16         return "1";
17     return 0;
18 }
19
20 int main(int argc, char **argv)
21 {
22     int i;
23     int no = argc-3;
24     Z3950_connection z[500]; /* allow at most 500 connections */
25     Z3950_resultset r[500];  /* and result sets .. */
26     Z3950_search s;
27     Z3950_options o;
28
29     nmem_init ();
30
31     o = Z3950_options_create ();
32     if (argc < 4)
33     {
34         fprintf (stderr, "usage:\n%s target1 .. targetN query sort\n",
35                  *argv);
36         exit (2);
37     }
38     if (no > 500)
39         no = 500;
40
41     /* function my_callback called when reading options .. */
42     Z3950_options_set_callback (o, my_callback, 0);
43
44     /* get 20 (at most) records from beginning */
45     Z3950_options_set (o, "count", "20");
46
47     Z3950_options_set (o, "implementationName", "sortapp");
48     Z3950_options_set (o, "preferredRecordSyntax", "usmarc");
49     Z3950_options_set (o, "elementSetName", "B");
50
51     /* create query */
52     s = Z3950_search_create ();
53     if (Z3950_search_prefix (s, argv[argc-2]))
54     {
55         printf ("bad PQF: %s\n", argv[argc-2]);
56         exit (1);
57     }
58     if (Z3950_search_sortby (s, argv[argc-1]))
59     {
60         printf ("bad sort spec: %s\n", argv[argc-1]);
61         exit (1);
62     }
63     /* connect - and search all */
64     for (i = 0; i<no; i++)
65     {
66         z[i] = Z3950_connection_create (o);
67         Z3950_connection_connect (z[i], argv[i+1], 0);
68         r[i] = Z3950_connection_search (z[i], s);
69     }
70
71     /* network I/O */
72     while (Z3950_event (no, z))
73         ;
74
75     /* handle errors */
76     for (i = 0; i<no; i++)
77     {
78         int error;
79         const char *errmsg, *addinfo;
80         if ((error = Z3950_connection_error(z[i], &errmsg, &addinfo)))
81             fprintf (stderr, "%s error: %s (%d) %s\n",
82                      Z3950_connection_host(z[i]),
83                      errmsg, error, addinfo);
84         else
85         {
86             /* OK, no major errors. Look at the result count */
87             int pos;
88             printf ("%s: %d hits\n", Z3950_connection_host(z[i]),
89                     Z3950_resultset_size(r[i]));
90             /* go through all records at target */
91             for (pos = 0; pos < 20; pos++)
92             {
93                 Z3950_record rec;
94                 const char *db, *syntax, *str;
95                 int len;
96
97                 rec = Z3950_resultset_record (r[i], pos);
98                 /* get database for record and record itself at pos */
99
100                 db = Z3950_record_get (rec,  "database", 0);
101                 str = Z3950_record_get (rec, "render", &len);
102                 syntax = Z3950_record_get (rec, "syntax", &len);
103                 /* if rec is non-null, we got a record for display */
104                 if (str)
105                 {
106                     printf ("%d %s %s\n", pos+1, syntax, 
107                             (db ? db : "unknown"));
108                     if (rec)
109                         fwrite (str, 1, len, stdout);
110                     putchar ('\n');
111                 }
112                 Z3950_record_destroy (rec);
113             }
114         }
115     }
116
117     /* destroy stuff and exit */
118     Z3950_search_destroy (s);
119     for (i = 0; i<no; i++)
120     {
121         Z3950_resultset_destroy (r[i]);
122         Z3950_connection_destroy (z[i]);
123     }
124     Z3950_options_destroy(o);
125     nmem_exit ();
126     xmalloc_trav("");
127     exit (0);
128 }