Mention init command
[idzebra-moved-to-github.git] / rset / rsbool.c
index ec03558..e3a0731 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: rsbool.c,v 1.28 2004-06-02 12:31:23 adam Exp $
+/* $Id: rsbool.c,v 1.32.2.1 2005-01-23 15:06:21 adam Exp $
    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
    Index Data Aps
 
@@ -40,7 +40,8 @@ static void r_rewind (RSFD rfd);
 static int r_forward(RSET ct, RSFD rfd, void *buf, int *term_index,
                      int (*cmpfunc)(const void *p1, const void *p2),
                      const void *untilbuf);
-static int r_count (RSET ct);
+/* static void r_pos (RSFD rfd, int *current, int *total);  */
+static int r_read_and_forward (RSFD rfd, void *buf, int *term_index);
 static int r_read_and (RSFD rfd, void *buf, int *term_index);
 static int r_read_or (RSFD rfd, void *buf, int *term_index);
 static int r_read_not (RSFD rfd, void *buf, int *term_index);
@@ -54,12 +55,26 @@ static const struct rset_control control_and =
     r_close,
     r_delete,
     r_rewind,
-    r_forward, /* rset_default_forward, */
-    r_count,
+    r_forward,
+    rset_default_pos,
     r_read_and,
     r_write,
 };
 
+static const struct rset_control control_and_forward = 
+{
+    "and",
+    r_create,
+    r_open,
+    r_close,
+    r_delete,
+    r_rewind,
+    r_forward,
+    rset_default_pos,
+    r_read_and_forward,
+    r_write,
+};
+
 static const struct rset_control control_or = 
 {
     "or",
@@ -68,12 +83,8 @@ static const struct rset_control control_or =
     r_close,
     r_delete,
     r_rewind,
-#if 1
     r_forward, 
-#else
-    rset_default_forward,
-#endif
-    r_count,
+    rset_default_pos,
     r_read_or,
     r_write,
 };
@@ -87,13 +98,14 @@ static const struct rset_control control_not =
     r_delete,
     r_rewind,
     r_forward, 
-    r_count,
+    rset_default_pos,
     r_read_not,
     r_write,
 };
 
 
 const struct rset_control *rset_kind_and = &control_and;
+const struct rset_control *rset_kind_and_forward = &control_and_forward;
 const struct rset_control *rset_kind_or = &control_or;
 const struct rset_control *rset_kind_not = &control_not;
 
@@ -227,7 +239,6 @@ static int r_forward (RSET ct, RSFD rfd, void *buf, int *term_index,
 {
     struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
-    int cmp=0;
     int rc;
 
 #if RSET_DEBUG
@@ -256,37 +267,6 @@ static int r_forward (RSET ct, RSFD rfd, void *buf, int *term_index,
                        rfd, rc, p->more_l, p->more_r);
 #endif
     return rc;
-
-    if (p->more_l && p->more_r)
-           cmp = (*info->cmp)(p->buf_l, p->buf_r);
-       else if (p->more_l)
-           cmp = -2;
-       else
-           cmp = 2;
-    if ( (cmp<0) && (p->more_l) )
-    {
-        memcpy (buf, p->buf_l, info->key_size);
-           *term_index = p->term_index_l;
-#if RSET_DEBUG
-        logf (LOG_DEBUG, "rsbool_forward returning L (cmp=%d)",cmp);
-#endif
-        return 1;
-    } else if ( (cmp>0) && (p->more_r) )
-    {
-        memcpy (buf, p->buf_r, info->key_size);
-           *term_index = p->term_index_r + info->term_index_s;
-#if RSET_DEBUG
-        logf (LOG_DEBUG, "rsbool_forward returning R (cmp=%d)",cmp);
-#endif
-        return 1;
-    }
-    /* return ( p->more_l || p->more_r); */
-    return 0;
-}
-
-static int r_count (RSET ct)
-{
-    return 0;
 }
 
 
@@ -365,8 +345,6 @@ static int r_read_and (RSFD rfd, void *buf, int *term_index)
         }
         else if (cmp > 1)  /* cmp == 2 */
         {
-#define OLDCODE 0
-#if OLDCODE
             memcpy (buf, p->buf_r, info->key_size);
             *term_index = p->term_index_r + info->term_index_s;
             
@@ -383,8 +361,92 @@ static int r_read_and (RSFD rfd, void *buf, int *term_index)
 #endif
                 return 1;
             }
