1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 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>
22 void yaz_oi_set_facetlist(
23 Z_OtherInformation **otherInformation, ODR odr,
24 Z_FacetList *facet_list)
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,
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;
43 Z_FacetList *yaz_oi_get_facetlist(Z_OtherInformation **otherInformation)
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,
52 z_external = oi->information.externallyDefinedInfo;
54 if (z_external && z_external->which == Z_External_userFacets) {
55 return z_external->u.facetList;
60 /* Little helper to extract a string attribute */
61 /* Gets the first string, there is usually only one */
62 /* in case of errors, returns null */
64 void yaz_facet_attr_init(struct yaz_facet_attr *attr_values)
66 attr_values->errcode = 0;
67 attr_values->errstring = 0;
68 attr_values->relation = 0;
69 attr_values->useattr = 0;
70 attr_values->useattrbuff[0] = 0;
71 attr_values->limit = 0;
74 static const char *stringattr(Z_ComplexAttribute *c)
77 Z_StringOrNumeric *son;
78 for (i = 0; i < c->num_list; i++)
81 if ( son->which == Z_StringOrNumeric_string)
87 /* Use attribute, @attr1, can be numeric or string */
88 static void useattr(Z_AttributeElement *ae, struct yaz_facet_attr *av)
91 if (ae->which == Z_AttributeValue_complex)
93 s = stringattr(ae->value.complex);
99 { /* already seen one, can't have duplicates */
100 av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_COMBI;
101 av->errstring = "multiple use attributes";
105 { /* complex that did not return a string */
106 av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_COMBI;
107 av->errstring = "non-string complex attribute";
111 { /* numeric - could translate 4 to 'title' etc */
112 sprintf(av->useattrbuff, ODR_INT_PRINTF, *ae->value.numeric);
113 av->useattr = av->useattrbuff;
118 /* TODO rename to sortorder attr */
119 static void relationattr(Z_AttributeElement *ae, struct yaz_facet_attr *av)
121 if (ae->which == Z_AttributeValue_numeric)
123 if (*ae->value.numeric == 0)
124 av->relation = "desc";
125 else if (*ae->value.numeric == 1)
126 av->relation = "asc";
128 if (*ae->value.numeric == 3) {
129 av->relation = "unknown/unordered";
131 av->errcode = YAZ_BIB1_UNSUPP_RELATION_ATTRIBUTE;
132 sprintf(av->useattrbuff, ODR_INT_PRINTF,
133 *ae-> attributeType);
134 av->errstring = av->useattrbuff;
139 av->errcode = YAZ_BIB1_UNSUPP_RELATION_ATTRIBUTE;
140 av->errstring = "non-numeric relation attribute";
144 static void limitattr(Z_AttributeElement *ae, struct yaz_facet_attr *av)
146 if (ae->which == Z_AttributeValue_numeric)
148 av->limit = *ae->value.numeric;
152 av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE;
153 av->errstring = "non-numeric limit attribute";
157 /* Get the index to be searched from the attributes.
159 can be either "string", or some well-known value like
161 Returns a null and sets errors in rr,
162 emtpy string if no attr found,
163 or the string itself - always a pointer to the Z-structs,
164 so no need to free that string!
167 void yaz_facet_attr_get_z_attributes(const Z_AttributeList *attributes,
168 struct yaz_facet_attr *av)
171 Z_AttributeElement *ae;
172 for (i=0; i < attributes->num_attributes; i++) {
173 ae = attributes->attributes[i];
174 /* ignoring the attributeSet here */
175 if (*ae->attributeType == 1)
176 { /* use attribute */
179 else if (*ae->attributeType == 2)
181 relationattr(ae, av);
183 else if (*ae->attributeType == 3)
188 { /* unknown attribute */
189 av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_TYPE;
190 sprintf(av->useattrbuff, ODR_INT_PRINTF,
191 *ae-> attributeType);
192 av->errstring = av->useattrbuff;
193 yaz_log(YLOG_WARN, "Unsupported attribute type %s", av->useattrbuff);
194 /* would like to give a better message, but the standard */
195 /* tells me to return the attribute type */
198 return; /* no need to dig deeper, return on first error */
203 Z_Term *term_create(ODR odr, const char *cstr)
205 Z_Term *term = odr_malloc(odr, sizeof(*term));
206 term->which = Z_Term_characterString;
207 term->u.characterString = odr_strdup(odr, cstr);
211 Z_FacetTerm* facet_term_create(ODR odr, Z_Term *term, int freq)
213 Z_FacetTerm *facet_term = odr_malloc(odr, sizeof(*facet_term));
214 facet_term->count = odr_malloc(odr, sizeof(*facet_term->count));
215 facet_term->term = term;
216 *facet_term->count = freq;
220 Z_FacetField* facet_field_create(ODR odr, Z_AttributeList *attributes,
223 Z_FacetField *facet_field = odr_malloc(odr, sizeof(*facet_field));
224 facet_field->attributes = attributes;
225 facet_field->num_terms = num_terms;
226 facet_field->terms = odr_malloc(odr, num_terms * sizeof(*facet_field->terms));
230 void facet_field_term_set(ODR odr, Z_FacetField *field,
231 Z_FacetTerm *facet_term, int index)
233 assert(0 <= index && index < field->num_terms);
234 field->terms[index] = facet_term;
237 Z_FacetList* facet_list_create(ODR odr, int num_facets)
239 Z_FacetList *facet_list = odr_malloc(odr, sizeof(*facet_list));
240 facet_list->num = num_facets;
241 facet_list->elements =
242 odr_malloc(odr, facet_list->num * sizeof(*facet_list->elements));
246 void facet_list_field_set(ODR odr, Z_FacetList *list, Z_FacetField *field,
249 assert(0 <= index && index < list->num);
250 list->elements[index] = field;
256 * c-file-style: "Stroustrup"
257 * indent-tabs-mode: nil
259 * vim: shiftwidth=4 tabstop=8 expandtab