home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / c / cbase.zoo / btree101.zoo / btsearch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-20  |  1.9 KB  |  83 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)btsearch.c    1.4 - 90/06/20" */
  5.  
  6. #include <blkio.h>
  7. #include <errno.h>
  8.  
  9. /* local headers */
  10. #include "btree_.h"
  11.  
  12. /*man---------------------------------------------------------------------------
  13. NAME
  14.      btsearch - search a btree
  15.  
  16. SYNOPSIS
  17.      #include <btree.h>
  18.  
  19.      int btsearch(btp, buf)
  20.      btree_t *btp;
  21.      const void *buf;
  22.  
  23. DESCRIPTION
  24.      The btsearch function searches the btree btp for the key pointed
  25.      to by buf.  If it is found, the cursor is set to the location of
  26.      the key and 1 is returned.  If it is not found, the cursor is set
  27.      to the next higher key and 0 is returned.
  28.  
  29.      btsearch will fail if one or more of the following is true:
  30.  
  31.      [EINVAL]       btp is not a valid btree pointer.
  32.      [EINVAL]       buf is the NULL pointer.
  33.      [BTELOCK]      btp is not read locked.
  34.  
  35. SEE ALSO
  36.      btdelcur, btdelete, btinsert.
  37.  
  38. DIAGNOSTICS
  39.      Upon successful completion, a value of 1 is returned if the key
  40.      was found or a value of 0 if it was not found.  On failure, a
  41.      value of -1 is returned, and errno set to indicate the error.
  42.      
  43. ------------------------------------------------------------------------------*/
  44. int btsearch(btp, buf)
  45. btree_t *btp;
  46. const void *buf;
  47. {
  48.     int found = 0;        /* key found flag */
  49.  
  50.     /* validate arguments */
  51.     if (!bt_valid(btp) || buf == NULL) {
  52.         errno = EINVAL;
  53.         return -1;
  54.     }
  55.  
  56.     /* check locks */
  57.     if (!(btp->flags & BTRDLCK)) {
  58.         errno = BTELOCK;
  59.         return -1;
  60.     }
  61.  
  62.     /* search to position to insert */
  63.     found = bt_search(btp, buf);
  64.     if (found == -1) {
  65.         BTEPRINT;
  66.         return -1;
  67.     }
  68.  
  69.     /* check if cursor on empty slot */
  70.     if (found == 0) {
  71.         if (btp->cbtpos.key > btp->cbtnp->n) {
  72.             btp->cbtpos.key = btp->cbtnp->n;
  73.             if (btnext(btp) == -1) {
  74.                 BTEPRINT;
  75.                 return -1;
  76.             }
  77.         }
  78.     }
  79.  
  80.     errno = 0;
  81.     return ((found == 1) ? 1: 0);
  82. }
  83.