home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0010 - 0019 / ibm0010-0019 / ibm0010.tar / ibm0010 / CODE4-1.ZIP / SOURCE.ZIP / I4SKIP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-14  |  3.7 KB  |  162 lines

  1.  
  2. /* i4skip.c & i4keycmp     (c)Copyright Sequiter Software Inc., 1987
  3.  
  4.    Moves the index file pointer a specified number of keys.
  5. */
  6.  
  7. #include "d4base.h"
  8. #include "u4error.h"
  9.  
  10. #include <string.h>
  11.  
  12. extern    INDEX    *v4index ;
  13. extern    BLOCK    *v4block ;
  14.  
  15. i4keycmp( index_ref, one, two )
  16. int index_ref ;
  17. char *one, *two ;
  18. {
  19.    #ifdef CLIPPER
  20.       return( memcmp(one, two, v4index[index_ref].key_len ) ) ;
  21.    #else
  22.       INDEX *index_ptr ;
  23.       index_ptr = v4index+index_ref ;
  24.  
  25.       if ( index_ptr->int_or_date )
  26.       {
  27.      if (  *((double *)one) < *((double *)two) )  return( -1 ) ;
  28.      if (  *((double *)one) > *((double *)two) )  return(  1 ) ;
  29.      return( 0 ) ;
  30.       }
  31.       else
  32.      return( memcmp(one, two, index_ptr->key_len ) ) ;
  33.    #endif
  34. }
  35.  
  36.  
  37. long i4skip( index_ref, num_skip )
  38. int  index_ref ;
  39. long num_skip ;
  40. {
  41.    INDEX *index_ptr ;
  42.    BLOCK *block_ptr ;
  43.    int      sign, rc ;
  44.    long   num_left ;
  45.  
  46.    index_ptr =    v4index + index_ref ;
  47.    if ( i4lock( index_ref, 1 ) < 0 )  return -1 ;
  48.  
  49.    num_left  =    num_skip ;
  50.    if ( num_skip < 0)
  51.     sign = -1 ;
  52.    else
  53.     sign =    1 ;
  54.  
  55.    rc = 0 ;
  56.  
  57.    if ( index_ptr->block_ref < 0)
  58.     rc =  i4top( index_ref ) ;
  59.    else
  60.    {
  61.       block_ptr =  v4block+  index_ptr->block_ref ;
  62.       if ( block_ptr->key_on >= block_ptr->num_keys )
  63.      rc =  i4bottom( index_ref ) ;     /* EOF */
  64.       #ifndef CLIPPER
  65.       else if ( ! b4leaf(index_ref) )
  66.      rc =  i4top( index_ref ) ;   /* BOF -    Not a leaf block */
  67.       #endif
  68.    }
  69.    if ( rc < 0) return( -num_skip ) ;     /* Error */
  70.    if ( rc == 3 )  return( 0L ) ;
  71.  
  72.    while (1)
  73.    {
  74.       #ifdef CLIPPER
  75.      /* First Skip Over Current Key */
  76.      if ( num_left == 0 )  return( num_skip ) ;  /* Successfully skipped */
  77.  
  78.      if ( b4leaf(index_ref) )
  79.      {
  80.         num_left -=  b4skip( index_ref, num_left ) ;
  81.         if ( num_left == 0 )  return( num_skip ) ;
  82.  
  83.         while (1) /* Loop needed for when (sign<0) */
  84.         {
  85.            rc =  b4up( index_ref ) ;
  86.            if ( rc == -2)
  87.            {
  88.           u4error( E_INTERNAL, "i4skip", (char *) 0 ) ;
  89.           return( -num_skip ) ; /* Error */
  90.            }
  91.            block_ptr =  v4block+ index_ptr->block_ref ;
  92.            if ( rc == -1)
  93.            {
  94.           block_ptr->key_on += sign ;
  95.           if ( block_ptr->key_on < 0 )    block_ptr->key_on =  0 ;
  96.           return( num_skip - num_left ) ;
  97.            }
  98.            /* rc == 0 */
  99.            if ( sign > 0 )
  100.            {
  101.           if ( block_ptr->key_on < block_ptr->num_keys )
  102.           {
  103.              num_left -- ;
  104.              break ;
  105.           }
  106.            }
  107.            else
  108.            {
  109.           if ( --block_ptr->key_on >= 0 )
  110.           {
  111.              num_left ++ ;
  112.              break ;
  113.           }
  114.            }
  115.         }
  116.      }
  117.      else
  118.      {
  119.         block_ptr =  v4block+ index_ptr->block_ref ;
  120.         if ( sign > 0 )
  121.            if ( ++block_ptr->key_on > block_ptr->num_keys ) /* EOF */
  122.           return( num_skip - num_left ) ;
  123.  
  124.         num_left -= sign ;
  125.         for ( rc = 0;  rc >= 0;  rc =  b4down(index_ref, -sign) ) ;
  126.         if (rc <= -2)  return( -num_skip ) ;  /* Error */
  127.      }
  128.       #else
  129.      /* Move to a leaf in the index tree. */
  130.      rc = 0 ;
  131.      while ( rc >= 0 )
  132.      {
  133.         rc =  b4down( index_ref, -sign ) ;
  134.         if (rc == -2)  return( -num_skip ) ;  /* Error */
  135.      }
  136.  
  137.      num_left -= b4skip( index_ref, num_left ) ;
  138.      if ( num_left == 0)  return( num_skip ) ;  /* Successfully skipped */
  139.  
  140.      /* now skip one to the next leaf block */
  141.      do
  142.      {
  143.         rc = b4up( index_ref ) ;
  144.         if ( rc == -2)
  145.         {
  146.            u4error( E_INTERNAL, "i4skip", (char *) 0 ) ;
  147.            return( -num_skip ) ; /* Error */
  148.         }
  149.  
  150.         if ( rc == -1)
  151.         {
  152.            if ( num_skip > 0 )
  153.           v4block[index_ptr->block_ref].key_on ++ ; /* To indicate EOF */
  154.            return( num_skip - num_left ) ;
  155.         }
  156.      }  while ( b4skip( index_ref, (long) sign) != (long) sign) ;
  157.  
  158.      num_left -= sign ;
  159.       #endif
  160.    }
  161. }
  162.