Versino 5.0.21
[yaz-moved-to-github.git] / zoom / zoomtst3.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <yaz/yaz-iconv.h>
10
11 #include <yaz/xmalloc.h>
12
13 #include <yaz/zoom.h>
14
15 int main(int argc, char **argv)
16 {
17     int i;
18     int same_target = 0;
19     int no = argc-2;
20     ZOOM_connection z[500]; /* allow at most 500 connections */
21     ZOOM_resultset r[500];  /* and result sets .. */
22     ZOOM_options o = ZOOM_options_create ();
23
24     if (argc < 3)
25     {
26         fprintf (stderr, "usage:\n%s target1 target2 ... targetN query\n"
27                          "%s number target query\n", *argv, *argv);
28         exit (1);
29     }
30     if (argc == 4 && yaz_isdigit(argv[1][0]) && !strchr(argv[1],'.'))
31     {
32         no = atoi(argv[1]);
33         same_target = 1;
34     }
35
36     if (no > 500)
37         no = 500;
38
39     /* async mode */
40     ZOOM_options_set (o, "async", "1");
41
42     /* get first 10 records of result set (using piggyback) */
43     ZOOM_options_set (o, "count", "10");
44     ZOOM_options_set (o, "step", "5");
45
46     /* preferred record syntax */
47     ZOOM_options_set (o, "preferredRecordSyntax", "usmarc");
48     ZOOM_options_set (o, "elementSetName", "F");
49
50     /* connect to all */
51     for (i = 0; i<no; i++)
52     {
53         /* create connection - pass options (they are the same for all) */
54         z[i] = ZOOM_connection_create (o);
55
56         /* connect and init */
57         if (same_target)
58             ZOOM_connection_connect (z[i], argv[2], 0);
59         else
60             ZOOM_connection_connect (z[i], argv[1+i], 0);
61     }
62     /* search all */
63     for (i = 0; i<no; i++)
64         r[i] = ZOOM_connection_search_pqf (z[i], argv[argc-1]);
65
66     /* network I/O. pass number of connections and array of connections */
67     while ((i = ZOOM_event (no, z)))
68     {
69         int peek = ZOOM_connection_peek_event(z[i-1]);
70         int event = ZOOM_connection_last_event(z[i-1]);
71         printf ("no = %d peek = %d event = %d %s\n", i-1,
72                 peek,
73                 event,
74                 ZOOM_get_event_str(event));
75     }
76
77     /* no more to be done. Inspect results */
78     for (i = 0; i<no; i++)
79     {
80         int error;
81         const char *errmsg, *addinfo;
82         const char *tname = (same_target ? argv[2] : argv[1+i]);
83         /* display errors if any */
84         if ((error = ZOOM_connection_error(z[i], &errmsg, &addinfo)))
85             fprintf (stderr, "%s error: %s (%d) %s\n", tname, errmsg,
86                      error, addinfo);
87         else
88         {
89             /* OK, no major errors. Look at the result count */
90             int pos;
91             printf ("%s: %ld hits\n", tname, (long) ZOOM_resultset_size(r[i]));
92             /* go through all records at target */
93             for (pos = 0; pos < 10; pos++)
94             {
95                 int len; /* length of buffer rec */
96                 const char *rec =
97                     ZOOM_record_get (
98                         ZOOM_resultset_record (r[i], pos), "render", &len);
99                 /* if rec is non-null, we got a record for display */
100                 if (rec)
101                     printf ("%d\n%.*s\n", pos+1, len, rec);
102             }
103         }
104     }
105     /* destroy and exit */
106     for (i = 0; i<no; i++)
107     {
108         ZOOM_resultset_destroy (r[i]);
109         ZOOM_connection_destroy (z[i]);
110     }
111     ZOOM_options_destroy(o);
112     exit (0);
113 }
114 /*
115  * Local variables:
116  * c-basic-offset: 4
117  * c-file-style: "Stroustrup"
118  * indent-tabs-mode: nil
119  * End:
120  * vim: shiftwidth=4 tabstop=8 expandtab
121  */
122