Add new function nmem_strsplitx.
[yaz-moved-to-github.git] / src / facet.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2011 Index Data
3  * See the file LICENSE for details.
4  */
5
6 /** 
7  * \file facet.c
8  * \brief Facet utilities
9  */
10
11 #if HAVE_CONFIG_H
12 #include <config.h>
13 #endif
14
15 #include <yaz/facet.h>
16 #include <yaz/diagbib1.h>
17 #include <yaz/oid_db.h>
18 #include <yaz/oid_std.h>
19 #include <yaz/otherinfo.h>
20 #include <assert.h>
21
22 void yaz_oi_set_facetlist(
23     Z_OtherInformation **otherInformation, ODR odr,
24     Z_FacetList *facet_list)
25 {
26     int categoryValue = 1;
27     Z_External *z_external = 0;
28     Z_OtherInformationUnit *oi =
29         yaz_oi_update(otherInformation, odr, yaz_oid_userinfo_facet_1,
30                       categoryValue, 0);
31     if (!oi)
32         return;
33     oi->which = Z_OtherInfo_externallyDefinedInfo;
34     z_external = odr_malloc(odr, sizeof(*z_external));
35     z_external->which = Z_External_userFacets;
36     z_external->direct_reference = odr_oiddup(odr, yaz_oid_userinfo_facet_1);
37     z_external->indirect_reference = 0;
38     z_external->descriptor = 0;
39     z_external->u.facetList = facet_list;
40     oi->information.externallyDefinedInfo = z_external;
41 }
42
43 Z_FacetList *yaz_oi_get_facetlist(Z_OtherInformation **otherInformation)
44 {
45     int categoryValue = 1;
46     Z_External *z_external = 0;
47     Z_OtherInformationUnit *oi =
48         yaz_oi_update(otherInformation, 0, yaz_oid_userinfo_facet_1,
49                       categoryValue, 0);
50     if (!oi)
51         return 0;
52     z_external = oi->information.externallyDefinedInfo;
53
54     if (z_external && z_external->which == Z_External_userFacets)
55         return z_external->u.facetList;
56     return 0;
57 }
58
59 /* Little helper to extract a string attribute */
60 /* Gets the first string, there is usually only one */
61 /* in case of errors, returns null */
62
63 void yaz_facet_attr_init(struct yaz_facet_attr *attr_values)
64 {
65     attr_values->errcode   = 0;
66     attr_values->errstring = 0;
67     attr_values->relation  = 0;
68     attr_values->useattr   = 0;
69     attr_values->useattrbuff[0] = 0;
70     attr_values->limit     = 0;
71 }
72
73 static const char *stringattr(Z_ComplexAttribute *c)
74 {
75     int i;
76      Z_StringOrNumeric *son;
77     for (i = 0; i < c->num_list; i++)
78     {
79         son = c->list[i];
80         if ( son->which == Z_StringOrNumeric_string)
81             return son->u.string;
82     }
83     return 0;
84 }
85
86 /* Use attribute, @attr1, can be numeric or string */
87 static void useattr(Z_AttributeElement *ae, struct yaz_facet_attr *av)
88 {
89     const char *s;
90     if (ae->which == Z_AttributeValue_complex)
91     {
92         s = stringattr(ae->value.complex);
93         if (s)
94         {
95             if (!av->useattr)
96                 av->useattr = s;
97             else
98             { /* already seen one, can't have duplicates */
99                 av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_COMBI;
100                 av->errstring = "multiple use attributes";
101             }
102         }
103         else
104         { /* complex that did not return a string */
105             av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_COMBI;
106             av->errstring = "non-string complex attribute";
107         }
108     }
109     else
110     { /* numeric - could translate 4 to 'title' etc */
111         sprintf(av->useattrbuff, ODR_INT_PRINTF, *ae->value.numeric);
112         av->useattr = av->useattrbuff;
113     }
114 }
115
116
117 static void sortorderattr(Z_AttributeElement *ae, struct yaz_facet_attr *av)
118 {
119     if (ae->which == Z_AttributeValue_numeric)
120     {
121         if (*ae->value.numeric == 0)
122             av->relation = "desc";
123         else if (*ae->value.numeric == 1)
124             av->relation = "asc";
125         else if (*ae->value.numeric == 3)
126             av->relation = "unknown/unordered";
127         else
128         {
129             av->errcode = YAZ_BIB1_UNSUPP_RELATION_ATTRIBUTE;
130             sprintf(av->useattrbuff, ODR_INT_PRINTF,
131                     *ae-> attributeType);
132             av->errstring = av->useattrbuff;
133         }
134     }
135     else
136     {
137         av->errcode = YAZ_BIB1_UNSUPP_RELATION_ATTRIBUTE;
138         av->errstring = "non-numeric relation attribute";
139     }
140 }
141
142 static void limitattr(Z_AttributeElement *ae, struct yaz_facet_attr *av)
143 {
144     if (ae->which == Z_AttributeValue_numeric)
145     {
146         av->limit = *ae->value.numeric;
147     }
148     else
149     {
150         av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE;
151         av->errstring = "non-numeric limit attribute";
152     }
153 }
154
155 /* Get the index to be searched from the attributes.
156    @attr 1
157      can be either "string", or some well-known value like
158      4 for title
159    Returns a null and sets errors in rr,
160    emtpy string if no attr found,
161    or the string itself - always a pointer to the Z-structs,
162    so no need to free that string!
163 */
164
165 void yaz_facet_attr_get_z_attributes(const Z_AttributeList *attributes,
166                                      struct yaz_facet_attr *av)
167 {
168     int i;
169     Z_AttributeElement *ae;
170     for (i = 0; i < attributes->num_attributes; i++)
171     {
172         ae = attributes->attributes[i];
173         /* ignoring the attributeSet here */
174         if (*ae->attributeType == 1)
175         { /* use attribute */
176             useattr(ae, av);
177         }
178         else if (*ae->attributeType == 2)
179         { /* sortorder */
180             sortorderattr(ae, av);
181         }
182         else if (*ae->attributeType == 3)
183         { /* limit */
184             limitattr(ae, av);
185         }
186         else
187         { /* unknown attribute */
188             av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_TYPE;
189             sprintf(av->useattrbuff, ODR_INT_PRINTF,
190                         *ae-> attributeType);
191             av->errstring = av->useattrbuff;
192             yaz_log(YLOG_WARN, "Unsupported attribute type %s", av->useattrbuff);
193             /* would like to give a better message, but the standard */
194             /* tells me to return the attribute type */
195         }
196         if (av->errcode)
197             return; /* no need to dig deeper, return on first error */
198     }
199     return;
200 } /* facetattrs */
201
202 Z_Term *term_create(ODR odr, const char *cstr)
203 {
204     Z_Term *term = odr_malloc(odr, sizeof(*term));
205     term->which = Z_Term_characterString;
206     term->u.characterString = odr_strdup(odr, cstr);
207     return term;
208 }
209
210 Z_FacetTerm* facet_term_create(ODR odr, Z_Term *term, int freq)
211 {
212     Z_FacetTerm *facet_term = odr_malloc(odr, sizeof(*facet_term));
213     facet_term->count = odr_malloc(odr, sizeof(*facet_term->count));
214     facet_term->term = term;
215     *facet_term->count = freq;
216     return facet_term;
217 }
218
219 Z_FacetField* facet_field_create(ODR odr, Z_AttributeList *attributes,
220                                  int num_terms)
221 {
222     Z_FacetField *facet_field = odr_malloc(odr, sizeof(*facet_field));
223     facet_field->attributes = attributes;
224     facet_field->num_terms = num_terms;
225     facet_field->terms = odr_malloc(odr, num_terms * sizeof(*facet_field->terms));
226     return facet_field;
227 }
228
229 void facet_field_term_set(ODR odr, Z_FacetField *field,
230                           Z_FacetTerm *facet_term, int index)
231 {
232     assert(0 <= index && index < field->num_terms);
233     field->terms[index] = facet_term;
234 }
235
236 Z_FacetList* facet_list_create(ODR odr, int num_facets)
237 {
238     Z_FacetList *facet_list = odr_malloc(odr, sizeof(*facet_list));
239     facet_list->num = num_facets;
240     facet_list->elements =
241         odr_malloc(odr, facet_list->num * sizeof(*facet_list->elements));
242     return facet_list;
243 }
244
245 void facet_list_field_set(ODR odr, Z_FacetList *list, Z_FacetField *field,
246                           int index)
247 {
248     assert(0 <= index && index < list->num);
249     list->elements[index] = field;
250 }
251
252 /*
253  * Local variables:
254  * c-basic-offset: 4
255  * c-file-style: "Stroustrup"
256  * indent-tabs-mode: nil
257  * End:
258  * vim: shiftwidth=4 tabstop=8 expandtab
259  */
260