Fix documentation of of chr's equivalent directive ZEB-672
[idzebra-moved-to-github.git] / test / api / testclient.c
1 /* This file is part of the Zebra server.
2    Copyright (C) Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 #include <stdlib.h>
24 #include <stdio.h>
25 #ifdef WIN32
26 #else
27 #include <unistd.h>
28 #endif
29
30 #include <yaz/xmalloc.h>
31 #include <yaz/options.h>
32 #include <yaz/zoom.h>
33
34 char *prog = "testclient";
35
36 int main(int argc, char **argv)
37 {
38     ZOOM_connection z;
39     ZOOM_resultset r;
40     int error;
41     const char *errmsg, *addinfo;
42     char *query = 0;
43     char *target = 0;
44     char *arg;
45     int delay_sec = 0;
46     int ret;
47     int retrieve_number = 0;
48     int retrieve_offset = 0;
49     char *format = 0;
50     int pos;
51     int check_count = -1;
52     int exit_code = 0;
53
54     while ((ret = options("d:n:o:f:c:", argv, argc, &arg)) != -2)
55     {
56         switch (ret)
57         {
58         case 0:
59             if (!target)
60                 target = xstrdup(arg);
61             else if (!query)
62                 query = xstrdup(arg);
63             break;
64         case 'd':
65             delay_sec = atoi(arg);
66             break;
67         case 'n':
68             retrieve_number = atoi(arg);
69             break;
70         case 'o':
71             retrieve_offset = atoi(arg);
72             break;
73         case 'f':
74             format = xstrdup(arg);
75             break;
76         case 'c':
77             check_count = atoi(arg);
78             break;
79         default:
80             printf ("%s: unknown option %s\n", prog, arg);
81             printf ("usage:\n%s [options] target query \n", prog);
82             printf (" eg.  indexdata.dk/gils computer\n");
83             exit (1);
84         }
85     }
86
87     if (!target || !query)
88     {
89         printf ("%s: missing target/query\n", prog);
90         printf ("usage:\n%s [options] target query \n", prog);
91         printf (" eg.  bagel.indexdata.dk/gils computer\n");
92         printf ("Options:\n");
93         printf (" -n num       number of records to fetch. Default: 0.\n");
94         printf (" -o off       offset for records - counting from 0.\n");
95         printf (" -f format    set record syntax. Default: none\n");
96         printf (" -d sec       delay a number of seconds before exit.\n");
97         printf (" -c count     expect count hits, fail if not.\n");
98         exit (3);
99     }
100     z = ZOOM_connection_new (target, 0);
101
102     if ((error = ZOOM_connection_error(z, &errmsg, &addinfo)))
103     {
104         printf ("Error: %s (%d) %s\n", errmsg, error, addinfo);
105         exit (2);
106     }
107
108     r = ZOOM_connection_search_pqf (z, query);
109     if ((error = ZOOM_connection_error(z, &errmsg, &addinfo)))
110     {
111         printf ("Error: %s (%d) %s\n", errmsg, error, addinfo);
112         if (check_count != -1)
113             exit_code = 10;
114     }
115     else
116     {
117         printf ("Result count: %ld\n", (long) ZOOM_resultset_size(r));
118         if (check_count != -1 && check_count != ZOOM_resultset_size(r))
119         {
120             printf("Wrong number of hits, expected %d, got %ld\n",
121                             check_count, (long) ZOOM_resultset_size(r) );
122             exit(3);
123         }
124     }
125     if (format)
126         ZOOM_resultset_option_set(r, "preferredRecordSyntax", format);
127     for (pos = 0; pos < retrieve_number; pos++)
128     {
129         int len;
130         const char *rec =
131             ZOOM_record_get(
132                 ZOOM_resultset_record(r, pos + retrieve_offset),
133                 "render", &len);
134
135         if (rec)
136             fwrite (rec, 1, len, stdout);
137     }
138     if (delay_sec > 0)
139         sleep(delay_sec);
140     ZOOM_resultset_destroy (r);
141     ZOOM_connection_destroy (z);
142     exit (exit_code);
143 }
144 /*
145  * Local variables:
146  * c-basic-offset: 4
147  * c-file-style: "Stroustrup"
148  * indent-tabs-mode: nil
149  * End:
150  * vim: shiftwidth=4 tabstop=8 expandtab
151  */
152