Added --with-yc option to configure. For the data1_node in data1.h:
[yaz-moved-to-github.git] / retrieval / d1_doespec.c
1 /*
2  * Copyright (c) 1995, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: d1_doespec.c,v $
7  * Revision 1.11  1997-11-06 11:36:44  adam
8  * Implemented variant match on simple elements -data1 tree and Espec-1.
9  *
10  * Revision 1.10  1997/10/02 12:10:24  quinn
11  * Attempt to fix bug in especs
12  *
13  * Revision 1.9  1997/09/17 12:10:35  adam
14  * YAZ version 1.4.
15  *
16  * Revision 1.8  1997/05/14 06:54:02  adam
17  * C++ support.
18  *
19  * Revision 1.7  1997/04/30 08:52:11  quinn
20  * Null
21  *
22  * Revision 1.6  1996/10/11  11:57:22  quinn
23  * Smallish
24  *
25  * Revision 1.5  1996/07/06  19:58:34  quinn
26  * System headerfiles gathered in yconfig
27  *
28  * Revision 1.4  1996/06/07  11:04:32  quinn
29  * Fixed tag->tagset dependency
30  *
31  * Revision 1.3  1995/11/13  09:27:33  quinn
32  * Fiddling with the variant stuff.
33  *
34  * Revision 1.2  1995/11/01  13:54:45  quinn
35  * Minor adjustments
36  *
37  * Revision 1.1  1995/11/01  11:56:07  quinn
38  * Added Retrieval (data management) functions en masse.
39  *
40  *
41  */
42
43
44 #include <assert.h>
45 #include <oid.h>
46 #include <log.h>
47 #include <proto.h>
48 #include <data1.h>
49
50 static int match_children(data1_handle dh, data1_node *n,
51                           Z_Espec1 *e, int i, Z_ETagUnit **t,
52     int num);
53
54 static int match_children_wildpath(data1_handle dh, data1_node *n,
55                                    Z_Espec1 *e, int i,
56                                    Z_ETagUnit **t, int num)
57 {
58     return 0;
59 }
60
61 /*
62  * Locate a specific triple within a variant.
63  * set is the set to look for, universal set is the set that applies to a
64  * triple with an unknown set.
65  */
66 static Z_Triple *find_triple(Z_Variant *var, oid_value universalset,
67     oid_value set, int zclass, int type)
68 {
69     int i;
70     oident *defaultsetent = oid_getentbyoid(var->globalVariantSetId);
71     oid_value defaultset = defaultsetent ? defaultsetent->value :
72         universalset;
73
74     for (i = 0; i < var->num_triples; i++)
75     {
76         oident *cursetent =
77             oid_getentbyoid(var->triples[i]->variantSetId);
78         oid_value curset = cursetent ? cursetent->value : defaultset;
79
80         if (set == curset &&
81             *var->triples[i]->zclass == zclass &&
82             *var->triples[i]->type == type)
83             return var->triples[i];
84     }
85     return 0;
86 }
87
88 static void mark_subtree(data1_node *n, int make_variantlist, int no_data,
89     int get_bytes, Z_Variant *vreq)
90 {
91     data1_node *c;
92
93 #if 1
94     if (n->which == DATA1N_tag)
95 #else
96     if (n->which == DATA1N_tag && (!n->child || n->child->which != DATA1N_tag))
97     /*
98      * This seems to cause multi-level elements to fall out when only a
99      * top-level elementRequest has been given... Problem is, I can't figure
100      * out what it was supposed to ACHIEVE.... delete when code has been
101      * verified.
102      */
103 #endif
104     {
105         n->u.tag.node_selected = 1;
106         n->u.tag.make_variantlist = make_variantlist;
107         n->u.tag.no_data_requested = no_data;
108         n->u.tag.get_bytes = get_bytes;
109     }
110
111     for (c = n->child; c; c = c->next)
112     {
113         if (c->which == DATA1N_tag && (!n->child ||
114             n->child->which != DATA1N_tag))
115         {
116             c->u.tag.node_selected = 1;
117             c->u.tag.make_variantlist = make_variantlist;
118             c->u.tag.no_data_requested = no_data;
119             c->u.tag.get_bytes = get_bytes;
120         }
121         mark_subtree(c, make_variantlist, no_data, get_bytes, vreq);
122     }
123 }
124
125
126 static void match_triple (data1_handle dh, Z_Variant *vreq,
127                           oid_value defsetval,
128                           oid_value var1, data1_node *n)
129 {
130     data1_node **c;
131
132     n = n->child;
133     if (n->which != DATA1N_variant)
134         return;
135     c = &n->child;
136     while (*c)
137     {
138         int remove_flag = 0;
139         Z_Triple *r;
140         
141         assert ((*c)->which == DATA1N_variant);
142         
143         if ((*c)->u.variant.type->zclass->zclass == 4 &&
144             (*c)->u.variant.type->type == 1)
145         {
146             if ((r = find_triple(vreq, defsetval, var1, 4, 1)) &&
147                 (r->which == Z_Triple_internationalString))
148             {
149                 const char *string_value =
150                     r->value.internationalString;
151                 if (strcmp ((*c)->u.variant.value, string_value))
152                     remove_flag = 1;
153             }
154         }
155         if (remove_flag)
156         {
157             data1_free_tree (dh, *c);
158             *c = (*c)->next;
159         }
160         else
161         {
162             match_triple (dh, vreq, defsetval, var1, *c);
163             c = &(*c)->next;
164         }
165     }
166 }
167                                 
168 static int match_children_here (data1_handle dh, data1_node *n,
169                                 Z_Espec1 *e, int i,
170                                 Z_ETagUnit **t, int num)
171 {
172     int counter = 0, hits = 0;
173     data1_node *c;
174     Z_ETagUnit *tp = *t;
175     Z_Occurrences *occur;
176
177     for (c = n->child; c ; c = c->next)
178     {
179         data1_tag *tag = 0;
180         if (c->which != DATA1N_tag)
181             return 0;
182
183         if (tp->which == Z_ETagUnit_specificTag)
184         {
185             Z_SpecificTag *want = tp->u.specificTag;
186             occur = want->occurrences;
187             if (c->u.tag.element)
188                 tag = c->u.tag.element->tag;
189             if (*want->tagType != ((tag && tag->tagset) ? tag->tagset->type :
190                 3))
191                 continue;
192             if (want->tagValue->which == Z_StringOrNumeric_numeric)
193             {
194                 if (!tag || tag->which != DATA1T_numeric)
195                     continue;
196                 if (*want->tagValue->u.numeric != tag->value.numeric)
197                     continue;
198             }
199             else
200             {
201                 assert(want->tagValue->which == Z_StringOrNumeric_string);
202                 if (tag && tag->which != DATA1T_string)
203                     continue;
204                 if (data1_matchstr(want->tagValue->u.string,
205                     tag ? tag->value.string : c->u.tag.tag))
206                     continue;
207             }
208         }
209         else
210             occur = tp->u.wildThing;
211
212         /*
213          * Ok, so we have a matching tag. Are we within occurrences-range?
214          */
215         counter++;
216         if (occur && occur->which == Z_Occurrences_last)
217         {
218             logf(LOG_WARN, "Can't do occurrences=last (yet)");
219             return 0;
220         }
221         if (!occur || occur->which == Z_Occurrences_all ||
222             (occur->which == Z_Occurrences_values && counter >=
223             *occur->u.values->start))
224         {
225             if (match_children(dh, c, e, i, t + 1, num - 1))
226             {
227                 c->u.tag.node_selected = 1;
228                 /*
229                  * Consider the variant specification if this is a complete
230                  * match.
231                  */
232                 if (num == 1)
233                 {
234                     int show_variantlist = 0;
235                     int no_data = 0;
236                     int get_bytes = -1;
237
238                     Z_Variant *vreq =
239                         e->elements[i]->u.simpleElement->variantRequest;
240                     oident *defset = oid_getentbyoid(e->defaultVariantSetId);
241                     oid_value defsetval = defset ? defset->value : VAL_NONE;
242                     oid_value var1 = oid_getvalbyname("Variant-1");
243
244                     if (!vreq)
245                         vreq = e->defaultVariantRequest;
246
247                     if (vreq)
248                     {
249                         Z_Triple *r;
250
251                         /*
252                          * 6,5: meta-data requested, variant list.
253                          */
254                         if (find_triple(vreq, defsetval, var1, 6, 5))
255                             show_variantlist = 1;
256                         /*
257                          * 9,1: Miscellaneous, no data requested.
258                          */
259                         if (find_triple(vreq, defsetval, var1, 9, 1))
260                             no_data = 1;
261
262                         /* howmuch */
263                         if ((r = find_triple(vreq, defsetval, var1, 5, 5)))
264                             if (r->which == Z_Triple_integer)
265                                 get_bytes = *r->value.integer;
266
267                         if (!show_variantlist)
268                             match_triple (dh, vreq, defsetval, var1, c);
269                     }
270                     mark_subtree(c, show_variantlist, no_data, get_bytes, vreq);
271                 }
272                 hits++;
273                 /*
274                  * have we looked at enough children?
275                  */
276                 if (!occur || (occur->which == Z_Occurrences_values &&
277                     (!occur->u.values->howMany ||
278                     counter - *occur->u.values->start >=
279                     *occur->u.values->howMany - 1)))
280                     return hits;
281             }
282         }
283     }
284     return hits;
285 }
286
287 static int match_children(data1_handle dh, data1_node *n, Z_Espec1 *e,
288                           int i, Z_ETagUnit **t, int num)
289 {
290     int res;
291
292     if (!num)
293         return 1;
294     switch (t[0]->which)
295     {
296         case Z_ETagUnit_wildThing:
297         case Z_ETagUnit_specificTag:
298             res = match_children_here(dh, n, e, i, t, num); break;
299         case Z_ETagUnit_wildPath:
300             res = match_children_wildpath(dh, n, e, i, t, num); break;
301         default:
302             abort();
303     }
304     return res;
305 }
306
307 int data1_doespec1 (data1_handle dh, data1_node *n, Z_Espec1 *e)
308 {
309     int i;
310
311     for (i = 0; i < e->num_elements; i++)
312     {
313         if (e->elements[i]->which != Z_ERequest_simpleElement)
314             return 100;
315         match_children(dh, n, e, i,
316                        e->elements[i]->u.simpleElement->path->tags,
317                        e->elements[i]->u.simpleElement->path->num_tags);
318     }
319     return 0;
320 }