Reformat: delete trailing whitespace
[yaz-moved-to-github.git] / zoom / zoomtst5.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2012 Index Data
3  * See the file LICENSE for details.
4  */
5
6 #include <stdio.h>
7 #include <string.h>
8
9 #include <yaz/nmem.h>
10 #include <yaz/xmalloc.h>
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 - 4;
24     ZOOM_connection z[500]; /* allow at most 500 connections */
25     ZOOM_resultset r[500];  /* and result sets .. */
26     ZOOM_query q;
27     ZOOM_options o;
28
29     o = ZOOM_options_create();
30     if (argc < 5)
31     {
32         fprintf(stderr, "usage:\n%s target1 .. targetN query strategy sort\n",
33                 *argv);
34         exit(2);
35     }
36     if (no > 500)
37         no = 500;
38
39     /* function my_callback called when reading options .. */
40     ZOOM_options_set_callback(o, my_callback, 0);
41
42     /* get 20 (at most) records from beginning */
43     ZOOM_options_set(o, "count", "20");
44
45     ZOOM_options_set(o, "implementationName", "sortapp");
46     ZOOM_options_set(o, "preferredRecordSyntax", "usmarc");
47     ZOOM_options_set(o, "elementSetName", "B");
48
49     /* create query */
50     q = ZOOM_query_create();
51     if (strncmp("cql:", argv[argc-3], 4) == 0)
52     {
53         if (ZOOM_query_cql(q, argv[argc-3] + 4))
54         {
55             printf("bad CQL: %s\n", argv[argc-3] + 4);
56             exit(1);
57         }
58     }
59     else if (ZOOM_query_prefix(q, argv[argc-3]))
60     {
61         printf("bad PQF: %s\n", argv[argc-3]);
62         exit(1);
63     }
64     if (ZOOM_query_sortby2(q, argv[argc-2], argv[argc-1]))
65     {
66         printf("bad sort spec: %s\n", argv[argc-1]);
67         exit(1);
68     }
69     /* connect - and search all */
70     for (i = 0; i < no; i++)
71     {
72         z[i] = ZOOM_connection_create(o);
73         ZOOM_connection_connect(z[i], argv[i+1], 0);
74         r[i] = ZOOM_connection_search(z[i], q);
75     }
76
77     /* network I/O */
78     while (ZOOM_event(no, z))
79         ;
80
81     /* handle errors */
82     for (i = 0; i < no; i++)
83     {
84         int error;
85         const char *errmsg, *addinfo;
86         if ((error = ZOOM_connection_error(z[i], &errmsg, &addinfo)))
87             fprintf(stderr, "%s error: %s (%d) %s\n",
88                     ZOOM_connection_option_get(z[i], "host"),
89                     errmsg, error, addinfo);
90         else
91         {
92             /* OK, no major errors. Look at the result count */
93             int pos;
94             printf("%s: %ld hits\n", ZOOM_connection_option_get(z[i], "host"),
95                    (long) ZOOM_resultset_size(r[i]));
96             /* go through first 20 records at target */
97             for (pos = 0; pos < 20; pos++)
98             {
99                 ZOOM_record rec;
100                 const char *db, *syntax, *str;
101                 int record_len, syntax_len;
102
103                 rec = ZOOM_resultset_record(r[i], pos);
104                 /* get database for record and record itself at pos */
105
106                 db = ZOOM_record_get(rec,  "database", 0);
107                 str = ZOOM_record_get(rec, "xml", &record_len);
108                 syntax = ZOOM_record_get(rec, "syntax", &syntax_len);
109                 /* if rec is non-null, we got a record for display */
110                 if (str)
111                 {
112                     printf("%d %s %s\n", pos+1, syntax,
113                            (db ? db : "unknown"));
114                     if (rec)
115                     {
116                         if (fwrite(str, 1, record_len, stdout) !=
117                             (size_t) record_len)
118                             printf("write to stdout failed\n");
119                     }
120                     printf("\n");
121                 }
122             }
123         }
124     }
125
126     /* destroy stuff and exit */
127     ZOOM_query_destroy(q);
128     for (i = 0; i < no; i++)
129     {
130         ZOOM_resultset_destroy(r[i]);
131         ZOOM_connection_destroy(z[i]);
132     }
133     ZOOM_options_destroy(o);
134     exit(0);
135 }
136 /*
137  * Local variables:
138  * c-basic-offset: 4
139  * c-file-style: "Stroustrup"
140  * indent-tabs-mode: nil
141  * End:
142  * vim: shiftwidth=4 tabstop=8 expandtab
143  */
144