1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2010 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 /* Little helper to extract a string attribute */
23 /* Gets the first string, there is usually only one */
24 /* in case of errors, returns null */
26 void facet_struct_init(struct yaz_facet_attr *attr_values)
28 attr_values->errcode = 0;
29 attr_values->errstring = 0;
30 attr_values->relation = 0;
31 attr_values->useattr = 0;
32 attr_values->useattrbuff[0] = 0;
33 attr_values->limit = 0;
36 const char *stringattr(Z_ComplexAttribute *c)
39 Z_StringOrNumeric *son;
40 for (i = 0; i < c->num_list; i++)
43 if ( son->which == Z_StringOrNumeric_string)
49 /* Use attribute, @attr1, can be numeric or string */
50 void useattr(Z_AttributeElement *ae, struct yaz_facet_attr *av)
53 if (ae->which == Z_AttributeValue_complex)
55 s = stringattr(ae->value.complex);
56 yaz_log(YLOG_DEBUG, "useattr %s %s", s, av->useattr);
62 { /* already seen one, can't have duplicates */
63 av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_COMBI;
64 av->errstring = "multiple use attributes";
68 { /* complex that did not return a string */
69 av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_COMBI;
70 av->errstring = "non-string complex attribute";
74 { /* numeric - could translate 4 to 'title' etc */
75 sprintf(av->useattrbuff, ODR_INT_PRINTF, *ae->value.numeric);
76 av->useattr = av->useattrbuff;
81 /* TODO rename to sortorder attr */
82 void relationattr(Z_AttributeElement *ae, struct yaz_facet_attr *av)
84 if (ae->which == Z_AttributeValue_numeric)
86 if (*ae->value.numeric == 0)
87 av->relation = "desc";
88 else if (*ae->value.numeric == 1)
91 if (*ae->value.numeric == 3) {
92 av->relation = "unknown/unordered";
94 av->errcode = YAZ_BIB1_UNSUPP_RELATION_ATTRIBUTE;
95 sprintf(av->useattrbuff, ODR_INT_PRINTF,
97 av->errstring = av->useattrbuff;
102 av->errcode = YAZ_BIB1_UNSUPP_RELATION_ATTRIBUTE;
103 av->errstring = "non-numeric relation attribute";
107 void limitattr(Z_AttributeElement *ae, struct yaz_facet_attr *av)
109 if (ae->which == Z_AttributeValue_numeric)
111 av->limit = *ae->value.numeric;
112 yaz_log(YLOG_DEBUG, "limitattr %d ", av->limit);
116 av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE;
117 av->errstring = "non-numeric limit attribute";
121 /* Get the index to be searched from the attributes.
123 can be either "string", or some well-known value like
125 Returns a null and sets errors in rr,
126 emtpy string if no attr found,
127 or the string itself - always a pointer to the Z-structs,
128 so no need to free that string!
131 void facetattrs(Z_AttributeList *attributes, struct yaz_facet_attr *av)
134 Z_AttributeElement *ae;
135 yaz_log(YLOG_DEBUG, "Attribute num attributes: %d",
136 attributes->num_attributes);
137 for (i=0; i < attributes->num_attributes; i++) {
138 ae = attributes->attributes[i];
139 /* ignoring the attributeSet here */
140 yaz_log(YLOG_DEBUG, "Attribute type %d", (int) *ae->attributeType);
141 if (*ae->attributeType == 1)
142 { /* use attribute */
145 else if (*ae->attributeType == 2)
147 relationattr(ae, av);
149 else if (*ae->attributeType == 3)
154 { /* unknown attribute */
155 av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_TYPE;
156 sprintf(av->useattrbuff, ODR_INT_PRINTF,
157 *ae-> attributeType);
158 av->errstring = av->useattrbuff;
159 yaz_log(YLOG_DEBUG, "Unsupported attribute type %s", av->useattrbuff);
160 /* would like to give a better message, but the standard */
161 /* tells me to return the attribute type */
164 return; /* no need to dig deeper, return on first error */
170 Z_FacetList *extract_facet_request(ODR odr, Z_OtherInformation *search_input)
172 Z_FacetList *facet_list =
173 yaz_oi_get_facetlist_oid(&search_input, odr,
174 yaz_oid_userinfo_facet_1, 1, 0);
178 Z_Term *term_create(ODR odr, const char *cstr)
180 Z_Term *term = odr_malloc(odr, sizeof(*term));
181 term->which = Z_Term_characterString;
182 term->u.characterString = odr_strdup(odr, cstr);
186 Z_FacetTerm* facet_term_create(ODR odr, Z_Term *term, int freq)
188 Z_FacetTerm *facet_term = odr_malloc(odr, sizeof(*facet_term));
189 facet_term->count = odr_malloc(odr, sizeof(*facet_term->count));
190 facet_term->term = term;
191 *facet_term->count = freq;
195 Z_FacetField* facet_field_create(ODR odr, Z_AttributeList *attributes,
198 Z_FacetField *facet_field = odr_malloc(odr, sizeof(*facet_field));
199 facet_field->attributes = attributes;
200 facet_field->num_terms = num_terms;
201 facet_field->terms = odr_malloc(odr, num_terms * sizeof(*facet_field->terms));
205 void facet_field_term_set(ODR odr, Z_FacetField *field,
206 Z_FacetTerm *facet_term, int index)
208 assert(0 <= index && index < field->num_terms);
209 field->terms[index] = facet_term;
212 Z_FacetList* facet_list_create(ODR odr, int num_facets)
214 Z_FacetList *facet_list = odr_malloc(odr, sizeof(*facet_list));
215 facet_list->num = num_facets;
216 facet_list->elements =
217 odr_malloc(odr, facet_list->num * sizeof(*facet_list->elements));
221 void facet_list_field_set(ODR odr, Z_FacetList *list, Z_FacetField *field,
224 assert(0 <= index && index < list->num);
225 list->elements[index] = field;
231 * c-file-style: "Stroustrup"
232 * indent-tabs-mode: nil
234 * vim: shiftwidth=4 tabstop=8 expandtab