X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Fpquery.c;h=a7ba2a243087ce9ddf7458afffa61a86c1808dcf;hp=f9f51f14547bd9ec794196d2fb85825f6fad198a;hb=e4c68bbbfdfbfb44c4dbb62c3f528106ab65ffcf;hpb=2b4785b39a2bbb4affd77fb9e24aaabc0b2dd069 diff --git a/src/pquery.c b/src/pquery.c index f9f51f1..a7ba2a2 100644 --- a/src/pquery.c +++ b/src/pquery.c @@ -119,7 +119,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; @@ -182,7 +182,7 @@ static int escape_string(char *out_buf, const char *in, int len) return out - out_buf; } -static int p_query_parse_attr(struct yaz_pqf_parser *li, ODR o, +int p_query_parse_attr(struct yaz_pqf_parser *li, ODR o, int num_attr, Odr_int *attr_list, char **attr_clist, Odr_oid **attr_set) { @@ -240,79 +240,78 @@ static int p_query_parse_attr(struct yaz_pqf_parser *li, ODR o, return 1; } -static Z_AttributesPlusTerm *rpn_term(struct yaz_pqf_parser *li, ODR o, - int num_attr, Odr_int *attr_list, - char **attr_clist, Odr_oid **attr_set) +Z_AttributeList *get_attributeList(ODR o, + int num_attr, Odr_int *attr_list, + char **attr_clist, Odr_oid **attr_set) { - Z_AttributesPlusTerm *zapt; - Odr_oct *term_octet; - Z_Term *term; + int i, k = 0; + Odr_int *attr_tmp; Z_AttributeElement **elements; + Z_AttributeList *attributes= (Z_AttributeList *) odr_malloc(o, sizeof(*attributes)); + attributes->num_attributes = num_attr; + if (!num_attr) { + attributes->attributes = (Z_AttributeElement**)odr_nullval(); + return attributes; + } + elements = (Z_AttributeElement**) odr_malloc (o, num_attr * sizeof(*elements)); - 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)); - - if (!num_attr) - elements = (Z_AttributeElement**)odr_nullval(); - else + attr_tmp = (Odr_int *)odr_malloc(o, num_attr * 2 * sizeof(*attr_tmp)); + memcpy(attr_tmp, attr_list, num_attr * 2 * sizeof(*attr_tmp)); + for (i = num_attr; --i >= 0; ) { - int i, k = 0; - Odr_int *attr_tmp; - - elements = (Z_AttributeElement**) - odr_malloc (o, num_attr * sizeof(*elements)); - - attr_tmp = (Odr_int *)odr_malloc(o, num_attr * 2 * sizeof(*attr_tmp)); - memcpy(attr_tmp, attr_list, num_attr * 2 * sizeof(*attr_tmp)); - for (i = num_attr; --i >= 0; ) + int j; + for (j = i+1; jattributeType = &attr_tmp[2*i]; + elements[k]->attributeSet = attr_set[i]; + + if (attr_clist[i]) { - int j; - for (j = i+1; jattributeType = &attr_tmp[2*i]; - elements[k]->attributeSet = attr_set[i]; - - if (attr_clist[i]) - { - elements[k]->which = Z_AttributeValue_complex; - elements[k]->value.complex = (Z_ComplexAttribute *) - odr_malloc(o, sizeof(Z_ComplexAttribute)); - elements[k]->value.complex->num_list = 1; - elements[k]->value.complex->list = - (Z_StringOrNumeric **) - odr_malloc(o, 1 * sizeof(Z_StringOrNumeric *)); - elements[k]->value.complex->list[0] = - (Z_StringOrNumeric *) - odr_malloc(o, sizeof(Z_StringOrNumeric)); - elements[k]->value.complex->list[0]->which = - Z_StringOrNumeric_string; - elements[k]->value.complex->list[0]->u.string = - attr_clist[i]; - elements[k]->value.complex->semanticAction = 0; - elements[k]->value.complex->num_semanticAction = 0; - } - else - { - elements[k]->which = Z_AttributeValue_numeric; - elements[k]->value.numeric = &attr_tmp[2*i+1]; - } - k++; + elements[k]->which = Z_AttributeValue_complex; + elements[k]->value.complex = (Z_ComplexAttribute *) + odr_malloc(o, sizeof(Z_ComplexAttribute)); + elements[k]->value.complex->num_list = 1; + elements[k]->value.complex->list = + (Z_StringOrNumeric **) + odr_malloc(o, 1 * sizeof(Z_StringOrNumeric *)); + elements[k]->value.complex->list[0] = + (Z_StringOrNumeric *) + odr_malloc(o, sizeof(Z_StringOrNumeric)); + elements[k]->value.complex->list[0]->which = + Z_StringOrNumeric_string; + elements[k]->value.complex->list[0]->u.string = + attr_clist[i]; + elements[k]->value.complex->semanticAction = 0; + elements[k]->value.complex->num_semanticAction = 0; } - num_attr = k; + else + { + elements[k]->which = Z_AttributeValue_numeric; + elements[k]->value.numeric = &attr_tmp[2*i+1]; + } + k++; } - zapt->attributes = (Z_AttributeList *) - odr_malloc(o, sizeof(*zapt->attributes)); - zapt->attributes->num_attributes = num_attr; - zapt->attributes->attributes = elements; + attributes->num_attributes = k; + attributes->attributes = elements; + return attributes; +} +static Z_AttributesPlusTerm *rpn_term_attributes(struct yaz_pqf_parser *li, ODR o, Z_AttributeList *attributes) { + Z_AttributesPlusTerm *zapt; + Odr_oct *term_octet; + Z_Term *term; + + zapt = (Z_AttributesPlusTerm *)odr_malloc(o, sizeof(*zapt)); + 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); @@ -347,6 +346,14 @@ static Z_AttributesPlusTerm *rpn_term(struct yaz_pqf_parser *li, ODR o, break; } return zapt; + +} + +static Z_AttributesPlusTerm *rpn_term(struct yaz_pqf_parser *li, ODR o, + int num_attr, Odr_int *attr_list, + char **attr_clist, Odr_oid **attr_set) +{ + return rpn_term_attributes(li, o, get_attributeList(o, num_attr, attr_list, attr_clist, attr_set)); } static Z_Operand *rpn_simple(struct yaz_pqf_parser *li, ODR o, @@ -682,7 +689,7 @@ Z_RPNQuery *p_query_rpn(ODR o, const char *qbuf) } -static Z_AttributesPlusTerm *p_query_scan_mk(struct yaz_pqf_parser *li, +static Z_AttributeList *p_query_scan_attributes_mk(struct yaz_pqf_parser *li, ODR o, Odr_oid **attributeSetP) { @@ -692,7 +699,6 @@ static Z_AttributesPlusTerm *p_query_scan_mk(struct yaz_pqf_parser *li, int num_attr = 0; int max_attr = 512; Odr_oid *top_set = 0; - Z_AttributesPlusTerm *apt; lex(li); if (li->query_look == 'r') @@ -741,12 +747,22 @@ static Z_AttributesPlusTerm *p_query_scan_mk(struct yaz_pqf_parser *li, else break; } + return get_attributeList(o, num_attr, attr_list, attr_clist, attr_set); +} + +static Z_AttributesPlusTerm *p_query_scan_mk(struct yaz_pqf_parser *li, + ODR o, + Odr_oid **attributeSetP) +{ + Z_AttributeList *attr_list = p_query_scan_attributes_mk(li, o, attributeSetP); + Z_AttributesPlusTerm *apt; + if (!li->query_look) { li->error = YAZ_PQF_ERROR_MISSING; return 0; } - apt = rpn_term(li, o, num_attr, attr_list, attr_clist, attr_set); + apt = rpn_term_attributes(li, o, attr_list); lex(li); @@ -796,6 +812,87 @@ Z_AttributesPlusTerm *yaz_pqf_scan(YAZ_PQF_Parser p, ODR o, return p_query_scan_mk(p, o, attributeSetP); } +Z_AttributeList *yaz_pqf_scan_attribute_list(YAZ_PQF_Parser p, ODR o, + Odr_oid **attributeSetP, + const char *qbuf) +{ + if (!p) + return 0; + p->query_buf = p->query_ptr = qbuf; + p->lex_buf = 0; + 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[length+1]; + Odr_oid *attributeSetId; + Z_FacetField *facet_field; + Z_AttributeList *attribute_list; + memcpy(buffer, facet, length); + buffer[length] = '\0'; + attribute_list = yaz_pqf_scan_attribute_list(pqf_parser, odr, &attributeSetId, buffer); + + if (!attribute_list) { + printf("Invalid facet definition: %s", facet); + return 0; + } + facet_field = odr_malloc(odr, sizeof(*facet_field)); + facet_field->attributes = attribute_list; + facet_field->num_terms = 0; + facet_field->terms = 0; + //debug_add_facet_term(odr, facet_field); + + 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)); + 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 = pos + 1; + } + facet_list->elements = elements; + return facet_list; +} + + + int yaz_pqf_error(YAZ_PQF_Parser p, const char **msg, size_t *off) { switch (p->error)