/* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2010 Index Data
+ * Copyright (C) 1995-2013 Index Data
* See the file LICENSE for details.
*/
/**
* \file pquery.c
* \brief Implements PQF parsing
*/
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
+
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
-#include <ctype.h>
#include <yaz/proto.h>
#include <yaz/oid_db.h>
};
static Z_RPNStructure *rpn_structure(struct yaz_pqf_parser *li, ODR o,
- int num_attr, int max_attr,
+ int num_attr, int max_attr,
Odr_int *attr_list, char **attr_clist,
Odr_oid **attr_set);
size_t off)
{
size_t len=strlen(src);
-
+
if (li->lex_len == len+off && !memcmp(li->lex_buf+off, src, len-off))
return 1;
return 0;
++(*qptr);
}
li->lex_buf = *qptr;
-
- if (**qptr == li->escape_char && isdigit(((const unsigned char *) *qptr)[1]))
+
+ if (**qptr == li->escape_char && yaz_isdigit((*qptr)[1]))
{
++(li->lex_len);
++(*qptr);
}
while (**qptr && **qptr != sep_char)
{
- if (**qptr == '\\')
+ if (**qptr == '\\' && (*qptr)[1])
{
++(li->lex_len);
++(*qptr);
char s[4];
int n = 0;
s[0] = *in;
- s[1] = *++in;
+ s[1] = *++in;
s[2] = *++in;
s[3] = '\0';
len = len - 2;
return 0;
}
}
- else
+ else
{
if (num_attr > 0)
attr_set[num_attr] = attr_set[num_attr-1];
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)
+static 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;
-
- 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));
-
+ Z_AttributeList *attributes= (Z_AttributeList *)
+ odr_malloc(o, sizeof(*attributes));
+ attributes->num_attributes = num_attr;
if (!num_attr)
- elements = (Z_AttributeElement**)odr_nullval();
- else
{
- int i, k = 0;
- Odr_int *attr_tmp;
-
- elements = (Z_AttributeElement**)
- odr_malloc (o, num_attr * sizeof(*elements));
+ attributes->attributes = (Z_AttributeElement**)odr_nullval();
+ return attributes;
+ }
+ 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; )
+ 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; j<num_attr; j++)
+ if (attr_tmp[2*j] == attr_tmp[2*i])
+ break;
+ if (j < num_attr)
+ continue;
+ elements[k] =
+ (Z_AttributeElement*)odr_malloc(o,sizeof(**elements));
+ elements[k]->attributeType = &attr_tmp[2*i];
+ elements[k]->attributeSet = attr_set[i];
+
+ if (attr_clist[i])
{
- int j;
- for (j = i+1; j<num_attr; j++)
- if (attr_tmp[2*j] == attr_tmp[2*i])
- break;
- if (j < num_attr)
- continue;
- elements[k] =
- (Z_AttributeElement*)odr_malloc(o,sizeof(**elements));
- elements[k]->attributeType = &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;
-
- zapt->term = term;
+ attributes->num_attributes = k;
+ attributes->attributes = elements;
+ return attributes;
+}
- 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);
- term_octet->buf[term_octet->size] = 0; /* null terminate */
-
- switch (li->term_type)
+Z_Term *z_Term_create(ODR o, int term_type, const char *buf, size_t len)
+{
+ Z_Term *term = (Z_Term *)odr_malloc(o, sizeof(*term));
+ switch (term_type)
{
case Z_Term_general:
term->which = Z_Term_general;
- term->u.general = term_octet;
+ term->u.general = odr_create_Odr_oct(o, buf, len);
break;
case Z_Term_characterString:
term->which = Z_Term_characterString;
- term->u.characterString = (char*) term_octet->buf;
- /* null terminated above */
+ term->u.characterString = odr_strdupn(o, buf, len);
break;
case Z_Term_numeric:
term->which = Z_Term_numeric;
- term->u.numeric = odr_intdup(o, odr_atoi((const char*) term_octet->buf));
+ term->u.numeric = odr_intdup(o, odr_atoi(odr_strdupn(o, buf, len)));
break;
case Z_Term_null:
term->which = Z_Term_null;
term->u.null = odr_nullval();
break;
}
+ return term;
+}
+
+static Z_AttributesPlusTerm *rpn_term_attributes(
+ struct yaz_pqf_parser *li, ODR o, Z_AttributeList *attributes)
+{
+ char *es_str = odr_malloc(o, li->lex_len+1);
+ int es_len = escape_string(es_str, li->lex_buf, li->lex_len);
+ Z_Term *term = z_Term_create(o, li->term_type, es_str, es_len);
+ Z_AttributesPlusTerm *zapt = (Z_AttributesPlusTerm *)
+ odr_malloc(o, sizeof(*zapt));
+
+ zapt->term = term;
+ zapt->attributes = attributes;
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,
int num_attr, Odr_int *attr_list,
char **attr_clist,
return 0;
}
zo->which = Z_Operand_resultSetId;
- zo->u.resultSetId = (char *)odr_malloc(o, li->lex_len+1);
- memcpy(zo->u.resultSetId, li->lex_buf, li->lex_len);
- zo->u.resultSetId[li->lex_len] = '\0';
+ zo->u.resultSetId = odr_strdupn(o, li->lex_buf, li->lex_len);
lex(li);
break;
default:
li->error = YAZ_PQF_ERROR_PROXIMITY;
return NULL;
}
-
+
if (!lex (li))
{
li->error = YAZ_PQF_ERROR_MISSING;
}
static Z_Complex *rpn_complex(struct yaz_pqf_parser *li, ODR o,
- int num_attr, int max_attr,
+ int num_attr, int max_attr,
Odr_int *attr_list, char **attr_clist,
Odr_oid **attr_set)
{
#endif
lex(li);
}
-
+
static Z_RPNStructure *rpn_structure(struct yaz_pqf_parser *li, ODR o,
- int num_attr, int max_attr,
+ int num_attr, int max_attr,
Odr_int *attr_list,
char **attr_clist,
Odr_oid **attr_set)
return zq;
}
+static void pqf_parser_begin(struct yaz_pqf_parser *li, const char *buf)
+{
+ li->query_buf = li->query_ptr = buf;
+ li->lex_buf = 0;
+}
+
Z_RPNQuery *p_query_rpn(ODR o, const char *qbuf)
{
struct yaz_pqf_parser li;
li.right_sep = "}\"";
li.escape_char = '@';
li.term_type = Z_Term_general;
- li.query_buf = li.query_ptr = qbuf;
- li.lex_buf = 0;
+
+ pqf_parser_begin(&li, qbuf);
return p_query_rpn_mk(o, &li);
}
-
-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)
{
int num_attr = 0;
int max_attr = 512;
Odr_oid *top_set = 0;
- Z_AttributesPlusTerm *apt;
lex(li);
if (li->query_look == 'r')
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);
{
if (!p)
return 0;
- p->query_buf = p->query_ptr = qbuf;
- p->lex_buf = 0;
+ pqf_parser_begin(p, qbuf);
return p_query_rpn_mk(o, p);
}
{
if (!p)
return 0;
- p->query_buf = p->query_ptr = qbuf;
- p->lex_buf = 0;
+ pqf_parser_begin(p, qbuf);
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;
+ pqf_parser_begin(p, qbuf);
+ return p_query_scan_attributes_mk(p, o, attributeSetP);
+}
+
+static Z_FacetField* parse_facet(ODR odr, const char *facet)
+{
+ YAZ_PQF_Parser pqf_parser = yaz_pqf_create();
+ struct yaz_pqf_parser *li = pqf_parser;
+ Odr_oid *attributeSetId;
+ Z_FacetField *facet_field = 0;
+ Z_AttributeList *attribute_list;
+
+ pqf_parser_begin(pqf_parser, facet);
+ attribute_list = p_query_scan_attributes_mk(li, odr, &attributeSetId);
+ if (attribute_list)
+ {
+ facet_field = (Z_FacetField *) odr_malloc(odr, sizeof(*facet_field));
+ facet_field->attributes = attribute_list;
+ facet_field->num_terms = 0;
+ facet_field->terms = odr_malloc(odr, 10 * sizeof(*facet_field->terms));
+ while (li->query_look == 't')
+ {
+ if (facet_field->num_terms < 10)
+ {
+ char *es_str = odr_malloc(odr, li->lex_len+1);
+ int es_len = escape_string(es_str, li->lex_buf, li->lex_len);
+ Z_Term *term = z_Term_create(odr, li->term_type, es_str, es_len);
+
+ facet_field->terms[facet_field->num_terms] =
+ (Z_FacetTerm *) odr_malloc(odr, sizeof(Z_FacetTerm));
+ facet_field->terms[facet_field->num_terms]->term = term;
+ facet_field->terms[facet_field->num_terms]->count =
+ odr_intdup(odr, 0);
+ facet_field->num_terms++;
+ }
+ lex(li);
+ }
+ }
+ yaz_pqf_destroy(pqf_parser);
+ return facet_field;
+}
+
+Z_FacetList *yaz_pqf_parse_facet_list(ODR o, const char *qbuf)
+{
+ char **darray;
+ int num;
+
+ nmem_strsplit(odr_getmem(o), ",", qbuf, &darray, &num);
+ if (num > 0)
+ {
+ int i;
+ Z_FacetList *fl = (Z_FacetList*) odr_malloc(o, sizeof(*fl));
+ fl->num = num;
+ fl->elements = (Z_FacetField **)
+ odr_malloc(o, num * sizeof(*fl->elements));
+ for (i = 0; i < num; i++)
+ {
+ fl->elements[i] = parse_facet(o, darray[i]);
+ if (!fl->elements[i])
+ return 0;
+ }
+ return fl;
+ }
+ else
+ return 0;
+}
+
int yaz_pqf_error(YAZ_PQF_Parser p, const char **msg, size_t *off)
{
switch (p->error)