8 /* Small routine to display GRS-1 record variants ... */
9 /* Copied verbatim from yaz/client/client.c */
10 static void display_variant(Z_Variant *v, int level)
14 for (i = 0; i < v->num_triples; i++)
16 printf("%*sclass=%d,type=%d", level * 4, "", *v->triples[i]->zclass,
17 *v->triples[i]->type);
18 if (v->triples[i]->which == Z_Triple_internationalString)
19 printf(",value=%s\n", v->triples[i]->value.internationalString);
25 /* Small routine to display a GRS-1 record ... */
26 /* Copied verbatim from yaz/client/client.c */
27 static void display_grs1(Z_GenericRecord *r, int level)
33 for (i = 0; i < r->num_elements; i++)
37 printf("%*s", level * 4, "");
41 printf("%d,", *t->tagType);
44 if (t->tagValue->which == Z_StringOrNumeric_numeric)
45 printf("%d) ", *t->tagValue->u.numeric);
47 printf("%s) ", t->tagValue->u.string);
48 if (t->content->which == Z_ElementData_subtree)
51 display_grs1(t->content->u.subtree, level+1);
53 else if (t->content->which == Z_ElementData_string)
54 printf("%s\n", t->content->u.string);
55 else if (t->content->which == Z_ElementData_numeric)
56 printf("%d\n", *t->content->u.numeric);
57 else if (t->content->which == Z_ElementData_oid)
59 int *ip = t->content->u.oid;
62 if ((oent = oid_getentbyoid(t->content->u.oid)))
63 printf("OID: %s\n", oent->desc);
67 while (ip && *ip >= 0)
68 printf(" %d", *(ip++));
72 else if (t->content->which == Z_ElementData_noDataRequested)
73 printf("[No data requested]\n");
74 else if (t->content->which == Z_ElementData_elementEmpty)
75 printf("[Element empty]\n");
76 else if (t->content->which == Z_ElementData_elementNotThere)
77 printf("[Element not there]\n");
80 if (t->appliedVariant)
81 display_variant(t->appliedVariant, level+1);
82 if (t->metaData && t->metaData->supportedVariants)
86 printf("%*s---- variant list\n", (level+1)*4, "");
87 for (c = 0; c < t->metaData->num_supportedVariants; c++)
89 printf("%*svariant #%d\n", (level+1)*4, "", c);
90 display_variant(t->metaData->supportedVariants[c], level + 2);
96 /* Small test main to illustrate the use of the C api */
97 int main (int argc, char **argv)
99 /* odr is a handle to memory assocated with RETURNED data from
103 /* zh is our Zebra Handle - describes the server as a whole */
106 /* the database we specify in our example */
107 char *base = "Default";
112 odr = odr_createmem (ODR_ENCODE);
115 zh = zebra_open ("zebra.cfg");
118 printf ("Couldn't init zebra\n");
122 /* This call controls the logging facility in YAZ/Zebra */
124 log_init(LOG_ALL, "", "out.log");
127 /* Each argument to main will be a query */
128 for (argno = 1; argno < argc; argno++)
130 /* parse the query and generate an RPN structure */
131 Z_RPNQuery *query = p_query_rpn (odr, PROTO_Z3950, argv[argno]);
135 const char *errString;
137 ZebraRetrievalRecord *records;
138 int noOfRecordsToFetch;
143 logf (LOG_WARN, "bad query %s\n", argv[argno]);
148 /* result set name will be called 1,2, etc */
149 sprintf (setname, "%d", i);
151 /* fire up the search */
152 zebra_search_rpn (zh, odr, query, 1, &base, setname);
155 errCode = zebra_errCode (zh);
156 errString = zebra_errString (zh);
157 errAdd = zebra_errAdd (zh);
162 printf ("Zebra Search Error %d %s %s\n",
163 errCode, errString, errAdd ? errAdd : "");
167 printf ("Zebra Search gave %d hits\n", zebra_hits (zh));
169 /* Deterimine number of records to fetch ... */
170 if (zebra_hits(zh) > 10)
171 noOfRecordsToFetch = 10;
173 noOfRecordsToFetch = zebra_hits(zh);
175 /* reset our memory - we've finished dealing with search */
178 /* prepare to fetch ... */
179 records = malloc (sizeof(*records) * noOfRecordsToFetch);
180 /* specify position of each record to fetch */
181 /* first one is numbered 1 and NOT 0 */
182 for (i = 0; i<noOfRecordsToFetch; i++)
183 records[i].position = i+1;
184 /* fetch them and request for GRS-1 records */
185 zebra_records_retrieve (zh, odr, setname, NULL, VAL_GRS1,
186 noOfRecordsToFetch, records);
189 errCode = zebra_errCode (zh);
190 errString = zebra_errString (zh);
191 errAdd = zebra_errAdd (zh);
196 printf ("Zebra Search Error %d %s %s\n",
197 errCode, errString, errAdd ? errAdd : "");
201 /* inspect each record in result */
202 for (i = 0; i<noOfRecordsToFetch; i++)
204 printf ("Record %d\n", i+1);
205 /* error when fetching this record? */
206 if (records[i].errCode)
208 printf (" Error %d\n", records[i].errCode);
212 if (records[i].format == VAL_GRS1)
214 Z_GenericRecord *grs_record =
215 (Z_GenericRecord *) records[i].buf;
217 display_grs1(grs_record, 0);
219 /* some other record we don't handle yet... */
222 printf (" Other record (ignored)\n");
227 odr_reset (odr); /* reset memory */