1 /* $Id: apitest.c,v 1.16.2.2 2006-08-14 10:38:57 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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <yaz/pquery.h>
29 /* Small routine to display GRS-1 record variants ... */
30 /* Copied verbatim from yaz/client/client.c */
31 static void display_variant(Z_Variant *v, int level)
35 for (i = 0; i < v->num_triples; i++)
37 printf("%*sclass=%d,type=%d", level * 4, "", *v->triples[i]->zclass,
38 *v->triples[i]->type);
39 if (v->triples[i]->which == Z_Triple_internationalString)
40 printf(",value=%s\n", v->triples[i]->value.internationalString);
46 /* Small routine to display a GRS-1 record ... */
47 /* Copied verbatim from yaz/client/client.c */
48 static void display_grs1(Z_GenericRecord *r, int level)
54 for (i = 0; i < r->num_elements; i++)
58 printf("%*s", level * 4, "");
62 printf("%d,", *t->tagType);
65 if (t->tagValue->which == Z_StringOrNumeric_numeric)
66 printf("%d) ", *t->tagValue->u.numeric);
68 printf("%s) ", t->tagValue->u.string);
69 if (t->content->which == Z_ElementData_subtree)
72 display_grs1(t->content->u.subtree, level+1);
74 else if (t->content->which == Z_ElementData_string)
75 printf("%s\n", t->content->u.string);
76 else if (t->content->which == Z_ElementData_numeric)
77 printf("%d\n", *t->content->u.numeric);
78 else if (t->content->which == Z_ElementData_oid)
80 int *ip = t->content->u.oid;
83 if ((oent = oid_getentbyoid(t->content->u.oid)))
84 printf("OID: %s\n", oent->desc);
88 while (ip && *ip >= 0)
89 printf(" %d", *(ip++));
93 else if (t->content->which == Z_ElementData_noDataRequested)
94 printf("[No data requested]\n");
95 else if (t->content->which == Z_ElementData_elementEmpty)
96 printf("[Element empty]\n");
97 else if (t->content->which == Z_ElementData_elementNotThere)
98 printf("[Element not there]\n");
101 if (t->appliedVariant)
102 display_variant(t->appliedVariant, level+1);
103 if (t->metaData && t->metaData->supportedVariants)
107 printf("%*s---- variant list\n", (level+1)*4, "");
108 for (c = 0; c < t->metaData->num_supportedVariants; c++)
110 printf("%*svariant #%d\n", (level+1)*4, "", c);
111 display_variant(t->metaData->supportedVariants[c], level + 2);
117 /* Small test main to illustrate the use of the C api */
118 int main (int argc, char **argv)
120 /* odr is a handle to memory assocated with RETURNED data from
122 ODR odr_input, odr_output;
124 /* zs is our Zebra Service - decribes whole server */
127 /* zh is our Zebra Handle - describes database session */
130 /* the database we specify in our example */
131 const char *base = "Default";
136 yaz_log_init_file("apitest.log");
138 odr_input = odr_createmem (ODR_DECODE);
139 odr_output = odr_createmem (ODR_ENCODE);
141 zs = zebra_start ("zebra.cfg");
144 printf ("zebra_start failed; missing zebra.cfg?\n");
148 zh = zebra_open (zs);
151 printf ("zebras_open failed\n");
154 zebra_select_databases (zh, 1, &base);
155 /* Each argument to main will be a query */
156 for (argno = 1; argno < argc; argno++)
158 /* parse the query and generate an RPN structure */
159 Z_RPNQuery *query = p_query_rpn (odr_input, PROTO_Z3950, argv[argno]);
165 ZebraRetrievalRecord *records;
166 int noOfRecordsToFetch;
171 logf (LOG_WARN, "bad query %s\n", argv[argno]);
172 odr_reset (odr_input);
180 r = zebra_string_norm (zh, 'w',
181 argv[argno], strlen(argv[argno]),
182 out_str, sizeof(out_str));
185 printf ("norm: '%s'\n", out_str);
189 printf ("norm fail: %d\n", r);
194 /* result set name will be called 1,2, etc */
195 sprintf (setname, "%d", argno);
197 /* fire up the search */
198 zebra_search_RPN (zh, odr_input, query, setname, &hits);
201 zebra_result (zh, &errCode, &errString);
206 printf ("Zebra Search Error %d %s\n",
211 printf ("Zebra Search gave %d hits\n", hits);
213 /* Deterimine number of records to fetch ... */
215 noOfRecordsToFetch = 10;
217 noOfRecordsToFetch = hits;
219 /* reset our memory - we've finished dealing with search */
220 odr_reset (odr_input);
221 odr_reset (odr_output);
223 /* prepare to fetch ... */
224 records = odr_malloc (odr_input, sizeof(*records) * noOfRecordsToFetch);
225 /* specify position of each record to fetch */
226 /* first one is numbered 1 and NOT 0 */
227 for (i = 0; i<noOfRecordsToFetch; i++)
228 records[i].position = i+1;
229 /* fetch them and request for GRS-1 records */
230 zebra_records_retrieve (zh, odr_input, setname, NULL, VAL_SUTRS,
231 noOfRecordsToFetch, records);
235 zebra_result (zh, &errCode, &errString);
240 printf ("Zebra Search Error %d %s\n",
245 /* inspect each record in result */
246 for (i = 0; i<noOfRecordsToFetch; i++)
248 printf ("Record %d\n", i+1);
249 /* error when fetching this record? */
250 if (records[i].errCode)
252 printf (" Error %d\n", records[i].errCode);
256 if (records[i].format == VAL_GRS1)
258 Z_GenericRecord *grs_record =
259 (Z_GenericRecord *) records[i].buf;
261 display_grs1(grs_record, 0);
263 else if (records[i].format == VAL_SUTRS)
266 printf ("%.*s", records[i].len, records[i].buf);
268 /* some other record we don't handle yet... */
271 printf (" Other record (ignored)\n");
275 /* reset our memory - we've finished dealing with present */
276 odr_reset (odr_input);
277 odr_reset (odr_output);
279 odr_destroy (odr_input);
280 odr_destroy (odr_output);