Added isamb_dump utility.
[idzebra-moved-to-github.git] / isamb / isamb.c
index 1695151..1bf6389 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: isamb.c,v 1.33 2004-06-01 18:04:41 adam Exp $
+/* $Id: isamb.c,v 1.36 2004-06-02 12:30:32 adam Exp $
    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
    Index Data Aps
 
@@ -326,8 +326,7 @@ void isamb_close (ISAMB isamb)
     xfree (isamb);
 }
 
-
-struct ISAMB_block *open_block (ISAMB b, ISAMC_P pos)
+static struct ISAMB_block *open_block (ISAMB b, ISAMC_P pos)
 {
     int cat = pos&CAT_MASK;
     struct ISAMB_block *p;
@@ -1008,6 +1007,63 @@ void isamb_pp_close (ISAMB_PP pp)
     isamb_pp_close_x (pp, 0, 0);
 }
 
+/* simple recursive dumper .. */
+static void isamb_dump_r (ISAMB b, ISAMB_P pos, void (*pr)(const char *str),
+                         int level)
+{
+    char buf[1024];
+    char prefix_str[1024];
+    if (pos)
+    {
+       struct ISAMB_block *p = open_block (b, pos);
+       sprintf(prefix_str, "%*s %d cat=%d size=%d max=%d", level*2, "",
+               pos, p->cat, p->size, b->file[p->cat].head.block_max);
+       (*pr)(prefix_str);
+       sprintf(prefix_str, "%*s %d", level*2, "", pos);
+       if (p->leaf)
+       {
+           while (p->offset < p->size)
+           {
+               char *src = p->bytes + p->offset;
+               char *dst = buf;
+               (*b->method->code_item)(ISAMC_DECODE, p->decodeClientData,
+                                       &dst, &src);
+               (*b->method->log_item)(LOG_DEBUG, buf, prefix_str);
+               p->offset = src - (char*) p->bytes;
+           }
+           assert(p->offset == p->size);
+       }
+       else
+       {
+           char *src = p->bytes + p->offset;
+           int sub;
+           int item_len;
+
+           decode_ptr (&src, &sub);
+           p->offset = src - (char*) p->bytes;
+
+           isamb_dump_r(b, sub, pr, level+1);
+           
+           while (p->offset < p->size)
+           {
+               decode_ptr (&src, &item_len);
+               (*b->method->log_item)(LOG_DEBUG, src, prefix_str);
+               src += item_len;
+               decode_ptr (&src, &sub);
+               
+               p->offset = src - (char*) p->bytes;
+               
+               isamb_dump_r(b, sub, pr, level+1);
+           }           
+       }
+       close_block(b,p);
+    }
+}
+
+void isamb_dump (ISAMB b, ISAMB_P pos, void (*pr)(const char *str))
+{
+    return isamb_dump_r(b, pos, pr, 0);
+}
 
 #if 0
 /* Old isamb_pp_read that Adam wrote, kept as a reference in case we need to
@@ -1120,6 +1176,7 @@ int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilbuf)
     {
         while ( (p->offset == p->size) && !descending )
         {  /* end of this block - climb higher */
+           assert (p->offset <= p->size);
 #if ISAMB_DEBUG
             logf(LOG_DEBUG,"isamb_pp_forward climbing from l=%d",
                             pp->level);
@@ -1140,6 +1197,7 @@ int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilbuf)
                             p->pos, p->offset);
 #endif
             assert(!p->leaf);
+           assert(p->offset <= p->size);
             /* skip the child we have handled */
             if (p->offset != p->size)
             { 
@@ -1153,21 +1211,23 @@ int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilbuf)
                 src += item_len;
                 decode_ptr(&src,&pos);
                 p->offset = src - (char*) p->bytes;
