X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Ffacet.c;h=d32fcf69d5bc8d2b24015934dd64de1302e92bff;hp=bf1904b34977de9b2f7fa52602cd1767e0be560a;hb=51e1ca7946289042c3c09e92afc1c1c465f5d870;hpb=79e871ca9f4d35a123a8a0dddb60b4a8c8b62dbd diff --git a/src/facet.c b/src/facet.c index bf1904b..d32fcf6 100644 --- a/src/facet.c +++ b/src/facet.c @@ -1,9 +1,9 @@ /* This file is part of the YAZ toolkit. - * Copyright (C) 1995-2010 Index Data + * Copyright (C) Index Data * See the file LICENSE for details. */ -/** +/** * \file facet.c * \brief Facet utilities */ @@ -17,94 +17,122 @@ #include #include #include +#include #include +void yaz_oi_set_facetlist( + Z_OtherInformation **otherInformation, ODR odr, + Z_FacetList *facet_list) +{ + int categoryValue = 1; + Z_External *z_external = 0; + Z_OtherInformationUnit *oi = + yaz_oi_update(otherInformation, odr, yaz_oid_userinfo_facet_1, + categoryValue, 0); + if (!oi) + return; + oi->which = Z_OtherInfo_externallyDefinedInfo; + z_external = odr_malloc(odr, sizeof(*z_external)); + z_external->which = Z_External_userFacets; + z_external->direct_reference = odr_oiddup(odr, yaz_oid_userinfo_facet_1); + z_external->indirect_reference = 0; + z_external->descriptor = 0; + z_external->u.facetList = facet_list; + oi->information.externallyDefinedInfo = z_external; +} + +Z_FacetList *yaz_oi_get_facetlist(Z_OtherInformation **otherInformation) +{ + Z_OtherInformation *oi = *otherInformation; + if (oi) + { + int i; + for (i = 0; i < oi->num_elements; i++) + { + Z_OtherInformationUnit *oiu = oi->list[i]; + if (oiu->which == Z_OtherInfo_externallyDefinedInfo + && oiu->information.externallyDefinedInfo->which == + Z_External_userFacets) + { + return oiu->information.externallyDefinedInfo->u.facetList; + } + } + } + return 0; +} + /* Little helper to extract a string attribute */ /* Gets the first string, there is usually only one */ /* in case of errors, returns null */ -void facet_struct_init(struct yaz_facet_attr *attr_values) { +void yaz_facet_attr_init(struct yaz_facet_attr *attr_values) +{ attr_values->errcode = 0; attr_values->errstring = 0; - attr_values->relation = 0; + attr_values->sortorder = 0; attr_values->useattr = 0; attr_values->useattrbuff[0] = 0; attr_values->limit = 0; + attr_values->start = 0; } -const char *stringattr( Z_ComplexAttribute *c ) { +static const char *stringattr(Z_ComplexAttribute *c) +{ int i; Z_StringOrNumeric *son; - for ( i = 0; i < c->num_list; i++ ) { + for (i = 0; i < c->num_list; i++) + { son = c->list[i]; - if ( son->which == Z_StringOrNumeric_string ) + if ( son->which == Z_StringOrNumeric_string) return son->u.string; } return 0; } /* Use attribute, @attr1, can be numeric or string */ -void useattr ( Z_AttributeElement *ae, - struct yaz_facet_attr *av ) +static void useattr(Z_AttributeElement *ae, struct yaz_facet_attr *av) { const char *s; - if ( ae->which == Z_AttributeValue_complex ) { - s = stringattr( ae->value.complex ); - yaz_log(YLOG_DEBUG, "useattr %s %s", s, av->useattr); - if (s) { + if (ae->which == Z_AttributeValue_complex) + { + s = stringattr(ae->value.complex); + if (s) + { if (!av->useattr) av->useattr = s; - else { /* already seen one, can't have duplicates */ + else + { /* already seen one, can't have duplicates */ av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_COMBI; av->errstring = "multiple use attributes"; } - } else { /* complex that did not return a string */ + } + else + { /* complex that did not return a string */ av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_COMBI; av->errstring = "non-string complex attribute"; } - } else { /* numeric - could translate 4 to 'title' etc */ - sprintf(av->useattrbuff, ODR_INT_PRINTF, *ae->value.numeric ); + } + else + { /* numeric - could translate 4 to 'title' etc */ + sprintf(av->useattrbuff, ODR_INT_PRINTF, *ae->value.numeric); av->useattr = av->useattrbuff; } -} /* useattr */ +} -/* TODO rename to sortorder attr */ -void relationattr ( Z_AttributeElement *ae, - struct yaz_facet_attr *av ) +static void numattr(Z_AttributeElement *ae, struct yaz_facet_attr *av, + int *v) { - if ( ae->which == Z_AttributeValue_numeric ) { - if ( *ae->value.numeric == 0 ) - av->relation = "desc"; - else if ( *ae->value.numeric == 1 ) - av->relation = "asc"; - else - if ( *ae->value.numeric == 3 ) { - av->relation = "unknown/unordered"; - } else { - av->errcode = YAZ_BIB1_UNSUPP_RELATION_ATTRIBUTE; - sprintf(av->useattrbuff, ODR_INT_PRINTF, - *ae-> attributeType); - av->errstring = av->useattrbuff; - } - } else { - av->errcode = YAZ_BIB1_UNSUPP_RELATION_ATTRIBUTE; - av->errstring = "non-numeric relation attribute"; + if (ae->which == Z_AttributeValue_numeric) + { + *v = *ae->value.numeric; } -} /* relationattr */ - -void limitattr ( Z_AttributeElement *ae, - struct yaz_facet_attr *av ) -{ - /* TODO - check numeric first, then value! */ - if ( ae->which == Z_AttributeValue_numeric ) { - av->limit = *ae->value.numeric; - yaz_log(YLOG_DEBUG, "limitattr %d ", av->limit); - } else { + else + { av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE; - av->errstring = "non-numeric limit attribute"; + av->errstring = "non-numeric limit/sort/start attribute"; } -} /* relationattr */ +} /* Get the index to be searched from the attributes. @attr 1 @@ -116,59 +144,60 @@ void limitattr ( Z_AttributeElement *ae, so no need to free that string! */ -void facetattrs( Z_AttributeList *attributes, - struct yaz_facet_attr *av ) +void yaz_facet_attr_get_z_attributes(const Z_AttributeList *attributes, + struct yaz_facet_attr *av) { int i; Z_AttributeElement *ae; - yaz_log(YLOG_DEBUG, "Attribute num attributes: %d", attributes->num_attributes); - for ( i=0; i < attributes->num_attributes; i++ ) { + for (i = 0; i < attributes->num_attributes; i++) + { ae = attributes->attributes[i]; /* ignoring the attributeSet here */ - yaz_log(YLOG_DEBUG, "Attribute type %d", (int) *ae->attributeType); - if ( *ae->attributeType == 1 ) { /* use attribute */ + if (*ae->attributeType == 1) + { /* use attribute */ useattr(ae, av); - } else if ( *ae->attributeType == 2 ) { /* sortorder */ - relationattr(ae, av); - } else if ( *ae->attributeType == 3 ) { /* limit */ - limitattr(ae, av); - } else { /* unknown attribute */ + } + else if (*ae->attributeType == 2) + { + numattr(ae, av, &av->sortorder); + } + else if (*ae->attributeType == 3) + { + numattr(ae, av, &av->limit); + } + else if (*ae->attributeType == 4) + { + numattr(ae, av, &av->start); + } + else + { /* unknown attribute */ av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_TYPE; sprintf(av->useattrbuff, ODR_INT_PRINTF, *ae-> attributeType); av->errstring = av->useattrbuff; - yaz_log(YLOG_DEBUG, "Unsupported attribute type %s", av->useattrbuff); + yaz_log(YLOG_WARN, "Unsupported attribute type %s", + av->useattrbuff); /* would like to give a better message, but the standard */ /* tells me to return the attribute type */ } - if ( av->errcode ) + if (av->errcode) return; /* no need to dig deeper, return on first error */ } return; -} /* facetattrs */ - - -Z_FacetList *extract_facet_request(ODR odr, Z_OtherInformation *search_input) { - Z_FacetList *facet_list = yaz_oi_get_facetlist_oid(&search_input, odr, yaz_oid_userinfo_facet_1, 1, 0); - return facet_list; } -Z_Term *term_create(ODR odr, const char *cstr) { - Z_Term *term = odr_malloc(odr, sizeof(*term)); - term->which = Z_Term_characterString; - term->u.characterString = odr_strdup(odr, cstr); - return term; -} - -Z_FacetTerm* facet_term_create(ODR odr, Z_Term *term, int freq) { +Z_FacetTerm *facet_term_create_cstr(ODR odr, const char *cstr, Odr_int freq) +{ Z_FacetTerm *facet_term = odr_malloc(odr, sizeof(*facet_term)); - facet_term->count = odr_malloc(odr, sizeof(*facet_term->count)); + Z_Term *term = z_Term_create(odr, Z_Term_general, cstr, strlen(cstr)); facet_term->term = term; - *facet_term->count = freq; + facet_term->count = odr_intdup(odr, freq); return facet_term; } -Z_FacetField* facet_field_create(ODR odr, Z_AttributeList *attributes, int num_terms) { +Z_FacetField* facet_field_create(ODR odr, Z_AttributeList *attributes, + int num_terms) +{ Z_FacetField *facet_field = odr_malloc(odr, sizeof(*facet_field)); facet_field->attributes = attributes; facet_field->num_terms = num_terms; @@ -176,19 +205,25 @@ Z_FacetField* facet_field_create(ODR odr, Z_AttributeList *attributes, int num_t return facet_field; } -void facet_field_term_set(ODR odr, Z_FacetField *field, Z_FacetTerm *facet_term, int index) { +void facet_field_term_set(ODR odr, Z_FacetField *field, + Z_FacetTerm *facet_term, int index) +{ assert(0 <= index && index < field->num_terms); field->terms[index] = facet_term; } -Z_FacetList* facet_list_create(ODR odr, int num_facets) { +Z_FacetList* facet_list_create(ODR odr, int num_facets) +{ Z_FacetList *facet_list = odr_malloc(odr, sizeof(*facet_list)); facet_list->num = num_facets; - facet_list->elements = odr_malloc(odr, facet_list->num * sizeof(*facet_list->elements)); + facet_list->elements = + odr_malloc(odr, facet_list->num * sizeof(*facet_list->elements)); return facet_list; } -void facet_list_field_set(ODR odr, Z_FacetList *list, Z_FacetField *field, int index) { +void facet_list_field_set(ODR odr, Z_FacetList *list, Z_FacetField *field, + int index) +{ assert(0 <= index && index < list->num); list->elements[index] = field; }