2 * Copyright (C) 1995-2000, Index Data
5 * $Header: /home/cvsroot/idis/index/Attic/apitest.c,v 1.11 2002-04-04 14:14:13 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 const 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 zebra_select_databases (zh, 1, &base);
140 /* Each argument to main will be a query */
141 for (argno = 1; argno < argc; argno++)
143 /* parse the query and generate an RPN structure */
144 Z_RPNQuery *query = p_query_rpn (odr_input, PROTO_Z3950, argv[argno]);
150 ZebraRetrievalRecord *records;
151 int noOfRecordsToFetch;
156 logf (LOG_WARN, "bad query %s\n", argv[argno]);
157 odr_reset (odr_input);
165 r = zebra_string_norm (zh, 'w',
166 argv[argno], strlen(argv[argno]),
167 out_str, sizeof(out_str));
170 printf ("norm: '%s'\n", out_str);
174 printf ("norm fail: %d\n", r);
179 /* result set name will be called 1,2, etc */
180 sprintf (setname, "%d", argno);
182 /* fire up the search */
183 zebra_search_rpn (zh, odr_input, odr_output, query, setname, &hits);
186 zebra_result (zh, &errCode, &errString);
191 printf ("Zebra Search Error %d %s\n",
196 printf ("Zebra Search gave %d hits\n", hits);
198 /* Deterimine number of records to fetch ... */
200 noOfRecordsToFetch = 10;
202 noOfRecordsToFetch = hits;
204 /* reset our memory - we've finished dealing with search */
205 odr_reset (odr_input);
206 odr_reset (odr_output);
208 /* prepare to fetch ... */
209 records = odr_malloc (odr_input, sizeof(*records) * noOfRecordsToFetch);
210 /* specify position of each record to fetch */
211 /* first one is numbered 1 and NOT 0 */
212 for (i = 0; i<noOfRecordsToFetch; i++)
213 records[i].position = i+1;
214 /* fetch them and request for GRS-1 records */
215 zebra_records_retrieve (zh, odr_input, setname, NULL, VAL_SUTRS,
216 noOfRecordsToFetch, records);
220 zebra_result (zh, &errCode, &errString);
225 printf ("Zebra Search Error %d %s\n",
230 /* inspect each record in result */
231 for (i = 0; i<noOfRecordsToFetch; i++)
233 printf ("Record %d\n", i+1);
234 /* error when fetching this record? */
235 if (records[i].errCode)
237 printf (" Error %d\n", records[i].errCode);
241 if (records[i].format == VAL_GRS1)
243 Z_GenericRecord *grs_record =
244 (Z_GenericRecord *) records[i].buf;
246 display_grs1(grs_record, 0);
248 else if (records[i].format == VAL_SUTRS)
251 printf ("%.*s", records[i].len, records[i].buf);
253 /* some other record we don't handle yet... */
256 printf (" Other record (ignored)\n");
260 /* reset our memory - we've finished dealing with present */
261 odr_reset (odr_input);
262 odr_reset (odr_output);
264 odr_destroy (odr_input);
265 odr_destroy (odr_output);