-                break; /* even if this puts us at the end of the block, we need to */
-                       /* descend to the last pos. UGLY coding, clean up some */
-                       /* day */
+                break; /* even if this puts us at the end of the block, we
+                         need to descend to the last pos. UGLY coding,
+                         clean up some day */
             }
         }
         if (!p->leaf)
         { 
             src = p->bytes + p->offset;
             if (p->offset == p->size)
-                cmp=-2 ; /* descend to the last node, as we have no value to cmp */
+                cmp=-2 ; /* descend to the last node, as we have
+                           no value to cmp */
             else
             {
                 decode_ptr(&src, &item_len);
 #if ISAMB_DEBUG
-                logf(LOG_DEBUG,"isamb_pp_forward (B) on a high node. ofs=%d sz=%d nxtpos=%d ",
+                logf(LOG_DEBUG,"isamb_pp_forward (B) on a high node. "
+                    "ofs=%d sz=%d nxtpos=%d ",
                         p->offset,p->size,pos);
                (*pp->isamb->method->log_item)(LOG_DEBUG, src, "");
 #endif
@@ -1209,7 +1269,8 @@ int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilbuf)
                 (pp->skipped_nodes[pp->maxlevel - pp->level -1])++;
 #if ISAMB_DEBUG
                 logf(LOG_DEBUG,
-                    "isamb_pp_forward: skipping block on level %d, noting on %d (%d)",
+                    "isamb_pp_forward: skipping block on level %d, noting "
+                    "on %d (%d)",
                     pp->level, pp->maxlevel - pp->level-1 , 
                     pp->skipped_nodes[pp->maxlevel - pp->level-1 ]);
 #endif
@@ -1219,39 +1280,46 @@ int isamb_pp_forward (ISAMB_PP pp, void *buf, const void *untilbuf)
         } /* not on a leaf */
         else
         { /* on a leaf */
-            src = p->bytes + p->offset;
-            dst=buf;
-            (*pp->isamb->method->code_item)(ISAMC_DECODE, p->decodeClientData,
-                                            &dst, &src);
-            p->offset = src - (char*) p->bytes;
-            if (untilbuf)
-                cmp=(*pp->isamb->method->compare_item)(untilbuf,buf);
-            else
-                cmp=-2;
+           if (p->offset == p->size) { 
+               descending = 0;
+           }
+           else
+           {
+               assert (p->offset < p->size);
+               src = p->bytes + p->offset;
+               dst=buf;
+               (*pp->isamb->method->code_item)(ISAMC_DECODE, p->decodeClientData,
+                                               &dst, &src);
+               p->offset = src - (char*) p->bytes;
+               if (untilbuf)
+                   cmp=(*pp->isamb->method->compare_item)(untilbuf,buf);
+               else
+                   cmp=-2;
 #if ISAMB_DEBUG
-            logf(LOG_DEBUG,"isamb_pp_forward on a leaf. cmp=%d", 
-                              cmp);
-           (*pp->isamb->method->log_item)(LOG_DEBUG, buf, "");
+               logf(LOG_DEBUG,"isamb_pp_forward on a leaf. cmp=%d", 
+                    cmp);
+               (*pp->isamb->method->log_item)(LOG_DEBUG, buf, "");
 #endif
-            if (cmp <2)
-            {
-#if ISAMB_DEBUG
-                if (untilbuf)
+               if (cmp <2)
                {
-                   (*pp->isamb->method->log_item)(LOG_DEBUG, buf, 
-                                                  "isamb_pp_forward returning 1");
-               }
-                else
-               {
-                   (*pp->isamb->method->log_item)(LOG_DEBUG, buf, 
-                                                  "isamb_pp_read returning 1 (fwd)");
-               }
+#if ISAMB_DEBUG
+                   if (untilbuf)
+                   {
+                       (*pp->isamb->method->log_item)(
+                           LOG_DEBUG, buf,  "isamb_pp_forward returning 1");
+                   }
+                   else
+                   {
+                       (*pp->isamb->method->log_item)(
+                           LOG_DEBUG, buf, "isamb_pp_read returning 1 (fwd)");
+                   }
 #endif
-                pp->returned_numbers++;
-                return 1;
-            }
-            else
-                pp->skipped_numbers++;
+                   pp->returned_numbers++;
+                   return 1;
+               }
+               else
+                   pp->skipped_numbers++;
+           }
         } /* leaf */
     } /* main loop */
 }