Added a few comments. Source in ztest also analyzed by Doxygen.
[yaz-moved-to-github.git] / ztest / ztest.c
index 541ed73..1a0579f 100644 (file)
@@ -1,12 +1,10 @@
-/*
- * Copyright (C) 1995-2007, Index Data ApS
+/* This file is part of the YAZ toolkit.
+ * Copyright (C) 1995-2008 Index Data
  * See the file LICENSE for details.
- *
- * $Id: ztest.c,v 1.93 2007-06-11 08:50:57 adam Exp $
  */
 
-/*
- * Demonstration of simple server
+/** \file
+ * \brief Demonstration of server
  */
 
 #include <stdio.h>
 #include <yaz/log.h>
 #include <yaz/backend.h>
 #include <yaz/ill.h>
+#include <yaz/diagbib1.h>
+
+#include "ztest.h"
 
 static int log_level=0;
 static int log_level_set=0;
 
-Z_GenericRecord *dummy_grs_record (int num, ODR o);
-char *dummy_marc_record (int num, ODR odr);
-char *dummy_xml_record (int num, ODR odr);
-
 int ztest_search(void *handle, bend_search_rr *rr);
 int ztest_sort(void *handle, bend_sort_rr *rr);
 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 
+/** \brief use term value as hit count 
+    \param s RPN structure
+    \return >= 0: search term number or -1: not found
    
     Traverse RPN tree 'in order' and use term value as hit count.
     Only terms  that looks a numeric is used.. Returns -1 if
@@ -66,8 +64,9 @@ static int get_term_hit(Z_RPNStructure *s)
     return h;
 }
 
-/** \fn get_hit_count
-    \brief gets hit count for numeric terms in RPN queries
+/** \brief gets hit count for numeric terms in RPN queries
+    \param q RPN Query
+    \return number of hits (random or number for term)
     
     This is just for testing.. A real database of course uses
     the content of a database to establish a value.. In our case, we
@@ -88,7 +87,7 @@ int ztest_search(void *handle, bend_search_rr *rr)
 {
     if (rr->num_bases != 1)
     {
-        rr->errcode = 23;
+        rr->errcode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
         return 0;
     }
     /* Throw Database unavailable if other than Default or Slow */
@@ -113,7 +112,7 @@ int ztest_search(void *handle, bend_search_rr *rr)
     }
     else
     {
-        rr->errcode = 109;
+        rr->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
         rr->errstring = rr->basenames[0];
         return 0;
     }
@@ -153,6 +152,7 @@ int ztest_esrequest (void *handle, bend_esrequest_rr *rr)
             Z_IORequest *ir = it->u.esRequest;
             Z_IOOriginPartToKeep *k = ir->toKeep;
             Z_IOOriginPartNotToKeep *n = ir->notToKeep;