-#else
-            
+        }
+        else  /* cmp == -2 */
+        {
+             memcpy (buf, p->buf_l, info->key_size);
+             *term_index = p->term_index_l;
+             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
+                                    &p->term_index_l);
+             if (p->tail)
+             {
+                 if (!p->more_l || (*info->cmp)(p->buf_l, buf) > 1)
+                     p->tail = 0;
+#if RSET_DEBUG
+                 logf (LOG_DEBUG, "r_read_and [%p] returning R tail m=%d/%d c=%d",
+                        rfd, p->more_l, p->more_r, cmp);
+                (*info->log_item)(LOG_DEBUG, buf, "");
+#endif
+                 return 1;
+             }
+        }
+    }
+#if RSET_DEBUG
+    logf (LOG_DEBUG, "r_read_and [%p] reached its end",rfd);
+#endif
+    return 0;
+}
+
+static int r_read_and_forward (RSFD rfd, void *buf, int *term_index)
+{
+    struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
+    struct rset_bool_info *info = p->info;
+
+    while (p->more_l || p->more_r)
+    {
+        int cmp;
+
+        if (p->more_l && p->more_r)
+            cmp = (*info->cmp)(p->buf_l, p->buf_r);
+        else if (p->more_l)
+            cmp = -2;
+        else
+            cmp = 2;
+#if RSET_DEBUG
+        logf (LOG_DEBUG, "r_read_and [%p] looping: m=%d/%d c=%d t=%d",
+                        rfd, p->more_l, p->more_r, cmp, p->tail);
+        (*info->log_item)(LOG_DEBUG, p->buf_l, "left ");
+        (*info->log_item)(LOG_DEBUG, p->buf_r, "right ");
+#endif
+        if (!cmp)
+        {
+            memcpy (buf, p->buf_l, info->key_size);
+               *term_index = p->term_index_l;
+            p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
+                                  &p->term_index_l);
+            p->tail = 1;
+        }
+        else if (cmp == 1)
+        {
+            memcpy (buf, p->buf_r, info->key_size);
+               *term_index = p->term_index_r + info->term_index_s;
+            p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
+                                  &p->term_index_r);
+            p->tail = 1;
+#if RSET_DEBUG
+            logf (LOG_DEBUG, "r_read_and [%p] returning R m=%d/%d c=%d",
+                    rfd, p->more_l, p->more_r, cmp);
+            key_logdump(LOG_DEBUG,buf);
+           (*info->log_item)(LOG_DEBUG, buf, "");
+#endif
+            return 1;
+        }
+        else if (cmp == -1)
+        {
+            memcpy (buf, p->buf_l, info->key_size);
+               *term_index = p->term_index_l;
+            p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
+                                  &p->term_index_l);
+            p->tail = 1;
+#if RSET_DEBUG
+            logf (LOG_DEBUG, "r_read_and [%p] returning L m=%d/%d c=%d",
+                    rfd, p->more_l, p->more_r, cmp);
+           (*info->log_item)(LOG_DEBUG, buf, "");
+#endif
+            return 1;
+        }
+        else if (cmp > 1)  /* cmp == 2 */
+        {
             if (p->tail)
             {
                 memcpy (buf, p->buf_r, info->key_size);
@@ -399,7 +461,8 @@ static int r_read_and (RSFD rfd, void *buf, int *term_index)
                (*info->log_item)(LOG_DEBUG, buf, "");
 #endif
                 return 1;
-            } else
+            }
+           else
             {
 #if RSET_DEBUG
                 logf (LOG_DEBUG, "r_read_and [%p] about to forward R m=%d/%d c=%d",
@@ -411,29 +474,13 @@ static int r_read_and (RSFD rfd, void *buf, int *term_index)
                                     p->buf_r, &p->term_index_r, 
                                     (info->cmp), p->buf_l);
                 else 
+               {
                     return 0; /* no point in reading further */
+               }
             }
-#endif
         }
         else  /* cmp == -2 */
         {
-#if OLDCODE
-             memcpy (buf, p->buf_l, info->key_size);
-             *term_index = p->term_index_l;
-             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
-                                    &p->term_index_l);
-             if (p->tail)
-             {
-                 if (!p->more_l || (*info->cmp)(p->buf_l, buf) > 1)
-                     p->tail = 0;
-#if RSET_DEBUG
-                 logf (LOG_DEBUG, "r_read_and [%p] returning R tail m=%d/%d c=%d",
-                        rfd, p->more_l, p->more_r, cmp);
-                (*info->log_item)(LOG_DEBUG, buf, "");
-#endif
-                 return 1;
-             }
-#else
             if (p->tail)
             {
                 memcpy (buf, p->buf_l, info->key_size);
@@ -462,9 +509,10 @@ static int r_read_and (RSFD rfd, void *buf, int *term_index)
                                     p->buf_l, &p->term_index_l, 
                                     (info->cmp), p->buf_r);
                 else 
+               {
                     return 0; /* no point in reading further */
+               }
             }
-#endif
         }
     }
 #if RSET_DEBUG
@@ -557,15 +605,17 @@ static int r_read_not (RSFD rfd, void *buf, int *term_index)
             return 1;
         }
         else if (cmp > 1)
+       {
 #if 0
             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
                                   &p->term_index_r);
 #else
-            p->more_r = rset_forward( 
-                            info->rset_r, p->rfd_r, 
-                            p->buf_r, &p->term_index_r, 
-                            (info->cmp), p->buf_l);
+           p->more_r = rset_forward( 
+               info->rset_r, p->rfd_r, 
+               p->buf_r, &p->term_index_r, 
+               (info->cmp), p->buf_l);
 #endif
+       }
         else
         {
             memcpy (buf, p->buf_l, info->key_size);