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 */
13 const char *stringattr( Z_ComplexAttribute *c ) {
15 Z_StringOrNumeric *son;
16 for ( i = 0; i < c->num_list; i++ ) {
18 if ( son->which == Z_StringOrNumeric_string )
24 /* Use attribute, @attr1, can be numeric or string */
25 void useattr ( Z_AttributeElement *ae,
26 struct attrvalues *av )
29 if ( ae->which == Z_AttributeValue_complex ) {
30 s = stringattr( ae->value.complex );
34 else { /* already seen one, can't have duplicates */
35 av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_COMBI;
36 av->errstring = "multiple use attributes";
38 } else { /* complex that did not return a string */
39 av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_COMBI;
40 av->errstring = "non-string complex attribute";
42 } else { /* numeric - could translate 4 to 'title' etc */
43 sprintf(av->useattrbuff, ODR_INT_PRINTF, *ae->value.numeric );
44 av->useattr = av->useattrbuff;
49 /* TODO rename to sortorder attr */
50 void relationattr ( Z_AttributeElement *ae,
51 struct attrvalues *av )
53 if ( ae->which == Z_AttributeValue_numeric ) {
54 if ( *ae->value.numeric == 0 )
55 av->relation = "desc";
56 else if ( *ae->value.numeric == 1 )
59 if ( *ae->value.numeric == 3 ) {
60 av->relation = "unknown/unordered";
62 av->errcode = YAZ_BIB1_UNSUPP_RELATION_ATTRIBUTE;
63 sprintf(av->useattrbuff, ODR_INT_PRINTF,
65 av->errstring = av->useattrbuff;
68 av->errcode = YAZ_BIB1_UNSUPP_RELATION_ATTRIBUTE;
69 av->errstring = "non-numeric relation attribute";
73 void limitattr ( Z_AttributeElement *ae,
74 struct attrvalues *av )
76 /* TODO - check numeric first, then value! */
77 if ( ae->which == Z_AttributeValue_numeric ) {
78 av->limit = *ae->value.numeric;
80 av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE;
81 av->errstring = "non-numeric limit attribute";
85 /* Get the index to be searched from the attributes.
87 can be either "string", or some well-known value like
89 Returns a null and sets errors in rr,
90 emtpy string if no attr found,
91 or the string itself - always a pointer to the Z-structs,
92 so no need to free that string!
95 void facetattrs( Z_AttributeList *attributes,
96 struct attrvalues *av )
99 Z_AttributeElement *ae;
100 for ( i=0; i < attributes->num_attributes; i++ ) {
101 ae = attributes->attributes[i];
102 /* ignoring the attributeSet here */
103 if ( *ae->attributeType == 1 ) { /* use attribute */
105 } else if ( *ae->attributeType == 2 ) { /* sortorder */
106 relationattr(ae, av);
107 } else if ( *ae->attributeType == 3 ) { /* limit */
109 } else { /* unknown attribute */
110 av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_TYPE;
111 sprintf(av->useattrbuff, ODR_INT_PRINTF,
112 *ae-> attributeType);
113 av->errstring = av->useattrbuff;
114 yaz_log(YLOG_DEBUG,"Unsupported attribute type %s",
116 /* would like to give a better message, but the standard */
117 /* tells me to return the attribute type */
120 return; /* no need to dig deeper, return on first error */
126 Z_FacetList *extract_facet_request(ODR odr, Z_OtherInformation *search_input) {
127 Z_FacetList *facet_list = yaz_oi_get_facetlist_oid(&search_input, odr, yaz_oid_userinfo_facet_1, 1, 0);
131 Z_Term *term_create(ODR odr, const char *cstr) {
132 Z_Term *term = odr_malloc(odr, sizeof(*term));
133 term->which = Z_Term_characterString;
134 term->u.characterString = odr_strdup(odr, cstr);
138 Z_FacetTerm* facet_term_create(ODR odr, Z_Term *term, int freq) {
139 Z_FacetTerm *facet_term = odr_malloc(odr, sizeof(*facet_term));
140 facet_term->count = odr_malloc(odr, sizeof(*facet_term->count));
141 facet_term->term = term;
142 *facet_term->count = freq;
146 Z_FacetField* facet_field_create(ODR odr, Z_AttributeList *attributes, int num_terms) {
147 Z_FacetField *facet_field = odr_malloc(odr, sizeof(*facet_field));
148 facet_field->attributes = attributes;
149 facet_field->num_terms = num_terms;
150 facet_field->terms = odr_malloc(odr, num_terms * sizeof(*facet_field->terms));
154 void facet_field_term_set(ODR odr, Z_FacetField *field, Z_FacetTerm *facet_term, int index) {
155 assert(0 <= index && index < field->num_terms);
156 field->terms[index] = facet_term;
159 Z_FacetList* facet_list_create(ODR odr, int num_facets) {
160 Z_FacetList *facet_list = odr_malloc(odr, sizeof(*facet_list));
161 facet_list->num = num_facets;
162 facet_list->elements = odr_malloc(odr, facet_list->num * sizeof(*facet_list->elements));
166 void facet_list_field_set(ODR odr, Z_FacetList *list, Z_FacetField *field, int index) {
167 assert(0 <= index && index < list->num);
168 list->elements[index] = field;