Added zebra_string_norm.
[idzebra-moved-to-github.git] / index / apitest.c
1
2 #include <stdio.h>
3
4 #include <yaz/log.h>
5 #include <yaz/pquery.h>
6 #include "zebraapi.h"
7
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)
11 {
12     int i;
13
14     for (i = 0; i < v->num_triples; i++)
15     {
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);
20         else
21             printf("\n");
22     }
23 }
24  
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)
28 {
29     int i;
30
31     if (!r)
32         return;
33     for (i = 0; i < r->num_elements; i++)
34     {
35         Z_TaggedElement *t;
36
37         printf("%*s", level * 4, "");
38         t = r->elements[i];
39         printf("(");
40         if (t->tagType)
41             printf("%d,", *t->tagType);
42         else
43             printf("?,");
44         if (t->tagValue->which == Z_StringOrNumeric_numeric)
45             printf("%d) ", *t->tagValue->u.numeric);
46         else
47             printf("%s) ", t->tagValue->u.string);
48         if (t->content->which == Z_ElementData_subtree)
49         {
50             printf("\n");
51             display_grs1(t->content->u.subtree, level+1);
52         }
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)
58         {
59             int *ip = t->content->u.oid;
60             oident *oent;
61
62             if ((oent = oid_getentbyoid(t->content->u.oid)))
63                 printf("OID: %s\n", oent->desc);
64             else
65             {
66                 printf("{");
67                 while (ip && *ip >= 0)
68                     printf(" %d", *(ip++));
69                 printf(" }\n");
70             }
71         }
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");
78         else
79             printf("??????\n");
80         if (t->appliedVariant)
81             display_variant(t->appliedVariant, level+1);
82         if (t->metaData && t->metaData->supportedVariants)
83         {
84             int c;
85
86             printf("%*s---- variant list\n", (level+1)*4, "");
87             for (c = 0; c < t->metaData->num_supportedVariants; c++)
88             {
89                 printf("%*svariant #%d\n", (level+1)*4, "", c);
90                 display_variant(t->metaData->supportedVariants[c], level + 2);
91             }
92         }
93     }
94 }
95
96 /* Small test main to illustrate the use of the C api */
97 int main (int argc, char **argv)
98 {
99     /* odr is a handle to memory assocated with RETURNED data from
100        various functions */
101     ODR odr_input, odr_output;
102     
103     /* zh is our Zebra Handle - describes the server as a whole */
104     ZebraHandle zh;
105     
106     /* the database we specify in our example */
107     char *base = "Default";
108     int argno;
109
110     nmem_init ();
111
112     log_init_file("apitest.log");
113
114     odr_input = odr_createmem (ODR_DECODE);    
115     odr_output = odr_createmem (ODR_ENCODE);    
116     
117    /* open Zebra */
118     zh = zebra_open ("zebra.cfg");
119     if (!zh)
120     {
121         printf ("Couldn't init zebra\n");
122         exit (1);
123     }
124
125     /* This call controls the logging facility in YAZ/Zebra */
126 #if 0
127     log_init(LOG_ALL, "", "out.log");
128 #endif
129
130     /* Each argument to main will be a query */
131     for (argno = 1; argno < argc; argno++)
132     {
133         /* parse the query and generate an RPN structure */
134         Z_RPNQuery *query = p_query_rpn (odr_input, PROTO_Z3950, argv[argno]);
135         char setname[64];
136         int errCode;
137         int i;
138         const char *errString;
139         char *errAdd;
140         ZebraRetrievalRecord *records;
141         int noOfRecordsToFetch;
142
143         /* bad query? */
144         if (!query)
145         {
146             logf (LOG_WARN, "bad query %s\n", argv[argno]);
147             odr_reset (odr_input);
148             continue;
149         }
150         else
151         {
152             char out_str[100];
153             int r;
154 #if 1
155             r = zebra_string_norm (zh, 'w',
156                                    argv[argno], strlen(argv[argno]),
157                                    out_str, sizeof(out_str));
158             if (r >= 0)
159             {
160                 printf ("norm: '%s'\n", out_str);
161             }
162             else
163             {
164                 printf ("norm fail: %d\n", r);
165             }
166 #endif
167
168         }
169         /* result set name will be called 1,2, etc */
170         sprintf (setname, "%d", argno);
171
172         /* fire up the search */
173         zebra_search_rpn (zh, odr_input, odr_output, query, 1, &base, setname);
174         
175         /* status ... */
176         errCode = zebra_errCode (zh);
177         errString = zebra_errString (zh);
178         errAdd = zebra_errAdd (zh);
179         
180         /* error? */
181         if (errCode)
182         {
183             printf ("Zebra Search Error %d %s %s\n",
184                     errCode, errString, errAdd ? errAdd : "");
185             continue;
186         }
187         /* ok ... */
188         printf ("Zebra Search gave %d hits\n", zebra_hits (zh));
189         
190         /* Deterimine number of records to fetch ... */
191         if (zebra_hits(zh) > 10)
192             noOfRecordsToFetch = 10;
193         else
194             noOfRecordsToFetch = zebra_hits(zh);
195
196         /* reset our memory - we've finished dealing with search */
197         odr_reset (odr_input);
198         odr_reset (odr_output);
199
200         /* prepare to fetch ... */
201         records = odr_malloc (odr_input, sizeof(*records) * noOfRecordsToFetch);
202         /* specify position of each record to fetch */
203         /* first one is numbered 1 and NOT 0 */
204         for (i = 0; i<noOfRecordsToFetch; i++)
205             records[i].position = i+1;
206         /* fetch them and request for GRS-1 records */
207         zebra_records_retrieve (zh, odr_input, setname, NULL, VAL_SUTRS,
208                                 noOfRecordsToFetch, records);
209
210         /* status ... */
211         errCode = zebra_errCode (zh);
212         errString = zebra_errString (zh);
213         errAdd = zebra_errAdd (zh);
214         
215         /* error ? */
216         if (errCode)
217         {
218             printf ("Zebra Search Error %d %s %s\n",
219                     errCode, errString, errAdd ? errAdd : "");
220         }
221         else
222         {
223             /* inspect each record in result */
224             for (i = 0; i<noOfRecordsToFetch; i++)
225             {
226                 printf ("Record %d\n", i+1);
227                 /* error when fetching this record? */
228                 if (records[i].errCode)
229                 {
230                     printf ("  Error %d\n", records[i].errCode);
231                     continue;
232                 }
233                 /* GRS-1 record ? */
234                 if (records[i].format == VAL_GRS1)
235                 {
236                     Z_GenericRecord *grs_record =
237                         (Z_GenericRecord *) records[i].buf;
238                     printf ("  GRS-1\n");
239                     display_grs1(grs_record, 0);
240                 }
241                 else if (records[i].format == VAL_SUTRS)
242                 {
243                     printf ("  SUTRS\n");
244                     printf ("%.*s", records[i].len, records[i].buf);
245                 }
246                 /* some other record we don't handle yet... */
247                 else
248                 {
249                     printf ("  Other record (ignored)\n");
250                 }
251             }
252         }
253         /* reset our memory - we've finished dealing with present */
254         odr_reset (odr_input); 
255         odr_reset (odr_output);
256     }
257     odr_destroy (odr_input);
258     odr_destroy (odr_output);
259     zebra_close (zh);
260     return 0;
261 }