/* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2010 Index Data
+ * Copyright (C) 1995-2011 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>
}
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);
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);
+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));
+ Odr_oct *term_octet = (Odr_oct *)odr_malloc(o, sizeof(*term_octet));
+ term_octet->buf = (unsigned char *)odr_malloc(o, 1 + len);
+ memcpy(term_octet->buf, buf, len);
+ term_octet->size = term_octet->len = len;
term_octet->buf[term_octet->size] = 0; /* null terminate */
- switch (li->term_type)
+ switch (term_type)
{
case Z_Term_general:
term->which = Z_Term_general;
term->u.null = odr_nullval();
break;
}
- return zapt;
+ 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,
static Z_FacetField* parse_facet(ODR odr, const char *facet, int length)
{
YAZ_PQF_Parser pqf_parser = yaz_pqf_create();
- char buffer[length+1];
+ char *buffer = odr_strdupn(odr, facet, length);
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;
+ 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;
}
- 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);
-
+ yaz_pqf_destroy(pqf_parser);
return facet_field;
}
-#define FACET_DElIMITER ','
+#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)
+ if (arg[index] == FACET_DELIMITER)
count++;
}
return count;
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);
+ 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
+ else {
num_elements--;
+ facet_list->num = num_elements;
+ }
facet = pos + 1;
}
- facet_list->elements = elements;
return facet_list;
}