1 /* $Id: apitest.c,v 1.25 2006-05-10 08:13:20 adam Exp $
2 Copyright (C) 1995-2005
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
27 #include <yaz/pquery.h>
28 #include <idzebra/api.h>
30 /* Small routine to display GRS-1 record variants ... */
31 /* Copied verbatim from yaz/client/client.c */
32 static void display_variant(Z_Variant *v, int level)
36 for (i = 0; i < v->num_triples; i++)
38 printf("%*sclass=%d,type=%d", level * 4, "", *v->triples[i]->zclass,
39 *v->triples[i]->type);
40 if (v->triples[i]->which == Z_Triple_internationalString)
41 printf(",value=%s\n", v->triples[i]->value.internationalString);
47 /* Small routine to display a GRS-1 record ... */
48 /* Copied verbatim from yaz/client/client.c */
49 static void display_grs1(Z_GenericRecord *r, int level)
55 for (i = 0; i < r->num_elements; i++)
59 printf("%*s", level * 4, "");
63 printf("%d,", *t->tagType);
66 if (t->tagValue->which == Z_StringOrNumeric_numeric)
67 printf("%d) ", *t->tagValue->u.numeric);
69 printf("%s) ", t->tagValue->u.string);
70 if (t->content->which == Z_ElementData_subtree)
73 display_grs1(t->content->u.subtree, level+1);
75 else if (t->content->which == Z_ElementData_string)
76 printf("%s\n", t->content->u.string);
77 else if (t->content->which == Z_ElementData_numeric)
78 printf("%d\n", *t->content->u.numeric);
79 else if (t->content->which == Z_ElementData_oid)
81 int *ip = t->content->u.oid;
84 if ((oent = oid_getentbyoid(t->content->u.oid)))
85 printf("OID: %s\n", oent->desc);
89 while (ip && *ip >= 0)
90 printf(" %d", *(ip++));
94 else if (t->content->which == Z_ElementData_noDataRequested)
95 printf("[No data requested]\n");
96 else if (t->content->which == Z_ElementData_elementEmpty)
97 printf("[Element empty]\n");
98 else if (t->content->which == Z_ElementData_elementNotThere)
99 printf("[Element not there]\n");
102 if (t->appliedVariant)
103 display_variant(t->appliedVariant, level+1);
104 if (t->metaData && t->metaData->supportedVariants)
108 printf("%*s---- variant list\n", (level+1)*4, "");
109 for (c = 0; c < t->metaData->num_supportedVariants; c++)
111 printf("%*svariant #%d\n", (level+1)*4, "", c);
112 display_variant(t->metaData->supportedVariants[c], level + 2);
118 /* Small test main to illustrate the use of the C api */
119 int main (int argc, char **argv)
121 /* odr is a handle to memory assocated with RETURNED data from
123 ODR odr_input, odr_output;
125 /* zs is our Zebra Service - decribes whole server */
128 /* zh is our Zebra Handle - describes database session */
131 /* the database we specify in our example */
132 const char *base = "Default";
137 yaz_log_init_file("apitest.log");
139 odr_input = odr_createmem (ODR_DECODE);
140 odr_output = odr_createmem (ODR_ENCODE);
142 zs = zebra_start ("zebra.cfg");
145 printf ("zebra_start failed; missing zebra.cfg?\n");
149 zh = zebra_open (zs, 0);
152 printf ("zebras_open failed\n");
155 if (zebra_select_databases (zh, 1, &base) != ZEBRA_OK)
157 printf ("zebra_select_databases failed\n");
160 /* Each argument to main will be a query */
161 for (argno = 1; argno < argc; argno++)
163 /* parse the query and generate an RPN structure */
164 Z_RPNQuery *query = p_query_rpn (odr_input, PROTO_Z3950, argv[argno]);
170 ZebraRetrievalRecord *records;
171 int noOfRecordsToFetch;
176 yaz_log (YLOG_WARN, "bad query %s\n", argv[argno]);
177 odr_reset (odr_input);
185 r = zebra_string_norm (zh, 'w',
186 argv[argno], strlen(argv[argno]),
187 out_str, sizeof(out_str));
190 printf ("norm: '%s'\n", out_str);
194 printf ("norm fail: %d\n", r);
199 /* result set name will be called 1,2, etc */
200 sprintf (setname, "%d", argno);
202 /* fire up the search */
203 zebra_search_RPN (zh, odr_input, query, setname, &hits);
206 zebra_result (zh, &errCode, &errString);
211 printf ("Zebra Search Error %d %s\n",
216 printf ("Zebra Search gave " ZINT_FORMAT " hits\n", hits);
218 /* Deterimine number of records to fetch ... */
220 noOfRecordsToFetch = 10;
222 noOfRecordsToFetch = hits;
224 /* reset our memory - we've finished dealing with search */
225 odr_reset (odr_input);
226 odr_reset (odr_output);
228 /* prepare to fetch ... */
229 records = odr_malloc (odr_input, sizeof(*records) * noOfRecordsToFetch);
230 /* specify position of each record to fetch */
231 /* first one is numbered 1 and NOT 0 */
232 for (i = 0; i<noOfRecordsToFetch; i++)
233 records[i].position = i+1;
234 /* fetch them and request for GRS-1 records */
235 zebra_records_retrieve (zh, odr_input, setname, NULL, VAL_SUTRS,
236 noOfRecordsToFetch, records);
240 zebra_result (zh, &errCode, &errString);
245 printf ("Zebra Search Error %d %s\n",
250 /* inspect each record in result */
251 for (i = 0; i<noOfRecordsToFetch; i++)
253 printf ("Record %d\n", i+1);
254 /* error when fetching this record? */
255 if (records[i].errCode)
257 printf (" Error %d\n", records[i].errCode);
261 if (records[i].format == VAL_GRS1)
263 Z_GenericRecord *grs_record =
264 (Z_GenericRecord *) records[i].buf;
266 display_grs1(grs_record, 0);
268 else if (records[i].format == VAL_SUTRS)
271 printf ("%.*s", records[i].len, records[i].buf);
273 /* some other record we don't handle yet... */
276 printf (" Other record (ignored)\n");
280 /* reset our memory - we've finished dealing with present */
281 odr_reset (odr_input);
282 odr_reset (odr_output);
284 odr_destroy (odr_input);
285 odr_destroy (odr_output);
293 * indent-tabs-mode: nil
295 * vim: shiftwidth=4 tabstop=8 expandtab