Reformat
[yaz-moved-to-github.git] / src / pquery.c
index 41a369d..8db6ac6 100644 (file)
@@ -1,5 +1,5 @@
 /* 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.
  */
 /**
@@ -13,7 +13,6 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
-#include <ctype.h>
 
 #include <yaz/proto.h>
 #include <yaz/oid_db.h>
@@ -77,7 +76,7 @@ static int query_token(struct yaz_pqf_parser *li)
     }
     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);
@@ -85,7 +84,7 @@ static int query_token(struct yaz_pqf_parser *li)
     }
     while (**qptr && **qptr != sep_char)
     {
-        if (**qptr == '\\')
+        if (**qptr == '\\' && (*qptr)[1])
         {
             ++(li->lex_len);
             ++(*qptr);
@@ -244,20 +243,22 @@ int p_query_parse_attr(struct yaz_pqf_parser *li, ODR o,
     return 1;
 }
 
-Z_AttributeList *get_attributeList(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)
 {
     int i, k = 0;
     Odr_int *attr_tmp;
     Z_AttributeElement **elements;
-    Z_AttributeList *attributes= (Z_AttributeList *) odr_malloc(o, sizeof(*attributes));
+    Z_AttributeList *attributes= (Z_AttributeList *)
+        odr_malloc(o, sizeof(*attributes));
     attributes->num_attributes = num_attr;
-    if (!num_attr) {
+    if (!num_attr)
+    {
         attributes->attributes = (Z_AttributeElement**)odr_nullval();
         return attributes;
     }
-    elements = (Z_AttributeElement**) odr_malloc (o, num_attr * sizeof(*elements));
+    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));
@@ -305,23 +306,16 @@ Z_AttributeList *get_attributeList(ODR o,
     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;
@@ -349,8 +343,21 @@ static Z_AttributesPlusTerm *rpn_term_attributes(struct yaz_pqf_parser *li, ODR
         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,
@@ -678,6 +685,12 @@ static Z_RPNQuery *p_query_rpn_mk(ODR o, struct yaz_pqf_parser *li)
     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;
@@ -687,12 +700,11 @@ Z_RPNQuery *p_query_rpn(ODR o, const char *qbuf)
     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_AttributeList *p_query_scan_attributes_mk(struct yaz_pqf_parser *li,
                                              ODR o,
                                              Odr_oid **attributeSetP)
@@ -755,8 +767,8 @@ static Z_AttributeList *p_query_scan_attributes_mk(struct yaz_pqf_parser *li,
 }
 
 static Z_AttributesPlusTerm *p_query_scan_mk(struct yaz_pqf_parser *li,
-                                                 ODR o,
-                                                 Odr_oid **attributeSetP)
+                                             ODR o,
+                                             Odr_oid **attributeSetP)
 {
     Z_AttributeList *attr_list = p_query_scan_attributes_mk(li, o, attributeSetP);
     Z_AttributesPlusTerm *apt;
@@ -800,8 +812,7 @@ Z_RPNQuery *yaz_pqf_parse(YAZ_PQF_Parser p, ODR o, const char *qbuf)
 {
     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);
 }
 
@@ -811,90 +822,83 @@ Z_AttributesPlusTerm *yaz_pqf_scan(YAZ_PQF_Parser p, ODR o,
 {
     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)
+                                             Odr_oid **attributeSetP,
+                                             const char *qbuf)
 {
     if (!p)
         return 0;
-    p->query_buf = p->query_ptr = qbuf;
-    p->lex_buf = 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, int length)
+static Z_FacetField* parse_facet(ODR odr, const char *facet)
 {
     YAZ_PQF_Parser pqf_parser = yaz_pqf_create();
-    char *buffer = odr_strdupn(odr, facet, length);
+    struct yaz_pqf_parser *li = pqf_parser;
     Odr_oid *attributeSetId;
     Z_FacetField *facet_field = 0;
-    Z_AttributeList *attribute_list =
-        yaz_pqf_scan_attribute_list(pqf_parser, odr, &attributeSetId, buffer);
-    
+    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 = odr_malloc(odr, sizeof(*facet_field));
+        facet_field = (Z_FacetField *) odr_malloc(odr, sizeof(*facet_field));
         facet_field->attributes = attribute_list;
         facet_field->num_terms = 0;
-        facet_field->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;
 }
 
-#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;
+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;
         }
-        facet = pos + 1;
+        return fl;
     }
-    return facet_list;
+    else
+        return 0;
 }
 
-
-
 int yaz_pqf_error(YAZ_PQF_Parser p, const char **msg, size_t *off)
 {
     switch (p->error)