1 /* $Id: apitest.c,v 1.13 2003-05-20 13:52:41 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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
28 #include <yaz/pquery.h>
31 /* Small routine to display GRS-1 record variants ... */
32 /* Copied verbatim from yaz/client/client.c */
33 static void display_variant(Z_Variant *v, int level)
37 for (i = 0; i < v->num_triples; i++)
39 printf("%*sclass=%d,type=%d", level * 4, "", *v->triples[i]->zclass,
40 *v->triples[i]->type);
41 if (v->triples[i]->which == Z_Triple_internationalString)
42 printf(",value=%s\n", v->triples[i]->value.internationalString);
48 /* Small routine to display a GRS-1 record ... */
49 /* Copied verbatim from yaz/client/client.c */
50 static void display_grs1(Z_GenericRecord *r, int level)
56 for (i = 0; i < r->num_elements; i++)
60 printf("%*s", level * 4, "");
64 printf("%d,", *t->tagType);
67 if (t->tagValue->which == Z_StringOrNumeric_numeric)
68 printf("%d) ", *t->tagValue->u.numeric);
70 printf("%s) ", t->tagValue->u.string);
71 if (t->content->which == Z_ElementData_subtree)
74 display_grs1(t->content->u.subtree, level+1);
76 else if (t->content->which == Z_ElementData_string)
77 printf("%s\n", t->content->u.string);
78 else if (t->content->which == Z_ElementData_numeric)
79 printf("%d\n", *t->content->u.numeric);
80 else if (t->content->which == Z_ElementData_oid)
82 int *ip = t->content->u.oid;
85 if ((oent = oid_getentbyoid(t->content->u.oid)))
86 printf("OID: %s\n", oent->desc);
90 while (ip && *ip >= 0)
91 printf(" %d", *(ip++));
95 else if (t->content->which == Z_ElementData_noDataRequested)
96 printf("[No data requested]\n");
97 else if (t->content->which == Z_ElementData_elementEmpty)
98 printf("[Element empty]\n");
99 else if (t->content->which == Z_ElementData_elementNotThere)
100 printf("[Element not there]\n");
103 if (t->appliedVariant)
104 display_variant(t->appliedVariant, level+1);
105 if (t->metaData && t->metaData->supportedVariants)
109 printf("%*s---- variant list\n", (level+1)*4, "");
110 for (c = 0; c < t->metaData->num_supportedVariants; c++)
112 printf("%*svariant #%d\n", (level+1)*4, "", c);
113 display_variant(t->metaData->supportedVariants[c], level + 2);
119 /* Small test main to illustrate the use of the C api */
120 int main (int argc, char **argv)
122 /* odr is a handle to memory assocated with RETURNED data from
124 ODR odr_input, odr_output;
126 /* zs is our Zebra Service - decribes whole server */
129 /* zh is our Zebra Handle - describes database session */
132 /* the database we specify in our example */
133 const char *base = "Default";
138 yaz_log_init_file("apitest.log");
140 odr_input = odr_createmem (ODR_DECODE);
141 odr_output = odr_createmem (ODR_ENCODE);
143 zs = zebra_start ("zebra.cfg");
146 printf ("zebra_start failed; missing zebra.cfg?\n");
150 zh = zebra_open (zs);
153 printf ("zebras_open failed\n");
156 zebra_select_databases (zh, 1, &base);
157 /* Each argument to main will be a query */
158 for (argno = 1; argno < argc; argno++)
160 /* parse the query and generate an RPN structure */
161 Z_RPNQuery *query = p_query_rpn (odr_input, PROTO_Z3950, argv[argno]);
167 ZebraRetrievalRecord *records;
168 int noOfRecordsToFetch;
173 logf (LOG_WARN, "bad query %s\n", argv[argno]);
174 odr_reset (odr_input);
182 r = zebra_string_norm (zh, 'w',
183 argv[argno], strlen(argv[argno]),
184 out_str, sizeof(out_str));
187 printf ("norm: '%s'\n", out_str);
191 printf ("norm fail: %d\n", r);
196 /* result set name will be called 1,2, etc */
197 sprintf (setname, "%d", argno);
199 /* fire up the search */
200 zebra_search_RPN (zh, query, setname, &hits);
203 zebra_result (zh, &errCode, &errString);
208 printf ("Zebra Search Error %d %s\n",
213 printf ("Zebra Search gave %d hits\n", hits);
215 /* Deterimine number of records to fetch ... */
217 noOfRecordsToFetch = 10;
219 noOfRecordsToFetch = hits;
221 /* reset our memory - we've finished dealing with search */
222 odr_reset (odr_input);
223 odr_reset (odr_output);
225 /* prepare to fetch ... */
226 records = odr_malloc (odr_input, sizeof(*records) * noOfRecordsToFetch);
227 /* specify position of each record to fetch */
228 /* first one is numbered 1 and NOT 0 */
229 for (i = 0; i<noOfRecordsToFetch; i++)
230 records[i].position = i+1;
231 /* fetch them and request for GRS-1 records */
232 zebra_records_retrieve (zh, odr_input, setname, NULL, VAL_SUTRS,
233 noOfRecordsToFetch, records);
237 zebra_result (zh, &errCode, &errString);
242 printf ("Zebra Search Error %d %s\n",
247 /* inspect each record in result */
248 for (i = 0; i<noOfRecordsToFetch; i++)
250 printf ("Record %d\n", i+1);
251 /* error when fetching this record? */
252 if (records[i].errCode)
254 printf (" Error %d\n", records[i].errCode);
258 if (records[i].format == VAL_GRS1)
260 Z_GenericRecord *grs_record =
261 (Z_GenericRecord *) records[i].buf;
263 display_grs1(grs_record, 0);
265 else if (records[i].format == VAL_SUTRS)
268 printf ("%.*s", records[i].len, records[i].buf);
270 /* some other record we don't handle yet... */
273 printf (" Other record (ignored)\n");
277 /* reset our memory - we've finished dealing with present */
278 odr_reset (odr_input);
279 odr_reset (odr_output);
281 odr_destroy (odr_input);
282 odr_destroy (odr_output);