4 #include <yaz/diagbib1.h>
5 #include <yaz/oid_db.h>
6 #include <yaz/oid_std.h>
7 #include <yaz/otherinfo.h>
10 /* Little helper to extract a string attribute */
11 /* Gets the first string, there is usually only one */
12 /* in case of errors, returns null */
14 void facet_struct_init(struct attrvalues *attr_values) {
15 attr_values->errcode = 0;
16 attr_values->errstring = 0;
17 attr_values->relation = 0;
18 attr_values->useattr = 0;
19 attr_values->useattrbuff[0] = 0;
20 attr_values->limit = 0;
23 const char *stringattr( Z_ComplexAttribute *c ) {
25 Z_StringOrNumeric *son;
26 for ( i = 0; i < c->num_list; i++ ) {
28 if ( son->which == Z_StringOrNumeric_string )
34 /* Use attribute, @attr1, can be numeric or string */
35 void useattr ( Z_AttributeElement *ae,
36 struct attrvalues *av )
39 if ( ae->which == Z_AttributeValue_complex ) {
40 s = stringattr( ae->value.complex );
41 yaz_log(YLOG_DEBUG, "useattr %s %s", s, av->useattr);
45 else { /* already seen one, can't have duplicates */
46 av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_COMBI;
47 av->errstring = "multiple use attributes";
49 } else { /* complex that did not return a string */
50 av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_COMBI;
51 av->errstring = "non-string complex attribute";
53 } else { /* numeric - could translate 4 to 'title' etc */
54 sprintf(av->useattrbuff, ODR_INT_PRINTF, *ae->value.numeric );
55 av->useattr = av->useattrbuff;
60 /* TODO rename to sortorder attr */
61 void relationattr ( Z_AttributeElement *ae,
62 struct attrvalues *av )
64 if ( ae->which == Z_AttributeValue_numeric ) {
65 if ( *ae->value.numeric == 0 )
66 av->relation = "desc";
67 else if ( *ae->value.numeric == 1 )
70 if ( *ae->value.numeric == 3 ) {
71 av->relation = "unknown/unordered";
73 av->errcode = YAZ_BIB1_UNSUPP_RELATION_ATTRIBUTE;
74 sprintf(av->useattrbuff, ODR_INT_PRINTF,
76 av->errstring = av->useattrbuff;
79 av->errcode = YAZ_BIB1_UNSUPP_RELATION_ATTRIBUTE;
80 av->errstring = "non-numeric relation attribute";
84 void limitattr ( Z_AttributeElement *ae,
85 struct attrvalues *av )
87 /* TODO - check numeric first, then value! */
88 if ( ae->which == Z_AttributeValue_numeric ) {
89 av->limit = *ae->value.numeric;
90 yaz_log(YLOG_DEBUG, "limitattr %d ", av->limit);
92 av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE;
93 av->errstring = "non-numeric limit attribute";
97 /* Get the index to be searched from the attributes.
99 can be either "string", or some well-known value like
101 Returns a null and sets errors in rr,
102 emtpy string if no attr found,
103 or the string itself - always a pointer to the Z-structs,
104 so no need to free that string!
107 void facetattrs( Z_AttributeList *attributes,
108 struct attrvalues *av )
111 Z_AttributeElement *ae;
112 yaz_log(YLOG_DEBUG, "Attribute num attributes: %d", attributes->num_attributes);
113 for ( i=0; i < attributes->num_attributes; i++ ) {
114 ae = attributes->attributes[i];
115 /* ignoring the attributeSet here */
116 yaz_log(YLOG_DEBUG, "Attribute type %d", (int) *ae->attributeType);
117 if ( *ae->attributeType == 1 ) { /* use attribute */
119 } else if ( *ae->attributeType == 2 ) { /* sortorder */
120 relationattr(ae, av);
121 } else if ( *ae->attributeType == 3 ) { /* limit */
123 } else { /* unknown attribute */
124 av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_TYPE;
125 sprintf(av->useattrbuff, ODR_INT_PRINTF,
126 *ae-> attributeType);
127 av->errstring = av->useattrbuff;
128 yaz_log(YLOG_DEBUG, "Unsupported attribute type %s", av->useattrbuff);
129 /* would like to give a better message, but the standard */
130 /* tells me to return the attribute type */
133 return; /* no need to dig deeper, return on first error */
139 Z_FacetList *extract_facet_request(ODR odr, Z_OtherInformation *search_input) {
140 Z_FacetList *facet_list = yaz_oi_get_facetlist_oid(&search_input, odr, yaz_oid_userinfo_facet_1, 1, 0);
144 Z_Term *term_create(ODR odr, const char *cstr) {
145 Z_Term *term = odr_malloc(odr, sizeof(*term));
146 term->which = Z_Term_characterString;
147 term->u.characterString = odr_strdup(odr, cstr);
151 Z_FacetTerm* facet_term_create(ODR odr, Z_Term *term, int freq) {
152 Z_FacetTerm *facet_term = odr_malloc(odr, sizeof(*facet_term));
153 facet_term->count = odr_malloc(odr, sizeof(*facet_term->count));
154 facet_term->term = term;
155 *facet_term->count = freq;
159 Z_FacetField* facet_field_create(ODR odr, Z_AttributeList *attributes, int num_terms) {
160 Z_FacetField *facet_field = odr_malloc(odr, sizeof(*facet_field));
161 facet_field->attributes = attributes;
162 facet_field->num_terms = num_terms;
163 facet_field->terms = odr_malloc(odr, num_terms * sizeof(*facet_field->terms));
167 void facet_field_term_set(ODR odr, Z_FacetField *field, Z_FacetTerm *facet_term, int index) {
168 assert(0 <= index && index < field->num_terms);
169 field->terms[index] = facet_term;
172 Z_FacetList* facet_list_create(ODR odr, int num_facets) {
173 Z_FacetList *facet_list = odr_malloc(odr, sizeof(*facet_list));
174 facet_list->num = num_facets;
175 facet_list->elements = odr_malloc(odr, facet_list->num * sizeof(*facet_list->elements));
179 void facet_list_field_set(ODR odr, Z_FacetList *list, Z_FacetField *field, int index) {
180 assert(0 <= index && index < list->num);
181 list->elements[index] = field;