Fixed NULL ptr reference caused by omitted record syntax OID. Bug
[yaz-moved-to-github.git] / ztest / ztest.c
index d45c8b1..159b2ac 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (C) 1995-2007, Index Data ApS
  * See the file LICENSE for details.
  *
- * $Id: ztest.c,v 1.87 2007-04-13 09:55:41 adam Exp $
+ * $Id: ztest.c,v 1.94 2007-06-14 10:44:18 adam Exp $
  */
 
 /*
@@ -33,6 +33,57 @@ int ztest_present(void *handle, bend_present_rr *rr);
 int ztest_esrequest(void *handle, bend_esrequest_rr *rr);
 int ztest_delete(void *handle, bend_delete_rr *rr);
 
+/** \fn get_term_hit
+    \brief use term value as hit count 
+   
+    Traverse RPN tree 'in order' and use term value as hit count.
+    Only terms  that looks a numeric is used.. Returns -1 if
+    no sub tree has a hit count term
+*/
+static int get_term_hit(Z_RPNStructure *s)
+{
+    int h = -1;
+    switch(s->which)
+    {
+    case Z_RPNStructure_simple:
+        if (s->u.simple->which == Z_Operand_APT)
+        {
+            Z_AttributesPlusTerm *apt = s->u.simple->u.attributesPlusTerm;
+            if (apt->term->which == Z_Term_general)
+            {
+                Odr_oct *oct = apt->term->u.general;
+                if (oct->len > 0 && oct->buf[0] >= '0' && oct->buf[0] <= '9')
+                    h = atoi_n((const char *) oct->buf, oct->len);
+            }
+        }
+        break;
+    case Z_RPNStructure_complex:
+        h = get_term_hit(s->u.complex->s1);
+        if (h == -1)
+            h = get_term_hit(s->u.complex->s2);
+        break;
+    }
+    return h;
+}
+
+/** \fn get_hit_count
+    \brief gets hit count for numeric terms in RPN queries
+    
+    This is just for testing.. A real database of course uses
+    the content of a database to establish a value.. In our case, we
+    have a way to trigger a certain hit count. Good for testing of
+    client applications etc
+*/
+static int get_hit_count(Z_Query *q)
+{
+    int h = -1;
+    if (q->which == Z_Query_type_1 || q->which == Z_Query_type_101)
+        h = get_term_hit(q->u.type_1->RPNStructure);
+    if (h == -1)
+        h = rand() % 24;
+    return h;
+}
+
 int ztest_search(void *handle, bend_search_rr *rr)
 {
     if (rr->num_bases != 1)
@@ -40,14 +91,6 @@ int ztest_search(void *handle, bend_search_rr *rr)
         rr->errcode = 23;
         return 0;
     }
-#if NMEM_DEBUG
-    /* if database is stop, stop this process.. For debugging only. */
-    if (!yaz_matchstr (rr->basenames[0], "stop"))
-    {
-        nmem_print_list_l(YLOG_LOG);
-        exit(0);
-    }
-#endif
     /* Throw Database unavailable if other than Default or Slow */
     if (!yaz_matchstr (rr->basenames[0], "Default"))
         ;  /* Default is OK in our test */
@@ -74,7 +117,8 @@ int ztest_search(void *handle, bend_search_rr *rr)
         rr->errstring = rr->basenames[0];
         return 0;
     }
-    rr->hits = rand() % 24;
+
+    rr->hits = get_hit_count(rr->query);
     return 0;
 }
 
@@ -138,20 +182,21 @@ int ztest_esrequest (void *handle, bend_esrequest_rr *rr)
                 if (r->direct_reference)
                 {
                     char oid_name_str[OID_STR_MAX];
-                    int oclass;
+                    oid_class oclass;
                     const char *oid_name = 
                         yaz_oid_to_string_buf(r->direct_reference,
                                           &oclass, oid_name_str);
                     if (oid_name)
                         yaz_log(log_level, "OID %s", oid_name);
-                    if (oid_name && !strcmp(oid_name, OID_STR_XML))
+                    if (!oid_oidcmp(r->direct_reference, yaz_oid_recsyn_xml))
                     {
                         yaz_log (log_level, "ILL XML request");
                         if (r->which == Z_External_octet)
                             yaz_log (log_level, "%.*s", r->u.octet_aligned->len,
                                      r->u.octet_aligned->buf); 
                     }
-                    if (oid_name && !strcmp(oid_name, OID_STR_ILL_1))
+                    if (!oid_oidcmp(r->direct_reference, 
+                                    yaz_oid_general_isoill_1))
                     {
                         yaz_log (log_level, "Decode ItemRequest begin");
                         if (r->which == ODR_EXTERNAL_single)
@@ -459,18 +504,13 @@ int ztest_present (void *handle, bend_present_rr *rr)
 int ztest_fetch(void *handle, bend_fetch_rr *r)
 {
     char *cp;
-    int oclass;
-    char oid_str_buf[OID_STR_MAX];
-    const char *oid_str = 0;
+    const Odr_oid *oid = r->request_format;
 
     r->last_in_set = 0;
     r->basename = "Default";
     r->output_format = r->request_format;
 
-    oid_str = yaz_oid_to_string_buf(r->request_format, &oclass,
-                                    oid_str_buf);
-
-    if (oid_str && !strcmp(oid_str, OID_STR_SUTRS))
+    if (oid && !oid_oidcmp(oid, yaz_oid_recsyn_sutrs))
     {
         /* this section returns a small record */
         char buf[100];
@@ -481,7 +521,7 @@ int ztest_fetch(void *handle, bend_fetch_rr *r)
         r->record = (char *) odr_malloc (r->stream, r->len+1);
         strcpy(r->record, buf);
     }
-    else if (oid_str && !strcmp(oid_str, OID_STR_GRS1))
+    else if (oid &&  !oid_oidcmp(oid, yaz_oid_recsyn_grs_1))
     {
         r->len = -1;
         r->record = (char*) dummy_grs_record(r->number, r->stream);
@@ -491,7 +531,7 @@ int ztest_fetch(void *handle, bend_fetch_rr *r)
             return 0;
         }
     }
-    else if (oid_str && !strcmp(oid_str, OID_STR_POSTSCRIPT))
+    else if (oid && !oid_oidcmp(oid, yaz_oid_recsyn_postscript))
     {
         char fname[20];
         FILE *f;
@@ -517,7 +557,7 @@ int ztest_fetch(void *handle, bend_fetch_rr *r)
         fread (r->record, size, 1, f);
         fclose (f);
     }
-    else if (oid_str && !strcmp(oid_str, OID_STR_XML))
+    else if (oid && !oid_oidcmp(oid, yaz_oid_recsyn_xml))
     {
         if ((cp = dummy_xml_record (r->number, r->stream)))
         {
@@ -535,8 +575,7 @@ int ztest_fetch(void *handle, bend_fetch_rr *r)
     {
         r->len = strlen(cp);
         r->record = cp;
-        r->output_format = yaz_string_to_oid_odr(
-            yaz_oid_std(), CLASS_RECSYN, OID_STR_USMARC, r->stream);
+        r->output_format = odr_oiddup(r->stream, yaz_oid_recsyn_usmarc);
     }
     else
     {
@@ -721,6 +760,9 @@ bend_initresult *bend_init(bend_initrequest *q)
     q->bend_srw_scan = ztest_scan;
     q->bend_srw_update = ztest_update;
 
+    q->query_charset = "ISO-8859-1";
+    q->records_in_same_charset = 0;
+
     return r;
 }