Moved matchstr to global util
[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.3  1995-11-13 09:27:33  quinn
8  * Fiddling with the variant stuff.
9  *
10  * Revision 1.2  1995/11/01  13:54:45  quinn
11  * Minor adjustments
12  *
13  * Revision 1.1  1995/11/01  11:56:07  quinn
14  * Added Retrieval (data management) functions en masse.
15  *
16  *
17  */
18
19
20 #include <assert.h>
21 #include <oid.h>
22 #include <log.h>
23 #include <proto.h>
24 #include <data1.h>
25
26 static int match_children(data1_node *n, Z_Espec1 *e, int i, Z_ETagUnit **t,
27     int num);
28
29 static int match_children_wildpath(data1_node *n, Z_Espec1 *e, int i,
30     Z_ETagUnit **t, int num)
31 {return 0;}
32
33 /*
34  * Locate a specific triple within a variant.
35  * set is the set to look for, universal set is the set that applies to a
36  * triple with an unknown set.
37  */
38 static Z_Triple *find_triple(Z_Variant *var, oid_value universalset,
39     oid_value set, int class, int type)
40 {
41     int i;
42     oident *defaultsetent = oid_getentbyoid(var->globalVariantSetId);
43     oid_value defaultset = defaultsetent ? defaultsetent->value :
44         universalset;
45
46     for (i = 0; i < var->num_triples; i++)
47     {
48         oident *cursetent =
49             oid_getentbyoid(var->triples[i]->variantSetId);
50         oid_value curset = cursetent ? cursetent->value : defaultset;
51
52         if (set == curset &&
53             *var->triples[i]->class == class &&
54             *var->triples[i]->type == type)
55             return var->triples[i];
56     }
57     return 0;
58 }
59
60 static void mark_subtree(data1_node *n, int make_variantlist, int no_data,
61     Z_Variant *vreq)
62 {
63     data1_node *c;
64
65     if (n->which == DATA1N_tag && (!n->child || n->child->which != DATA1N_tag))
66     {
67         n->u.tag.node_selected = 1;
68         n->u.tag.make_variantlist = make_variantlist;
69         n->u.tag.no_data_requested = no_data;
70     }
71
72     for (c = n->child; c; c = c->next)
73     {
74         if (c->which == DATA1N_tag && (!n->child ||
75             n->child->which != DATA1N_tag))
76         {
77             c->u.tag.node_selected = 1;
78             c->u.tag.make_variantlist = make_variantlist;
79             c->u.tag.no_data_requested = no_data;
80         }
81         mark_subtree(c, make_variantlist, no_data, vreq);
82     }
83 }
84
85 static int match_children_here(data1_node *n, Z_Espec1 *e, int i,
86     Z_ETagUnit **t, int num)
87 {
88     int counter = 0, hits = 0;
89     data1_node *c;
90     Z_ETagUnit *tp = *t;
91     Z_Occurrences *occur;
92
93     for (c = n->child; c ; c = c->next)
94     {
95         data1_tag *tag = 0;
96
97         if (c->which != DATA1N_tag)
98             return 0;
99
100         if (tp->which == Z_ETagUnit_specificTag)
101         {
102             Z_SpecificTag *want = tp->u.specificTag;
103             occur = want->occurrences;
104             if (c->u.tag.element)
105                 tag = c->u.tag.element->tag;
106             if (*want->tagType != (tag ? tag->tagset->type : 3))
107                 continue;
108             if (want->tagValue->which == Z_StringOrNumeric_numeric)
109             {
110                 if (!tag || tag->which != DATA1T_numeric)
111                     continue;
112                 if (*want->tagValue->u.numeric != tag->value.numeric)
113                     continue;
114             }
115             else
116             {
117                 assert(want->tagValue->which == Z_StringOrNumeric_string);
118                 if (tag && tag->which != DATA1T_string)
119                     continue;
120                 if (data1_matchstr(want->tagValue->u.string,
121                     tag ? tag->value.string : c->u.tag.tag))
122                     continue;
123             }
124         }
125         else
126             occur = tp->u.wildThing;
127
128         /*
129          * Ok, so we have a matching tag. Are we within occurrences-range?
130          */
131         counter++;
132         if (occur && occur->which == Z_Occurrences_last)
133         {
134             logf(LOG_WARN, "Can't do occurrences=last (yet)");
135             return 0;
136         }
137         if (!occur || occur->which == Z_Occurrences_all ||
138             (occur->which == Z_Occurrences_values && counter >=
139             *occur->u.values->start))
140         {
141             if (match_children(c, e, i, t + 1, num - 1))
142             {
143                 c->u.tag.node_selected = 1;
144                 /*
145                  * Consider the variant specification if this is a complete
146                  * match.
147                  */
148                 if (num == 1)
149                 {
150                     int show_variantlist = 0;
151                     int no_data = 0;
152                     Z_Variant *vreq =
153                         e->elements[i]->u.simpleElement->variantRequest;
154                     oident *defset = oid_getentbyoid(e->defaultVariantSetId);
155                     oid_value defsetval = defset ? defset->value : VAL_NONE;
156                     oid_value var1 = oid_getvalbyname("Variant-1");
157
158                     if (!vreq)
159                         vreq = e->defaultVariantRequest;
160
161                     if (vreq)
162                     {
163                         /*
164                          * 6,5: meta-data requested, variant list.
165                          */
166                         if (find_triple(vreq, defsetval, var1, 6, 5))
167                             show_variantlist = 1;
168                         /*
169                          * 9,1: Miscellaneous, no data requested.
170                          */
171                         if (find_triple(vreq, defsetval, var1, 9, 1))
172                             no_data = 1;
173                     }
174                     mark_subtree(c, show_variantlist, no_data, vreq);
175                 }
176                 hits++;
177                 /*
178                  * have we looked at enough children?
179                  */
180                 if (!occur || (occur->which == Z_Occurrences_values &&
181                     counter - *occur->u.values->start >=
182                     *occur->u.values->howMany - 1))
183                     return hits;
184             }
185         }
186     }
187     return hits;
188 }
189
190 static int match_children(data1_node *n, Z_Espec1 *e, int i, Z_ETagUnit **t,
191     int num)
192 {
193     int res;
194
195     if (!num)
196         return 1;
197     switch (t[0]->which)
198     {
199         case Z_ETagUnit_wildThing:
200         case Z_ETagUnit_specificTag: res = match_children_here(n, e, i,
201             t, num); break;
202         case Z_ETagUnit_wildPath: res = match_children_wildpath(n, e, i,
203             t, num); break;
204         default:
205             abort();
206     }
207     return res;
208 }
209
210 int data1_doespec1(data1_node *n, Z_Espec1 *e)
211 {
212     int i;
213
214     for (i = 0; i < e->num_elements; i++)
215     {
216         if (e->elements[i]->which != Z_ERequest_simpleElement)
217             return 100;
218         match_children(n, e, i, e->elements[i]->u.simpleElement->path->tags,
219             e->elements[i]->u.simpleElement->path->num_tags);
220     }
221     return 0;
222 }