Happy new year
[yaz-moved-to-github.git] / src / pquery.c
index a7ba2a2..1a45b40 100644 (file)
@@ -1,11 +1,15 @@
 /* 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>
@@ -826,24 +830,20 @@ Z_AttributeList *yaz_pqf_scan_attribute_list(YAZ_PQF_Parser p, 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;
 }
 
@@ -875,6 +875,7 @@ Z_FacetList *yaz_pqf_parse_facet_list(ODR odr, const char *facet) {
     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)
@@ -883,11 +884,12 @@ Z_FacetList *yaz_pqf_parse_facet_list(ODR odr, const char *facet) {
         if (elements[index]) {
             index++;
         }
-        else
+        else {
             num_elements--;
+            facet_list->num = num_elements;
+        }
         facet = pos + 1;
     }
-    facet_list->elements = elements;
     return facet_list;
 }