File: testclient.c

package info (click to toggle)
idzebra 2.2.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,572 kB
  • sloc: ansic: 54,389; xml: 27,058; sh: 5,892; makefile: 1,102; perl: 210; tcl: 64
file content (152 lines) | stat: -rw-r--r-- 4,291 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* This file is part of the Zebra server.
   Copyright (C) Index Data

Zebra is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.

Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/

#if HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#ifdef WIN32
#else
#include <unistd.h>
#endif

#include <yaz/xmalloc.h>
#include <yaz/options.h>
#include <yaz/zoom.h>

char *prog = "testclient";

int main(int argc, char **argv)
{
    ZOOM_connection z;
    ZOOM_resultset r;
    int error;
    const char *errmsg, *addinfo;
    char *query = 0;
    char *target = 0;
    char *arg;
    int delay_sec = 0;
    int ret;
    int retrieve_number = 0;
    int retrieve_offset = 0;
    char *format = 0;
    int pos;
    int check_count = -1;
    int exit_code = 0;

    while ((ret = options("d:n:o:f:c:", argv, argc, &arg)) != -2)
    {
        switch (ret)
        {
        case 0:
            if (!target)
                target = xstrdup(arg);
            else if (!query)
                query = xstrdup(arg);
            break;
        case 'd':
            delay_sec = atoi(arg);
            break;
        case 'n':
            retrieve_number = atoi(arg);
            break;
        case 'o':
            retrieve_offset = atoi(arg);
            break;
        case 'f':
            format = xstrdup(arg);
            break;
        case 'c':
            check_count = atoi(arg);
            break;
        default:
            printf ("%s: unknown option %s\n", prog, arg);
            printf ("usage:\n%s [options] target query \n", prog);
            printf (" eg.  indexdata.dk/gils computer\n");
            exit (1);
        }
    }

    if (!target || !query)
    {
        printf ("%s: missing target/query\n", prog);
        printf ("usage:\n%s [options] target query \n", prog);
        printf (" eg.  bagel.indexdata.dk/gils computer\n");
        printf ("Options:\n");
        printf (" -n num       number of records to fetch. Default: 0.\n");
        printf (" -o off       offset for records - counting from 0.\n");
        printf (" -f format    set record syntax. Default: none\n");
        printf (" -d sec       delay a number of seconds before exit.\n");
        printf (" -c count     expect count hits, fail if not.\n");
        exit (3);
    }
    z = ZOOM_connection_new (target, 0);

    if ((error = ZOOM_connection_error(z, &errmsg, &addinfo)))
    {
        printf ("Error: %s (%d) %s\n", errmsg, error, addinfo);
        exit (2);
    }

    r = ZOOM_connection_search_pqf (z, query);
    if ((error = ZOOM_connection_error(z, &errmsg, &addinfo)))
    {
        printf ("Error: %s (%d) %s\n", errmsg, error, addinfo);
        if (check_count != -1)
            exit_code = 10;
    }
    else
    {
        printf ("Result count: %ld\n", (long) ZOOM_resultset_size(r));
        if (check_count != -1 && check_count != ZOOM_resultset_size(r))
        {
            printf("Wrong number of hits, expected %d, got %ld\n",
                            check_count, (long) ZOOM_resultset_size(r) );
            exit(3);
        }
    }
    if (format)
        ZOOM_resultset_option_set(r, "preferredRecordSyntax", format);
    for (pos = 0; pos < retrieve_number; pos++)
    {
        int len;
        const char *rec =
            ZOOM_record_get(
                ZOOM_resultset_record(r, pos + retrieve_offset),
                "render", &len);

        if (rec)
            fwrite (rec, 1, len, stdout);
    }
    if (delay_sec > 0)
        sleep(delay_sec);
    ZOOM_resultset_destroy (r);
    ZOOM_connection_destroy (z);
    exit (exit_code);
}
/*
 * Local variables:
 * c-basic-offset: 4
 * c-file-style: "Stroustrup"
 * indent-tabs-mode: nil
 * End:
 * vim: shiftwidth=4 tabstop=8 expandtab
 */