1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
8 * \brief Facet utilities
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 <yaz/pquery.h>
23 void yaz_oi_set_facetlist(
24 Z_OtherInformation **otherInformation, ODR odr,
25 Z_FacetList *facet_list)
27 int categoryValue = 1;
28 Z_External *z_external = 0;
29 Z_OtherInformationUnit *oi =
30 yaz_oi_update(otherInformation, odr, yaz_oid_userinfo_facet_1,
34 oi->which = Z_OtherInfo_externallyDefinedInfo;
35 z_external = odr_malloc(odr, sizeof(*z_external));
36 z_external->which = Z_External_userFacets;
37 z_external->direct_reference = odr_oiddup(odr, yaz_oid_userinfo_facet_1);
38 z_external->indirect_reference = 0;
39 z_external->descriptor = 0;
40 z_external->u.facetList = facet_list;
41 oi->information.externallyDefinedInfo = z_external;
44 Z_FacetList *yaz_oi_get_facetlist(Z_OtherInformation **otherInformation)
46 Z_OtherInformation *oi = *otherInformation;
50 for (i = 0; i < oi->num_elements; i++)
52 Z_OtherInformationUnit *oiu = oi->list[i];
53 if (oiu->which == Z_OtherInfo_externallyDefinedInfo
54 && oiu->information.externallyDefinedInfo->which ==
55 Z_External_userFacets)
57 return oiu->information.externallyDefinedInfo->u.facetList;
64 /* Little helper to extract a string attribute */
65 /* Gets the first string, there is usually only one */
66 /* in case of errors, returns null */
68 void yaz_facet_attr_init(struct yaz_facet_attr *attr_values)
70 attr_values->errcode = 0;
71 attr_values->errstring = 0;
72 attr_values->sortorder = 0;
73 attr_values->useattr = 0;
74 attr_values->useattrbuff[0] = 0;
75 attr_values->limit = 0;
76 attr_values->start = 0;
79 static const char *stringattr(Z_ComplexAttribute *c)
82 Z_StringOrNumeric *son;
83 for (i = 0; i < c->num_list; i++)
86 if ( son->which == Z_StringOrNumeric_string)
92 /* Use attribute, @attr1, can be numeric or string */
93 static void useattr(Z_AttributeElement *ae, struct yaz_facet_attr *av)
96 if (ae->which == Z_AttributeValue_complex)
98 s = stringattr(ae->value.complex);
104 { /* already seen one, can't have duplicates */
105 av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_COMBI;
106 av->errstring = "multiple use attributes";
110 { /* complex that did not return a string */
111 av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_COMBI;
112 av->errstring = "non-string complex attribute";
116 { /* numeric - could translate 4 to 'title' etc */
117 sprintf(av->useattrbuff, ODR_INT_PRINTF, *ae->value.numeric);
118 av->useattr = av->useattrbuff;
123 static void numattr(Z_AttributeElement *ae, struct yaz_facet_attr *av,
126 if (ae->which == Z_AttributeValue_numeric)
128 *v = *ae->value.numeric;
132 av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE;
133 av->errstring = "non-numeric limit/sort/start attribute";
137 /* Get the index to be searched from the attributes.
139 can be either "string", or some well-known value like
141 Returns a null and sets errors in rr,
142 emtpy string if no attr found,
143 or the string itself - always a pointer to the Z-structs,
144 so no need to free that string!
147 void yaz_facet_attr_get_z_attributes(const Z_AttributeList *attributes,
148 struct yaz_facet_attr *av)
151 Z_AttributeElement *ae;
152 for (i = 0; i < attributes->num_attributes; i++)
154 ae = attributes->attributes[i];
155 /* ignoring the attributeSet here */
156 if (*ae->attributeType == 1)
157 { /* use attribute */
160 else if (*ae->attributeType == 2)
162 numattr(ae, av, &av->sortorder);
164 else if (*ae->attributeType == 3)
166 numattr(ae, av, &av->limit);
168 else if (*ae->attributeType == 4)
170 numattr(ae, av, &av->start);
173 { /* unknown attribute */
174 av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_TYPE;
175 sprintf(av->useattrbuff, ODR_INT_PRINTF,
176 *ae-> attributeType);
177 av->errstring = av->useattrbuff;
178 yaz_log(YLOG_WARN, "Unsupported attribute type %s", av->useattrbuff);
179 /* would like to give a better message, but the standard */
180 /* tells me to return the attribute type */
183 return; /* no need to dig deeper, return on first error */
189 Z_Term *term_create(ODR odr, const char *cstr)
191 Z_Term *term = odr_malloc(odr, sizeof(*term));
192 term->which = Z_Term_characterString;
193 term->u.characterString = odr_strdup(odr, cstr);
197 Z_FacetTerm* facet_term_create(ODR odr, Z_Term *term, int freq)
199 Z_FacetTerm *facet_term = odr_malloc(odr, sizeof(*facet_term));
200 facet_term->count = odr_malloc(odr, sizeof(*facet_term->count));
201 facet_term->term = term;
202 *facet_term->count = freq;
207 Z_FacetTerm *facet_term_create_cstr(ODR odr, const char *cstr, Odr_int freq)
209 Z_FacetTerm *facet_term = odr_malloc(odr, sizeof(*facet_term));
210 Z_Term *term = z_Term_create(odr, Z_Term_general, cstr, strlen(cstr));
211 facet_term->term = term;
212 facet_term->count = odr_intdup(odr, freq);
216 Z_FacetField* facet_field_create(ODR odr, Z_AttributeList *attributes,
219 Z_FacetField *facet_field = odr_malloc(odr, sizeof(*facet_field));
220 facet_field->attributes = attributes;
221 facet_field->num_terms = num_terms;
222 facet_field->terms = odr_malloc(odr, num_terms * sizeof(*facet_field->terms));
226 void facet_field_term_set(ODR odr, Z_FacetField *field,
227 Z_FacetTerm *facet_term, int index)
229 assert(0 <= index && index < field->num_terms);
230 field->terms[index] = facet_term;
233 Z_FacetList* facet_list_create(ODR odr, int num_facets)
235 Z_FacetList *facet_list = odr_malloc(odr, sizeof(*facet_list));
236 facet_list->num = num_facets;
237 facet_list->elements =
238 odr_malloc(odr, facet_list->num * sizeof(*facet_list->elements));
242 void facet_list_field_set(ODR odr, Z_FacetList *list, Z_FacetField *field,
245 assert(0 <= index && index < list->num);
246 list->elements[index] = field;
252 * c-file-style: "Stroustrup"
253 * indent-tabs-mode: nil
255 * vim: shiftwidth=4 tabstop=8 expandtab