X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Fpquery.c;h=41a369df53ea9130db5b7ce705e14bcef4a79bc5;hp=63aa7b9a133d2a09841f600c678e0472328c74ee;hb=0f073baf2183a54f2a5691e697fde68c9a5f50c5;hpb=be351d15fb835ad4d9d39b34016eef3cf3bf0c99 diff --git a/src/pquery.c b/src/pquery.c index 63aa7b9..41a369d 100644 --- a/src/pquery.c +++ b/src/pquery.c @@ -6,6 +6,10 @@ * \file pquery.c * \brief Implements PQF parsing */ +#if HAVE_CONFIG_H +#include +#endif + #include #include #include @@ -119,7 +123,7 @@ static int lex(struct yaz_pqf_parser *li) return li->query_look = query_token(li); } -static int escape_string(char *out_buf, const char *in, int len) +int escape_string(char *out_buf, const char *in, int len) { char *out = out_buf; @@ -307,11 +311,11 @@ static Z_AttributesPlusTerm *rpn_term_attributes(struct yaz_pqf_parser *li, ODR Z_Term *term; zapt = (Z_AttributesPlusTerm *)odr_malloc(o, sizeof(*zapt)); - term_octet = (Odr_oct *)odr_malloc(o, sizeof(*term_octet)); term = (Z_Term *)odr_malloc(o, sizeof(*term)); zapt->term = term; zapt->attributes = attributes; + term_octet = (Odr_oct *)odr_malloc(o, sizeof(*term_octet)); term_octet->buf = (unsigned char *)odr_malloc(o, 1 + li->lex_len); term_octet->size = term_octet->len = escape_string((char *) (term_octet->buf), li->lex_buf, li->lex_len); @@ -823,6 +827,73 @@ Z_AttributeList *yaz_pqf_scan_attribute_list(YAZ_PQF_Parser p, ODR o, return p_query_scan_attributes_mk(p, o, attributeSetP); } +static Z_FacetField* parse_facet(ODR odr, const char *facet, int length) +{ + YAZ_PQF_Parser pqf_parser = yaz_pqf_create(); + char *buffer = odr_strdupn(odr, facet, length); + Odr_oid *attributeSetId; + Z_FacetField *facet_field = 0; + Z_AttributeList *attribute_list = + yaz_pqf_scan_attribute_list(pqf_parser, odr, &attributeSetId, buffer); + + if (attribute_list) + { + facet_field = odr_malloc(odr, sizeof(*facet_field)); + facet_field->attributes = attribute_list; + facet_field->num_terms = 0; + facet_field->terms = 0; + } + yaz_pqf_destroy(pqf_parser); + return facet_field; +} + +#define FACET_DElIMITER ',' + +static int scan_facet_argument(const char *arg) { + int index; + int length = strlen(arg); + int count = 1; + for (index = 0; index < length; index++) { + if (arg[index] == FACET_DElIMITER) + count++; + } + return count; +} + +/** + * yax_pdg_parse_facet_list: Parses a comma-separated list of AttributeList(s) into a FacetList. + * It does not handle the optional facet term(s). + * + */ +Z_FacetList *yaz_pqf_parse_facet_list(ODR odr, const char *facet) { + Z_FacetList *facet_list = 0; + Z_FacetField **elements; + int index = 0; + int num_elements = scan_facet_argument(facet); + if (num_elements == 0) + return facet_list; + facet_list = odr_malloc(odr, sizeof(*facet_list)); + facet_list->num = num_elements; + elements = odr_malloc(odr, num_elements * sizeof(*elements)); + facet_list->elements = elements; + for (index = 0; index < num_elements;) { + const char *pos = strchr(facet, FACET_DElIMITER); + if (pos == 0) + pos = facet + strlen(facet); + elements[index] = parse_facet(odr, (const char *) facet, (pos - facet)); + if (elements[index]) { + index++; + } + else { + num_elements--; + facet_list->num = num_elements; + } + facet = pos + 1; + } + return facet_list; +} + + int yaz_pqf_error(YAZ_PQF_Parser p, const char **msg, size_t *off) {