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

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