+            const char *xml_in_response = 0;
             
             if (k && k->contact)
             {
@@ -192,8 +192,10 @@ int ztest_esrequest (void *handle, bend_esrequest_rr *rr)
                     {
                         yaz_log (log_level, "ILL XML request");
                         if (r->which == Z_External_octet)
-                            yaz_log (log_level, "%.*s", r->u.octet_aligned->len,
+                            yaz_log (log_level, "%.*s",
+                                     r->u.octet_aligned->len,
                                      r->u.octet_aligned->buf); 
+                        xml_in_response = "<dummy>x</dummy>";
                     }
                     if (!oid_oidcmp(r->direct_reference, 
                                     yaz_oid_general_isoill_1))
@@ -300,7 +302,13 @@ int ztest_esrequest (void *handle, bend_esrequest_rr *rr)
                 ext->u.itemOrder->u.taskPackage->originPart = k;
                 ext->u.itemOrder->u.taskPackage->targetPart = targetPart;
 
-                targetPart->itemRequest = 0;
+                if (xml_in_response)
+                    targetPart->itemRequest =
+                        z_ext_record_xml(rr->stream, xml_in_response,
+                                         strlen(xml_in_response));
+                else
+                    targetPart->itemRequest = 0;
+                    
                 targetPart->statusOrErrorReport = 0;
                 targetPart->auxiliaryStatus = 0;
             }
@@ -345,7 +353,7 @@ int ztest_esrequest (void *handle, bend_esrequest_rr *rr)
                 yaz_log (log_level, "database: %s", toKeep->databaseName);
                 if (!strcmp(toKeep->databaseName, "fault"))
                 {
-                    rr->errcode = 109;
+                    rr->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
                     rr->errstring = toKeep->databaseName;
                 }
                 if (!strcmp(toKeep->databaseName, "accept"))
@@ -504,18 +512,84 @@ int ztest_present (void *handle, bend_present_rr *rr)
 int ztest_fetch(void *handle, bend_fetch_rr *r)
 {
     char *cp;
-    oid_class 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(oid, &oclass, oid_str_buf);
-    
-    if (oid && !oid_oidcmp(oid, yaz_oid_recsyn_sutrs))
+    if (!oid || yaz_oid_is_iso2709(oid))
+    {
+        cp = dummy_marc_record(r->number, r->stream);
+        if (!cp)
+        {
+            r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
+            return 0;
+        }
+        else
+        {
+            r->len = strlen(cp);
+            r->record = cp;
+            r->output_format = odr_oiddup(r->stream, yaz_oid_recsyn_usmarc);
+        }
+    }
+    else if (!oid_oidcmp(oid, yaz_oid_recsyn_opac))
+    {
+        Z_OPACRecord *rec;
+        int i;
+        cp = dummy_marc_record(r->number, r->stream);
+        if (!cp)
+        {
+            r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
+            return 0;
+        }
+        rec = odr_malloc(r->stream, sizeof(*rec));
+        rec->bibliographicRecord =
+            z_ext_record_usmarc(r->stream, cp, strlen(cp));
+        rec->num_holdingsData = 1;
+        rec->holdingsData = odr_malloc(r->stream, sizeof(*rec->holdingsData));
+        for (i = 0; i < rec->num_holdingsData; i++)
+        {
+            Z_HoldingsRecord *hr = odr_malloc(r->stream, sizeof(*hr));
+            Z_HoldingsAndCircData *hc = odr_malloc(r->stream, sizeof(*hc));
+
+            rec->holdingsData[i] = hr;
+            hr->which = Z_HoldingsRecord_holdingsAndCirc;
+            hr->u.holdingsAndCirc = hc;
+            
+            hc->typeOfRecord = odr_strdup(r->stream, "x");
+            hc->typeOfRecord[0] = cp[5]; /* LDR 6 */
+
+            hc->encodingLevel = odr_strdup(r->stream, "x");
+            hc->encodingLevel[0] = cp[16]; /* LDR 17 */
+
+            hc->format = 0; /* OPT */
+            hc->receiptAcqStatus = 0; /* OPT */
+            hc->generalRetention = 0; /* OPT */
+            hc->completeness = 0; /* OPT */
+            hc->dateOfReport = 0; /* OPT */
+            hc->nucCode = 0; /* OPT */
+            hc->localLocation = 0; /* OPT */
+            hc->shelvingLocation = 0; /* OPT */
+            hc->callNumber = 0; /* OPT */
+            hc->shelvingData = 0; /* OPT */
+            hc->copyNumber = 0; /* OPT */
+            hc->publicNote = 0; /* OPT */
+            hc->reproductionNote = 0; /* OPT */
+            hc->termsUseRepro = 0; /* OPT */
+            hc->enumAndChron = 0; /* OPT */
+
+            hc->num_volumes = 0;
+            hc->volumes = 0;
+
+            hc->num_circulationData = 0;
+            hc->circulationData = 0;
+        }
+
+        r->len = -1;
+        r->record = (char*) rec;
+    }
+    else if (!oid_oidcmp(oid, yaz_oid_recsyn_sutrs))
     {
         /* this section returns a small record */
         char buf[100];
@@ -526,17 +600,17 @@ 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 &&  !oid_oidcmp(oid, yaz_oid_recsyn_grs_1))
+    else if (!oid_oidcmp(oid, yaz_oid_recsyn_grs_1))
     {
         r->len = -1;
         r->record = (char*) dummy_grs_record(r->number, r->stream);
         if (!r->record)
         {
-            r->errcode = 13;
+            r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
             return 0;
         }
     }
-    else if (oid && !oid_oidcmp(oid, yaz_oid_recsyn_postscript))
+    else if (!oid_oidcmp(oid, yaz_oid_recsyn_postscript))
     {
         char fname[20];
         FILE *f;
@@ -546,14 +620,14 @@ int ztest_fetch(void *handle, bend_fetch_rr *r)
         f = fopen(fname, "rb");
         if (!f)
         {
-            r->errcode = 13;
+            r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
             return 0;
         }
         fseek (f, 0L, SEEK_END);
         size = ftell (f);
         if (size <= 0 || size >= 5000000)
         {
-            r->errcode = 14;
+            r->errcode = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
             return 0;
         }
         fseek (f, 0L, SEEK_SET);
@@ -562,7 +636,7 @@ int ztest_fetch(void *handle, bend_fetch_rr *r)
         fread (r->record, size, 1, f);
         fclose (f);
     }
-    else if (oid && !oid_oidcmp(oid, yaz_oid_recsyn_xml))
+    else if (!oid_oidcmp(oid, yaz_oid_recsyn_xml))
     {
         if ((cp = dummy_xml_record (r->number, r->stream)))
         {
@@ -571,20 +645,16 @@ int ztest_fetch(void *handle, bend_fetch_rr *r)
         }
         else 
         {
-            r->errcode = 14;
+            r->errcode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
             r->surrogate_flag = 1;
             return 0;
         }
     }
-    else if ((cp = dummy_marc_record(r->number, r->stream)))
-    {
-        r->len = strlen(cp);
-        r->record = cp;
-        r->output_format = odr_oiddup(r->stream, yaz_oid_recsyn_usmarc);
-    }
     else
     {
-        r->errcode = 13;
+        char buf[OID_STR_MAX];
+        r->errcode = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
+        r->errstring = odr_strdup(r->stream, oid_oid_to_dotstring(oid, buf));
         return 0;
     }
     r->errcode = 0;
@@ -617,7 +687,7 @@ int ztest_scan(void *handle, bend_scan_rr *q)
     }
     else
     {
-        q->errcode = 109;
+        q->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
         q->errstring = q->basenames[0];
         return 0;
     }
@@ -633,7 +703,7 @@ int ztest_scan(void *handle, bend_scan_rr *q)
     }
     if (q->num_entries > 200)
     {
-        q->errcode = 31;
+        q->errcode = YAZ_BIB1_RESOURCES_EXHAUSTED_NO_RESULTS_AVAILABLE;
         return 0;
     }
     if (q->term)
@@ -641,12 +711,12 @@ int ztest_scan(void *handle, bend_scan_rr *q)
         int len;
         if (q->term->term->which != Z_Term_general)
         {
-            q->errcode = 229; /* unsupported term type */
+            q->errcode = YAZ_BIB1_TERM_TYPE_UNSUPP;
             return 0;
         }
         if (*q->step_size != 0)
         {
-            q->errcode = 205; /*Only zero step size supported for Scan */
+            q->errcode = YAZ_BIB1_ONLY_ZERO_STEP_SIZE_SUPPORTED_FOR_SCAN;
             return 0;
         }
         len = q->term->term->u.general->len;
@@ -692,7 +762,8 @@ int ztest_scan(void *handle, bend_scan_rr *q)
                 {
                     list[q->num_entries].term = entries[pos];
                     list[q->num_entries].occurrences = -1;
-                    list[q->num_entries].errcode = 233;
+                    list[q->num_entries].errcode =
+                        YAZ_BIB1_SCAN_UNSUPP_VALUE_OF_POSITION_IN_RESPONSE;
                     list[q->num_entries].errstring = "SD for Scan Term";
                 }
                 else