Do not use // comments
[yaz-moved-to-github.git] / zoom / zoomtst5.c
1 /*
2  * $Id: zoomtst5.c,v 1.10 2005-06-25 15:46:08 adam Exp $
3  *
4  * Asynchronous multi-target client doing search, sort and present
5  */
6
7 #include <stdio.h>
8 #include <string.h>
9
10 #include <yaz/nmem.h>
11 #include <yaz/xmalloc.h>
12 #include <yaz/zoom.h>
13
14 const char *my_callback (void *handle, const char *name)
15 {
16     if (!strcmp (name, "async"))
17         return "1";
18     return 0;
19 }
20
21 int main(int argc, char **argv)
22 {
23     int i;
24     int no = argc-3;
25     ZOOM_connection z[500]; /* allow at most 500 connections */
26     ZOOM_resultset r[500];  /* and result sets .. */
27     ZOOM_query q;
28     ZOOM_options o;
29
30     o = ZOOM_options_create ();
31     if (argc < 4)
32     {
33         fprintf (stderr, "usage:\n%s target1 .. targetN query sort\n",
34                  *argv);
35         exit (2);
36     }
37     if (no > 500)
38         no = 500;
39
40     /* function my_callback called when reading options .. */
41     ZOOM_options_set_callback (o, my_callback, 0);
42
43     /* get 20 (at most) records from beginning */
44     ZOOM_options_set (o, "count", "20");
45
46     ZOOM_options_set (o, "implementationName", "sortapp");
47     ZOOM_options_set (o, "preferredRecordSyntax", "usmarc");
48     ZOOM_options_set (o, "elementSetName", "B");
49
50     /* create query */
51     q = ZOOM_query_create ();
52     if (ZOOM_query_prefix (q, argv[argc-2]))
53     {
54         printf ("bad PQF: %s\n", argv[argc-2]);
55         exit (1);
56     }
57     if (ZOOM_query_sortby (q, argv[argc-1]))
58     {
59         printf ("bad sort spec: %s\n", argv[argc-1]);
60         exit (1);
61     }
62     /* connect - and search all */
63     for (i = 0; i<no; i++)
64     {
65         z[i] = ZOOM_connection_create (o);
66         ZOOM_connection_connect (z[i], argv[i+1], 0);
67         r[i] = ZOOM_connection_search (z[i], q);
68     }
69
70     /* network I/O */
71     while (ZOOM_event (no, z))
72         ;
73
74     /* handle errors */
75     for (i = 0; i<no; i++)
76     {
77         int error;
78         const char *errmsg, *addinfo;
79         if ((error = ZOOM_connection_error(z[i], &errmsg, &addinfo)))
80             fprintf (stderr, "%s error: %s (%d) %s\n",
81                      ZOOM_connection_option_get(z[i], "host"),
82                      errmsg, error, addinfo);
83         else
84         {
85             /* OK, no major errors. Look at the result count */
86             int pos;
87             printf ("%s: %d hits\n", ZOOM_connection_option_get(z[i], "host"),
88                     ZOOM_resultset_size(r[i]));
89             /* go through first 20 records at target */
90             for (pos = 0; pos < 20; pos++)
91             {
92                 ZOOM_record rec;
93                 const char *db, *syntax, *str;
94                 int len;
95
96                 rec = ZOOM_resultset_record (r[i], pos);
97                 /* get database for record and record itself at pos */
98
99                 db = ZOOM_record_get (rec,  "database", 0);
100                 str = ZOOM_record_get (rec, "xml", &len);
101                 syntax = ZOOM_record_get (rec, "syntax", &len);
102                 /* if rec is non-null, we got a record for display */
103                 if (str)
104                 {
105                     printf ("%d %s %s\n", pos+1, syntax, 
106                             (db ? db : "unknown"));
107                     if (rec)
108                         fwrite (str, 1, len, stdout);
109                     printf ("\n");
110                 }
111             }
112         }
113     }
114
115     /* destroy stuff and exit */
116     ZOOM_query_destroy (q);
117     for (i = 0; i<no; i++)
118     {
119         ZOOM_resultset_destroy (r[i]);
120         ZOOM_connection_destroy (z[i]);
121     }
122     ZOOM_options_destroy(o);
123     exit(0);
124 }
125 /*
126  * Local variables:
127  * c-basic-offset: 4
128  * indent-tabs-mode: nil
129  * End:
130  * vim: shiftwidth=4 tabstop=8 expandtab
131  */
132