home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c160 / 1.ddi / SOURCE / I4SKIP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-04  |  4.5 KB  |  183 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 "d4all.h"
  8. #include "u4error.h"
  9. #include "p4misc.h"
  10.  
  11. #include <string.h>
  12.  
  13. extern    INDEX    *v4index ;
  14. extern    BLOCK    *v4block ;
  15. extern  int      v4lock_wait ;
  16.  
  17. i4keycmp( int index_ref, char *one, char *two )
  18. {
  19.    #ifdef CLIPPER
  20.       #ifdef LANGUAGE
  21.          return( u4memcmp( (unsigned char *) one, 
  22.                            (unsigned char *) two, (size_t) v4index[index_ref].key_len)) ;
  23.       #else
  24.          return( memcmp(one, two, (size_t) v4index[index_ref].key_len ) ) ;
  25.       #endif
  26.    #else
  27.       INDEX *index_ptr ;
  28.       double dif ;
  29.  
  30.       #ifdef PORTABLE
  31.          double d1,d2 ;
  32.       #endif
  33.  
  34.       index_ptr = v4index+index_ref ;
  35.  
  36.       if ( index_ptr->int_or_date )
  37.       {
  38.          #ifdef PORTABLE
  39.             memcpy( &d1, (double *) one, sizeof(double) ) ;
  40.             memcpy( &d2, (double *) two, sizeof(double) ) ;
  41.             dif =  d1 - d2 ;
  42.          #else
  43.             dif =  *((double *)one) - *((double *)two) ;
  44.          #endif
  45.          if ( dif >  1.0e-13 )  return  1 ;
  46.          if ( dif < -1.0e-13)  return -1 ;
  47.          return 0 ;
  48.       }
  49.       else
  50.          #ifdef LANGUAGE
  51.         return( u4memcmp( (unsigned char *) one, (unsigned char *) two, 
  52.                     (size_t) index_ptr->key_len ) ) ;
  53.          #else
  54.         return( memcmp(one, two, (size_t) index_ptr->key_len ) ) ;
  55.          #endif
  56.    #endif
  57. }
  58.  
  59.  
  60. long i4skip( int index_ref, long num_skip )
  61. {
  62.    INDEX *index_ptr ;
  63.    BLOCK *block_ptr ;
  64.    int      sign, rc ;
  65.    long   num_left ;
  66.  
  67.    index_ptr =    v4index + index_ref ;
  68.    if ( (rc = i4lock( index_ref, v4lock_wait)) < 0 )  return -num_skip ;
  69.  
  70.    num_left  =    num_skip ;
  71.    if ( num_skip < 0)
  72.     sign = -1 ;
  73.    else
  74.     sign =    1 ;
  75.  
  76.    rc = 0 ;
  77.  
  78.    if ( index_ptr->block_ref < 0)
  79.     rc =  i4top( index_ref ) ;
  80.    else
  81.    {
  82.       block_ptr =  v4block+  index_ptr->block_ref ;
  83.       if ( block_ptr->key_on >= block_ptr->num_keys )
  84.      rc =  i4bottom( index_ref ) ;     /* EOF */
  85.       #ifndef CLIPPER
  86.       else if ( ! b4leaf(index_ref) )
  87.      rc =  i4top( index_ref ) ;   /* BOF -    Not a leaf block */
  88.       #endif
  89.    }
  90.    if ( rc < 0) return( -num_skip ) ;     /* Error */
  91.    if ( rc == 3 )  return( 0L ) ;
  92.  
  93.    for(;;)
  94.    {
  95.       #ifdef CLIPPER
  96.      /* First Skip Over Current Key */
  97.      if ( num_left == 0 )  return( num_skip ) ;  /* Successfully skipped */
  98.  
  99.      if ( b4leaf(index_ref) )
  100.      {
  101.         num_left -=  b4skip( index_ref, num_left ) ;
  102.         if ( num_left == 0 )  return( num_skip ) ;
  103.  
  104.         while (1) /* Loop needed for when (sign<0) */
  105.         {
  106.            rc =  b4up( index_ref ) ;
  107.            if ( rc == -2)
  108.            {
  109.           u4error( E_INTERNAL, "i4skip", (char *) 0 ) ;
  110.           return( -num_skip ) ; /* Error */
  111.            }
  112.            block_ptr =  v4block+ index_ptr->block_ref ;
  113.            if ( rc == -1)
  114.            {
  115.           block_ptr->key_on += sign ;
  116.           if ( block_ptr->key_on < 0 )    block_ptr->key_on =  0 ;
  117.           return( num_skip - num_left ) ;
  118.            }
  119.            /* rc == 0 */
  120.            if ( sign > 0 )
  121.            {
  122.           if ( block_ptr->key_on < block_ptr->num_keys )
  123.           {
  124.              num_left -- ;
  125.              break ;
  126.           }
  127.            }
  128.            else
  129.            {
  130.           if ( --block_ptr->key_on >= 0 )
  131.           {
  132.              num_left ++ ;
  133.              break ;
  134.           }
  135.            }
  136.         }
  137.      }
  138.      else
  139.      {
  140.         block_ptr =  v4block+ index_ptr->block_ref ;
  141.         if ( sign > 0 )
  142.            if ( ++block_ptr->key_on > block_ptr->num_keys ) /* EOF */
  143.           return( num_skip - num_left ) ;
  144.  
  145.         num_left -= sign ;
  146.         for ( rc = 0;  rc >= 0;  rc =  b4down(index_ref, -sign) ) ;
  147.         if (rc <= -2)  return( -num_skip ) ;  /* Error */
  148.      }
  149.       #else
  150.      /* Move to a leaf in the index tree. */
  151.      rc = 0 ;
  152.      while ( rc >= 0 )
  153.      {
  154.         rc =  b4down( index_ref, -sign ) ;
  155.         if (rc == -2)  return( -num_skip ) ;  /* Error */
  156.      }
  157.  
  158.      num_left -= b4skip( index_ref, num_left ) ;
  159.      if ( num_left == 0)  return( num_skip ) ;  /* Successfully skipped */
  160.  
  161.      /* now skip one to the next leaf block */
  162.      do
  163.      {
  164.         rc = b4up( index_ref ) ;
  165.         if ( rc == -2)
  166.         {
  167.            u4error( E_INTERNAL, "i4skip", (char *) 0 ) ;
  168.            return( -num_skip ) ; /* Error */
  169.         }
  170.  
  171.         if ( rc == -1)
  172.         {
  173.            if ( num_skip > 0 )
  174.           v4block[index_ptr->block_ref].key_on ++ ; /* To indicate EOF */
  175.            return( num_skip - num_left ) ;
  176.         }
  177.      }  while ( b4skip( index_ref, (long) sign) != (long) sign) ;
  178.  
  179.      num_left -= sign ;
  180.       #endif
  181.    }
  182. }
  183.