ZOOM fix: fatal errors aren't reset.
[yaz-moved-to-github.git] / zoom / zoom-c.c
index 1da198d..b36d9b2 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: zoom-c.c,v 1.15 2001-12-30 22:21:11 adam Exp $
+ * $Id: zoom-c.c,v 1.20 2002-01-09 12:44:31 adam Exp $
  *
  * ZOOM layer for C, connections, result sets, queries.
  */
@@ -69,9 +69,21 @@ static ZOOM_Event ZOOM_connection_get_event(ZOOM_connection c)
 
 static void clear_error (ZOOM_connection c)
 {
-    c->error = ZOOM_ERROR_NONE;
-    xfree (c->addinfo);
-    c->addinfo = 0;
+
+    switch (c->error)
+    {
+    case ZOOM_ERROR_CONNECT:
+    case ZOOM_ERROR_MEMORY:
+    case ZOOM_ERROR_DECODE:
+    case ZOOM_ERROR_CONNECTION_LOST:
+    case ZOOM_ERROR_INIT:
+    case ZOOM_ERROR_INTERNAL:
+        break;
+    default:
+        c->error = ZOOM_ERROR_NONE;
+        xfree (c->addinfo);
+        c->addinfo = 0;
+    }
 }
 
 ZOOM_task ZOOM_connection_add_task (ZOOM_connection c, int which)
@@ -245,7 +257,9 @@ void ZOOM_connection_connect(ZOOM_connection c,
     ZOOM_options_set(c->options, "host", c->host_port);
 
     c->async = ZOOM_options_get_bool (c->options, "async", 0);
-    
+    c->error = ZOOM_ERROR_NONE;
+
     task = ZOOM_connection_add_task (c, ZOOM_TASK_CONNECT);
 
     if (!c->async)
@@ -446,7 +460,6 @@ static void ZOOM_resultset_retrieve (ZOOM_resultset r,
 {
     ZOOM_task task;
     ZOOM_connection c;
-    const char *cp;
 
     if (!r)
        return;
@@ -924,8 +937,23 @@ void *ZOOM_record_get (ZOOM_record rec, const char *type, size_t *len)
     {
        if (npr->which == Z_NamePlusRecord_databaseRecord)
        {
-            *len = -1;
-           return (Z_External *) npr->u.databaseRecord;
+           Z_External *r = (Z_External *) npr->u.databaseRecord;
+           
+           if (r->which == Z_External_sutrs)
+           {
+               *len = r->u.sutrs->len;
+               return r->u.sutrs->buf;
+           }
+           else if (r->which == Z_External_octet)
+           {
+               *len = r->u.octet_aligned->len;
+               return r->u.octet_aligned->buf;
+           }
+           else /* grs-1, explain, ... */
+           {
+               *len = -1;
+                return (Z_External *) npr->u.databaseRecord;
+           }
        }
        return 0;
     }
@@ -1093,7 +1121,17 @@ static int scan_response (ZOOM_connection c, Z_ScanResponse *res)
         response_diag(c, res->entries->nonsurrogateDiagnostics[0]);
     scan->scan_response = res;
     nmem_transfer (scan->odr->mem, nmem);
+    if (res->stepSize)
+        ZOOM_options_set_int (scan->options, "stepSize", *res->stepSize);
+    if (res->positionOfTerm)
+        ZOOM_options_set_int (scan->options, "position", *res->positionOfTerm);
+    if (res->scanStatus)
+        ZOOM_options_set_int (scan->options, "scanStatus", *res->scanStatus);
+    if (res->numberOfEntriesReturned)
+        ZOOM_options_set_int (scan->options, "number",
+                              *res->numberOfEntriesReturned);
     nmem_destroy (nmem);
+    return 1;
 }
 
 static int send_sort (ZOOM_connection c)
@@ -1325,7 +1363,8 @@ size_t ZOOM_scanset_size (ZOOM_scanset scan)
     return scan->scan_response->entries->num_entries;
 }
 
-const char *ZOOM_scanset_term (ZOOM_scanset scan, size_t i, int *occ, size_t *len)
+const char *ZOOM_scanset_term (ZOOM_scanset scan, size_t pos,
+                               int *occ, size_t *len)
 {
     const char *term = 0;
     size_t noent = ZOOM_scanset_size (scan);
@@ -1333,11 +1372,11 @@ const char *ZOOM_scanset_term (ZOOM_scanset scan, size_t i, int *occ, size_t *le
     
     *len = 0;
     *occ = 0;
-    if (i >= noent)
+    if (pos >= noent)
         return 0;
-    if (res->entries->entries[i]->which == Z_Entry_termInfo)
+    if (res->entries->entries[pos]->which == Z_Entry_termInfo)
     {
-        Z_TermInfo *t = res->entries->entries[i]->u.termInfo;
+        Z_TermInfo *t = res->entries->entries[pos]->u.termInfo;
         
         if (t->term->which == Z_Term_general)
         {
@@ -1349,6 +1388,17 @@ const char *ZOOM_scanset_term (ZOOM_scanset scan, size_t i, int *occ, size_t *le
     return term;
 }
 
+const char *ZOOM_scanset_option_get (ZOOM_scanset scan, const char *key)
+{
+    return ZOOM_options_get (scan->options, key);
+}
+
+void ZOOM_scanset_option_set (ZOOM_scanset scan, const char *key,
+                              const char *val)
+{
+    ZOOM_options_set (scan->options, key, val);
+}
+
 static int ZOOM_connection_exec_task (ZOOM_connection c)
 {
     ZOOM_task task = c->tasks;
@@ -1840,7 +1890,7 @@ int ZOOM_event (int no, ZOOM_connection *cs)
        }
        if (r == 0 && c->mask)
        {
-            ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_IO_TIMEOUT);
+            ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_TIMEOUT);
            /* timeout and this connection was waiting */
            c->error = ZOOM_ERROR_TIMEOUT;
             do_close (c);