home *** CD-ROM | disk | FTP | other *** search
/ Frostbyte's 1980s DOS Shareware Collection / floppyshareware.zip / floppyshareware / DOOG / CBASE09.ZIP / BTREE.ZIP / BTNEXT.C < prev    next >
Text File  |  1989-08-31  |  2KB  |  104 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "btnext.c    1.1 - 89/07/03" */
  5.  
  6. #include <blkio.h>
  7. #include <errno.h>
  8. #include "btree_.h"
  9.  
  10. /*man---------------------------------------------------------------------------
  11. NAME
  12.      btnext - next btree key
  13.  
  14. SYNOPSIS
  15.      #include <btree.h>
  16.  
  17.      int btnext(btp)
  18.      btree_t *btp;
  19.  
  20. DESCRIPTION
  21.      The btnext function advances the cursor of btree btp to the next
  22.      higher key.  If cursor is currently null, it will be advanced to
  23.      the first key.  If the cursor is currently on the last key, it
  24.      will advance to null.  If the tree is empty, the cursor will
  25.      remain on null.
  26.  
  27.      btnext will fail if one or more of the following is true:
  28.  
  29.      [EINVAL]       btp is not a valid btree pointer.
  30.      [BTELOCK]      btp is not locked.
  31.      [BTENOPEN]     btp is not open.
  32.  
  33. SEE ALSO
  34.      btcursor, btfirst, btlast, btprev.
  35.  
  36. DIAGNOSTICS
  37.      Upon successful completion, 0 is returned.  Otherwise, a value of -1
  38.      is returned and errno is set to indicate the error.
  39.  
  40. ------------------------------------------------------------------------------*/
  41. int btnext(btp)
  42. btree_t *btp;
  43. {
  44.     int rs = 0;
  45.  
  46.     errno = 0;
  47.  
  48.     /* validate arguments */
  49.     if (!bt_valid(btp)) {
  50.         errno = EINVAL;
  51.         return -1;
  52.     }
  53.  
  54.     /* check if not open */
  55.     if (!(btp->flags & BTOPEN)) {
  56.         errno = BTENOPEN;
  57.         return -1;
  58.     }
  59.  
  60.     /* check locks */
  61.     if (!(btp->flags & BTLOCKS)) {
  62.         errno = BTELOCK;
  63.         return -1;
  64.     }
  65.  
  66.     /* advance cursor to next key in current node */
  67.     if (btp->cbtpos.node != 0) {
  68.         btp->cbtpos.key++;
  69.         if (btp->cbtpos.key <= btp->cbtnp->n) {
  70.             errno = 0;
  71.             return 0;
  72.         }
  73.     }
  74.  
  75.     /* advance cursor to null */
  76.     if (btp->cbtpos.node == btp->bthdr.last) {
  77.         btp->cbtpos.node = 0;
  78.         btp->cbtpos.key = 0;
  79.         bt_ndinit(btp, btp->cbtnp);
  80.         errno = 0;
  81.         return 0;
  82.     }
  83.  
  84.     /* advance cursor to first key in next node */
  85.     if (btp->cbtpos.node == 0) {
  86.         btp->cbtpos.node = btp->bthdr.first;
  87.         btp->cbtpos.key = 1;
  88.     } else {
  89.         btp->cbtpos.node = btp->cbtnp->rsib;
  90.         btp->cbtpos.key = 1;
  91.     }
  92.     rs = bt_ndget(btp, btp->cbtpos.node, btp->cbtnp);
  93.     if (rs == -1) {
  94.         BTEPRINT;
  95.         btp->cbtpos.node = 0;
  96.         btp->cbtpos.key = 0;
  97.         bt_ndinit(btp, btp->cbtnp);
  98.         return -1;
  99.     }
  100.  
  101.     errno = 0;
  102.     return 0;
  103. }
  104.