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 / I4SEEK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-14  |  2.1 KB  |  93 lines

  1.  
  2. /* i4seek.C   (c)Copyright Sequiter Software Inc., 1987, 1988, 1989.  All rights reserved.
  3.  
  4.    Locates the index key.
  5.  
  6.    Following Returns
  7.  
  8.       0 -  Exact Find
  9.       1 -  Inexact Find
  10.       2 -  On key After
  11.       3 -  EOF
  12.  
  13. */
  14.  
  15. #include "d4base.h"
  16.  
  17. extern INDEX   *v4index ;
  18. extern BLOCK   *v4block ;
  19.  
  20.  
  21. i4seek( index_ref, search_string )
  22. int   index_ref ;
  23. void *search_string ;
  24. {
  25.    INDEX  *index_ptr ;
  26.    int     rc, ref ;
  27.  
  28.    #ifdef CLIPPER
  29.       int  key_len, compare_len ;
  30.  
  31.       key_len =  v4index[index_ref].key_len ;
  32.    #endif
  33.  
  34.    if ( i4lock( index_ref, 1 ) < 0 )  return -1 ;
  35.  
  36.    /* Do initial search, moving up only as far as necessary */
  37.    while ( b4up(index_ref) >= 0 ) ;
  38.  
  39.    /*  Repeat until the key is found */
  40.    for(;;)
  41.    {
  42.       /* Locate in the current block */
  43.       rc =  b4search( index_ref, search_string ) ;
  44.       if ( rc < 0) return( -1) ;
  45.  
  46.       /* If we are on a 'leaf' of the 'tree' */
  47.       if ( b4leaf(index_ref) ) 
  48.       {
  49.      if ( rc == 3 )
  50.      {
  51.         #ifdef CLIPPER 
  52.            while (1)
  53.            {
  54.           if ( b4up(index_ref) < 0 )  return 3 ;
  55.  
  56.           ref     =  v4index[index_ref].block_ref ;
  57.  
  58.           if ( v4block[ref].key_on < v4block[ref].num_keys )
  59.           {
  60.              compare_len =  strlen(search_string) ;
  61.              if ( compare_len > key_len )
  62.             compare_len =  key_len ;
  63.  
  64.              if ( memcmp(search_string, b4key(index_ref)->value,
  65.                      compare_len) == 0 ) 
  66.              {
  67.             if ( compare_len == key_len ) 
  68.                return 0 ; 
  69.             else
  70.                return 1 ;
  71.              }
  72.              return( 2 ) ;
  73.           }
  74.            }
  75.         #else
  76.            /* EOF */
  77.                /* Note: Assignment to 'ref' seems to fix the introduction of 
  78.                         an Optimization error */
  79.                ref =  v4index[index_ref].block_ref ;
  80.            v4block[ ref ].key_on =  v4block[ ref ].num_keys+1 ;
  81.         #endif
  82.      }
  83.      return( rc) ;  /* b4search has the same "rc" conventions */
  84.       }
  85.       else
  86.       {
  87.      /* We should be able to move down as we are not on a leaf */
  88.      if ( b4down( index_ref, -1 )  < 0 )  return( -1 ) ;
  89.       }
  90.    }
  91. }
  92.  
  93.