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

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)btsetcur.c    1.4 - 90/06/20" */
  5.  
  6. #include <blkio.h>
  7. #include <errno.h>
  8. /*#include <string.h>*/
  9.  
  10. /* local headers */
  11. #include "btree_.h"
  12.  
  13. /*man---------------------------------------------------------------------------
  14. NAME
  15.      btsetcur - set btree cursor
  16.  
  17. SYNOPSIS
  18.      #include <btree.h>
  19.  
  20.      int btsetcur(btp, btposp)
  21.      btree_t *btp;
  22.      const btpos_t *btposp;
  23.  
  24. DESCRIPTION
  25.      The btsetcur function sets the current cursor position for btree
  26.      btp to the value in btposp.  btposp should point to a cursor
  27.      value saved previously with btgetcur.  If btposp is the NULL
  28.      pointer, the cursor is set to null.  It is important to remember
  29.      that a btree position is not valid after an insertion, deletion,
  30.      or unlock.
  31.  
  32.      btsetcur will fail if one or more of the following is true:
  33.  
  34.      [EINVAL]       btp is not a valid btree pointer.
  35.      [BTELOCK]      btp is not locked.
  36.      [BTENKEY]      btposp points to an invalid cursor value.
  37.      [BTENOPEN]     btp is not open.
  38.  
  39. SEE ALSO
  40.      btgetcur.
  41.  
  42. DIAGNOSTICS
  43.      Upon successful completion, a value of 0 is returned.  Otherwise,
  44.      a value of -1 is returned, and errno set to indicate the error.
  45.  
  46. ------------------------------------------------------------------------------*/
  47. int btsetcur(btp, btposp)
  48. btree_t *btp;
  49. const btpos_t *btposp;
  50. {
  51.     /* validate arguments */
  52.     if (!bt_valid(btp)) {
  53.         errno = EINVAL;
  54.         return -1;
  55.     }
  56.  
  57.     /* check if not open */
  58.     if (!(btp->flags & BTOPEN)) {
  59.         errno = BTENOPEN;
  60.         return -1;
  61.     }
  62.  
  63.     /* check locks */
  64.     if (!(btp->flags & BTLOCKS)) {
  65.         errno = BTELOCK;
  66.         return -1;
  67.     }
  68.  
  69.     /* set cursor */
  70.     if (btposp == NULL) {
  71.         btp->cbtpos.node = NIL;        /* set cursor to null */
  72.         btp->cbtpos.key = 0;
  73.     } else {
  74.         memcpy(&btp->cbtpos, btposp, sizeof(btp->cbtpos));
  75.     }
  76.  
  77.     /* read in new current node */
  78.     if (btp->cbtpos.node == NIL) {
  79.         bt_ndinit(btp, btp->cbtnp);
  80.     } else {
  81.         if (bt_ndget(btp, btp->cbtpos.node, btp->cbtnp) == -1) {
  82.             BTEPRINT;
  83.             if (errno == BEEOF) errno = BTENKEY;
  84.             return -1;
  85.         }
  86.         if (btp->cbtnp->n == 0) {    /* check if block empty */
  87.             btp->cbtpos.node = NIL;
  88.             btp->cbtpos.key = 0;
  89.             bt_ndinit(btp, btp->cbtnp);
  90.             errno = BTENKEY;
  91.             return -1;
  92.         }
  93.     }
  94.  
  95.     /* check if key number in range */
  96.     if (btp->cbtpos.key > btp->cbtnp->n) {
  97.         btp->cbtpos.node = NIL;
  98.         btp->cbtpos.key = 0;
  99.         bt_ndinit(btp, btp->cbtnp);
  100.         errno = BTENKEY;
  101.         return -1;
  102.     }
  103.  
  104.     errno = 0;
  105.     return 0;
  106. }
  107.