2 * Copyright (C) 1995-2000, Index Data
5 * $Header: /home/cvsroot/idis/index/Attic/apitest.c,v 1.10 2000-09-06 08:59:36 adam Exp $
11 #include <yaz/pquery.h>
14 /* Small routine to display GRS-1 record variants ... */
15 /* Copied verbatim from yaz/client/client.c */
16 static void display_variant(Z_Variant *v, int level)
20 for (i = 0; i < v->num_triples; i++)
22 printf("%*sclass=%d,type=%d", level * 4, "", *v->triples[i]->zclass,
23 *v->triples[i]->type);
24 if (v->triples[i]->which == Z_Triple_internationalString)
25 printf(",value=%s\n", v->triples[i]->value.internationalString);
31 /* Small routine to display a GRS-1 record ... */
32 /* Copied verbatim from yaz/client/client.c */
33 static void display_grs1(Z_GenericRecord *r, int level)
39 for (i = 0; i < r->num_elements; i++)
43 printf("%*s", level * 4, "");
47 printf("%d,", *t->tagType);
50 if (t->tagValue->which == Z_StringOrNumeric_numeric)
51 printf("%d) ", *t->tagValue->u.numeric);
53 printf("%s) ", t->tagValue->u.string);
54 if (t->content->which == Z_ElementData_subtree)
57 display_grs1(t->content->u.subtree, level+1);
59 else if (t->content->which == Z_ElementData_string)
60 printf("%s\n", t->content->u.string);
61 else if (t->content->which == Z_ElementData_numeric)
62 printf("%d\n", *t->content->u.numeric);
63 else if (t->content->which == Z_ElementData_oid)
65 int *ip = t->content->u.oid;
68 if ((oent = oid_getentbyoid(t->content->u.oid)))
69 printf("OID: %s\n", oent->desc);
73 while (ip && *ip >= 0)
74 printf(" %d", *(ip++));
78 else if (t->content->which == Z_ElementData_noDataRequested)
79 printf("[No data requested]\n");
80 else if (t->content->which == Z_ElementData_elementEmpty)
81 printf("[Element empty]\n");
82 else if (t->content->which == Z_ElementData_elementNotThere)
83 printf("[Element not there]\n");
86 if (t->appliedVariant)
87 display_variant(t->appliedVariant, level+1);
88 if (t->metaData && t->metaData->supportedVariants)
92 printf("%*s---- variant list\n", (level+1)*4, "");
93 for (c = 0; c < t->metaData->num_supportedVariants; c++)
95 printf("%*svariant #%d\n", (level+1)*4, "", c);
96 display_variant(t->metaData->supportedVariants[c], level + 2);
102 /* Small test main to illustrate the use of the C api */
103 int main (int argc, char **argv)
105 /* odr is a handle to memory assocated with RETURNED data from
107 ODR odr_input, odr_output;
109 /* zs is our Zebra Service - decribes whole server */
112 /* zh is our Zebra Handle - describes database session */
115 /* the database we specify in our example */
116 char *base = "Default";
121 yaz_log_init_file("apitest.log");
123 odr_input = odr_createmem (ODR_DECODE);
124 odr_output = odr_createmem (ODR_ENCODE);
126 zs = zebra_start ("zebra.cfg");
129 printf ("zebra_start failed; missing zebra.cfg?\n");
133 zh = zebra_open (zs);
136 printf ("zebras_open failed\n");
139 /* Each argument to main will be a query */
140 for (argno = 1; argno < argc; argno++)
142 /* parse the query and generate an RPN structure */
143 Z_RPNQuery *query = p_query_rpn (odr_input, PROTO_Z3950, argv[argno]);
147 const char *errString;
149 ZebraRetrievalRecord *records;
150 int noOfRecordsToFetch;
155 logf (LOG_WARN, "bad query %s\n", argv[argno]);
156 odr_reset (odr_input);
164 r = zebra_string_norm (zh, 'w',
165 argv[argno], strlen(argv[argno]),
166 out_str, sizeof(out_str));
169 printf ("norm: '%s'\n", out_str);
173 printf ("norm fail: %d\n", r);
178 /* result set name will be called 1,2, etc */
179 sprintf (setname, "%d", argno);
181 /* fire up the search */
182 zebra_search_rpn (zh, odr_input, odr_output, query, 1, &base, setname);
185 errCode = zebra_errCode (zh);
186 errString = zebra_errString (zh);
187 errAdd = zebra_errAdd (zh);
192 printf ("Zebra Search Error %d %s %s\n",
193 errCode, errString, errAdd ? errAdd : "");
197 printf ("Zebra Search gave %d hits\n", zebra_hits (zh));
199 /* Deterimine number of records to fetch ... */
200 if (zebra_hits(zh) > 10)
201 noOfRecordsToFetch = 10;
203 noOfRecordsToFetch = zebra_hits(zh);
205 /* reset our memory - we've finished dealing with search */
206 odr_reset (odr_input);
207 odr_reset (odr_output);
209 /* prepare to fetch ... */
210 records = odr_malloc (odr_input, sizeof(*records) * noOfRecordsToFetch);
211 /* specify position of each record to fetch */
212 /* first one is numbered 1 and NOT 0 */
213 for (i = 0; i<noOfRecordsToFetch; i++)
214 records[i].position = i+1;
215 /* fetch them and request for GRS-1 records */
216 zebra_records_retrieve (zh, odr_input, setname, NULL, VAL_SUTRS,
217 noOfRecordsToFetch, records);
220 errCode = zebra_errCode (zh);
221 errString = zebra_errString (zh);
222 errAdd = zebra_errAdd (zh);
227 printf ("Zebra Search Error %d %s %s\n",
228 errCode, errString, errAdd ? errAdd : "");
232 /* inspect each record in result */
233 for (i = 0; i<noOfRecordsToFetch; i++)
235 printf ("Record %d\n", i+1);
236 /* error when fetching this record? */
237 if (records[i].errCode)
239 printf (" Error %d\n", records[i].errCode);
243 if (records[i].format == VAL_GRS1)
245 Z_GenericRecord *grs_record =
246 (Z_GenericRecord *) records[i].buf;
248 display_grs1(grs_record, 0);
250 else if (records[i].format == VAL_SUTRS)
253 printf ("%.*s", records[i].len, records[i].buf);
255 /* some other record we don't handle yet... */
258 printf (" Other record (ignored)\n");
262 /* reset our memory - we've finished dealing with present */
263 odr_reset (odr_input);
264 odr_reset (odr_output);
266 odr_destroy (odr_input);
267 odr_destroy (odr_output);