1 /* $Id: apitest.c,v 1.22 2005-03-09 12:14:42 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);
152 printf ("zebras_open failed\n");
155 zebra_select_databases (zh, 1, &base);
156 /* Each argument to main will be a query */
157 for (argno = 1; argno < argc; argno++)
159 /* parse the query and generate an RPN structure */
160 Z_RPNQuery *query = p_query_rpn (odr_input, PROTO_Z3950, argv[argno]);
166 ZebraRetrievalRecord *records;
167 int noOfRecordsToFetch;
172 yaz_log (YLOG_WARN, "bad query %s\n", argv[argno]);
173 odr_reset (odr_input);
181 r = zebra_string_norm (zh, 'w',
182 argv[argno], strlen(argv[argno]),
183 out_str, sizeof(out_str));
186 printf ("norm: '%s'\n", out_str);
190 printf ("norm fail: %d\n", r);
195 /* result set name will be called 1,2, etc */
196 sprintf (setname, "%d", argno);
198 /* fire up the search */
199 zebra_search_RPN (zh, odr_input, query, setname, &hits);
202 zebra_result (zh, &errCode, &errString);
207 printf ("Zebra Search Error %d %s\n",
212 printf ("Zebra Search gave " ZINT_FORMAT " hits\n", hits);
214 /* Deterimine number of records to fetch ... */
216 noOfRecordsToFetch = 10;
218 noOfRecordsToFetch = hits;
220 /* reset our memory - we've finished dealing with search */
221 odr_reset (odr_input);
222 odr_reset (odr_output);
224 /* prepare to fetch ... */
225 records = odr_malloc (odr_input, sizeof(*records) * noOfRecordsToFetch);
226 /* specify position of each record to fetch */
227 /* first one is numbered 1 and NOT 0 */
228 for (i = 0; i<noOfRecordsToFetch; i++)
229 records[i].position = i+1;
230 /* fetch them and request for GRS-1 records */
231 zebra_records_retrieve (zh, odr_input, setname, NULL, VAL_SUTRS,
232 noOfRecordsToFetch, records);
236 zebra_result (zh, &errCode, &errString);
241 printf ("Zebra Search Error %d %s\n",
246 /* inspect each record in result */
247 for (i = 0; i<noOfRecordsToFetch; i++)
249 printf ("Record %d\n", i+1);
250 /* error when fetching this record? */
251 if (records[i].errCode)
253 printf (" Error %d\n", records[i].errCode);
257 if (records[i].format == VAL_GRS1)
259 Z_GenericRecord *grs_record =
260 (Z_GenericRecord *) records[i].buf;
262 display_grs1(grs_record, 0);
264 else if (records[i].format == VAL_SUTRS)
267 printf ("%.*s", records[i].len, records[i].buf);
269 /* some other record we don't handle yet... */
272 printf (" Other record (ignored)\n");
276 /* reset our memory - we've finished dealing with present */
277 odr_reset (odr_input);
278 odr_reset (odr_output);
280 odr_destroy (odr_input);
281 odr_destroy (odr_output);