home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c160 / 1.ddi / SOURCE / I4SEEK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-22  |  2.2 KB  |  95 lines

  1.  
  2. /* i4seek.c   (c)Copyright Sequiter Software Inc., 1987-1990.  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 "d4all.h"
  16. #include "p4misc.h"
  17.  
  18. #include <string.h>
  19.  
  20. extern INDEX   *v4index ;
  21. extern BLOCK   *v4block ;
  22. extern int      v4lock_wait ;
  23.  
  24. i4seek( int index_ref, void *search_string )
  25. {
  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 ( (rc = i4lock( index_ref, v4lock_wait)) < 0 )  return rc ;
  35.  
  36.    /* Do initial search, moving up only as far as necessary */
  37.    while ( b4up(index_ref) >= 0 ) ;
  38.    if ( v4index[index_ref].block_ref < 0 )
  39.       if ( b4down( index_ref, -1 ) < 0 )  return -1 ;
  40.  
  41.    /*  Repeat until the key is found */
  42.    for(;;)
  43.    {
  44.       /* Locate in the current block */
  45.       rc =  b4search( index_ref, (char *) search_string ) ;
  46.       if ( rc < 0) return( -1) ;
  47.  
  48.       /* If we are on a 'leaf' of the 'tree' */
  49.       if ( b4leaf(index_ref) ) 
  50.       {
  51.      if ( rc == 3 )
  52.      {
  53.         #ifdef CLIPPER 
  54.            while (1)
  55.            {
  56.           if ( b4up(index_ref) < 0 )  return 3 ;
  57.  
  58.           ref     =  v4index[index_ref].block_ref ;
  59.  
  60.           if ( v4block[ref].key_on < v4block[ref].num_keys )
  61.           {
  62.              compare_len =  (int) strlen((char *)search_string) ;
  63.              if ( compare_len > key_len )
  64.             compare_len =  key_len ;
  65.  
  66.              if ( memcmp(search_string, i4key(index_ref)->value,
  67.                      (size_t) compare_len) == 0 ) 
  68.              {
  69.             if ( compare_len == key_len ) 
  70.                return 0 ; 
  71.             else
  72.                return 1 ;
  73.              }
  74.              return( 2 ) ;
  75.           }
  76.            }
  77.         #else
  78.            /* EOF */
  79.                /* Note: Assignment to 'ref' seems to fix the introduction of 
  80.                         an Optimization error */
  81.                ref =  v4index[index_ref].block_ref ;
  82.            v4block[ ref ].key_on =  v4block[ ref ].num_keys+1 ;
  83.         #endif
  84.      }
  85.      return( rc) ;  /* b4search has the same "rc" conventions */
  86.       }
  87.       else
  88.       {
  89.      /* We should be able to move down as we are not on a leaf */
  90.      if ( b4down( index_ref, -1 )  < 0 )  return( -1 ) ;
  91.       }
  92.    }
  93. }
  94.  
  95.